1 22 package org.objectweb.fractal.jmx.agent; 23 24 import javax.management.Descriptor ; 25 import javax.management.JMException ; 26 import javax.management.modelmbean.RequiredModelMBean ; 27 import javax.management.modelmbean.DescriptorSupport ; 28 import javax.management.modelmbean.ModelMBeanInfo ; 29 import javax.management.modelmbean.ModelMBeanInfoSupport ; 30 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 31 import javax.management.modelmbean.ModelMBeanOperationInfo ; 32 import javax.management.modelmbean.ModelMBeanConstructorInfo ; 33 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 34 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 35 36 import java.util.Vector ; 37 import java.lang.reflect.Method ; 38 39 43 public class Introspector { 44 private final static boolean VERBOSE = true; 45 private final static boolean VERBOSE_DEBUG = false; 46 47 private Introspector() {} 48 49 58 public static RequiredModelMBean createMBean(Object obj) 59 throws JMException { 60 try{ 61 return createMBean(obj, obj.getClass()); 62 } 63 catch(InvalidTargetObjectTypeException e){ 64 throw new IllegalStateException (e.toString()); 65 } 66 } 67 68 80 public static RequiredModelMBean createMBean(Object obj, Class assign) 81 throws JMException , InvalidTargetObjectTypeException { 82 Class objClass = obj.getClass(); 83 if (!assign.isAssignableFrom(objClass)) 84 throw new InvalidTargetObjectTypeException (assign + " not a superclass or superinterface of " + objClass); 85 Method [] methods = assign.getMethods(); 86 Vector attributes = new Vector (); 87 Vector operators = new Vector (); 88 89 for (int i = 0; i < methods.length; i++) { 91 if ((methods[i].getName().startsWith("get")) 93 || (methods[i].getName().startsWith("set")) 94 || (methods[i].getName().startsWith("is"))) { 95 try { 96 ModelMBeanAttributeInfo mmai = doAttribute(methods, i); 97 if (mmai != null) 98 attributes.add(mmai); 99 } catch (Exception e) { 100 if (VERBOSE) 101 e.printStackTrace(); 102 } 103 } 104 105 try { 107 ModelMBeanOperationInfo mmoi = doOperation(methods, i); 108 if (mmoi != null) 109 operators.add(mmoi); 110 } catch (Exception e) { 111 if (VERBOSE) 112 e.printStackTrace(); 113 } 114 } 115 116 ModelMBeanAttributeInfo [] attributeArray = new ModelMBeanAttributeInfo [attributes.size()]; 118 for (int i_ = 0; i_ < attributeArray.length; i_++) { 119 attributeArray[i_] = (ModelMBeanAttributeInfo ) attributes.get(i_); 120 } 121 122 ModelMBeanOperationInfo [] operatorArray = new ModelMBeanOperationInfo [operators.size()]; 124 for (int i_ = 0; i_ < operatorArray.length; i_++) { 125 operatorArray[i_] = (ModelMBeanOperationInfo ) operators.get(i_); 126 } 127 128 Descriptor desc = new DescriptorSupport (); 130 desc.setField("name", objClass.getName()); 131 desc.setField("descriptorType", "MBean"); 132 133 ModelMBeanInfo mminfo = 135 new ModelMBeanInfoSupport ( 136 objClass.getName(), 137 objClass.getName(), 138 attributeArray, 139 new ModelMBeanConstructorInfo [0], 140 operatorArray, 141 new ModelMBeanNotificationInfo [0], 142 desc); 143 RequiredModelMBean model = new RequiredModelMBean (mminfo); 144 model.setManagedResource(obj, "ObjectReference"); 145 return model; 146 } 147 148 157 private static ModelMBeanOperationInfo doOperation(Method methods[], int i) throws JMException { 158 if (VERBOSE_DEBUG) 159 System.out.println("ObjectIntrospector: processing operation: " + methods[i].getName()); 160 161 Descriptor desc = new DescriptorSupport (); 162 desc.setField("name", methods[i].getName()); 163 desc.setField("descriptorType", "operation"); 164 desc.setField("currencyTimeLimit", "1"); 166 167 if ((methods[i].getName().startsWith("get") || methods[i].getName().startsWith("is")) 170 && (methods[i].getParameterTypes().length == 0)) 171 desc.setField("role", "getter"); 172 else if (methods[i].getName().startsWith("set") && methods[i].getParameterTypes().length == 1) 173 desc.setField("role", "setter"); 174 else 175 desc.setField("role", "operation"); 176 177 ModelMBeanOperationInfo mmoi = new ModelMBeanOperationInfo (methods[i].getName(), methods[i], desc); 178 return mmoi; 179 } 180 181 192 private static ModelMBeanAttributeInfo doAttribute(Method methods[], int i) throws JMException { 193 if (VERBOSE_DEBUG) 194 System.out.println("ObjectIntrospector: processing attribute: " + methods[i].getName()); 195 196 String name = null; 197 Method getter = null; 198 Method setter = null; 199 String type = null; 200 201 if ((methods[i].getName().startsWith("get")) && (methods[i].getParameterTypes().length == 0)) { 203 String setName = "s" + methods[i].getName().substring(1); 204 name = methods[i].getName().substring(3); 205 getter = methods[i]; 206 type = methods[i].getReturnType().getName(); 207 for (int i_ = 0; i_ < methods.length; i_++) { 209 if (methods[i_].getName().equals(setName)) { 210 setter = methods[i_]; 211 break; 212 } 213 } 214 215 } else if ((methods[i].getName().startsWith("set")) && (methods[i].getParameterTypes().length == 1)) { 217 String getName = "g" + methods[i].getName().substring(1); 218 name = methods[i].getName().substring(3); 219 for (int i_ = 0; i_ < methods.length; i_++) { 224 if (methods[i_].getName().equals(getName)) { 225 return null; 226 } 227 } 228 setter = methods[i]; 229 type = methods[i].getParameterTypes()[0].getName(); 230 231 } else if ((methods[i].getName().startsWith("is")) && (methods[i].getParameterTypes() == null)) { 233 name = methods[i].getName().substring(2); 234 getter = methods[i]; 235 type = boolean.class.getName(); 236 } else { 238 return null; 239 } 240 241 Descriptor desc = new DescriptorSupport (); 242 desc.setField("name", name); desc.setField("descriptorType", "attribute"); desc.setField("displayName", name); 245 desc.setField("currencyTimeLimit", "1"); 247 248 if (getter != null) 249 desc.setField("getMethod", getter.getName()); 250 if (setter != null) 251 desc.setField("setMethod", setter.getName()); 252 253 ModelMBeanAttributeInfo mmai = 254 new ModelMBeanAttributeInfo (name, type, name, (getter != null), (setter != null), false, desc); 255 256 return mmai; 257 } 258 } 259 | Popular Tags |