1 23 24 package com.sun.enterprise.admin.server.core.mbean.meta; 25 26 import java.util.Vector ; 28 import java.util.Collection ; 29 import java.lang.reflect.Method ; 30 import java.lang.reflect.Constructor ; 31 32 import javax.management.MBeanAttributeInfo ; 34 import javax.management.MBeanConstructorInfo ; 35 import javax.management.MBeanOperationInfo ; 36 import javax.management.MBeanParameterInfo ; 37 import javax.management.MBeanNotificationInfo ; 38 import javax.management.MBeanInfo ; 39 import javax.management.IntrospectionException ; 40 41 import com.sun.enterprise.admin.server.core.jmx.Introspector; 43 51 52 public class MBeanInfoBuilder 53 { 54 public static final String kSetterPrefix = "set"; 55 public static final String kGetterPrefix = "get"; 56 private Class mClass = null; 57 private MBeanOperationInfo [] mOperations = null; 58 private MBeanAttributeInfo [] mAttributes = null; 59 private MBeanConstructorInfo [] mConstructors = null; 60 private MBeanNotificationInfo [] mNotifications = null; 61 private MBeanInfo mMBeanInfo = null; 62 63 66 67 public MBeanInfoBuilder(Class aClass) throws IntrospectionException 68 { 69 if(aClass == null) 70 { 71 throw new IllegalArgumentException (); 72 } 73 mClass = aClass; 74 createOperations(); 75 createConstructors(); 76 createAttributes(); 77 createMBeanInfo(); 78 createNotifications(); 79 } 80 81 public MBeanInfoBuilder(String aClassName) 82 { 83 } 84 85 public MBeanInfoBuilder(String aClassName, ClassLoader cl) 86 { 87 } 88 89 96 97 public MBeanOperationInfo [] getMBeanOperations() 98 { 99 return ( mOperations ); 100 } 101 public MBeanConstructorInfo [] getMBeanConstructors() 102 { 103 return ( mConstructors ); 104 } 105 public MBeanInfo getMBeanInfo() 106 { 107 return ( mMBeanInfo ); 108 } 109 public MBeanAttributeInfo [] getMBeanAttributes() 110 { 111 return ( mAttributes ); 112 } 113 private void createOperations() 114 { 115 Collection excludeList = new Vector (); 116 excludeList.add("java.lang.Object"); 117 excludeList.add("com.sun.enterprise.admin.server.core.mbean.config.AdminBase"); 118 Introspector reflector = new Introspector(mClass); 119 Method [] methods = reflector.getCallableInstanceMethods(excludeList); 120 Vector oprVector = new Vector (); 121 for (int i = 0 ; i < methods.length ; i++) 122 { 123 String name = methods[i].getName(); 124 boolean isGetter = name.startsWith(kGetterPrefix); 125 boolean isSetter = name.startsWith(kSetterPrefix); 126 if (!isGetter && !isSetter) 127 { 128 oprVector.add(new MBeanOperationInfo (name, methods[i])); 129 } 130 } 131 mOperations = new MBeanOperationInfo [oprVector.size()]; 132 oprVector.toArray (mOperations); 133 } 134 private void createConstructors() 135 { 136 Constructor [] ctors = mClass.getConstructors(); 137 mConstructors = new MBeanConstructorInfo [ctors.length]; 138 for (int i = 0 ; i < ctors.length ; i++) 139 { 140 String ctorDesc = ctors[i].getName(); 141 MBeanConstructorInfo ctorInfo = 142 new MBeanConstructorInfo (ctorDesc, ctors[i]); 143 mConstructors[i] = ctorInfo; 144 } 145 } 146 147 private void createNotifications() 148 { 149 } 150 151 private void createMBeanInfo() 152 { 153 mMBeanInfo = new MBeanInfo (mClass.getName(), 154 mClass.getName(), 155 mAttributes, mConstructors, mOperations, mNotifications); 156 } 157 161 private void createAttributes() throws IntrospectionException 162 { 163 Vector attrVector = new Vector (); 164 Method [] declMethods = new Introspector(mClass).getDeclaredConcretePublicMethods(); 165 Method [] getters = this.getGetters(declMethods); 166 Method [] setters = this.getSetters(declMethods); 167 168 for (int i = 0 ; i < getters.length ; i ++) 169 { 170 MBeanAttributeInfo attr = null; 171 String methodName = getters[i].getName(); 172 String attrName = getAttributeNameFromGetter(methodName); 173 Method getter = findAttrIn(attrName, getters); 174 Method setter = findAttrIn(attrName, setters); 175 boolean isReadable = ( getter != null ); 176 boolean isWritable = ( setter != null ); 177 boolean isIs = false; 178 179 attr = new MBeanAttributeInfo (attrName, null, attrName, 180 isReadable, isWritable, isIs); 181 attrVector.add(attr); 182 } 183 mAttributes = new MBeanAttributeInfo [attrVector.size()]; 184 attrVector.toArray(mAttributes); 185 } 186 187 private Method [] getGetters(Method [] methods) 188 { 189 Vector getters = new Vector (); 190 for (int i = 0 ; i < methods.length ; i++) 191 { 192 String methodName = methods[i].getName(); 193 if (methodName.startsWith (kGetterPrefix)) 194 { 195 getters.add (methods[i]); 196 } 197 } 198 Method [] getterMethods = new Method [getters.size()]; 199 200 return ( (Method []) getters.toArray (getterMethods) ); 201 } 202 203 private Method [] getSetters(Method [] methods) 204 { 205 Vector setters = new Vector (); 206 for (int i = 0 ; i < methods.length ; i++) 207 { 208 String methodName = methods[i].getName(); 209 if (methodName.startsWith (kSetterPrefix)) 210 { 211 setters.add (methods[i]); 212 } 213 } 214 Method [] setterMethods = new Method [setters.size()]; 215 216 return ( (Method []) setters.toArray (setterMethods) ); 217 } 218 219 private String getAttributeNameFromGetter(String getterName) 220 { 221 int getStartsAt = getterName.indexOf(kGetterPrefix); 222 int attributeStartsAt = getStartsAt + kGetterPrefix.length(); 223 224 return ( getterName.substring (attributeStartsAt) ); 225 } 226 227 private Method findAttrIn(String attrName, Method [] methods) 228 { 229 Method matcher = null; 230 boolean found = false; 231 232 for (int i = 0 ; i < methods.length ; i++) 233 { 234 String methodName = methods[i].getName(); 235 if (methodName.endsWith(attrName)) 236 { 237 matcher = methods[i]; 238 break; 239 } 240 } 241 return ( matcher ); 242 } 243 } | Popular Tags |