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

Print this page




  32 import java.io.InputStream;
  33 import java.security.AccessControlContext;
  34 import java.security.AccessController;
  35 import java.security.CodeSigner;
  36 import java.security.CodeSource;
  37 import java.security.Permission;
  38 import java.security.PermissionCollection;
  39 import java.security.PrivilegedAction;
  40 import java.security.PrivilegedExceptionAction;
  41 import java.security.SecureClassLoader;
  42 import java.util.Enumeration;
  43 import java.util.List;
  44 import java.util.NoSuchElementException;
  45 import java.util.Objects;
  46 import java.util.Set;
  47 import java.util.WeakHashMap;
  48 import java.util.jar.Attributes;
  49 import java.util.jar.Attributes.Name;
  50 import java.util.jar.JarFile;
  51 import java.util.jar.Manifest;



  52 import sun.misc.Resource;
  53 import sun.misc.URLClassPath;
  54 import sun.net.www.ParseUtil;
  55 import sun.security.util.SecurityConstants;
  56 
  57 /**
  58  * This class loader is used to load classes and resources from a search
  59  * path of URLs referring to both JAR files and directories. Any URL that
  60  * ends with a '/' is assumed to refer to a directory. Otherwise, the URL
  61  * is assumed to refer to a JAR file which will be opened as needed.
  62  * <p>
  63  * The AccessControlContext of the thread that created the instance of
  64  * URLClassLoader will be used when subsequently loading classes and
  65  * resources.
  66  * <p>
  67  * The classes that are loaded are by default granted permission only to
  68  * access the URLs specified when the URLClassLoader was created.
  69  *
  70  * @author  David Connelly
  71  * @since   1.2


 752      * loading the class.
 753      *
 754      * @param urls the URLs to search for classes and resources
 755      * @exception  NullPointerException if {@code urls} is {@code null}.
 756      * @return the resulting class loader
 757      */
 758     public static URLClassLoader newInstance(final URL[] urls) {
 759         // Save the caller's context
 760         final AccessControlContext acc = AccessController.getContext();
 761         // Need a privileged block to create the class loader
 762         URLClassLoader ucl = AccessController.doPrivileged(
 763             new PrivilegedAction<>() {
 764                 public URLClassLoader run() {
 765                     return new FactoryURLClassLoader(urls, acc);
 766                 }
 767             });
 768         return ucl;
 769     }
 770 
 771     static {
 772         sun.misc.SharedSecrets.setJavaNetAccess (
 773             new sun.misc.JavaNetAccess() {
 774                 public URLClassPath getURLClassPath (URLClassLoader u) {
 775                     return u.ucp;
 776                 }
 777             }
 778         );
 779         ClassLoader.registerAsParallelCapable();
 780     }
 781 }
 782 
 783 final class FactoryURLClassLoader extends URLClassLoader {
 784 
 785     static {
 786         ClassLoader.registerAsParallelCapable();
 787     }
 788 
 789     FactoryURLClassLoader(URL[] urls, ClassLoader parent,
 790                           AccessControlContext acc) {
 791         super(urls, parent, acc);
 792     }
 793 
 794     FactoryURLClassLoader(URL[] urls, AccessControlContext acc) {


  32 import java.io.InputStream;
  33 import java.security.AccessControlContext;
  34 import java.security.AccessController;
  35 import java.security.CodeSigner;
  36 import java.security.CodeSource;
  37 import java.security.Permission;
  38 import java.security.PermissionCollection;
  39 import java.security.PrivilegedAction;
  40 import java.security.PrivilegedExceptionAction;
  41 import java.security.SecureClassLoader;
  42 import java.util.Enumeration;
  43 import java.util.List;
  44 import java.util.NoSuchElementException;
  45 import java.util.Objects;
  46 import java.util.Set;
  47 import java.util.WeakHashMap;
  48 import java.util.jar.Attributes;
  49 import java.util.jar.Attributes.Name;
  50 import java.util.jar.JarFile;
  51 import java.util.jar.Manifest;
  52 
  53 import jdk.internal.misc.JavaNetAccess;
  54 import jdk.internal.misc.SharedSecrets;
  55 import sun.misc.Resource;
  56 import sun.misc.URLClassPath;
  57 import sun.net.www.ParseUtil;
  58 import sun.security.util.SecurityConstants;
  59 
  60 /**
  61  * This class loader is used to load classes and resources from a search
  62  * path of URLs referring to both JAR files and directories. Any URL that
  63  * ends with a '/' is assumed to refer to a directory. Otherwise, the URL
  64  * is assumed to refer to a JAR file which will be opened as needed.
  65  * <p>
  66  * The AccessControlContext of the thread that created the instance of
  67  * URLClassLoader will be used when subsequently loading classes and
  68  * resources.
  69  * <p>
  70  * The classes that are loaded are by default granted permission only to
  71  * access the URLs specified when the URLClassLoader was created.
  72  *
  73  * @author  David Connelly
  74  * @since   1.2


 755      * loading the class.
 756      *
 757      * @param urls the URLs to search for classes and resources
 758      * @exception  NullPointerException if {@code urls} is {@code null}.
 759      * @return the resulting class loader
 760      */
 761     public static URLClassLoader newInstance(final URL[] urls) {
 762         // Save the caller's context
 763         final AccessControlContext acc = AccessController.getContext();
 764         // Need a privileged block to create the class loader
 765         URLClassLoader ucl = AccessController.doPrivileged(
 766             new PrivilegedAction<>() {
 767                 public URLClassLoader run() {
 768                     return new FactoryURLClassLoader(urls, acc);
 769                 }
 770             });
 771         return ucl;
 772     }
 773 
 774     static {
 775         SharedSecrets.setJavaNetAccess(
 776             new JavaNetAccess() {
 777                 public URLClassPath getURLClassPath(URLClassLoader u) {
 778                     return u.ucp;
 779                 }
 780             }
 781         );
 782         ClassLoader.registerAsParallelCapable();
 783     }
 784 }
 785 
 786 final class FactoryURLClassLoader extends URLClassLoader {
 787 
 788     static {
 789         ClassLoader.registerAsParallelCapable();
 790     }
 791 
 792     FactoryURLClassLoader(URL[] urls, ClassLoader parent,
 793                           AccessControlContext acc) {
 794         super(urls, parent, acc);
 795     }
 796 
 797     FactoryURLClassLoader(URL[] urls, AccessControlContext acc) {