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

Print this page




  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


 442      * Defines a Class using the class bytes obtained from the specified
 443      * Resource. The resulting Class must be resolved before it can be
 444      * used.
 445      */
 446     private Class<?> defineClass(String name, Resource res) throws IOException {
 447         long t0 = System.nanoTime();
 448         int i = name.lastIndexOf('.');
 449         URL url = res.getCodeSourceURL();
 450         if (i != -1) {
 451             String pkgname = name.substring(0, i);
 452             // Check if package already loaded.
 453             Manifest man = res.getManifest();
 454             definePackageInternal(pkgname, man, url);
 455         }
 456         // Now read the class bytes and define the class
 457         java.nio.ByteBuffer bb = res.getByteBuffer();
 458         if (bb != null) {
 459             // Use (direct) ByteBuffer:
 460             CodeSigner[] signers = res.getCodeSigners();
 461             CodeSource cs = new CodeSource(url, signers);
 462             sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
 463             return defineClass(name, bb, cs);
 464         } else {
 465             byte[] b = res.getBytes();
 466             // must read certificates AFTER reading bytes.
 467             CodeSigner[] signers = res.getCodeSigners();
 468             CodeSource cs = new CodeSource(url, signers);
 469             sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
 470             return defineClass(name, b, 0, b.length, cs);
 471         }
 472     }
 473 
 474     /**
 475      * Defines a new package by name in this ClassLoader. The attributes
 476      * contained in the specified Manifest will be used to obtain package
 477      * version and sealing information. For sealed packages, the additional
 478      * URL specifies the code source URL from which the package was loaded.
 479      *
 480      * @param name  the package name
 481      * @param man   the Manifest containing package version and sealing
 482      *              information
 483      * @param url   the code source url for the package, or null if none
 484      * @exception   IllegalArgumentException if the package name duplicates
 485      *              an existing package either in this class loader or one
 486      *              of its ancestors
 487      * @return the newly defined Package object
 488      */
 489     protected Package definePackage(String name, Manifest man, URL url)




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


 443      * Defines a Class using the class bytes obtained from the specified
 444      * Resource. The resulting Class must be resolved before it can be
 445      * used.
 446      */
 447     private Class<?> defineClass(String name, Resource res) throws IOException {
 448         long t0 = System.nanoTime();
 449         int i = name.lastIndexOf('.');
 450         URL url = res.getCodeSourceURL();
 451         if (i != -1) {
 452             String pkgname = name.substring(0, i);
 453             // Check if package already loaded.
 454             Manifest man = res.getManifest();
 455             definePackageInternal(pkgname, man, url);
 456         }
 457         // Now read the class bytes and define the class
 458         java.nio.ByteBuffer bb = res.getByteBuffer();
 459         if (bb != null) {
 460             // Use (direct) ByteBuffer:
 461             CodeSigner[] signers = res.getCodeSigners();
 462             CodeSource cs = new CodeSource(url, signers);
 463             PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
 464             return defineClass(name, bb, cs);
 465         } else {
 466             byte[] b = res.getBytes();
 467             // must read certificates AFTER reading bytes.
 468             CodeSigner[] signers = res.getCodeSigners();
 469             CodeSource cs = new CodeSource(url, signers);
 470             PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
 471             return defineClass(name, b, 0, b.length, cs);
 472         }
 473     }
 474 
 475     /**
 476      * Defines a new package by name in this ClassLoader. The attributes
 477      * contained in the specified Manifest will be used to obtain package
 478      * version and sealing information. For sealed packages, the additional
 479      * URL specifies the code source URL from which the package was loaded.
 480      *
 481      * @param name  the package name
 482      * @param man   the Manifest containing package version and sealing
 483      *              information
 484      * @param url   the code source url for the package, or null if none
 485      * @exception   IllegalArgumentException if the package name duplicates
 486      *              an existing package either in this class loader or one
 487      *              of its ancestors
 488      * @return the newly defined Package object
 489      */
 490     protected Package definePackage(String name, Manifest man, URL url)