1 7 8 package java.rmi.server; 9 10 import java.net.MalformedURLException ; 11 import java.net.URL ; 12 13 import java.util.Iterator ; 14 15 import sun.misc.Service; 16 17 93 public class RMIClassLoader { 94 95 96 private static final RMIClassLoaderSpi defaultProvider = 97 newDefaultProviderInstance(); 98 99 100 private static final RMIClassLoaderSpi provider = 101 (RMIClassLoaderSpi ) java.security.AccessController.doPrivileged( 102 new java.security.PrivilegedAction () { 103 public Object run() { return initializeProvider(); } 104 }); 105 106 109 private RMIClassLoader() {} 110 111 131 @Deprecated 132 public static Class <?> loadClass(String name) 133 throws MalformedURLException , ClassNotFoundException 134 { 135 return loadClass((String ) null, name); 136 } 137 138 166 public static Class <?> loadClass(URL codebase, String name) 167 throws MalformedURLException , ClassNotFoundException 168 { 169 return provider.loadClass( 170 codebase != null ? codebase.toString() : null, name, null); 171 } 172 173 199 public static Class <?> loadClass(String codebase, String name) 200 throws MalformedURLException , ClassNotFoundException 201 { 202 return provider.loadClass(codebase, name, null); 203 } 204 205 243 public static Class <?> loadClass(String codebase, String name, 244 ClassLoader defaultLoader) 245 throws MalformedURLException , ClassNotFoundException 246 { 247 return provider.loadClass(codebase, name, defaultLoader); 248 } 249 250 290 public static Class <?> loadProxyClass(String codebase, String [] interfaces, 291 ClassLoader defaultLoader) 292 throws ClassNotFoundException , MalformedURLException 293 { 294 return provider.loadProxyClass(codebase, interfaces, defaultLoader); 295 } 296 297 335 public static ClassLoader getClassLoader(String codebase) 336 throws MalformedURLException , SecurityException 337 { 338 return provider.getClassLoader(codebase); 339 } 340 341 359 363 public static String getClassAnnotation(Class <?> cl) { 364 return provider.getClassAnnotation(cl); 365 } 366 367 586 public static RMIClassLoaderSpi getDefaultProviderInstance() { 587 SecurityManager sm = System.getSecurityManager(); 588 if (sm != null) { 589 sm.checkPermission(new RuntimePermission ("setFactory")); 590 } 591 return defaultProvider; 592 } 593 594 605 @Deprecated 606 public static Object getSecurityContext(ClassLoader loader) 607 { 608 return sun.rmi.server.LoaderHandler.getSecurityContext(loader); 609 } 610 611 614 private static RMIClassLoaderSpi newDefaultProviderInstance() { 615 return new RMIClassLoaderSpi () { 616 public Class loadClass(String codebase, String name, 617 ClassLoader defaultLoader) 618 throws MalformedURLException , ClassNotFoundException 619 { 620 return sun.rmi.server.LoaderHandler.loadClass( 621 codebase, name, defaultLoader); 622 } 623 624 public Class loadProxyClass(String codebase, String [] interfaces, 625 ClassLoader defaultLoader) 626 throws MalformedURLException , ClassNotFoundException 627 { 628 return sun.rmi.server.LoaderHandler.loadProxyClass( 629 codebase, interfaces, defaultLoader); 630 } 631 632 public ClassLoader getClassLoader(String codebase) 633 throws MalformedURLException 634 { 635 return sun.rmi.server.LoaderHandler.getClassLoader(codebase); 636 } 637 638 public String getClassAnnotation(Class <?> cl) { 639 return sun.rmi.server.LoaderHandler.getClassAnnotation(cl); 640 } 641 }; 642 } 643 644 649 private static RMIClassLoaderSpi initializeProvider() { 650 653 String providerClassName = 654 System.getProperty("java.rmi.server.RMIClassLoaderSpi"); 655 656 if (providerClassName != null) { 657 if (providerClassName.equals("default")) { 658 return defaultProvider; 659 } 660 661 try { 662 Class providerClass = 663 Class.forName(providerClassName, false, 664 ClassLoader.getSystemClassLoader()); 665 return (RMIClassLoaderSpi ) providerClass.newInstance(); 666 667 } catch (ClassNotFoundException e) { 668 throw new NoClassDefFoundError (e.getMessage()); 669 } catch (IllegalAccessException e) { 670 throw new IllegalAccessError (e.getMessage()); 671 } catch (InstantiationException e) { 672 throw new InstantiationError (e.getMessage()); 673 } catch (ClassCastException e) { 674 Error error = new LinkageError ( 675 "provider class not assignable to RMIClassLoaderSpi"); 676 error.initCause(e); 677 throw error; 678 } 679 } 680 681 684 Iterator iter = Service.providers(RMIClassLoaderSpi .class, 685 ClassLoader.getSystemClassLoader()); 686 if (iter.hasNext()) { 687 try { 688 return (RMIClassLoaderSpi ) iter.next(); 689 } catch (ClassCastException e) { 690 Error error = new LinkageError ( 691 "provider class not assignable to RMIClassLoaderSpi"); 692 error.initCause(e); 693 throw error; 694 } 695 } 696 697 700 return defaultProvider; 701 } 702 } 703 | Popular Tags |