1 17 18 package org.apache.tomcat.util.modeler.modules; 19 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.Modifier ; 22 import java.math.BigDecimal ; 23 import java.math.BigInteger ; 24 import java.util.ArrayList ; 25 import java.util.Enumeration ; 26 import java.util.Hashtable ; 27 import java.util.List ; 28 29 import javax.management.ObjectName ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.tomcat.util.modeler.AttributeInfo; 34 import org.apache.tomcat.util.modeler.ManagedBean; 35 import org.apache.tomcat.util.modeler.OperationInfo; 36 import org.apache.tomcat.util.modeler.ParameterInfo; 37 import org.apache.tomcat.util.modeler.Registry; 38 39 public class MbeansDescriptorsIntrospectionSource extends ModelerSource 40 { 41 private static Log log = LogFactory.getLog(MbeansDescriptorsIntrospectionSource.class); 42 43 Registry registry; 44 String location; 45 String type; 46 Object source; 47 List mbeans=new ArrayList (); 48 49 public void setRegistry(Registry reg) { 50 this.registry=reg; 51 } 52 53 public void setLocation( String loc ) { 54 this.location=loc; 55 } 56 57 61 public void setType( String type ) { 62 this.type=type; 63 } 64 65 public void setSource( Object source ) { 66 this.source=source; 67 } 68 69 public List loadDescriptors( Registry registry, String location, 70 String type, Object source) 71 throws Exception 72 { 73 setRegistry(registry); 74 setLocation(location); 75 setType(type); 76 setSource(source); 77 execute(); 78 return mbeans; 79 } 80 81 public void execute() throws Exception { 82 if( registry==null ) registry=Registry.getRegistry(); 83 try { 84 ManagedBean managed=createManagedBean(registry, null, (Class )source, type); 85 if( managed==null ) return; 86 managed.setName( type ); 87 88 mbeans.add(managed); 89 90 } catch( Exception ex ) { 91 log.error( "Error reading descriptors ", ex); 92 } 93 } 94 95 96 97 99 static Hashtable specialMethods=new Hashtable (); 100 static { 101 specialMethods.put( "preDeregister", ""); 102 specialMethods.put( "postDeregister", ""); 103 } 104 105 private static String strArray[]=new String [0]; 106 private static ObjectName objNameArray[]=new ObjectName [0]; 107 109 private static Class [] supportedTypes = new Class [] { 110 Boolean .class, 111 Boolean.TYPE, 112 Byte .class, 113 Byte.TYPE, 114 Character .class, 115 Character.TYPE, 116 Short .class, 117 Short.TYPE, 118 Integer .class, 119 Integer.TYPE, 120 Long .class, 121 Long.TYPE, 122 Float .class, 123 Float.TYPE, 124 Double .class, 125 Double.TYPE, 126 String .class, 127 strArray.getClass(), 128 BigDecimal .class, 129 BigInteger .class, 130 ObjectName .class, 131 objNameArray.getClass(), 132 java.io.File .class, 133 }; 134 135 142 private boolean supportedType(Class ret) { 143 for (int i = 0; i < supportedTypes.length; i++) { 144 if (ret == supportedTypes[i]) { 145 return true; 146 } 147 } 148 if (isBeanCompatible(ret)) { 149 return true; 150 } 151 return false; 152 } 153 154 161 protected boolean isBeanCompatible(Class javaType) { 162 if (javaType.isArray() || javaType.isPrimitive()) { 164 return false; 165 } 166 167 if (javaType.getName().startsWith("java.") || 170 javaType.getName().startsWith("javax.")) { 171 return false; 172 } 173 174 try { 175 javaType.getConstructor(new Class []{}); 176 } catch (java.lang.NoSuchMethodException e) { 177 return false; 178 } 179 180 Class superClass = javaType.getSuperclass(); 182 if (superClass != null && 183 superClass != java.lang.Object .class && 184 superClass != java.lang.Exception .class && 185 superClass != java.lang.Throwable .class) { 186 if (!isBeanCompatible(superClass)) { 187 return false; 188 } 189 } 190 return true; 191 } 192 193 203 private void initMethods(Class realClass, 204 Method methods[], 205 Hashtable attMap, Hashtable getAttMap, 206 Hashtable setAttMap, Hashtable invokeAttMap) 207 { 208 for (int j = 0; j < methods.length; ++j) { 209 String name=methods[j].getName(); 210 211 if( Modifier.isStatic(methods[j].getModifiers())) 212 continue; 213 if( ! Modifier.isPublic( methods[j].getModifiers() ) ) { 214 if( log.isDebugEnabled()) 215 log.debug("Not public " + methods[j] ); 216 continue; 217 } 218 if( methods[j].getDeclaringClass() == Object .class ) 219 continue; 220 Class params[]=methods[j].getParameterTypes(); 221 222 if( name.startsWith( "get" ) && params.length==0) { 223 Class ret=methods[j].getReturnType(); 224 if( ! supportedType( ret ) ) { 225 if( log.isDebugEnabled() ) 226 log.debug("Unsupported type " + methods[j]); 227 continue; 228 } 229 name=unCapitalize( name.substring(3)); 230 231 getAttMap.put( name, methods[j] ); 232 attMap.put( name, methods[j] ); 234 } else if( name.startsWith( "is" ) && params.length==0) { 235 Class ret=methods[j].getReturnType(); 236 if( Boolean.TYPE != ret ) { 237 if( log.isDebugEnabled() ) 238 log.debug("Unsupported type " + methods[j] + " " + ret ); 239 continue; 240 } 241 name=unCapitalize( name.substring(2)); 242 243 getAttMap.put( name, methods[j] ); 244 attMap.put( name, methods[j] ); 246 247 } else if( name.startsWith( "set" ) && params.length==1) { 248 if( ! supportedType( params[0] ) ) { 249 if( log.isDebugEnabled() ) 250 log.debug("Unsupported type " + methods[j] + " " + params[0]); 251 continue; 252 } 253 name=unCapitalize( name.substring(3)); 254 setAttMap.put( name, methods[j] ); 255 attMap.put( name, methods[j] ); 256 } else { 257 if( params.length == 0 ) { 258 if( specialMethods.get( methods[j].getName() ) != null ) 259 continue; 260 invokeAttMap.put( name, methods[j]); 261 } else { 262 boolean supported=true; 263 for( int i=0; i<params.length; i++ ) { 264 if( ! supportedType( params[i])) { 265 supported=false; 266 break; 267 } 268 } 269 if( supported ) 270 invokeAttMap.put( name, methods[j]); 271 } 272 } 273 } 274 } 275 276 289 public ManagedBean createManagedBean(Registry registry, String domain, 290 Class realClass, String type) 291 { 292 ManagedBean mbean= new ManagedBean(); 293 294 Method methods[]=null; 295 296 Hashtable attMap=new Hashtable (); 297 Hashtable getAttMap=new Hashtable (); 299 Hashtable setAttMap=new Hashtable (); 301 Hashtable invokeAttMap=new Hashtable (); 303 304 methods = realClass.getMethods(); 305 306 initMethods(realClass, methods, attMap, getAttMap, setAttMap, invokeAttMap ); 307 308 try { 309 310 Enumeration en=attMap.keys(); 311 while( en.hasMoreElements() ) { 312 String name=(String )en.nextElement(); 313 AttributeInfo ai=new AttributeInfo(); 314 ai.setName( name ); 315 Method gm=(Method )getAttMap.get(name); 316 if( gm!=null ) { 317 ai.setGetMethod( gm.getName()); 319 Class t=gm.getReturnType(); 320 if( t!=null ) 321 ai.setType( t.getName() ); 322 } 323 Method sm=(Method )setAttMap.get(name); 324 if( sm!=null ) { 325 Class t=sm.getParameterTypes()[0]; 327 if( t!=null ) 328 ai.setType( t.getName()); 329 ai.setSetMethod( sm.getName()); 330 } 331 ai.setDescription("Introspected attribute " + name); 332 if( log.isDebugEnabled()) log.debug("Introspected attribute " + 333 name + " " + gm + " " + sm); 334 if( gm==null ) 335 ai.setReadable(false); 336 if( sm==null ) 337 ai.setWriteable(false); 338 if( sm!=null || gm!=null ) 339 mbean.addAttribute(ai); 340 } 341 342 en=invokeAttMap.keys(); 343 while( en.hasMoreElements() ) { 344 String name=(String )en.nextElement(); 345 Method m=(Method )invokeAttMap.get(name); 346 if( m!=null && name != null ) { 347 OperationInfo op=new OperationInfo(); 348 op.setName(name); 349 op.setReturnType(m.getReturnType().getName()); 350 op.setDescription("Introspected operation " + name); 351 Class parms[]=m.getParameterTypes(); 352 for(int i=0; i<parms.length; i++ ) { 353 ParameterInfo pi=new ParameterInfo(); 354 pi.setType(parms[i].getName()); 355 pi.setName( "param" + i); 356 pi.setDescription("Introspected parameter param" + i); 357 op.addParameter(pi); 358 } 359 mbean.addOperation(op); 360 } else { 361 log.error("Null arg " + name + " " + m ); 362 } 363 } 364 365 386 387 if( log.isDebugEnabled()) 388 log.debug("Setting name: " + type ); 389 mbean.setName( type ); 390 391 return mbean; 392 } catch( Exception ex ) { 393 ex.printStackTrace(); 394 return null; 395 } 396 } 397 398 399 407 private static String unCapitalize(String name) { 408 if (name == null || name.length() == 0) { 409 return name; 410 } 411 char chars[] = name.toCharArray(); 412 chars[0] = Character.toLowerCase(chars[0]); 413 return new String (chars); 414 } 415 416 } 417 418 | Popular Tags |