1 23 24 package com.sun.enterprise.admin.server.core.jmx; 25 26 import java.lang.reflect.*; 28 29 import javax.management.NotCompliantMBeanException ; 31 32 38 39 public class MBeanIntrospector extends Introspector 40 { 41 private boolean mIsStandard; 42 private boolean mIsDynamic; 43 private Class mMBeanInterfaceClass; 44 45 48 public MBeanIntrospector(Class c) throws NotCompliantMBeanException 49 { 50 super(c); 51 checkBasic(c); 52 checkMBeanType(c); 53 checkCompliance(); 54 } 55 56 public boolean isStandardMBean() 57 { 58 return mIsStandard; 59 } 60 61 public boolean isDynamicMBean() 62 { 63 return mIsDynamic; 64 } 65 66 public Class getMBeanInterfaceClass() 67 { 68 return mMBeanInterfaceClass; 69 } 70 71 public boolean isSupported(String methodName, Class [] signature) 72 { 73 boolean isSupported = false; 74 try 75 { 76 Method m = mMBeanInterfaceClass.getMethod(methodName, signature); 77 isSupported = (m != null); 78 } 79 catch (Exception e) 80 { 81 } 82 return isSupported; 83 } 84 85 private void checkBasic(Class c) throws NotCompliantMBeanException 86 { 87 if (!isInstantiableJavaClass(c)) 88 { 89 throw new NotCompliantMBeanException (); 90 } 91 } 92 93 private void checkMBeanType(Class c) 94 { 95 109 110 boolean isStandard = isStandard(c); 111 boolean isDynamic = isDynamic(c); 112 if (!(isStandard || isDynamic)) { 114 Class superClass = c.getSuperclass(); 115 if ((superClass != null) && 117 (superClass != java.lang.Object .class)) 118 { 119 checkMBeanType(superClass); 120 } 121 } 122 127 if (isStandard) 128 { 129 mIsStandard = true; 130 setStandardMBeanInterface(c); 131 } 132 if (isDynamic) 133 { 134 mIsDynamic = true; 135 setDynamicMBeanInterface(); 136 } 137 } 138 139 private void checkCompliance() throws NotCompliantMBeanException 140 { 141 if (!(mIsStandard || mIsDynamic) || (mIsStandard && mIsDynamic)) 143 { 144 throw new NotCompliantMBeanException (); 145 } 146 } 147 148 private void setStandardMBeanInterface(Class c) 149 { 150 String className = deriveStandardMBeanIntfClassName(c); 151 mMBeanInterfaceClass = getImplementedMBeanClass(c, className); 152 } 153 154 private void setDynamicMBeanInterface() 155 { 156 mMBeanInterfaceClass = javax.management.DynamicMBean .class; 157 } 158 159 private boolean isStandard(Class c) 160 { 161 boolean isStandard = false; 162 166 String className = deriveStandardMBeanIntfClassName(c); 167 if (getImplementedMBeanClass(c, className) != null) 168 { 169 isStandard = true; 170 } 171 return isStandard; 172 } 173 174 private boolean isDynamic(Class c) 175 { 176 boolean isDynamic = false; 177 Class [] interfaces = c.getInterfaces(); 178 182 int length = interfaces.length; 183 for (int i = 0; i < length; i++) 184 { 185 if (interfaces[i] == javax.management.DynamicMBean .class) 186 { 187 isDynamic = true; 188 break; 189 } 190 } 191 return isDynamic; 192 } 193 194 private boolean isInstantiableJavaClass(Class c) 195 { 196 boolean isInstantiable = false; 197 if (!c.isPrimitive() && !c.isArray()) 198 { 199 int modifiers = c.getModifiers(); 200 boolean isInterface = Modifier.isInterface(modifiers); 201 boolean isAbstract = Modifier.isAbstract(modifiers); 202 isInstantiable = !(isInterface || isAbstract); 203 } 204 return isInstantiable; 205 } 206 207 210 private String deriveStandardMBeanIntfClassName(Class c) 211 { 212 String className = truncateClassName(c); 213 return (className + "MBean"); 214 } 215 216 219 private String truncateClassName(Class c) 220 { 221 String className = c.getName(); 222 int lastDot = className.lastIndexOf('.'); 223 className = className.substring(lastDot + 1); 224 return className; 225 } 226 227 private Class getImplementedMBeanClass(Class c, String intfName) 228 { 229 Class implementedMBean = null; 230 Class [] interfaces = c.getInterfaces(); 231 235 for (int i = 0; i < interfaces.length; i++) 236 { 237 String className = truncateClassName(interfaces[i]); 238 if (className.equals(intfName)) 239 { 240 implementedMBean = interfaces[i]; 241 break; 242 } 243 } 244 return implementedMBean; 245 } 246 247 private final String convertToCamelCase(String str) 248 { 249 String camelCase = ""; 250 char c = str.charAt(0); 251 camelCase += Character.toUpperCase(c); 252 if (str.length() > 1) 253 { 254 camelCase += str.substring(1); 255 } 256 return camelCase; 257 } 258 } 259 | Popular Tags |