1 22 package org.jboss.mx.loading; 23 24 import java.net.URL ; 25 import java.security.AccessController ; 26 import java.security.PrivilegedAction ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 31 import javax.management.AttributeNotFoundException ; 32 import javax.management.InstanceNotFoundException ; 33 import javax.management.MBeanException ; 34 import javax.management.MBeanServer ; 35 import javax.management.ObjectName ; 36 import javax.management.ReflectionException ; 37 38 import org.jboss.mx.loading.LoadMgr3.PkgClassLoader; 39 import org.jboss.mx.util.ObjectNameFactory; 40 41 48 public class HeirarchicalLoaderRepository3 extends UnifiedLoaderRepository3 49 { 50 private static ObjectName DEFAULT_LOADER_OBJECT_NAME = ObjectNameFactory.create(DEFAULT_LOADER_NAME); 51 52 55 static class NoParentClassLoader extends ClassLoader 56 { 57 NoParentClassLoader() 58 { 59 super(HeirarchicalLoaderRepository3.class.getClassLoader()); 60 } 61 62 67 public URL getResource(String name) 68 { 69 return null; 70 } 71 78 protected synchronized Class loadClass(String name, boolean resolve) 79 throws ClassNotFoundException 80 { 81 throw new ClassNotFoundException ("NoParentClassLoader has no classes"); 82 } 83 89 protected Class findClass(String name) throws ClassNotFoundException 90 { 91 throw new ClassNotFoundException ("NoParentClassLoader has no classes"); 92 } 93 } 94 static class CacheClassLoader extends UnifiedClassLoader3 95 { 96 Class cacheClass; 97 CacheClassLoader(Class cacheClass, LoaderRepository rep) 98 { 99 super(null, null, new NoParentClassLoader(), rep); 100 this.cacheClass = cacheClass; 101 } 102 103 protected Class findClass(String name) throws ClassNotFoundException 104 { 105 Class c = cacheClass; 106 if( name.equals(cacheClass.getName()) == false ) 107 c = null; 108 return c; 109 } 110 } 111 112 115 private UnifiedLoaderRepository3 parentRepository; 116 119 private boolean java2ParentDelegation; 120 121 122 private PkgClassLoader packageClassLoader; 123 124 132 public HeirarchicalLoaderRepository3(UnifiedLoaderRepository3 parent) 133 throws AttributeNotFoundException , InstanceNotFoundException , MBeanException , ReflectionException 134 { 135 this.parentRepository = parent; 136 init(); 137 } 138 147 public HeirarchicalLoaderRepository3(MBeanServer server) 148 throws AttributeNotFoundException , InstanceNotFoundException , MBeanException , ReflectionException 149 { 150 this(server, DEFAULT_LOADER_OBJECT_NAME); 151 } 152 162 public HeirarchicalLoaderRepository3(MBeanServer server, ObjectName parentName) 163 throws AttributeNotFoundException , InstanceNotFoundException , MBeanException , ReflectionException 164 { 165 this.parentRepository = (UnifiedLoaderRepository3) server.getAttribute(parentName, 166 "Instance"); 167 init(); 168 } 169 170 173 private void init() 174 { 175 ClassLoader loader = RepositoryClassLoader.class.getClassLoader(); 177 RepositoryClassLoader ucl = null; 178 if( loader instanceof RepositoryClassLoader ) 179 ucl = (RepositoryClassLoader) loader; 180 else 181 ucl = new UnifiedClassLoader3(null, null, HeirarchicalLoaderRepository3.this); 182 packageClassLoader = new PkgClassLoader(ucl, 3); 183 } 184 185 187 public RepositoryClassLoader newClassLoader(final URL url, boolean addToRepository) 188 throws Exception 189 { 190 UnifiedClassLoader3 ucl = null; 191 if( java2ParentDelegation == false ) 192 ucl = new UnifiedClassLoader3(url, null, new NoParentClassLoader(), this); 193 else 194 ucl = new UnifiedClassLoader3(url, null, this); 195 196 if( addToRepository ) 197 { 198 this.addClassLoader(ucl); 199 } 200 return ucl; 201 } 202 public RepositoryClassLoader newClassLoader(final URL url, final URL origURL, boolean addToRepository) 203 throws Exception 204 { 205 UnifiedClassLoader3 ucl = null; 206 if( java2ParentDelegation == false ) 207 ucl = new UnifiedClassLoader3(url, origURL, new NoParentClassLoader(), this); 208 else 209 ucl = new UnifiedClassLoader3(url, origURL, this); 210 211 if( addToRepository ) 212 { 213 this.addClassLoader(ucl); 214 } 215 return ucl; 216 } 217 218 225 public boolean getUseParentFirst() 226 { 227 return java2ParentDelegation; 228 } 229 236 public void setUseParentFirst(boolean flag) 237 { 238 java2ParentDelegation = flag; 239 } 240 241 249 public Class loadClass(String name, boolean resolve, ClassLoader scl) 250 throws ClassNotFoundException 251 { 252 Class foundClass = null; 253 254 if( java2ParentDelegation == true ) 255 { 256 try 257 { 258 foundClass = parentRepository.loadClass(name, resolve, scl); 260 } 261 catch(ClassNotFoundException e) 262 { 263 if( foundClass == null ) 265 foundClass = super.loadClass(name, resolve, scl); 266 } 267 } 268 else 269 { 270 try 271 { 272 foundClass = super.loadClass(name, resolve, scl); 274 } 275 catch(ClassNotFoundException e) 276 { 277 if( foundClass == null ) 279 foundClass = parentRepository.loadClass(name, resolve, scl); 280 } 281 } 282 283 if( foundClass != null ) 284 return foundClass; 285 286 289 throw new ClassNotFoundException (name); 290 } 291 292 301 public Class getCachedClass(String classname) 302 { 303 Class clazz = null; 304 if( java2ParentDelegation == true ) 305 { 306 clazz = parentRepository.getCachedClass(classname); 308 if( clazz == null ) 310 clazz = super.getCachedClass(classname); 311 } 312 else 313 { 314 clazz = super.getCachedClass(classname); 316 } 317 return clazz; 318 } 319 320 327 public URL getResource(String name, ClassLoader scl) 328 { 329 URL resource = null; 330 331 if( java2ParentDelegation == true ) 332 { 333 337 resource = getParentResource(name, scl); 338 if( resource == null ) 340 resource = super.getResource(name, scl); 341 } 342 else 343 { 344 resource = super.getResource(name, scl); 346 if( resource == null ) 348 { 349 353 resource = getParentResource(name, scl); 354 } 355 } 356 357 return resource; 358 } 359 360 368 public void getResources(String name, ClassLoader cl, List urls) 369 { 370 if( java2ParentDelegation == true ) 371 { 372 parentRepository.getResources(name, cl, urls); 374 super.getResources(name, cl, urls); 376 } 377 else 378 { 379 super.getResources(name, cl, urls); 381 parentRepository.getResources(name, cl, urls); 383 } 384 } 385 386 389 public URL [] getURLs() 390 { 391 URL [] ourURLs = super.getURLs(); 392 URL [] parentURLs = parentRepository.getURLs(); 393 int size = ourURLs.length + parentURLs.length; 394 URL [] urls = new URL [size]; 395 System.arraycopy(ourURLs, 0, urls, 0, ourURLs.length); 396 System.arraycopy(parentURLs, 0, urls, ourURLs.length, parentURLs.length); 397 return urls; 398 } 399 400 404 public Class loadClassFromCache(String name) 405 { 406 Class foundClass = null; 407 408 if( java2ParentDelegation == true ) 409 { 410 foundClass = parentRepository.loadClassFromCache(name); 412 if( foundClass == null ) 414 foundClass = super.loadClassFromCache(name); 415 } 416 else 417 { 418 foundClass = super.loadClassFromCache(name); 420 423 } 424 return foundClass; 425 } 426 427 432 public Set getPackageClassLoaders(String name) 433 { 434 Set pkgSet = super.getPackageClassLoaders(name); 435 Set parentPkgSet = parentRepository.getPackageClassLoaders(name); 436 GetClassLoadersAction action = new GetClassLoadersAction(name, pkgSet, 437 parentPkgSet); 438 Set theSet = (Set ) AccessController.doPrivileged(action); 439 return theSet; 440 441 } 442 443 public int compare(LoaderRepository lr) 444 { 445 if (lr == this) 446 return 0; 447 return reverseCompare(lr); 448 } 449 450 protected int reverseCompare(LoaderRepository lr) 451 { 452 if (lr != parentRepository) 454 return 0; 455 456 if (java2ParentDelegation) 458 return +1; 459 else 460 return -1; 461 } 462 463 471 private URL getParentResource(String name, ClassLoader scl) 472 { 473 URL resource = parentRepository.getResourceFromGlobalCache(name); 475 476 if( resource != null ) 478 return resource; 479 480 resource = parentRepository.getResourceFromRepository(name, scl); 482 483 return resource; 484 } 485 486 private class GetClassLoadersAction implements PrivilegedAction 487 { 488 private String name; 489 Set pkgSet; 490 Set parentPkgSet; 491 492 GetClassLoadersAction(String name, Set pkgSet, Set parentPkgSet) 493 { 494 this.name = name; 495 this.pkgSet = pkgSet; 496 this.parentPkgSet = parentPkgSet; 497 } 498 499 public Object run() 500 { 501 Set theSet = ClassLoaderUtils.newPackageSet(); 503 if( pkgSet != null ) 504 { 505 Iterator iter = pkgSet.iterator(); 506 while( iter.hasNext() ) 507 { 508 RepositoryClassLoader ucl = (RepositoryClassLoader) iter.next(); 509 PkgClassLoader pkgUcl = new PkgClassLoader(ucl, 0); 510 theSet.add(pkgUcl); 511 } 512 } 513 514 if( java2ParentDelegation == false ) 515 { 516 Class cacheClass = parentRepository.loadClassFromCache(name); 517 if( cacheClass != null ) 518 { 519 RepositoryClassLoader ucl = new CacheClassLoader(cacheClass, HeirarchicalLoaderRepository3.this); 520 PkgClassLoader pkgUcl = new PkgClassLoader(ucl, 1); 521 theSet.add(pkgUcl); 522 } 523 } 524 525 if( parentPkgSet != null ) 526 { 527 Iterator iter = parentPkgSet.iterator(); 528 while( iter.hasNext() ) 529 { 530 RepositoryClassLoader ucl = (RepositoryClassLoader) iter.next(); 531 PkgClassLoader pkgUcl = new PkgClassLoader(ucl, 2); 532 theSet.add(pkgUcl); 533 } 534 } 535 536 if( java2ParentDelegation == false ) 537 { 538 theSet.add(packageClassLoader); 539 } 540 541 return theSet; 542 } 543 } 544 545 } 546 | Popular Tags |