< prev index next >

jdk/src/java.base/share/classes/java/net/URLClassLoader.java

Print this page




  93      *
  94      * <p>If there is a security manager, this method first
  95      * calls the security manager's {@code checkCreateClassLoader} method
  96      * to ensure creation of a class loader is allowed.
  97      *
  98      * @param urls the URLs from which to load classes and resources
  99      * @param parent the parent class loader for delegation
 100      * @exception  SecurityException  if a security manager exists and its
 101      *             {@code checkCreateClassLoader} method doesn't allow
 102      *             creation of a class loader.
 103      * @exception  NullPointerException if {@code urls} is {@code null}.
 104      * @see SecurityManager#checkCreateClassLoader
 105      */
 106     public URLClassLoader(URL[] urls, ClassLoader parent) {
 107         super(parent);
 108         // this is to make the stack depth consistent with 1.1
 109         SecurityManager security = System.getSecurityManager();
 110         if (security != null) {
 111             security.checkCreateClassLoader();
 112         }
 113         ucp = new URLClassPath(urls);
 114         this.acc = AccessController.getContext();
 115     }
 116 
 117     URLClassLoader(URL[] urls, ClassLoader parent,
 118                    AccessControlContext acc) {
 119         super(parent);
 120         // this is to make the stack depth consistent with 1.1
 121         SecurityManager security = System.getSecurityManager();
 122         if (security != null) {
 123             security.checkCreateClassLoader();
 124         }
 125         ucp = new URLClassPath(urls);
 126         this.acc = acc;
 127     }
 128 
 129     /**
 130      * Constructs a new URLClassLoader for the specified URLs using the
 131      * default delegation parent {@code ClassLoader}. The URLs will
 132      * be searched in the order specified for classes and resources after
 133      * first searching in the parent class loader. Any URL that ends with
 134      * a '/' is assumed to refer to a directory. Otherwise, the URL is
 135      * assumed to refer to a JAR file which will be downloaded and opened
 136      * as needed.
 137      *
 138      * <p>If there is a security manager, this method first
 139      * calls the security manager's {@code checkCreateClassLoader} method
 140      * to ensure creation of a class loader is allowed.
 141      *
 142      * @param urls the URLs from which to load classes and resources
 143      *
 144      * @exception  SecurityException  if a security manager exists and its
 145      *             {@code checkCreateClassLoader} method doesn't allow
 146      *             creation of a class loader.
 147      * @exception  NullPointerException if {@code urls} is {@code null}.
 148      * @see SecurityManager#checkCreateClassLoader
 149      */
 150     public URLClassLoader(URL[] urls) {
 151         super();
 152         // this is to make the stack depth consistent with 1.1
 153         SecurityManager security = System.getSecurityManager();
 154         if (security != null) {
 155             security.checkCreateClassLoader();
 156         }
 157         ucp = new URLClassPath(urls);
 158         this.acc = AccessController.getContext();
 159     }
 160 
 161     URLClassLoader(URL[] urls, AccessControlContext acc) {
 162         super();
 163         // this is to make the stack depth consistent with 1.1
 164         SecurityManager security = System.getSecurityManager();
 165         if (security != null) {
 166             security.checkCreateClassLoader();
 167         }
 168         ucp = new URLClassPath(urls);
 169         this.acc = acc;
 170     }
 171 
 172     /**
 173      * Constructs a new URLClassLoader for the specified URLs, parent
 174      * class loader, and URLStreamHandlerFactory. The parent argument
 175      * will be used as the parent class loader for delegation. The
 176      * factory argument will be used as the stream handler factory to
 177      * obtain protocol handlers when creating new jar URLs.
 178      *
 179      * <p>If there is a security manager, this method first
 180      * calls the security manager's {@code checkCreateClassLoader} method
 181      * to ensure creation of a class loader is allowed.
 182      *
 183      * @param urls the URLs from which to load classes and resources
 184      * @param parent the parent class loader for delegation
 185      * @param factory the URLStreamHandlerFactory to use when creating URLs
 186      *
 187      * @exception  SecurityException  if a security manager exists and its
 188      *             {@code checkCreateClassLoader} method doesn't allow
 189      *             creation of a class loader.
 190      * @exception  NullPointerException if {@code urls} is {@code null}.
 191      * @see SecurityManager#checkCreateClassLoader
 192      */
 193     public URLClassLoader(URL[] urls, ClassLoader parent,
 194                           URLStreamHandlerFactory factory) {
 195         super(parent);
 196         // this is to make the stack depth consistent with 1.1
 197         SecurityManager security = System.getSecurityManager();
 198         if (security != null) {
 199             security.checkCreateClassLoader();
 200         }
 201         ucp = new URLClassPath(urls, factory);
 202         acc = AccessController.getContext();




































































 203     }
 204 
 205     /* A map (used as a set) to keep track of closeable local resources
 206      * (either JarFiles or FileInputStreams). We don't care about
 207      * Http resources since they don't need to be closed.
 208      *
 209      * If the resource is coming from a jar file
 210      * we keep a (weak) reference to the JarFile object which can
 211      * be closed if URLClassLoader.close() called. Due to jar file
 212      * caching there will typically be only one JarFile object
 213      * per underlying jar file.
 214      *
 215      * For file resources, which is probably a less common situation
 216      * we have to keep a weak reference to each stream.
 217      */
 218 
 219     private WeakHashMap<Closeable,Void>
 220         closeables = new WeakHashMap<>();
 221 
 222     /**


 718      * Creates a new instance of URLClassLoader for the specified
 719      * URLs and parent class loader. If a security manager is
 720      * installed, the {@code loadClass} method of the URLClassLoader
 721      * returned by this method will invoke the
 722      * {@code SecurityManager.checkPackageAccess} method before
 723      * loading the class.
 724      *
 725      * @param urls the URLs to search for classes and resources
 726      * @param parent the parent class loader for delegation
 727      * @exception  NullPointerException if {@code urls} is {@code null}.
 728      * @return the resulting class loader
 729      */
 730     public static URLClassLoader newInstance(final URL[] urls,
 731                                              final ClassLoader parent) {
 732         // Save the caller's context
 733         final AccessControlContext acc = AccessController.getContext();
 734         // Need a privileged block to create the class loader
 735         URLClassLoader ucl = AccessController.doPrivileged(
 736             new PrivilegedAction<>() {
 737                 public URLClassLoader run() {
 738                     return new FactoryURLClassLoader(urls, parent, acc);
 739                 }
 740             });
 741         return ucl;
 742     }
 743 
 744     /**
 745      * Creates a new instance of URLClassLoader for the specified
 746      * URLs and default parent class loader. If a security manager is
 747      * installed, the {@code loadClass} method of the URLClassLoader
 748      * returned by this method will invoke the
 749      * {@code SecurityManager.checkPackageAccess} before
 750      * loading the class.
 751      *
 752      * @param urls the URLs to search for classes and resources
 753      * @exception  NullPointerException if {@code urls} is {@code null}.
 754      * @return the resulting class loader
 755      */
 756     public static URLClassLoader newInstance(final URL[] urls) {
 757         // Save the caller's context
 758         final AccessControlContext acc = AccessController.getContext();


 768 
 769     static {
 770         SharedSecrets.setJavaNetURLClassLoaderAccess(
 771             new JavaNetURLClassLoaderAccess() {
 772                 @Override
 773                 public AccessControlContext getAccessControlContext(URLClassLoader u) {
 774                     return u.acc;
 775                 }
 776             }
 777         );
 778         ClassLoader.registerAsParallelCapable();
 779     }
 780 }
 781 
 782 final class FactoryURLClassLoader extends URLClassLoader {
 783 
 784     static {
 785         ClassLoader.registerAsParallelCapable();
 786     }
 787 
 788     FactoryURLClassLoader(URL[] urls, ClassLoader parent,
 789                           AccessControlContext acc) {
 790         super(urls, parent, acc);
 791     }
 792 
 793     FactoryURLClassLoader(URL[] urls, AccessControlContext acc) {
 794         super(urls, acc);
 795     }
 796 
 797     public final Class<?> loadClass(String name, boolean resolve)
 798         throws ClassNotFoundException
 799     {
 800         // First check if we have permission to access the package. This
 801         // should go away once we've added support for exported packages.
 802         SecurityManager sm = System.getSecurityManager();
 803         if (sm != null) {
 804             int i = name.lastIndexOf('.');
 805             if (i != -1) {
 806                 sm.checkPackageAccess(name.substring(0, i));
 807             }
 808         }
 809         return super.loadClass(name, resolve);
 810     }


  93      *
  94      * <p>If there is a security manager, this method first
  95      * calls the security manager's {@code checkCreateClassLoader} method
  96      * to ensure creation of a class loader is allowed.
  97      *
  98      * @param urls the URLs from which to load classes and resources
  99      * @param parent the parent class loader for delegation
 100      * @exception  SecurityException  if a security manager exists and its
 101      *             {@code checkCreateClassLoader} method doesn't allow
 102      *             creation of a class loader.
 103      * @exception  NullPointerException if {@code urls} is {@code null}.
 104      * @see SecurityManager#checkCreateClassLoader
 105      */
 106     public URLClassLoader(URL[] urls, ClassLoader parent) {
 107         super(parent);
 108         // this is to make the stack depth consistent with 1.1
 109         SecurityManager security = System.getSecurityManager();
 110         if (security != null) {
 111             security.checkCreateClassLoader();
 112         }
 113         this.ucp = new URLClassPath(urls);
 114         this.acc = AccessController.getContext();
 115     }
 116 
 117     URLClassLoader(String name, URL[] urls, ClassLoader parent,
 118                    AccessControlContext acc) {
 119         super(name, parent);
 120         // this is to make the stack depth consistent with 1.1
 121         SecurityManager security = System.getSecurityManager();
 122         if (security != null) {
 123             security.checkCreateClassLoader();
 124         }
 125         this.ucp = new URLClassPath(urls);
 126         this.acc = acc;
 127     }
 128 
 129     /**
 130      * Constructs a new URLClassLoader for the specified URLs using the
 131      * default delegation parent {@code ClassLoader}. The URLs will
 132      * be searched in the order specified for classes and resources after
 133      * first searching in the parent class loader. Any URL that ends with
 134      * a '/' is assumed to refer to a directory. Otherwise, the URL is
 135      * assumed to refer to a JAR file which will be downloaded and opened
 136      * as needed.
 137      *
 138      * <p>If there is a security manager, this method first
 139      * calls the security manager's {@code checkCreateClassLoader} method
 140      * to ensure creation of a class loader is allowed.
 141      *
 142      * @param urls the URLs from which to load classes and resources
 143      *
 144      * @exception  SecurityException  if a security manager exists and its
 145      *             {@code checkCreateClassLoader} method doesn't allow
 146      *             creation of a class loader.
 147      * @exception  NullPointerException if {@code urls} is {@code null}.
 148      * @see SecurityManager#checkCreateClassLoader
 149      */
 150     public URLClassLoader(URL[] urls) {
 151         super();
 152         // this is to make the stack depth consistent with 1.1
 153         SecurityManager security = System.getSecurityManager();
 154         if (security != null) {
 155             security.checkCreateClassLoader();
 156         }
 157         this.ucp = new URLClassPath(urls);
 158         this.acc = AccessController.getContext();
 159     }
 160 
 161     URLClassLoader(URL[] urls, AccessControlContext acc) {
 162         super();
 163         // this is to make the stack depth consistent with 1.1
 164         SecurityManager security = System.getSecurityManager();
 165         if (security != null) {
 166             security.checkCreateClassLoader();
 167         }
 168         this.ucp = new URLClassPath(urls);
 169         this.acc = acc;
 170     }
 171 
 172     /**
 173      * Constructs a new URLClassLoader for the specified URLs, parent
 174      * class loader, and URLStreamHandlerFactory. The parent argument
 175      * will be used as the parent class loader for delegation. The
 176      * factory argument will be used as the stream handler factory to
 177      * obtain protocol handlers when creating new jar URLs.
 178      *
 179      * <p>If there is a security manager, this method first
 180      * calls the security manager's {@code checkCreateClassLoader} method
 181      * to ensure creation of a class loader is allowed.
 182      *
 183      * @param urls the URLs from which to load classes and resources
 184      * @param parent the parent class loader for delegation
 185      * @param factory the URLStreamHandlerFactory to use when creating URLs
 186      *
 187      * @exception  SecurityException  if a security manager exists and its
 188      *             {@code checkCreateClassLoader} method doesn't allow
 189      *             creation of a class loader.
 190      * @exception  NullPointerException if {@code urls} is {@code null}.
 191      * @see SecurityManager#checkCreateClassLoader
 192      */
 193     public URLClassLoader(URL[] urls, ClassLoader parent,
 194                           URLStreamHandlerFactory factory) {
 195         super(parent);
 196         // this is to make the stack depth consistent with 1.1
 197         SecurityManager security = System.getSecurityManager();
 198         if (security != null) {
 199             security.checkCreateClassLoader();
 200         }
 201         this.ucp = new URLClassPath(urls, factory);
 202         this.acc = AccessController.getContext();
 203     }
 204 
 205 
 206     /**
 207      * Constructs a new named {@code URLClassLoader} for the specified URLs.
 208      * The URLs will be searched in the order specified for classes
 209      * and resources after first searching in the specified parent class loader.
 210      * Any URL that ends with a '/' is assumed to refer to a directory.
 211      * Otherwise, the URL is assumed to refer to a JAR file which will be
 212      * downloaded and opened as needed.
 213      *
 214      * @param  name class loader name; or {@code null} if not named
 215      * @param  urls the URLs from which to load classes and resources
 216      * @param  parent the parent class loader for delegation
 217      *
 218      * @throws IllegalArgumentException if the given name is empty.
 219      * @throws NullPointerException if {@code urls} is {@code null}.
 220      *
 221      * @throws SecurityException if a security manager exists and its
 222      *         {@link SecurityManager#checkCreateClassLoader()} method doesn't
 223      *         allow creation of a class loader.
 224      *
 225      * @since 9
 226      */
 227     public URLClassLoader(String name,
 228                           URL[] urls,
 229                           ClassLoader parent) {
 230         super(name, parent);
 231         // this is to make the stack depth consistent with 1.1
 232         SecurityManager security = System.getSecurityManager();
 233         if (security != null) {
 234             security.checkCreateClassLoader();
 235         }
 236         this.ucp = new URLClassPath(urls);
 237         this.acc = AccessController.getContext();
 238     }
 239 
 240     /**
 241      * Constructs a new named {@code URLClassLoader} for the specified URLs,
 242      * parent class loader, and URLStreamHandlerFactory.
 243      * The parent argument will be used as the parent class loader for delegation.
 244      * The factory argument will be used as the stream handler factory to
 245      * obtain protocol handlers when creating new jar URLs.
 246      *
 247      * @param  name class loader name; or {@code null} if not named
 248      * @param  urls the URLs from which to load classes and resources
 249      * @param  parent the parent class loader for delegation
 250      * @param  factory the URLStreamHandlerFactory to use when creating URLs
 251      *
 252      * @throws IllegalArgumentException if the given name is empty.
 253      * @throws NullPointerException if {@code urls} is {@code null}.
 254      *
 255      * @throws SecurityException if a security manager exists and its
 256      *         {@code checkCreateClassLoader} method doesn't allow
 257      *         creation of a class loader.
 258      *
 259      * @since 9
 260      */
 261     public URLClassLoader(String name, URL[] urls, ClassLoader parent,
 262                           URLStreamHandlerFactory factory) {
 263         super(name, parent);
 264         // this is to make the stack depth consistent with 1.1
 265         SecurityManager security = System.getSecurityManager();
 266         if (security != null) {
 267             security.checkCreateClassLoader();
 268         }
 269         this.ucp = new URLClassPath(urls, factory);
 270         this.acc = AccessController.getContext();
 271     }
 272 
 273     /* A map (used as a set) to keep track of closeable local resources
 274      * (either JarFiles or FileInputStreams). We don't care about
 275      * Http resources since they don't need to be closed.
 276      *
 277      * If the resource is coming from a jar file
 278      * we keep a (weak) reference to the JarFile object which can
 279      * be closed if URLClassLoader.close() called. Due to jar file
 280      * caching there will typically be only one JarFile object
 281      * per underlying jar file.
 282      *
 283      * For file resources, which is probably a less common situation
 284      * we have to keep a weak reference to each stream.
 285      */
 286 
 287     private WeakHashMap<Closeable,Void>
 288         closeables = new WeakHashMap<>();
 289 
 290     /**


 786      * Creates a new instance of URLClassLoader for the specified
 787      * URLs and parent class loader. If a security manager is
 788      * installed, the {@code loadClass} method of the URLClassLoader
 789      * returned by this method will invoke the
 790      * {@code SecurityManager.checkPackageAccess} method before
 791      * loading the class.
 792      *
 793      * @param urls the URLs to search for classes and resources
 794      * @param parent the parent class loader for delegation
 795      * @exception  NullPointerException if {@code urls} is {@code null}.
 796      * @return the resulting class loader
 797      */
 798     public static URLClassLoader newInstance(final URL[] urls,
 799                                              final ClassLoader parent) {
 800         // Save the caller's context
 801         final AccessControlContext acc = AccessController.getContext();
 802         // Need a privileged block to create the class loader
 803         URLClassLoader ucl = AccessController.doPrivileged(
 804             new PrivilegedAction<>() {
 805                 public URLClassLoader run() {
 806                     return new FactoryURLClassLoader(null, urls, parent, acc);
 807                 }
 808             });
 809         return ucl;
 810     }
 811 
 812     /**
 813      * Creates a new instance of URLClassLoader for the specified
 814      * URLs and default parent class loader. If a security manager is
 815      * installed, the {@code loadClass} method of the URLClassLoader
 816      * returned by this method will invoke the
 817      * {@code SecurityManager.checkPackageAccess} before
 818      * loading the class.
 819      *
 820      * @param urls the URLs to search for classes and resources
 821      * @exception  NullPointerException if {@code urls} is {@code null}.
 822      * @return the resulting class loader
 823      */
 824     public static URLClassLoader newInstance(final URL[] urls) {
 825         // Save the caller's context
 826         final AccessControlContext acc = AccessController.getContext();


 836 
 837     static {
 838         SharedSecrets.setJavaNetURLClassLoaderAccess(
 839             new JavaNetURLClassLoaderAccess() {
 840                 @Override
 841                 public AccessControlContext getAccessControlContext(URLClassLoader u) {
 842                     return u.acc;
 843                 }
 844             }
 845         );
 846         ClassLoader.registerAsParallelCapable();
 847     }
 848 }
 849 
 850 final class FactoryURLClassLoader extends URLClassLoader {
 851 
 852     static {
 853         ClassLoader.registerAsParallelCapable();
 854     }
 855 
 856     FactoryURLClassLoader(String name, URL[] urls, ClassLoader parent,
 857                           AccessControlContext acc) {
 858         super(name, urls, parent, acc);
 859     }
 860 
 861     FactoryURLClassLoader(URL[] urls, AccessControlContext acc) {
 862         super(urls, acc);
 863     }
 864 
 865     public final Class<?> loadClass(String name, boolean resolve)
 866         throws ClassNotFoundException
 867     {
 868         // First check if we have permission to access the package. This
 869         // should go away once we've added support for exported packages.
 870         SecurityManager sm = System.getSecurityManager();
 871         if (sm != null) {
 872             int i = name.lastIndexOf('.');
 873             if (i != -1) {
 874                 sm.checkPackageAccess(name.substring(0, i));
 875             }
 876         }
 877         return super.loadClass(name, resolve);
 878     }
< prev index next >