Coverage Report - net.admin4j.ui.servlets.JmxServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
JmxServlet
56%
29/51
35%
5/14
3.455
JmxServlet$JmxUtils
3%
1/29
0%
0/22
3.455
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.ui.servlets;
 15  
 
 16  
 import java.io.IOException;
 17  
 import java.lang.management.ManagementFactory;
 18  
 import java.util.ArrayList;
 19  
 import java.util.Arrays;
 20  
 import java.util.HashMap;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 import java.util.TreeSet;
 24  
 
 25  
 import javax.management.AttributeList;
 26  
 import javax.management.MBeanInfo;
 27  
 import javax.management.MBeanOperationInfo;
 28  
 import javax.management.MBeanParameterInfo;
 29  
 import javax.management.MBeanServer;
 30  
 import javax.management.ObjectInstance;
 31  
 import javax.management.ObjectName;
 32  
 import javax.management.openmbean.CompositeData;
 33  
 import javax.management.openmbean.TabularData;
 34  
 import javax.servlet.ServletException;
 35  
 import javax.servlet.http.HttpServletRequest;
 36  
 import javax.servlet.http.HttpServletResponse;
 37  
 
 38  
 import net.admin4j.deps.commons.lang3.exception.ExceptionUtils;
 39  
 import net.admin4j.util.Admin4jRuntimeException;
 40  
 import net.admin4j.util.GuiUtils;
 41  
 
 42  
 /**
 43  
  * Provides basic display capability for published JMX beans.
 44  
  * 
 45  
  * <p>You will need to map this servlet to an url pattern (e.g. '/admin4j/jmx').</p>
 46  
  * <p>This servlet does <b>not</b> require other web resources.</p>
 47  
  * @author D. Ashmore
 48  
  * @since 1.0
 49  
  */
 50  
 @SuppressWarnings("serial")
 51  6
 public class JmxServlet extends AdminDisplayServlet {
 52  
     
 53  
     public static final String PUBLIC_HANDLE="jmx";
 54  
         
 55  
         @Override
 56  
         protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 57  
            
 58  6
             MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
 59  6
         Map<String, ObjectInstance> jmxBeanMap = new HashMap<String, ObjectInstance>();
 60  
         
 61  6
         String selectedJmxBeanStr = request.getParameter("jmxBean");
 62  6
         ObjectInstance selectedJmxBean = null;
 63  
         
 64  
         ObjectInstance jmxBean;
 65  6
         for (Object obj: mbs.queryMBeans(null, null)) {
 66  116
             jmxBean = (ObjectInstance)obj;
 67  116
             jmxBeanMap.put(jmxBean.getObjectName().getCanonicalName(), jmxBean);
 68  116
             if (jmxBean.getObjectName().getCanonicalName().equals(selectedJmxBeanStr)) {
 69  0
                 selectedJmxBean = jmxBean;
 70  
             }
 71  
         }
 72  
                 
 73  6
         String selectedOperationExecStr = request.getParameter("opExec");
 74  6
         String operationInvokationResultStr = null;
 75  
         
 76  6
         if (selectedOperationExecStr != null) {
 77  
 
 78  
             try {
 79  0
                 Object invokeReturn = mbs.invoke(selectedJmxBean.getObjectName(), selectedOperationExecStr, null, null);
 80  0
                 if (invokeReturn != null) {
 81  0
                     operationInvokationResultStr = invokeReturn.toString();
 82  
                 }
 83  0
                 else operationInvokationResultStr = "Executed successfully.";
 84  
             }
 85  0
             catch (Throwable t) {
 86  
                 operationInvokationResultStr = ExceptionUtils.getStackTrace(t);
 87  0
             }
 88  
         }
 89  
         
 90  6
         AttributeList aList = new AttributeList();
 91  6
         List<MBeanOperationInfo> oList = new ArrayList<MBeanOperationInfo>();
 92  6
         MBeanInfo mbInfo = null;
 93  
         
 94  6
         if (selectedJmxBean != null) {
 95  
             try {
 96  0
                 mbInfo = mbs.getMBeanInfo(selectedJmxBean.getObjectName());
 97  0
             } catch (Exception e) {
 98  0
                 throw new Admin4jRuntimeException(e);
 99  0
             } 
 100  
                         
 101  
             try {
 102  0
                 aList = mbs.getAttributes(selectedJmxBean.getObjectName(), findMBeanAttr(mbs, selectedJmxBean.getObjectName()));   
 103  0
                 oList = Arrays.asList(mbInfo.getOperations());
 104  0
             } catch (Exception e) {
 105  0
                 throw new Admin4jRuntimeException(e);
 106  0
             } 
 107  
         }
 108  
         
 109  6
         Map<String,Object> variableMap = new HashMap<String,Object>();
 110  6
         variableMap.put("jmxBeanList", new TreeSet<String>(jmxBeanMap.keySet()));
 111  6
         variableMap.put("selectedJmxBeanName", selectedJmxBeanStr);
 112  6
         variableMap.put("selectedOperationExecStr", selectedOperationExecStr);
 113  6
         variableMap.put("mBeanAttributeList", aList);
 114  6
         variableMap.put("mBeanOperationList", oList);
 115  6
         variableMap.put("mBeanInfo", mbInfo);
 116  6
         variableMap.put("operationInvokationResultStr", operationInvokationResultStr);
 117  6
         variableMap.put("JmxUtils", new JmxUtils());
 118  
         
 119  6
         this.displayFreeMarkerPage(request, response, "jmxServletDisplay.ftl", variableMap);
 120  6
         }
 121  
 
 122  
         private String[] findMBeanAttr(MBeanServer mbs, ObjectName oName) throws Exception {
 123  0
                 List<String> nameList = new ArrayList<String>();
 124  0
                 MBeanInfo mbInfo = mbs.getMBeanInfo(oName);
 125  
                 
 126  0
                 for (int i = 0; i < mbInfo.getAttributes().length; i++) {
 127  0
                         if (mbInfo.getAttributes()[i].isReadable()) {
 128  0
                                 nameList.add(mbInfo.getAttributes()[i].getName());
 129  
                         }
 130  
                 }
 131  
                 
 132  0
                 return (String[])nameList.toArray(new String[0]);
 133  
         }
 134  
         
 135  6
         public static class JmxUtils {
 136  
             public boolean isCompositeData(Object obj) {
 137  0
                 return obj instanceof CompositeData;
 138  
             }
 139  
             
 140  
             public boolean isTabularData(Object obj) {
 141  0
             return obj instanceof TabularData;
 142  
         }
 143  
             
 144  
             public boolean isArray(Object obj) {
 145  0
                 if (obj == null)  return false;
 146  0
             return obj.getClass().isArray();
 147  
         }
 148  
             
 149  
             public boolean isOperationExecutable(Object obj) {
 150  0
                 if (obj == null)  return false;
 151  0
                 if (obj instanceof MBeanOperationInfo) {
 152  0
                     MBeanOperationInfo oInfo = (MBeanOperationInfo)obj;
 153  0
                     return oInfo.getSignature() == null || (oInfo.getSignature() != null && oInfo.getSignature().length == 0);
 154  
                 }
 155  
                 
 156  0
                 return false;
 157  
             }
 158  
             
 159  
             public String htmlEscape(Object obj) {
 160  0
                 return new GuiUtils().htmlEscape(obj);
 161  
             }
 162  
 
 163  
             public List<String> compositeKeyList(Object obj) {
 164  0
                 List<String> list = new ArrayList<String>();
 165  
                 
 166  0
                 if (obj instanceof CompositeData) {
 167  0
                     list.addAll( ((CompositeData)obj).getCompositeType().keySet());
 168  
                 }
 169  
                 
 170  0
                 return list;
 171  
             }
 172  
             
 173  
             public Object compositeValue(Object composite, Object key) {
 174  0
                 if (composite instanceof CompositeData) {
 175  0
                     return ((CompositeData)composite).get( (String)key);
 176  
                 }
 177  
                 
 178  0
                 return null;
 179  
             }
 180  
             
 181  
             public String formatOperationSignature(MBeanOperationInfo oInfo) {                
 182  0
                 if (oInfo == null)  return "()";
 183  0
                 MBeanParameterInfo[] pInfo = oInfo.getSignature();
 184  0
                 StringBuffer buffer = new StringBuffer(64);
 185  0
                 buffer.append("(");
 186  
                 
 187  0
                 for (int i = 0; i < pInfo.length; i++) {
 188  0
                     if (i > 0) buffer.append(", ");
 189  0
                     buffer.append(pInfo[i].getType());
 190  0
                     buffer.append(" ");
 191  0
                     buffer.append(pInfo[i].getName());
 192  
                 }
 193  
                 
 194  0
                 buffer.append(")");
 195  0
                 return buffer.toString();
 196  
             }
 197  
         }
 198  
 
 199  
         /* (non-Javadoc)
 200  
      * @see net.admin4j.ui.servlets.Admin4JServlet#getServletLabel()
 201  
      */
 202  
     @Override
 203  
     public String getServletLabel() {
 204  3
         return "JMX Browser";
 205  
     }
 206  
         
 207  
 }