alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  
"; } } values = values + "</tr>"; } } else { String[] strData = s.split(" "); if (strData != null && strData.length > 0) { values = values + "<tr>"; for (String d : (String[]) strData) { if (!d.isEmpty()) { values = values + "<td>" + d + ""; } } values = values + "</tr>"; } } } values = values + "</table>"; } return values; } private static String formatStringForDisplay(String strToFormat) { String[] strs = strToFormat.split(","); String formattedStr = ""; if (strs != null && strs.length > 10) { for (int i = 0; i < strs.length; i++) { String str = strs[i]; if (!formattedStr.equals("")) { formattedStr = formattedStr + ","; } if (i % 10 == 0 && i != 0) { formattedStr = formattedStr + "\n"; } formattedStr = formattedStr + str; } return formattedStr; } return strToFormat; } private static Map<String, Object> getMonitoringStatInfo(String endpoint) { Map<String, Object> monitorInfoMap = new HashMap(); try { Map<String, Object> responseMap = RestUtil.restRequest(endpoint, null, "GET", null, false); Map<String, Object> dataMap = (Map) responseMap.get("data"); if (dataMap != null) { Map<String, Object> extraPropsMap = (Map) dataMap.get("extraProperties"); if (extraPropsMap != null) { Map<String, Object> entityMap = (Map) extraPropsMap.get("entity"); if (entityMap != null) { monitorInfoMap = entityMap; } } } } catch (Exception ex) { GuiUtil.getLogger().severe("Error in getMonitoringStatInfo ; \nendpoint = " + endpoint + "attrs=" + "method=GET"); //we don't need to call GuiUtil.handleError() because thats taken care of in restRequest() when we pass in the handler. } return monitorInfoMap; } final private static List<String> levels= new ArrayList(); static{ levels.add("OFF"); levels.add("LOW"); levels.add("HIGH"); } //monitoring component names public static final String JVM = GuiUtil.getMessage("monitoring.Jvm"); public static final String WEB_CONTAINER = GuiUtil.getMessage("monitoring.Web"); public static final String HTTP_SERVICE = GuiUtil.getMessage("monitoring.Http"); public static final String THREAD_POOL = GuiUtil.getMessage("monitoring.ThreadPool"); public static final String JDBC_CONNECTION_POOL = GuiUtil.getMessage("monitoring.Jdbc"); public static final String CONNECTOR_CONNECTION_POOL = GuiUtil.getMessage("monitoring.Connector"); public static final String EJB_CONTAINER = GuiUtil.getMessage("monitoring.Ejb"); public static final String TRANSACTION_SERVICE = GuiUtil.getMessage("monitoring.TransactionService"); public static final String ORB = GuiUtil.getMessage("monitoring.Orb"); public static final String CONNECTOR_SERVICE = GuiUtil.getMessage("monitoring.ConnectorService"); public static final String JMS_SERVICE = GuiUtil.getMessage("monitoring.JmsService"); public static final String WEB_SERVICES_CONTAINER = GuiUtil.getMessage("monitoring.WebServices"); public static final String JPA = GuiUtil.getMessage("monitoring.Jpa"); public static final String SECURITY = GuiUtil.getMessage("monitoring.Security"); public static final String JERSEY = GuiUtil.getMessage("monitoring.Jersey"); public static final String DEPLOYMENT = GuiUtil.getMessage("monitoring.Deployment"); final private static List monDisplayList= new ArrayList(); static{ monDisplayList.add(JVM); monDisplayList.add(WEB_CONTAINER); monDisplayList.add(HTTP_SERVICE); monDisplayList.add(THREAD_POOL); monDisplayList.add(JDBC_CONNECTION_POOL); monDisplayList.add(CONNECTOR_CONNECTION_POOL); monDisplayList.add(EJB_CONTAINER); monDisplayList.add(TRANSACTION_SERVICE); monDisplayList.add(ORB); monDisplayList.add(CONNECTOR_SERVICE); monDisplayList.add(JMS_SERVICE); monDisplayList.add(WEB_SERVICES_CONTAINER); monDisplayList.add(JPA); monDisplayList.add(SECURITY); monDisplayList.add(JERSEY); monDisplayList.add(DEPLOYMENT); } final private static List monNamesList = new ArrayList(); static{ monNamesList.add("jvm"); monNamesList.add("webContainer"); monNamesList.add("httpService"); monNamesList.add("threadPool"); monNamesList.add("jdbcConnectionPool"); monNamesList.add("connectorConnectionPool"); monNamesList.add("ejbContainer"); monNamesList.add("transactionService"); monNamesList.add("orb"); monNamesList.add("connectorService"); monNamesList.add("jmsService"); monNamesList.add("webServicesContainer"); monNamesList.add("jpa"); monNamesList.add("security"); monNamesList.add("jersey"); monNamesList.add("deployment"); } final private static List containerDispList= new ArrayList(); final private static List containerNameList= new ArrayList(); }

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish MonitoringHandlers.java source code file:

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.

