1 23 24 38 39 package com.sun.enterprise.admin.monitor.registry.spi; 40 import javax.management.*; 41 import javax.management.j2ee.statistics.*; 42 import java.lang.reflect.*; 43 import java.util.*; 44 import java.util.logging.*; 45 50 public class ManagedResourceIntrospector { 51 final boolean READABLE = true; 52 final boolean WRITABLE = true; 53 final boolean ISGETTER = true; 54 55 DynamicMBean mbean; 56 Vector attributes; 57 public static final String LOGGER_NAME="this.is.console"; 58 final Logger logger; 59 60 ManagedResourceIntrospector(DynamicMBean mbean){ 61 this.mbean=mbean; 62 attributes=new Vector(); 63 logger = Logger.getLogger(LOGGER_NAME); 64 } 65 66 MBeanInfo introspect(Stats stats){ 67 return new MBeanInfo( 68 mbean.getClass().getName(), "Managed Object for "+stats.getClass().getName()+ " managed resource", getAttributeInfo(stats), null, getOperationInfo(stats), null ); 75 76 } 77 78 83 MBeanAttributeInfo[] getAttributeInfo(Stats stats){ 84 MBeanAttributeInfo[] attrInfo=null; 85 if(stats != null){ 86 Object [] attrs = deriveUnderlyingAttributes(stats); 87 attrInfo = new MBeanAttributeInfo[attrs.length]; 88 for(int i= 0; i < attrs.length; i++){ 89 attrInfo[i] = new MBeanAttributeInfo((String )attrs[i],Statistic.class.getName(), 90 "Attribute"+attrs[i], READABLE, !WRITABLE, !ISGETTER); 91 92 } 93 } 94 return attrInfo; 95 } 96 97 102 Object [] deriveUnderlyingAttributes(Stats stats){ 103 String [] attrs = stats.getStatisticNames(); 104 105 for(int i=0; i< attrs.length; i++){ 106 introspectEachStatistic((stats.getStatistic(attrs[i])).getClass(), attrs[i]); 107 } 108 String [] a = new String [attributes.size()]; 109 return attributes.toArray(a); 110 } 111 112 void introspectEachStatistic(Class statistic, String statName){ 113 Set a = new HashSet(Arrays.asList(statistic.getMethods())); 114 Iterator it = a.iterator(); 115 while(it.hasNext()){ 116 String s = (String )((Method) it.next()).getName(); 117 if(s.startsWith("get")&& !s.equals("getClass")){ 118 s = s.replaceFirst("get",""); 119 attributes.add(AttributeStringHelper.joinAttributes(statName,s)); 120 } 121 } 122 } 123 124 129 MBeanOperationInfo[] getOperationInfo(Stats stats){ 130 Method[] opers = stats.getClass().getMethods(); 131 MBeanOperationInfo[] operInfo = new MBeanOperationInfo[opers.length]; 132 for(int i= 0; i < opers.length; i++){ 133 if(!isAttrGetterOrSetter(opers[i])){ 134 operInfo[i]= createOperationInfo(opers[i]); 135 } 136 } 137 operInfo = addMoreMBeanOperations(operInfo); 138 return operInfo; 139 } 140 141 145 private MBeanOperationInfo[] addMoreMBeanOperations(MBeanOperationInfo[] operInfo){ 146 MBeanOperationInfo oper = new MBeanOperationInfo("listAttributes", "Method listAttributes", null, String .class.getName(), MBeanOperationInfo.INFO ); 152 MBeanOperationInfo[] opers = new MBeanOperationInfo[operInfo.length+1]; 153 opers = operInfo; 154 opers[opers.length-1] = oper; 155 return operInfo; 156 } 157 158 163 boolean isAttrGetterOrSetter(Method operation){ 164 if(operation.getName().startsWith("get") 165 || operation.getName().startsWith("set")){ 166 return true; 167 } 168 return false; 169 } 170 171 175 MBeanOperationInfo createOperationInfo(Method oper){ 176 return new MBeanOperationInfo(oper.getName(), "Method "+oper.getName(), getParameterInfo(oper.getParameterTypes()), oper.getReturnType().getName(), MBeanOperationInfo.INFO ); 182 } 183 184 188 MBeanParameterInfo[] getParameterInfo(Class [] paramTypes){ 189 MBeanParameterInfo[] params=null; 190 if(paramTypes != null){ 191 params = new MBeanParameterInfo[paramTypes.length]; 192 193 for(int i=0; i<paramTypes.length;i++){ 194 try{ 195 params[i] = new MBeanParameterInfo("param"+i, 196 paramTypes[i].getName(), 197 paramTypes[i].getName()); 198 } 199 catch(java.lang.IllegalArgumentException e){ 200 logger.log(Level.INFO, e.toString()); 201 } 202 } 203 } 204 return params; 205 } 206 } 207 | Popular Tags |