1 7 8 package com.sun.jmx.mbeanserver; 9 10 import java.lang.reflect.Constructor ; 11 import java.lang.reflect.InvocationTargetException ; 12 import java.io.*; 13 14 import javax.management.*; 15 16 import com.sun.jmx.trace.Trace; 17 import sun.reflect.misc.ReflectUtil; 18 19 28 class MBeanInstantiatorImpl implements MBeanInstantiator { 29 30 private final ModifiableClassLoaderRepository clr; 31 33 34 private final static String dbgTag = "MBeanInstantiatorImpl"; 35 36 public MBeanInstantiatorImpl(ModifiableClassLoaderRepository clr) { 37 this.clr = clr; 38 } 39 40 41 public void testCreation(Class c) throws NotCompliantMBeanException { 42 Introspector.testCreation(c); 43 } 44 45 public Class findClassWithDefaultLoaderRepository(String className) 46 throws ReflectionException { 47 48 Class theClass; 49 if (className == null) { 50 throw new RuntimeOperationsException(new 51 IllegalArgumentException ("The class name cannot be null"), 52 "Exception occured during object instantiation"); 53 } 54 55 try { 56 if (clr == null) throw new ClassNotFoundException (className); 57 theClass = clr.loadClass(className); 58 } 59 catch (ClassNotFoundException ee) { 60 throw new ReflectionException(ee, 61 "The MBean class could not be loaded by the default loader repository"); 62 } 63 64 return theClass; 65 } 66 67 68 public Class findClass(String className, ClassLoader loader) 69 throws ReflectionException { 70 71 return loadClass(className,loader); 72 } 73 74 public Class findClass(String className, ObjectName aLoader) 75 throws ReflectionException, InstanceNotFoundException { 76 Class theClass = null; 77 78 if (aLoader == null) 79 throw new RuntimeOperationsException(new 80 IllegalArgumentException (), "Null loader passed in parameter"); 81 82 ClassLoader loader = null; 84 synchronized(this) { 85 if (clr!=null) 86 loader = clr.getClassLoader(aLoader); 87 } 88 if (loader == null) { 89 throw new InstanceNotFoundException("The loader named " + 90 aLoader + " is not registered in the MBeanServer"); 91 } 92 return findClass(className,loader); 93 } 94 95 96 public Class [] findSignatureClasses(String signature[], 97 ClassLoader loader) 98 throws ReflectionException { 99 100 if (signature == null) return null; 101 final ClassLoader aLoader = (ClassLoader ) loader; 102 final int length= signature.length; 103 final Class tab[]=new Class [length]; 104 105 if (length == 0) return tab; 106 try { 107 for (int i= 0; i < length; i++) { 108 112 final Class primCla = 113 StandardMetaDataImpl.findClassForPrim(signature[i]); 114 if (primCla != null) { 115 tab[i] = primCla; 116 continue; 117 } 118 119 if (aLoader != null) { 123 tab[i] = Class.forName(signature[i], false, aLoader); 127 } else { 128 tab[i] = findClass(signature[i], 131 this.getClass().getClassLoader()); 132 } 133 } 134 } catch (ClassNotFoundException e) { 135 debugX("findSignatureClasses",e); 136 throw new ReflectionException(e, 137 "The parameter class could not be found"); 138 } catch (RuntimeException e) { 139 debugX("findSignatureClasses",e); 140 throw e; 141 } 142 return tab; 143 } 144 145 146 public Object instantiate(Class theClass) 147 throws ReflectionException, MBeanException { 148 Object moi = null; 149 150 151 Constructor cons = 154 StandardMetaDataImpl.findConstructor(theClass, null); 155 if (cons == null) { 156 throw new ReflectionException(new 157 NoSuchMethodException ("No such constructor")); 158 } 159 try { 161 ReflectUtil.checkPackageAccess(theClass); 162 moi= cons.newInstance((Object []) null); 163 } catch (InvocationTargetException e) { 164 Throwable t = e.getTargetException(); 166 if (t instanceof RuntimeException ) { 167 throw new RuntimeMBeanException((RuntimeException )t, 168 "RuntimeException thrown in the MBean's empty constructor"); 169 } else if (t instanceof Error ) { 170 throw new RuntimeErrorException((Error ) t, 171 "Error thrown in the MBean's empty constructor"); 172 } else { 173 throw new MBeanException((Exception ) t, 174 "Exception thrown in the MBean's empty constructor"); 175 } 176 } catch (NoSuchMethodError error) { 177 throw new ReflectionException(new 178 NoSuchMethodException ("No constructor"), 179 "No such constructor"); 180 } catch (InstantiationException e) { 181 throw new ReflectionException(e, 182 "Exception thrown trying to invoke the MBean's empty constructor"); 183 } catch (IllegalAccessException e) { 184 throw new ReflectionException(e, 185 "Exception thrown trying to invoke the MBean's empty constructor"); 186 } catch (IllegalArgumentException e) { 187 throw new ReflectionException(e, 188 "Exception thrown trying to invoke the MBean's empty constructor"); 189 } 190 return moi; 191 192 } 193 194 195 196 public Object instantiate(Class theClass, Object params[], 197 String signature[], ClassLoader loader) 198 throws ReflectionException, MBeanException { 199 201 final Class [] tab; 204 Object moi= null; 205 try { 206 ClassLoader aLoader= (ClassLoader ) theClass.getClassLoader(); 209 tab = 212 ((signature == null)?null: 213 findSignatureClasses(signature,aLoader)); 214 } 215 catch (IllegalArgumentException e) { 217 throw new ReflectionException(e, 218 "The constructor parameter classes could not be loaded"); 219 } 220 221 Constructor cons = null; 223 cons= StandardMetaDataImpl.findConstructor(theClass, tab); 224 225 if (cons == null) { 226 throw new ReflectionException(new 227 NoSuchMethodException ("No such constructor")); 228 } 229 try { 230 ReflectUtil.checkPackageAccess(theClass); 231 moi = cons.newInstance(params); 232 } 233 catch (NoSuchMethodError error) { 234 throw new ReflectionException(new 235 NoSuchMethodException ("No such constructor found"), 236 "No such constructor" ); 237 } 238 catch (InstantiationException e) { 239 throw new ReflectionException(e, 240 "Exception thrown trying to invoke the MBean's constructor"); 241 } 242 catch (IllegalAccessException e) { 243 throw new ReflectionException(e, 244 "Exception thrown trying to invoke the MBean's constructor"); 245 } 246 catch (InvocationTargetException e) { 247 Throwable th = e.getTargetException(); 249 if (th instanceof RuntimeException ) { 250 throw new RuntimeMBeanException((RuntimeException )th, 251 "RuntimeException thrown in the MBean's constructor"); 252 } else if (th instanceof Error ) { 253 throw new RuntimeErrorException((Error ) th, 254 "Error thrown in the MBean's constructor"); 255 } else { 256 throw new MBeanException((Exception ) th, 257 "Exception thrown in the MBean's constructor"); 258 } 259 } 260 return moi; 261 } 262 263 public ObjectInputStream deserialize(ClassLoader loader, byte[] data) 264 throws OperationsException { 265 266 if (data == null) { 268 throw new RuntimeOperationsException(new 269 IllegalArgumentException (), "Null data passed in parameter"); 270 } 271 if (data.length == 0) { 272 throw new RuntimeOperationsException(new 273 IllegalArgumentException (), "Empty data passed in parameter"); 274 } 275 276 ByteArrayInputStream bIn; 278 ObjectInputStream objIn; 279 String typeStr; 280 281 bIn = new ByteArrayInputStream(data); 282 try { 283 objIn = new ObjectInputStreamWithLoader(bIn,loader); 284 } catch (IOException e) { 285 throw new OperationsException( 286 "An IOException occured trying to de-serialize the data"); 287 } 288 289 return objIn; 290 } 291 292 public ObjectInputStream deserialize(String className, 293 ObjectName loaderName, 294 byte[] data, 295 ClassLoader loader) 296 throws InstanceNotFoundException, 297 OperationsException, 298 ReflectionException { 299 300 if (data == null) { 302 throw new RuntimeOperationsException(new 303 IllegalArgumentException (), "Null data passed in parameter"); 304 } 305 if (data.length == 0) { 306 throw new RuntimeOperationsException(new 307 IllegalArgumentException (), "Empty data passed in parameter"); 308 } 309 if (className == null) { 310 throw new RuntimeOperationsException(new 311 IllegalArgumentException (), "Null className passed in parameter"); 312 } 313 Class theClass = null; 314 if (loaderName == null) { 315 theClass = findClass(className, loader); 317 318 } else { 319 try { 321 ClassLoader instance = null; 322 323 if (clr!=null) 324 instance = clr.getClassLoader(loaderName); 325 if (instance == null) 326 throw new ClassNotFoundException (className); 327 theClass = Class.forName(className, false, instance); 328 } 329 catch (ClassNotFoundException e) { 330 throw new ReflectionException(e, 331 "The MBean class could not be loaded by the " + 332 loaderName.toString() + " class loader"); 333 } 334 } 335 336 ByteArrayInputStream bIn; 338 ObjectInputStream objIn; 339 String typeStr; 340 341 bIn = new ByteArrayInputStream(data); 342 try { 343 objIn = new ObjectInputStreamWithLoader(bIn, 344 theClass.getClassLoader()); 345 } catch (IOException e) { 346 throw new OperationsException( 347 "An IOException occured trying to de-serialize the data"); 348 } 349 350 return objIn; 351 } 352 353 354 public Object instantiate(String className) 355 throws ReflectionException, 356 MBeanException { 357 358 return instantiate(className, (Object []) null, (String []) null, null); 359 } 360 361 362 public Object instantiate(String className, ObjectName loaderName, 363 ClassLoader loader) 364 throws ReflectionException, MBeanException, 365 InstanceNotFoundException { 366 367 return instantiate(className, loaderName, (Object []) null, 368 (String []) null, loader); 369 } 370 371 372 public Object instantiate(String className, 373 Object params[], 374 String signature[], 375 ClassLoader loader) 376 throws ReflectionException, 377 MBeanException { 378 379 Class theClass = findClassWithDefaultLoaderRepository(className); 380 return instantiate(theClass, params, signature, loader); 381 } 382 383 384 385 public Object instantiate(String className, 386 ObjectName loaderName, 387 Object params[], 388 String signature[], 389 ClassLoader loader) 390 throws ReflectionException, 391 MBeanException, 392 InstanceNotFoundException { 393 394 Class theClass; 397 398 if (loaderName == null) { 399 theClass = findClass(className, loader); 400 } else { 401 theClass = findClass(className, loaderName); 402 } 403 return instantiate(theClass, params, signature, loader); 404 } 405 406 407 public ModifiableClassLoaderRepository getClassLoaderRepository() { 408 return clr; 409 } 410 411 415 static Class loadClass(String className, ClassLoader loader) 416 throws ReflectionException { 417 418 Class theClass = null; 419 if (className == null) { 420 throw new RuntimeOperationsException(new 421 IllegalArgumentException ("The class name cannot be null"), 422 "Exception occured during object instantiation"); 423 } 424 try { 425 if (loader == null) 426 loader = MBeanInstantiatorImpl.class.getClassLoader(); 427 if (loader != null) { 428 theClass = Class.forName(className, false, loader); 429 } else { 430 theClass = Class.forName(className); 431 } 432 } catch (ClassNotFoundException e) { 433 throw new ReflectionException(e, 434 "The MBean class could not be loaded by the context classloader"); 435 } 436 return theClass; 437 } 438 439 440 441 445 static Class [] loadSignatureClasses(String signature[], 446 ClassLoader loader) 447 throws ReflectionException { 448 449 if (signature == null) return null; 450 final ClassLoader aLoader = 451 (loader==null?MBeanInstantiatorImpl.class.getClassLoader():loader); 452 final int length= signature.length; 453 final Class tab[]=new Class [length]; 454 455 if (length == 0) return tab; 456 try { 457 for (int i= 0; i < length; i++) { 458 462 final Class primCla = 463 StandardMetaDataImpl.findClassForPrim(signature[i]); 464 if (primCla != null) { 465 tab[i] = primCla; 466 continue; 467 } 468 469 tab[i] = Class.forName(signature[i], false, aLoader); 476 } 477 } catch (ClassNotFoundException e) { 478 debugX("findSignatureClasses",e); 479 throw new ReflectionException(e, 480 "The parameter class could not be found"); 481 } catch (RuntimeException e) { 482 debugX("findSignatureClasses",e); 483 throw e; 484 } 485 return tab; 486 } 487 488 489 492 private static boolean isTraceOn() { 493 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER); 494 } 495 496 private static void trace(String clz, String func, String info) { 497 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info); 498 } 499 500 private static void trace(String func, String info) { 501 trace(dbgTag, func, info); 502 } 503 504 private static boolean isDebugOn() { 505 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER); 506 } 507 508 private static void debug(String clz, String func, String info) { 509 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info); 510 } 511 512 private static void debug(String func, String info) { 513 debug(dbgTag, func, info); 514 } 515 516 private static void debugX(String func,Throwable e) { 517 if (isDebugOn()) { 518 final StringWriter s = new StringWriter(); 519 e.printStackTrace(new PrintWriter(s)); 520 final String stack = s.toString(); 521 522 debug(dbgTag,func,"Exception caught in "+ func+"(): "+e); 523 debug(dbgTag,func,stack); 524 525 } 529 } 530 531 } 532 | Popular Tags |