Java - Glassfish tags/keywords

arraylist, handler, handlerinput, handlerinput, handleroutput, list, list, listiterator, log, logging, map, map, net, network, object, string, string, text, utf-8, util

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>) handlerCtx.getInputValue("allRows");
        String objectNameStr = null;
        for (Map<String, String> oneRow : allRows) {
            String name = oneRow.get("monCompName");
            String value = null;
            ListIterator ni = monDisplayList.listIterator();
            ListIterator vi = monNamesList.listIterator();
            while (ni.hasNext() && vi.hasNext()) {
                String dispName = (String) ni.next();
                String mvalue = (String) vi.next();
                if (name.equals(dispName)) {
                    value = mvalue;
                    objectNameStr = endpoint;
                }
            }
            if (value == null) {
                ListIterator ci = containerDispList.listIterator();
                ListIterator cni = containerNameList.listIterator();
                while (ci.hasNext() && cni.hasNext()) {
                    String cDispName = (String) ci.next();
                    String cName = (String) cni.next();
                    if (name.equals(cDispName)) {
                        value = "Level";
                        objectNameStr = containerEndpoint+"/"+cName;
                    }
                }
            }
            Map<String,Object> attrMap = new HashMap();
            attrMap.put((value == null) ? name : value, oneRow.get("level"));
            String entityUrl = (objectNameStr == null) ? containerEndpoint+"/"+name : objectNameStr ;
            RestResponse response = RestUtil.sendUpdateRequest(entityUrl, attrMap, null, null, null);
            if (!response.isSuccess()) {
                GuiUtil.getLogger().severe("Update monitor level failed.  parent=" + endpoint + "; attrsMap =" + attrMap);
                GuiUtil.handleError(handlerCtx, GuiUtil.getMessage("msg.error.checkLog"));
                return;
            }
        }
     }

    /**
     *	<p> Add list to new list
     */
    @Handler(id = "addToMonitorList",
        input = {
            @HandlerInput(name = "oldList", type = List.class),
            @HandlerInput(name = "newList", type = List.class)},
        output = {
            @HandlerOutput(name = "result", type = List.class)
            })
    public static void addToMonitorList(HandlerContext handlerCtx) {
       List<String> oldList = (List) handlerCtx.getInputValue("oldList");
       List<String> newList = (List) handlerCtx.getInputValue("newList");
        if (newList == null){
            newList = new ArrayList();
        }
        if (oldList != null) {
                for (String sk : oldList) {
                    newList.add(sk);
                }
            }
        handlerCtx.setOutputValue("result", newList);
    }
    
    @Handler(id = "getValidMonitorLevels",
    output = {
        @HandlerOutput(name = "monitorLevelList", type = List.class)
    })
    public static void getValidMonitorLevels(HandlerContext handlerCtx) {
        handlerCtx.setOutputValue("monitorLevelList",  levels);
     }
    
    @Handler(id = "getFirstValueFromList",
    input={
        @HandlerInput(name="values",   type=List.class, required=true)},
    output = {
        @HandlerOutput(name = "firstValue", type = String.class)
    })
    public static void getFirstValueFromList(HandlerContext handlerCtx) {
        List values = (List) handlerCtx.getInputValue("values");
        String firstval = "";
        if ((values != null) && (values.size()!=0)){
            firstval = (String)values.get(0);

        }
        handlerCtx.setOutputValue("firstValue",  firstval);
     }

    @Handler(id = "getAppName",
      input={
        @HandlerInput(name="endpoint",   type=String.class, required=true),
        @HandlerInput(name="name",   type=String.class, required=true)},
       output = {
        @HandlerOutput(name = "appName", type = String.class),
        @HandlerOutput(name = "appFullName", type = String.class)
       })
    public static void getAppName(HandlerContext handlerCtx) {
        String endpoint = (String) handlerCtx.getInputValue("endpoint");
        String name = (String) handlerCtx.getInputValue("name");
        String appName = name;
        String fullName = name;
        try {
            List<String> applications = new ArrayList(RestUtil.getChildMap(endpoint).keySet());
            for (String oneApp : applications) {
                List<String> modules = new ArrayList(RestUtil.getChildMap(endpoint + "/" + oneApp + "/module").keySet());
                if (modules.contains(name)) {
                    appName = oneApp;
                    break;
                }
            }
            if (fullName != null && !(name.equals(appName))) {
                fullName = URLEncoder.encode(appName, "UTF-8") + "/" + URLEncoder.encode(name, "UTF-8");
            }
            if (appName != null) {
                appName = URLEncoder.encode(appName, "UTF-8");
            }
        } catch (Exception ex) {
            GuiUtil.handleException(handlerCtx, ex);
        }
        
        handlerCtx.setOutputValue("appName",  appName);
        handlerCtx.setOutputValue("appFullName",  fullName);
    }
    
    @Handler(id = "getWebStatsUrl",
      input={
        @HandlerInput(name="app",   type=String.class, required=true),
        @HandlerInput(name="compVal",   type=String.class, required=true),
        @HandlerInput(name="vsList",   type=List.class, required=true),
        @HandlerInput(name="monitorURL",   type=String.class, required=true),
        @HandlerInput(name="moduleProps",   type=Map.class, required=true)},
       output = {
        @HandlerOutput(name = "webServletUrl", type = String.class),
        @HandlerOutput(name = "webServletType", type = String.class),
        @HandlerOutput(name = "webUrl", type = String.class),
        @HandlerOutput(name = "webType", type = String.class)
       })
    public static void getWebStatsUrl(HandlerContext handlerCtx) {
        String app = (String) handlerCtx.getInputValue("app");
        String monitorURL = (String) handlerCtx.getInputValue("monitorURL");
        List<String> vsList = (List) handlerCtx.getInputValue("vsList");
        String compVal = (String) handlerCtx.getInputValue("compVal");
        Map<String, String> moduleProps = (Map) handlerCtx.getInputValue("moduleProps");
        String webUrl = "EMPTY";
        String webServletUrl = "EMPTY";
        String statType = "EMPTY";
        String webType = "EMPTY";
        String monitorEndpoint = monitorURL + "/applications/" + app;
        
        try {
            for (String vs : vsList) {
                if (doesMonitoringDataExist(monitorEndpoint + "/" + URLEncoder.encode(vs, "UTF-8"))) {
                    webUrl = monitorEndpoint + "/" + URLEncoder.encode(vs, "UTF-8");
                    webType = "Web";
                    break;
                }
            }
            if (compVal != null && !(compVal.equals(""))) {
                String[] compStrs = compVal.split("/");
                if (vsList.contains(compStrs[0])) {
                    if (moduleProps.containsKey(compStrs[1]) && moduleProps.get(compStrs[1]).equals("Servlet")) {
                        monitorEndpoint = monitorEndpoint + "/" + URLEncoder.encode(compStrs[0], "UTF-8") + "/" + URLEncoder.encode(compStrs[1], "UTF-8");

                        if (RestUtil.doesProxyExist(monitorEndpoint)) {
                            webServletUrl = monitorEndpoint;
                            statType = "ServletInstance";
                        }
                    }
                }
            }
        } catch (UnsupportedEncodingException ex) {
            GuiUtil.getLogger().info(GuiUtil.getCommonMessage("log.error.getWebStatsUrl" )+ ex.getLocalizedMessage());
            if (GuiUtil.getLogger().isLoggable(Level.FINE)){
                ex.printStackTrace();
            }
        }
        handlerCtx.setOutputValue("webServletUrl", webServletUrl);
        handlerCtx.setOutputValue("webServletType", statType);
        handlerCtx.setOutputValue("webUrl", webUrl);
        handlerCtx.setOutputValue("webType", webType);
    }

    @Handler(id = "getStatsUrl",
      input={
        @HandlerInput(name="app",   type=String.class, required=true),
        @HandlerInput(name="moduleProps",   type=Map.class, required=true),
        @HandlerInput(name="monitorURL",   type=String.class, required=true),
        @HandlerInput(name="compVal",   type=String.class, required=true)},
       output = {
        @HandlerOutput(name = "statUrl", type = String.class),
        @HandlerOutput(name = "statType", type = String.class)
       })
    public static void getStatsUrl(HandlerContext handlerCtx) {
        String app = (String) handlerCtx.getInputValue("app");
        String comp = (String) handlerCtx.getInputValue("compVal");
        String monitorURL = (String) handlerCtx.getInputValue("monitorURL");
        Map<String, String> moduleProps = (Map) handlerCtx.getInputValue("moduleProps");
        String statUrl = "EMPTY";
        String statType = "";

        if (comp != null) {
            String[] compStrs = comp.split("/");
            try {
                statUrl = monitorURL + "/applications/" + app;
                for (String str : compStrs) {
                    statUrl = statUrl  + "/" + URLEncoder.encode(str, "UTF-8");
                }                
            } catch (UnsupportedEncodingException ex) {
                GuiUtil.getLogger().info(GuiUtil.getCommonMessage("log.error.getStatsUrl") + ex.getLocalizedMessage());
            if (GuiUtil.getLogger().isLoggable(Level.FINE)){
                ex.printStackTrace();
            }
            }

            if (RestUtil.doesProxyExist(statUrl)) {
                if (compStrs.length == 1) {
                    statType = (String) moduleProps.get(compStrs[0]);
                } else {
                    statType = modifyStatType(compStrs[1]);
                }
            }
        }

        handlerCtx.setOutputValue("statUrl", statUrl);
        handlerCtx.setOutputValue("statType", statType);
    }
    /*
     * Filter the request,session,jsp and servlets. 
     * Filed an issue :12687.
     * Once this issue is resolved, we can remove this handler.
     */
    @Handler(id = "filterWebStats",
      input={
        @HandlerInput(name="webStats",   type=List.class, required=true),
        @HandlerInput(name="statType",   type=String.class, required=true)},
       output = {
        @HandlerOutput(name = "stats", type = List.class)
       })
    public static void filterWebStats(HandlerContext handlerCtx) {
        List<Map> webStats = (List) handlerCtx.getInputValue("webStats");
        String statType = (String) handlerCtx.getInputValue("statType");
        List<String> requestStatNames = java.util.Arrays.asList("MaxTime", "ProcessingTime", "RequestCount", "ErrorCount");
        List stats = new ArrayList();
        if (webStats != null) {
            for (Map webStat : webStats) {
                String statName = (String) webStat.get("name");
                if (requestStatNames.contains(statName) && statType.equals("Request")) {
                    stats.add(webStat);
                } else if (statName.contains(statType) && !(statType.equals("Request"))) {
                    stats.add(webStat);
                }
            }
        }

        handlerCtx.setOutputValue("stats", stats);
    }
    
    /*
     * Returns true if the given pool name is the child of an entity
     * (jdbc connection pools or connector connection pools).
     * This is used in monitoringResourceStats.jsf.
     */
    @Handler(id = "isPool",
      input={
        @HandlerInput(name="poolName",   type=String.class, required=true),
        @HandlerInput(name="endpoint",   type=String.class, required=true)},
       output = {
        @HandlerOutput(name = "result", type = Boolean.class)
       })
    public static void isPool(HandlerContext handlerCtx) {
        String poolName = (String) handlerCtx.getInputValue("poolName");
        String endpoint = (String) handlerCtx.getInputValue("endpoint");
        Boolean result = false;
        try {
            List<String> poolNames = new ArrayList(RestUtil.getChildMap(endpoint).keySet());
            if (poolNames.contains(poolName)) {
                result = true;
            }
        } catch (Exception ex) {
            GuiUtil.handleException(handlerCtx, ex);
        }
        
        handlerCtx.setOutputValue("result", result);

    }
    
    /*
     * Returns the jdbc connection pools, connector connection pools,
     * first jdbc element and first connector element for the given set of
     * pool names  and resources endpoint. 
     */
    @Handler(id = "gf.getMonitoringPools",
      input={
        @HandlerInput(name="poolNames",   type=List.class, required=true),
        @HandlerInput(name="endpoint",   type=String.class, required=true)},
       output = {
        @HandlerOutput(name = "jdbcList", type = List.class),
        @HandlerOutput(name = "firstJdbc", type = String.class),
        @HandlerOutput(name = "connectorList", type = List.class),
        @HandlerOutput(name = "firstConnector", type = String.class)
       })
    public static void getMonitoringPools(HandlerContext handlerCtx) {
        List<String> poolNames = (List) handlerCtx.getInputValue("poolNames");
        String endpoint = (String) handlerCtx.getInputValue("endpoint");
        List<String> jdbcMonitorList = new ArrayList();
        List<String> connectorMonitorList = new ArrayList();
        String fisrtJdbc = null;
        String firstConnector = null;

        try {
            List<String> jdbcPools = new ArrayList(RestUtil.getChildMap(endpoint + "/jdbc-connection-pool").keySet());
            List<String> connectorPools = new ArrayList(RestUtil.getChildMap(endpoint + "/connector-connection-pool").keySet());
            for (String poolName : poolNames) {
                if (jdbcPools.contains(poolName)) {
                    jdbcMonitorList.add(poolName);
                } else if (connectorPools.contains(poolName)) {
                    connectorMonitorList.add(poolName);
                }
            }
        } catch (Exception ex) {
            GuiUtil.handleException(handlerCtx, ex);
        }

        handlerCtx.setOutputValue("jdbcList", jdbcMonitorList);
        handlerCtx.setOutputValue("firstJdbc", fisrtJdbc);
        handlerCtx.setOutputValue("connectorList", connectorMonitorList);
        handlerCtx.setOutputValue("firstConnector", firstConnector);
    }

    @Handler(id = "gf.getInstanceMonitorURL",
      input={
        @HandlerInput(name="instanceName",   type=String.class, defaultValue="server")},
       output = {
        @HandlerOutput(name = "monitorURL", type = String.class)
       })
    public static void getInstanceMonitorURL(HandlerContext handlerCtx) {
        String instanceName = (String) handlerCtx.getInputValue("instanceName");
        String monitorURL = (String) GuiUtil.getSessionValue("MONITOR_URL") + "/" + instanceName;
        handlerCtx.setOutputValue("monitorURL", monitorURL);
    }

    public static Boolean doesAppProxyExist(String appName, String moduleName) {
        boolean proxyexist = false;
        Map<String, Object> subComps = getSubComponents(appName, moduleName);
        if (subComps != null && subComps.size() > 0) {
            proxyexist = true;
        }
        return proxyexist;
    }

    public static Map<String, Object> getSubComponents(String appName, String moduleName) {
        String endpoint = GuiUtil.getSessionValue("REST_URL") + "/applications/application/list-sub-components";
        Map<String, Object> attrs = new HashMap();
        attrs.put("appname", appName);
        attrs.put("modulename", moduleName);

        try {
            Map<String, Object> responseMap = RestUtil.restRequest(endpoint, attrs, "GET", null, false);
            Map<String, Object> propsMap = (Map) ((Map) responseMap.get("data")).get("properties");
            if (propsMap != null && propsMap.size() > 0) {
                return propsMap;
            }
        } catch (Exception ex) {
            GuiUtil.getLogger().severe("Error in getSubComponents ; \nendpoint = " + endpoint + "attrs=" + attrs + "method=GET");
            //we don't need to call GuiUtil.handleError() because thats taken care of in restRequest() when we pass in the handler.
        }
        return null;
    }

    public static Boolean doesMonitoringDataExist(String endpoint) {
        if (RestUtil.doesProxyExist(endpoint)) {
            if (getMonitoringStatInfo(endpoint).size() > 0) {
                return true;
            }
        }
        return false;
    }

    public static String modifyStatType(String name) {
        String[] nameStrs = name.split("-");
        String modifiedName = "";
        for (int i = 0; i < nameStrs.length; i++) {
            String tmp = nameStrs[i].substring(0, 1).toUpperCase() + nameStrs[i].substring(1);
            modifiedName = modifiedName + tmp;
        }
        return modifiedName;
    }
   
    private static String formatActiveIdsForDisplay(String str) {
        String values = " ";
        String[] strArray = str.split("%%%EOL%%%");
        if (strArray != null && strArray.length > 0) {
            values = values + "<table>";
            for (String s : (String[]) strArray) {
                if (s.startsWith("Transaction")) {
                    String sh = s.replaceFirst(" ", "_");
                    String[] strHeaders = sh.split(" ");
                    if (strHeaders != null && strHeaders.length > 0) {
                        values = values + "<tr>";
                        for (String h : (String[]) strHeaders) {
                            if (!h.isEmpty()) {
                                values = values + "<td>" + h + "
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.