jdk/src/share/classes/java/net/URLClassLoader.java
changeset 21328 13dbd66f4c09
parent 19409 d7c7b9d56631
child 23720 7d5147c21927
equal deleted inserted replaced
21327:9fc34e0b2a47 21328:13dbd66f4c09
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package java.net;
    26 package java.net;
    27 
    27 
    28 import java.io.*;
    28 import java.io.Closeable;
    29 import java.util.*;
    29 import java.io.File;
    30 import java.util.jar.Manifest;
    30 import java.io.FilePermission;
    31 import java.util.jar.JarFile;
    31 import java.io.IOException;
    32 import java.util.jar.Attributes;
    32 import java.io.InputStream;
    33 import java.util.jar.Attributes.Name;
    33 import java.security.AccessControlContext;
       
    34 import java.security.AccessController;
    34 import java.security.CodeSigner;
    35 import java.security.CodeSigner;
    35 import java.security.PrivilegedAction;
       
    36 import java.security.PrivilegedExceptionAction;
       
    37 import java.security.AccessController;
       
    38 import java.security.AccessControlContext;
       
    39 import java.security.SecureClassLoader;
       
    40 import java.security.CodeSource;
    36 import java.security.CodeSource;
    41 import java.security.Permission;
    37 import java.security.Permission;
    42 import java.security.PermissionCollection;
    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;
    43 import sun.misc.Resource;
    52 import sun.misc.Resource;
    44 import sun.misc.URLClassPath;
    53 import sun.misc.URLClassPath;
    45 import sun.net.www.ParseUtil;
    54 import sun.net.www.ParseUtil;
    46 import sun.security.util.SecurityConstants;
    55 import sun.security.util.SecurityConstants;
    47 
    56 
    82      * @param urls the URLs from which to load classes and resources
    91      * @param urls the URLs from which to load classes and resources
    83      * @param parent the parent class loader for delegation
    92      * @param parent the parent class loader for delegation
    84      * @exception  SecurityException  if a security manager exists and its
    93      * @exception  SecurityException  if a security manager exists and its
    85      *             {@code checkCreateClassLoader} method doesn't allow
    94      *             {@code checkCreateClassLoader} method doesn't allow
    86      *             creation of a class loader.
    95      *             creation of a class loader.
       
    96      * @exception  NullPointerException if {@code urls} is {@code null}.
    87      * @see SecurityManager#checkCreateClassLoader
    97      * @see SecurityManager#checkCreateClassLoader
    88      */
    98      */
    89     public URLClassLoader(URL[] urls, ClassLoader parent) {
    99     public URLClassLoader(URL[] urls, ClassLoader parent) {
    90         super(parent);
   100         super(parent);
    91         // this is to make the stack depth consistent with 1.1
   101         // this is to make the stack depth consistent with 1.1
   125      * @param urls the URLs from which to load classes and resources
   135      * @param urls the URLs from which to load classes and resources
   126      *
   136      *
   127      * @exception  SecurityException  if a security manager exists and its
   137      * @exception  SecurityException  if a security manager exists and its
   128      *             {@code checkCreateClassLoader} method doesn't allow
   138      *             {@code checkCreateClassLoader} method doesn't allow
   129      *             creation of a class loader.
   139      *             creation of a class loader.
       
   140      * @exception  NullPointerException if {@code urls} is {@code null}.
   130      * @see SecurityManager#checkCreateClassLoader
   141      * @see SecurityManager#checkCreateClassLoader
   131      */
   142      */
   132     public URLClassLoader(URL[] urls) {
   143     public URLClassLoader(URL[] urls) {
   133         super();
   144         super();
   134         // this is to make the stack depth consistent with 1.1
   145         // this is to make the stack depth consistent with 1.1
   167      * @param factory the URLStreamHandlerFactory to use when creating URLs
   178      * @param factory the URLStreamHandlerFactory to use when creating URLs
   168      *
   179      *
   169      * @exception  SecurityException  if a security manager exists and its
   180      * @exception  SecurityException  if a security manager exists and its
   170      *             {@code checkCreateClassLoader} method doesn't allow
   181      *             {@code checkCreateClassLoader} method doesn't allow
   171      *             creation of a class loader.
   182      *             creation of a class loader.
       
   183      * @exception  NullPointerException if {@code urls} is {@code null}.
   172      * @see SecurityManager#checkCreateClassLoader
   184      * @see SecurityManager#checkCreateClassLoader
   173      */
   185      */
   174     public URLClassLoader(URL[] urls, ClassLoader parent,
   186     public URLClassLoader(URL[] urls, ClassLoader parent,
   175                           URLStreamHandlerFactory factory) {
   187                           URLStreamHandlerFactory factory) {
   176         super(parent);
   188         super(parent);
   258     * The method makes a best effort attempt to close all opened files,
   270     * The method makes a best effort attempt to close all opened files,
   259     * by catching {@link IOException}s internally. Unchecked exceptions
   271     * by catching {@link IOException}s internally. Unchecked exceptions
   260     * and errors are not caught. Calling close on an already closed
   272     * and errors are not caught. Calling close on an already closed
   261     * loader has no effect.
   273     * loader has no effect.
   262     * <p>
   274     * <p>
   263     * @throws IOException if closing any file opened by this class loader
   275     * @exception IOException if closing any file opened by this class loader
   264     * resulted in an IOException. Any such exceptions are caught internally.
   276     * resulted in an IOException. Any such exceptions are caught internally.
   265     * If only one is caught, then it is re-thrown. If more than one exception
   277     * If only one is caught, then it is re-thrown. If more than one exception
   266     * is caught, then the second and following exceptions are added
   278     * is caught, then the second and following exceptions are added
   267     * as suppressed exceptions of the first one caught, which is then re-thrown.
   279     * as suppressed exceptions of the first one caught, which is then re-thrown.
   268     *
   280     *
   269     * @throws SecurityException if a security manager is set, and it denies
   281     * @exception SecurityException if a security manager is set, and it denies
   270     *   {@link RuntimePermission}{@code ("closeClassLoader")}
   282     *   {@link RuntimePermission}{@code ("closeClassLoader")}
   271     *
   283     *
   272     * @since 1.7
   284     * @since 1.7
   273     */
   285     */
   274     public void close() throws IOException {
   286     public void close() throws IOException {
   337      *
   349      *
   338      * @param name the name of the class
   350      * @param name the name of the class
   339      * @return the resulting class
   351      * @return the resulting class
   340      * @exception ClassNotFoundException if the class could not be found,
   352      * @exception ClassNotFoundException if the class could not be found,
   341      *            or if the loader is closed.
   353      *            or if the loader is closed.
       
   354      * @exception NullPointerException if {@code name} is {@code null}.
   342      */
   355      */
   343     protected Class<?> findClass(final String name)
   356     protected Class<?> findClass(final String name)
   344          throws ClassNotFoundException
   357          throws ClassNotFoundException
   345     {
   358     {
   346         try {
   359         try {
   619      * that directory.
   632      * that directory.
   620      * <p>
   633      * <p>
   621      * If the protocol is not "file", then permission
   634      * If the protocol is not "file", then permission
   622      * to connect to and accept connections from the URL's host is granted.
   635      * to connect to and accept connections from the URL's host is granted.
   623      * @param codesource the codesource
   636      * @param codesource the codesource
       
   637      * @exception NullPointerException if {@code codesource} is {@code null}.
   624      * @return the permissions granted to the codesource
   638      * @return the permissions granted to the codesource
   625      */
   639      */
   626     protected PermissionCollection getPermissions(CodeSource codesource)
   640     protected PermissionCollection getPermissions(CodeSource codesource)
   627     {
   641     {
   628         PermissionCollection perms = super.getPermissions(codesource);
   642         PermissionCollection perms = super.getPermissions(codesource);
   698      * {@code SecurityManager.checkPackageAccess} method before
   712      * {@code SecurityManager.checkPackageAccess} method before
   699      * loading the class.
   713      * loading the class.
   700      *
   714      *
   701      * @param urls the URLs to search for classes and resources
   715      * @param urls the URLs to search for classes and resources
   702      * @param parent the parent class loader for delegation
   716      * @param parent the parent class loader for delegation
       
   717      * @exception  NullPointerException if {@code urls} is {@code null}.
   703      * @return the resulting class loader
   718      * @return the resulting class loader
   704      */
   719      */
   705     public static URLClassLoader newInstance(final URL[] urls,
   720     public static URLClassLoader newInstance(final URL[] urls,
   706                                              final ClassLoader parent) {
   721                                              final ClassLoader parent) {
   707         // Save the caller's context
   722         // Save the caller's context
   723      * returned by this method will invoke the
   738      * returned by this method will invoke the
   724      * {@code SecurityManager.checkPackageAccess} before
   739      * {@code SecurityManager.checkPackageAccess} before
   725      * loading the class.
   740      * loading the class.
   726      *
   741      *
   727      * @param urls the URLs to search for classes and resources
   742      * @param urls the URLs to search for classes and resources
       
   743      * @exception  NullPointerException if {@code urls} is {@code null}.
   728      * @return the resulting class loader
   744      * @return the resulting class loader
   729      */
   745      */
   730     public static URLClassLoader newInstance(final URL[] urls) {
   746     public static URLClassLoader newInstance(final URL[] urls) {
   731         // Save the caller's context
   747         // Save the caller's context
   732         final AccessControlContext acc = AccessController.getContext();
   748         final AccessControlContext acc = AccessController.getContext();