|
Glassfish example source code file (MonitoringHandlers.java)
This example Glassfish source code file (MonitoringHandlers.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Glassfish MonitoringHandlers.java source code
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.admingui.common.handlers;
import java.io.UnsupportedEncodingException;
import org.glassfish.admingui.common.util.GuiUtil;
import com.sun.jsftemplating.annotation.Handler;
import com.sun.jsftemplating.annotation.HandlerInput;
import com.sun.jsftemplating.annotation.HandlerOutput;
import com.sun.jsftemplating.layout.descriptors.handler.HandlerContext;
import java.net.URLEncoder;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Date;
import java.util.ListIterator;
import java.util.logging.Level;
import org.glassfish.admingui.common.util.RestResponse;
import org.glassfish.admingui.common.util.RestUtil;
/**
*
* @author Ana
*/
public class MonitoringHandlers {
@Handler(id = "getMonitorLevels",
input = {
@HandlerInput(name = "endpoint", type = String.class, required = true)},
output = {
@HandlerOutput(name = "monitorCompList", type = List.class)
})
public static void getMonitorLevels(HandlerContext handlerCtx) {
String endpoint = (String) handlerCtx.getInputValue("endpoint");
List result = new ArrayList();
try {
String monitoringServiceEndPoint = endpoint + "/monitoring-service";
Map<String,Object> attrs = RestUtil.getEntityAttrs(monitoringServiceEndPoint+"/container-monitoring", "entitiy");
if (attrs != null) {
for (String prop : attrs.keySet()) {
Map oneRow = new HashMap();
String cname = null;
String pname = prop;
ListIterator ci = containerDispList.listIterator();
ListIterator vi = containerNameList.listIterator();
while (ci.hasNext() && vi.hasNext()) {
String dispName = (String) ci.next();
String value = (String) vi.next();
if (pname.equals(value)) {
cname = dispName;
}
}
oneRow.put("monCompName", (cname == null) ? pname : cname);
oneRow.put("level", attrs.get(prop));
oneRow.put("selected", false);
result.add(oneRow);
}
}
String monitoringLevelsEndPoint = monitoringServiceEndPoint + "/module-monitoring-levels";
attrs = RestUtil.getEntityAttrs(monitoringLevelsEndPoint, "entity");
for (String oneMonComp : attrs.keySet()) {
Map oneRow = new HashMap();
String name = null;
ListIterator ni = monDisplayList.listIterator();
ListIterator vi = monNamesList.listIterator();
while (ni.hasNext() && vi.hasNext()) {
String dispName = (String) ni.next();
String value = (String) vi.next();
if ((oneMonComp.equals(value))) {
name = dispName;
}
}
if (name == null) {
name = oneMonComp;
}
oneRow.put("monCompName", name);
oneRow.put("level", attrs.get(oneMonComp));
oneRow.put("selected", false);
result.add(oneRow);
}
} catch (Exception ex) {
GuiUtil.handleException(handlerCtx, ex);
}
handlerCtx.setOutputValue( "monitorCompList", result);
}
/*
* This handler returns a list of statistical data for an endpoint.
* Useful for populating table
*/
@Handler(id="getStats",
input={
@HandlerInput(name="endpoint", type=String.class, required=true),
@HandlerInput(name="statType", type=String.class),
@HandlerInput(name="type", type=String.class)},
output={
@HandlerOutput(name="result", type=List.class),
@HandlerOutput(name="hasStats", type=Boolean.class)})
public static void getStats(HandlerContext handlerCtx) {
String endpoint = (String) handlerCtx.getInputValue("endpoint");
String statType = (String) handlerCtx.getInputValue("statType");
String type = (String) handlerCtx.getInputValue("type");
Locale locale = GuiUtil.getLocale();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, locale);
NumberFormat nf = NumberFormat.getNumberInstance(locale);
List result = new ArrayList();
try {
//This check is to get the correct type of statistics.
if ((type == null || statType == null) || type.equals(statType)) {
if (RestUtil.doesProxyExist(endpoint)) {
Map<String, Object> stats = getMonitoringStatInfo(endpoint);
//Jersey monitoring data format
if (statType != null && statType.equals("jersey")) {
Map<String, Object> jerseyStats = new HashMap();
for (String stat : stats.keySet()) {
Map<String, Object> jerseyStat = (Map) stats.get(stat);
if (jerseyStat != null) {
jerseyStats.putAll(jerseyStat);
}
}
stats = jerseyStats;
}
for (String statName : stats.keySet()) {
if (!(stats.get(statName).getClass().equals(HashMap.class))) {
continue;
}
Map<String, Object> monAttrs = (Map) stats.get(statName);
Map<String, String> statMap = new HashMap();
String val = "";
String details = "--";
String desc = "--";
String start = "--";
String last = "--";
String unit = "";
String mname = null;
String runtimes = null;
String queuesize = null;
String thresholds = "--";
if (monAttrs.size() != 0) {
if (monAttrs.containsKey("name")) {
mname = (String) monAttrs.get("name");
} else if (monAttrs.containsKey("appname")) {
mname = (String) monAttrs.get("appname");
}
unit = (String) monAttrs.get("unit");
desc = (String) monAttrs.get("description");
Long lastTime = (Long) monAttrs.get("lastsampletime");
if (lastTime != -1) {
last = df.format(new Date(lastTime));
}
Long startTime = (Long) monAttrs.get("starttime");
if (startTime != -1) {
start = df.format(new Date(startTime));
}
if (monAttrs.containsKey("count")) {
val = monAttrs.get("count") + " " + unit;
} else if (monAttrs.containsKey("current")) {
if (unit != null) {
if (unit.equals("String")) {
if (mname.equals("LiveThreads")) {
String str = (String) monAttrs.get("current");
val = formatStringForDisplay(str);
} else {
val = (String) monAttrs.get("current");
}
} else if (unit.equals("List")) {
String str = (String) monAttrs.get("current");
String formatStr = formatActiveIdsForDisplay(str);
if (!formatStr.isEmpty() && !formatStr.equals("")) {
val = formatStr;
}
} else {
Long currentVal = (Long) monAttrs.get("current");
val = currentVal + unit;
}
}
} else if (monAttrs.containsKey("applicationtype")) {
val = (String) monAttrs.get("applicationtype");
}
//Update the details
if (monAttrs.containsKey("appName")) {
details = (GuiUtil.getMessage("msg.AppName") + ": " + monAttrs.get("appName") + "<br/>");
}
if (monAttrs.containsKey("appname")) {
details = (GuiUtil.getMessage("msg.AppName") + ": " + monAttrs.get("appname") + "<br/>");
}
if (monAttrs.containsKey("environment")) {
details = details + (GuiUtil.getMessage("msg.Environment") + ": " + monAttrs.get("environment") + "<br/>");
}
if (monAttrs.containsKey("address")) {
details = details + (GuiUtil.getMessage("msg.Address") + ": " + monAttrs.get("address") + "<br/>");
}
if (monAttrs.containsKey("deploymenttype")) {
details = details + (GuiUtil.getMessage("msg.DepType") + ": " + monAttrs.get("deploymenttype") + "<br/>");
}
if (monAttrs.containsKey("endpointname")) {
details = details + (GuiUtil.getMessage("msg.EndPointName") + ": " + monAttrs.get("endpointname") + "<br/>");
}
if (monAttrs.containsKey("classname")) {
details = (GuiUtil.getMessage("msg.ClassName") + ": " + monAttrs.get("classname") + "<br/>");
}
if (monAttrs.containsKey("impltype")) {
details = details + (GuiUtil.getMessage("msg.ImplClass") + ": " + monAttrs.get("implclass") + "<br/>");
}
if (monAttrs.containsKey("implclass") && monAttrs.containsKey("impltype")) {
details = details + (GuiUtil.getMessage("msg.ImplType") + ": " + monAttrs.get("impltype") + "<br/>");
}
if (monAttrs.containsKey("namespace")) {
details = details + (GuiUtil.getMessage("msg.NameSpace") + ": " + monAttrs.get("namespace") + "<br/>");
}
if (monAttrs.containsKey("portname")) {
details = details + (GuiUtil.getMessage("msg.PortName") + ": " + monAttrs.get("portname") + "<br/>");
}
if (monAttrs.containsKey("servicename")) {
details = details + (GuiUtil.getMessage("msg.ServiceName") + ": " + monAttrs.get("servicename") + "<br/>");
}
if (monAttrs.containsKey("tester")) {
details = details + (GuiUtil.getMessage("msg.Tester") + ": " + monAttrs.get("tester") + "<br/>");
}
if (monAttrs.containsKey("wsdl")) {
details = details + (GuiUtil.getMessage("msg.WSDL") + ": " + monAttrs.get("wsdl") + "<br/>");
}
if (monAttrs.containsKey("maxtime")) {
details = (GuiUtil.getMessage("msg.MaxTime") + ": " + monAttrs.get("maxtime") + " " + unit + "<br/>");
}
if (monAttrs.containsKey("mintime")) {
details = details + (GuiUtil.getMessage("msg.MinTime") + ": " + monAttrs.get("mintime") + " " + unit + "<br/>");
}
if (monAttrs.containsKey("totaltime")) {
details = details + (GuiUtil.getMessage("msg.TotalTime") + ": " + monAttrs.get("totaltime") + " " + unit + "<br/>");
}
if (monAttrs.containsKey("highwatermark")) {
details = (GuiUtil.getMessage("msg.HWaterMark") + ": " + monAttrs.get("highwatermark") + " " + unit + "<br/>");
}
if (monAttrs.containsKey("lowwatermark")) {
details = details + (GuiUtil.getMessage("msg.LWaterMark") + ": " + monAttrs.get("lowwatermark") + " " + unit + "<br/>");
}
if (monAttrs.containsKey("activeruntimes")) {
runtimes = (String) monAttrs.get("activeruntimes");
}
if (monAttrs.containsKey("queuesize")) {
queuesize = (String) monAttrs.get("queuesize");
}
if (monAttrs.containsKey("hardmaximum") && monAttrs.get("hardmaximum") != null) {
val = monAttrs.get("hardmaximum") + " " + "hard max " + "<br/>" + monAttrs.get("hardminimum") + " " + "hard min";
}
if (monAttrs.containsKey("newthreshold") && monAttrs.get("newThreshold") != null) {
thresholds = monAttrs.get("newthreshold") + " " + "new " + "<br/>" + monAttrs.get("queuedownthreshold") + " " + "queue down";
}
if (monAttrs.containsKey("queuesize") && monAttrs.containsKey("environment")) {
details = details + monAttrs.get("environment");
}
statMap.put("name", mname);
statMap.put("startTime", start);
statMap.put("lastTime", last);
statMap.put("description", desc);
statMap.put("value", (val == null) ? "" : val);
statMap.put("details", (details == null) ? "--" : details);
statMap.put("thresholds", (thresholds == null) ? "--" : thresholds);
statMap.put("queueSize", (queuesize == null) ? "--" : queuesize);
statMap.put("runtimes", (runtimes == null) ? "--" : runtimes);
result.add(statMap);
}
}
}
}
handlerCtx.setOutputValue("result", result);
handlerCtx.setOutputValue("hasStats", (result.size() == 0) ? false : true);
} catch (Exception ex) {
GuiUtil.handleException(handlerCtx, ex);
}
}
@Handler(id = "updateMonitorLevels",
input = {
@HandlerInput(name = "allRows", type = List.class, required = true),
@HandlerInput(name = "endpoint", type = String.class),
@HandlerInput(name = "containerEndpoint", type = String.class)})
public static void updateMonitorLevels(HandlerContext handlerCtx) {
String endpoint = (String) handlerCtx.getInputValue("endpoint");
String containerEndpoint = (String) handlerCtx.getInputValue("containerEndpoint");
List<Map allRows = (List |