jdk/src/java.base/share/classes/java/net/URLClassLoader.java
author psandoz
Fri, 02 Dec 2016 10:05:53 -0800
changeset 42354 131a371f5efc
parent 42227 074dfec7f994
child 43196 2064adad356a
permissions -rw-r--r--
8166914: URLClassLoader spec needs to mention that it's MR-aware Reviewed-by: mchung, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
41596
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3849
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3849
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3849
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3849
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3849
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    28
import java.io.Closeable;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    29
import java.io.File;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    30
import java.io.FilePermission;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    31
import java.io.IOException;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    32
import java.io.InputStream;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    33
import java.security.AccessControlContext;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    34
import java.security.AccessController;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.CodeSigner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PermissionCollection;
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    39
import java.security.PrivilegedAction;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    40
import java.security.PrivilegedExceptionAction;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    41
import java.security.SecureClassLoader;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    42
import java.util.Enumeration;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    43
import java.util.List;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    44
import java.util.NoSuchElementException;
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 41911
diff changeset
    45
import java.util.Objects;
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    46
import java.util.Set;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    47
import java.util.WeakHashMap;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    48
import java.util.jar.Attributes;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    49
import java.util.jar.Attributes.Name;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    50
import java.util.jar.JarFile;
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
    51
import java.util.jar.Manifest;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31918
diff changeset
    52
36674
7ab530dd6f10 8149122: Move sun.misc.URLClassPath and Resouce to an internal package
chegar
parents: 36511
diff changeset
    53
import jdk.internal.loader.Resource;
7ab530dd6f10 8149122: Move sun.misc.URLClassPath and Resouce to an internal package
chegar
parents: 36511
diff changeset
    54
import jdk.internal.loader.URLClassPath;
41596
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
    55
import jdk.internal.misc.JavaNetURLClassLoaderAccess;
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
    56
import jdk.internal.misc.SharedSecrets;
34953
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 32834
diff changeset
    57
import jdk.internal.perf.PerfCounter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * This class loader is used to load classes and resources from a search
39638
c228d5b513eb 8155770: Correct URLClassLoader API documentation to explicitly say jar-scheme URL's are accepted
sdrach
parents: 37311
diff changeset
    63
 * path of URLs referring to both JAR files and directories. Any {@code jar:}
c228d5b513eb 8155770: Correct URLClassLoader API documentation to explicitly say jar-scheme URL's are accepted
sdrach
parents: 37311
diff changeset
    64
 * scheme URL (see {@link java.net.JarURLConnection}) is assumed to refer to a
c228d5b513eb 8155770: Correct URLClassLoader API documentation to explicitly say jar-scheme URL's are accepted
sdrach
parents: 37311
diff changeset
    65
 * JAR file.  Any {@code file:} scheme URL that ends with a '/' is assumed to
c228d5b513eb 8155770: Correct URLClassLoader API documentation to explicitly say jar-scheme URL's are accepted
sdrach
parents: 37311
diff changeset
    66
 * refer to a directory. Otherwise, the URL is assumed to refer to a JAR file
c228d5b513eb 8155770: Correct URLClassLoader API documentation to explicitly say jar-scheme URL's are accepted
sdrach
parents: 37311
diff changeset
    67
 * which will be opened as needed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * The AccessControlContext of the thread that created the instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * URLClassLoader will be used when subsequently loading classes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * The classes that are loaded are by default granted permission only to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * access the URLs specified when the URLClassLoader was created.
42354
131a371f5efc 8166914: URLClassLoader spec needs to mention that it's MR-aware
psandoz
parents: 42227
diff changeset
    75
 * <p>
131a371f5efc 8166914: URLClassLoader spec needs to mention that it's MR-aware
psandoz
parents: 42227
diff changeset
    76
 * This class loader supports the loading of classes from the contents of a
131a371f5efc 8166914: URLClassLoader spec needs to mention that it's MR-aware
psandoz
parents: 42227
diff changeset
    77
 * <a href="../util/jar/JarFile.html#multirelease">multi-release</a> JAR file
131a371f5efc 8166914: URLClassLoader spec needs to mention that it's MR-aware
psandoz
parents: 42227
diff changeset
    78
 * that is referred to by a given URL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 */
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
    83
public class URLClassLoader extends SecureClassLoader implements Closeable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /* The search path for classes and resources */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
    85
    private final URLClassPath ucp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /* The context to be used when loading classes and resources */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
    88
    private final AccessControlContext acc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Constructs a new URLClassLoader for the given URLs. The URLs will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * searched in the order specified for classes and resources after first
39638
c228d5b513eb 8155770: Correct URLClassLoader API documentation to explicitly say jar-scheme URL's are accepted
sdrach
parents: 37311
diff changeset
    93
     * searching in the specified parent class loader.  Any {@code jar:}
c228d5b513eb 8155770: Correct URLClassLoader API documentation to explicitly say jar-scheme URL's are accepted
sdrach
parents: 37311
diff changeset
    94
     * scheme URL is assumed to refer to a JAR file.  Any {@code file:} scheme
c228d5b513eb 8155770: Correct URLClassLoader API documentation to explicitly say jar-scheme URL's are accepted
sdrach
parents: 37311
diff changeset
    95
     * URL that ends with a '/' is assumed to refer to a directory.  Otherwise,
c228d5b513eb 8155770: Correct URLClassLoader API documentation to explicitly say jar-scheme URL's are accepted
sdrach
parents: 37311
diff changeset
    96
     * the URL is assumed to refer to a JAR file which will be downloaded and
c228d5b513eb 8155770: Correct URLClassLoader API documentation to explicitly say jar-scheme URL's are accepted
sdrach
parents: 37311
diff changeset
    97
     * opened as needed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <p>If there is a security manager, this method first
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   100
     * calls the security manager's {@code checkCreateClassLoader} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param urls the URLs from which to load classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param parent the parent class loader for delegation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @exception  SecurityException  if a security manager exists and its
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   106
     *             {@code checkCreateClassLoader} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *             creation of a class loader.
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
   108
     * @exception  NullPointerException if {@code urls} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public URLClassLoader(URL[] urls, ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        super(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   118
        this.ucp = new URLClassPath(urls);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   119
        this.acc = AccessController.getContext();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   120
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   121
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   122
    URLClassLoader(String name, URL[] urls, ClassLoader parent,
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   123
                   AccessControlContext acc) {
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   124
        super(name, parent);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   125
        // this is to make the stack depth consistent with 1.1
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   126
        SecurityManager security = System.getSecurityManager();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   127
        if (security != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   128
            security.checkCreateClassLoader();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   129
        }
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   130
        this.ucp = new URLClassPath(urls);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   131
        this.acc = acc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Constructs a new URLClassLoader for the specified URLs using the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   136
     * default delegation parent {@code ClassLoader}. The URLs will
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * be searched in the order specified for classes and resources after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * first searching in the parent class loader. Any URL that ends with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * a '/' is assumed to refer to a directory. Otherwise, the URL is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * assumed to refer to a JAR file which will be downloaded and opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   144
     * calls the security manager's {@code checkCreateClassLoader} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param urls the URLs from which to load classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   150
     *             {@code checkCreateClassLoader} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *             creation of a class loader.
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
   152
     * @exception  NullPointerException if {@code urls} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public URLClassLoader(URL[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   162
        this.ucp = new URLClassPath(urls);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   163
        this.acc = AccessController.getContext();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   164
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   165
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   166
    URLClassLoader(URL[] urls, AccessControlContext acc) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   167
        super();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   168
        // this is to make the stack depth consistent with 1.1
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   169
        SecurityManager security = System.getSecurityManager();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   170
        if (security != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   171
            security.checkCreateClassLoader();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   172
        }
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   173
        this.ucp = new URLClassPath(urls);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   174
        this.acc = acc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Constructs a new URLClassLoader for the specified URLs, parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * class loader, and URLStreamHandlerFactory. The parent argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * will be used as the parent class loader for delegation. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * factory argument will be used as the stream handler factory to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * obtain protocol handlers when creating new jar URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   185
     * calls the security manager's {@code checkCreateClassLoader} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param urls the URLs from which to load classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param parent the parent class loader for delegation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param factory the URLStreamHandlerFactory to use when creating URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   193
     *             {@code checkCreateClassLoader} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *             creation of a class loader.
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
   195
     * @exception  NullPointerException if {@code urls} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public URLClassLoader(URL[] urls, ClassLoader parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                          URLStreamHandlerFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        super(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   206
        this.ucp = new URLClassPath(urls, factory);
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   207
        this.acc = AccessController.getContext();
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   208
    }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   209
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   210
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   211
    /**
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   212
     * Constructs a new named {@code URLClassLoader} for the specified URLs.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   213
     * The URLs will be searched in the order specified for classes
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   214
     * and resources after first searching in the specified parent class loader.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   215
     * Any URL that ends with a '/' is assumed to refer to a directory.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   216
     * Otherwise, the URL is assumed to refer to a JAR file which will be
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   217
     * downloaded and opened as needed.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   218
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   219
     * @param  name class loader name; or {@code null} if not named
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   220
     * @param  urls the URLs from which to load classes and resources
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   221
     * @param  parent the parent class loader for delegation
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   222
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   223
     * @throws IllegalArgumentException if the given name is empty.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   224
     * @throws NullPointerException if {@code urls} is {@code null}.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   225
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   226
     * @throws SecurityException if a security manager exists and its
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   227
     *         {@link SecurityManager#checkCreateClassLoader()} method doesn't
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   228
     *         allow creation of a class loader.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   229
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   230
     * @since 9
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   231
     */
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   232
    public URLClassLoader(String name,
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   233
                          URL[] urls,
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   234
                          ClassLoader parent) {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   235
        super(name, parent);
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   236
        // this is to make the stack depth consistent with 1.1
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   237
        SecurityManager security = System.getSecurityManager();
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   238
        if (security != null) {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   239
            security.checkCreateClassLoader();
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   240
        }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   241
        this.ucp = new URLClassPath(urls);
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   242
        this.acc = AccessController.getContext();
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   243
    }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   244
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   245
    /**
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   246
     * Constructs a new named {@code URLClassLoader} for the specified URLs,
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   247
     * parent class loader, and URLStreamHandlerFactory.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   248
     * The parent argument will be used as the parent class loader for delegation.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   249
     * The factory argument will be used as the stream handler factory to
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   250
     * obtain protocol handlers when creating new jar URLs.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   251
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   252
     * @param  name class loader name; or {@code null} if not named
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   253
     * @param  urls the URLs from which to load classes and resources
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   254
     * @param  parent the parent class loader for delegation
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   255
     * @param  factory the URLStreamHandlerFactory to use when creating URLs
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   256
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   257
     * @throws IllegalArgumentException if the given name is empty.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   258
     * @throws NullPointerException if {@code urls} is {@code null}.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   259
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   260
     * @throws SecurityException if a security manager exists and its
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   261
     *         {@code checkCreateClassLoader} method doesn't allow
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   262
     *         creation of a class loader.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   263
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   264
     * @since 9
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   265
     */
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   266
    public URLClassLoader(String name, URL[] urls, ClassLoader parent,
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   267
                          URLStreamHandlerFactory factory) {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   268
        super(name, parent);
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   269
        // this is to make the stack depth consistent with 1.1
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   270
        SecurityManager security = System.getSecurityManager();
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   271
        if (security != null) {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   272
            security.checkCreateClassLoader();
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   273
        }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   274
        this.ucp = new URLClassPath(urls, factory);
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   275
        this.acc = AccessController.getContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   278
    /* A map (used as a set) to keep track of closeable local resources
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   279
     * (either JarFiles or FileInputStreams). We don't care about
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   280
     * Http resources since they don't need to be closed.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   281
     *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   282
     * If the resource is coming from a jar file
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   283
     * we keep a (weak) reference to the JarFile object which can
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   284
     * be closed if URLClassLoader.close() called. Due to jar file
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   285
     * caching there will typically be only one JarFile object
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   286
     * per underlying jar file.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   287
     *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   288
     * For file resources, which is probably a less common situation
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   289
     * we have to keep a weak reference to each stream.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   290
     */
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   291
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   292
    private WeakHashMap<Closeable,Void>
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   293
        closeables = new WeakHashMap<>();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   294
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   295
    /**
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   296
     * Returns an input stream for reading the specified resource.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   297
     * If this loader is closed, then any resources opened by this method
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   298
     * will be closed.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   299
     *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   300
     * <p> The search order is described in the documentation for {@link
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   301
     * #getResource(String)}.  </p>
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   302
     *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   303
     * @param  name
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   304
     *         The resource name
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   305
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   306
     * @return  An input stream for reading the resource, or {@code null}
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   307
     *          if the resource could not be found
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   308
     *
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 41911
diff changeset
   309
     * @throws  NullPointerException If {@code name} is {@code null}
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 41911
diff changeset
   310
     *
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   311
     * @since  1.7
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   312
     */
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   313
    public InputStream getResourceAsStream(String name) {
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 41911
diff changeset
   314
        Objects.requireNonNull(name);
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   315
        URL url = getResource(name);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   316
        try {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   317
            if (url == null) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   318
                return null;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   319
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   320
            URLConnection urlc = url.openConnection();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   321
            InputStream is = urlc.getInputStream();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   322
            if (urlc instanceof JarURLConnection) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   323
                JarURLConnection juc = (JarURLConnection)urlc;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   324
                JarFile jar = juc.getJarFile();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   325
                synchronized (closeables) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   326
                    if (!closeables.containsKey(jar)) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   327
                        closeables.put(jar, null);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   328
                    }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   329
                }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   330
            } else if (urlc instanceof sun.net.www.protocol.file.FileURLConnection) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   331
                synchronized (closeables) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   332
                    closeables.put(is, null);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   333
                }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   334
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   335
            return is;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   336
        } catch (IOException e) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   337
            return null;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   338
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   339
    }
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   340
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   341
   /**
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   342
    * Closes this URLClassLoader, so that it can no longer be used to load
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   343
    * new classes or resources that are defined by this loader.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   344
    * Classes and resources defined by any of this loader's parents in the
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   345
    * delegation hierarchy are still accessible. Also, any classes or resources
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   346
    * that are already loaded, are still accessible.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   347
    * <p>
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   348
    * In the case of jar: and file: URLs, it also closes any files
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   349
    * that were opened by it. If another thread is loading a
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   350
    * class when the {@code close} method is invoked, then the result of
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   351
    * that load is undefined.
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   352
    * <p>
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   353
    * The method makes a best effort attempt to close all opened files,
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   354
    * by catching {@link IOException}s internally. Unchecked exceptions
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   355
    * and errors are not caught. Calling close on an already closed
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   356
    * loader has no effect.
23720
7d5147c21927 8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents: 21328
diff changeset
   357
    *
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
   358
    * @exception IOException if closing any file opened by this class loader
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   359
    * resulted in an IOException. Any such exceptions are caught internally.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   360
    * If only one is caught, then it is re-thrown. If more than one exception
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   361
    * is caught, then the second and following exceptions are added
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   362
    * as suppressed exceptions of the first one caught, which is then re-thrown.
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   363
    *
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
   364
    * @exception SecurityException if a security manager is set, and it denies
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   365
    *   {@link RuntimePermission}{@code ("closeClassLoader")}
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   366
    *
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   367
    * @since 1.7
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   368
    */
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   369
    public void close() throws IOException {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   370
        SecurityManager security = System.getSecurityManager();
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   371
        if (security != null) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   372
            security.checkPermission(new RuntimePermission("closeClassLoader"));
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   373
        }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   374
        List<IOException> errors = ucp.closeLoaders();
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   375
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   376
        // now close any remaining streams.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   377
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   378
        synchronized (closeables) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   379
            Set<Closeable> keys = closeables.keySet();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   380
            for (Closeable c : keys) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   381
                try {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   382
                    c.close();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   383
                } catch (IOException ioex) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   384
                    errors.add(ioex);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   385
                }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   386
            }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   387
            closeables.clear();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   388
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   389
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   390
        if (errors.isEmpty()) {
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   391
            return;
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   392
        }
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   393
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   394
        IOException firstex = errors.remove(0);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   395
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   396
        // Suppress any remaining exceptions
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   397
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   398
        for (IOException error: errors) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   399
            firstex.addSuppressed(error);
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   400
        }
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents: 5506
diff changeset
   401
        throw firstex;
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   402
    }
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   403
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Appends the specified URL to the list of URLs to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   408
     * If the URL specified is {@code null} or is already in the
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   409
     * list of URLs, or if this loader is closed, then invoking this
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   410
     * method has no effect.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @param url the URL to be added to the search path of URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    protected void addURL(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        ucp.addURL(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Returns the search path of URLs for loading classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * This includes the original list of URLs specified to the constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * along with any URLs subsequently appended by the addURL() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @return the search path of URLs for loading classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public URL[] getURLs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return ucp.getURLs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Finds and loads the class with the specified name from the URL search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * path. Any URLs referring to JAR files are loaded and opened as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * until the class is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param name the name of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @return the resulting class
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   435
     * @exception ClassNotFoundException if the class could not be found,
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   436
     *            or if the loader is closed.
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
   437
     * @exception NullPointerException if {@code name} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    protected Class<?> findClass(final String name)
26487
605e2ffaec14 8057936: java.net.URLClassLoader.findClass uses exceptions in control flow
michaelm
parents: 26214
diff changeset
   440
        throws ClassNotFoundException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    {
26487
605e2ffaec14 8057936: java.net.URLClassLoader.findClass uses exceptions in control flow
michaelm
parents: 26214
diff changeset
   442
        final Class<?> result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        try {
26487
605e2ffaec14 8057936: java.net.URLClassLoader.findClass uses exceptions in control flow
michaelm
parents: 26214
diff changeset
   444
            result = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26487
diff changeset
   445
                new PrivilegedExceptionAction<>() {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 9035
diff changeset
   446
                    public Class<?> run() throws ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        String path = name.replace('.', '/').concat(".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        Resource res = ucp.getResource(path, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                                return defineClass(name, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                throw new ClassNotFoundException(name, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        } else {
26487
605e2ffaec14 8057936: java.net.URLClassLoader.findClass uses exceptions in control flow
michaelm
parents: 26214
diff changeset
   456
                            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            throw (ClassNotFoundException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
26487
605e2ffaec14 8057936: java.net.URLClassLoader.findClass uses exceptions in control flow
michaelm
parents: 26214
diff changeset
   463
        if (result == null) {
605e2ffaec14 8057936: java.net.URLClassLoader.findClass uses exceptions in control flow
michaelm
parents: 26214
diff changeset
   464
            throw new ClassNotFoundException(name);
605e2ffaec14 8057936: java.net.URLClassLoader.findClass uses exceptions in control flow
michaelm
parents: 26214
diff changeset
   465
        }
605e2ffaec14 8057936: java.net.URLClassLoader.findClass uses exceptions in control flow
michaelm
parents: 26214
diff changeset
   466
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /*
3228
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   470
     * Retrieve the package using the specified package name.
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   471
     * If non-null, verify the package using the specified code
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   472
     * source and manifest.
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   473
     */
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   474
    private Package getAndVerifyPackage(String pkgname,
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   475
                                        Manifest man, URL url) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   476
        Package pkg = getDefinedPackage(pkgname);
3228
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   477
        if (pkg != null) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   478
            // Package found, so check package sealing.
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   479
            if (pkg.isSealed()) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   480
                // Verify that code source URL is the same.
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   481
                if (!pkg.isSealed(url)) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   482
                    throw new SecurityException(
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   483
                        "sealing violation: package " + pkgname + " is sealed");
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   484
                }
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   485
            } else {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   486
                // Make sure we are not attempting to seal the package
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   487
                // at this code source URL.
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   488
                if ((man != null) && isSealed(pkgname, man)) {
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   489
                    throw new SecurityException(
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   490
                        "sealing violation: can't seal package " + pkgname +
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   491
                        ": already loaded");
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   492
                }
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   493
            }
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   494
        }
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   495
        return pkg;
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   496
    }
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   497
d7c00dbea781 6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents: 2448
diff changeset
   498
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * Defines a Class using the class bytes obtained from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * Resource. The resulting Class must be resolved before it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 9035
diff changeset
   503
    private Class<?> defineClass(String name, Resource res) throws IOException {
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3228
diff changeset
   504
        long t0 = System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        URL url = res.getCodeSourceURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            String pkgname = name.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            // Check if package already loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            Manifest man = res.getManifest();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   511
            if (getAndVerifyPackage(pkgname, man, url) == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   512
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   513
                    if (man != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   514
                        definePackage(pkgname, man, url);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   515
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   516
                        definePackage(pkgname, null, null, null, null, null, null, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   517
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   518
                } catch (IllegalArgumentException iae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   519
                    // parallel-capable class loaders: re-verify in case of a
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   520
                    // race condition
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   521
                    if (getAndVerifyPackage(pkgname, man, url) == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   522
                        // Should never happen
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   523
                        throw new AssertionError("Cannot find package " +
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   524
                                                 pkgname);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   525
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   526
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   527
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        // Now read the class bytes and define the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        java.nio.ByteBuffer bb = res.getByteBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (bb != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            // Use (direct) ByteBuffer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            CodeSigner[] signers = res.getCodeSigners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            CodeSource cs = new CodeSource(url, signers);
34953
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 32834
diff changeset
   535
            PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return defineClass(name, bb, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            byte[] b = res.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            // must read certificates AFTER reading bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            CodeSigner[] signers = res.getCodeSigners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            CodeSource cs = new CodeSource(url, signers);
34953
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 32834
diff changeset
   542
            PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            return defineClass(name, b, 0, b.length, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
37311
9ceaae6762fd 8153665: URLClassLoader::definePackage no longer inspect packages from ancestors
mchung
parents: 36674
diff changeset
   548
     * Defines a new package by name in this {@code URLClassLoader}.
9ceaae6762fd 8153665: URLClassLoader::definePackage no longer inspect packages from ancestors
mchung
parents: 36674
diff changeset
   549
     * The attributes contained in the specified {@code Manifest}
9ceaae6762fd 8153665: URLClassLoader::definePackage no longer inspect packages from ancestors
mchung
parents: 36674
diff changeset
   550
     * will be used to obtain package version and sealing information.
9ceaae6762fd 8153665: URLClassLoader::definePackage no longer inspect packages from ancestors
mchung
parents: 36674
diff changeset
   551
     * For sealed packages, the additional URL specifies the code source URL
9ceaae6762fd 8153665: URLClassLoader::definePackage no longer inspect packages from ancestors
mchung
parents: 36674
diff changeset
   552
     * from which the package was loaded.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @param name  the package name
37311
9ceaae6762fd 8153665: URLClassLoader::definePackage no longer inspect packages from ancestors
mchung
parents: 36674
diff changeset
   555
     * @param man   the {@code Manifest} containing package version and sealing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *              information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @param url   the code source url for the package, or null if none
37311
9ceaae6762fd 8153665: URLClassLoader::definePackage no longer inspect packages from ancestors
mchung
parents: 36674
diff changeset
   558
     * @throws      IllegalArgumentException if the package name is
9ceaae6762fd 8153665: URLClassLoader::definePackage no longer inspect packages from ancestors
mchung
parents: 36674
diff changeset
   559
     *              already defined by this class loader
9ceaae6762fd 8153665: URLClassLoader::definePackage no longer inspect packages from ancestors
mchung
parents: 36674
diff changeset
   560
     * @return      the newly defined {@code Package} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
37311
9ceaae6762fd 8153665: URLClassLoader::definePackage no longer inspect packages from ancestors
mchung
parents: 36674
diff changeset
   562
    protected Package definePackage(String name, Manifest man, URL url) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        String path = name.replace('.', '/').concat("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        String specTitle = null, specVersion = null, specVendor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        String implTitle = null, implVersion = null, implVendor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        String sealed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        URL sealBase = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        Attributes attr = man.getAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            specTitle   = attr.getValue(Name.SPECIFICATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            specVendor  = attr.getValue(Name.SPECIFICATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            implTitle   = attr.getValue(Name.IMPLEMENTATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            implVendor  = attr.getValue(Name.IMPLEMENTATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            sealed      = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (specTitle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            if (specVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            if (specVendor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (implTitle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if (implVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            if (implVendor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            if (sealed == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if ("true".equalsIgnoreCase(sealed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            sealBase = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return definePackage(name, specTitle, specVersion, specVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                             implTitle, implVersion, implVendor, sealBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Returns true if the specified package name is sealed according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * given manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    private boolean isSealed(String name, Manifest man) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        String path = name.replace('.', '/').concat("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        Attributes attr = man.getAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        String sealed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (sealed == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            if ((attr = man.getMainAttributes()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return "true".equalsIgnoreCase(sealed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Finds the resource with the specified name on the URL search path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @param name the name of the resource
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   633
     * @return a {@code URL} for the resource, or {@code null}
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   634
     * if the resource could not be found, or if the loader is closed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    public URL findResource(final String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
         * The same restriction to finding classes applies to resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   640
        URL url = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26487
diff changeset
   641
            new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   642
                public URL run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    return ucp.findResource(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   647
        return url != null ? URLClassPath.checkURL(url) : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Returns an Enumeration of URLs representing all of the resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * on the URL search path having the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @param name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @exception IOException if an I/O exception occurs
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   656
     * @return an {@code Enumeration} of {@code URL}s
1940
e81514210873 4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents: 715
diff changeset
   657
     *         If the loader is closed, the Enumeration will be empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    public Enumeration<URL> findResources(final String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   662
        final Enumeration<URL> e = ucp.findResources(name, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26487
diff changeset
   664
        return new Enumeration<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            private URL url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            private boolean next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                do {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   672
                    URL u = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26487
diff changeset
   673
                        new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   674
                            public URL run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                                if (!e.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                return e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    if (u == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        break;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   682
                    url = URLClassPath.checkURL(u);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                } while (url == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                return url != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            public URL nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                if (!next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                URL u = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * Returns the permissions for the given codesource object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * The implementation of this method first calls super.getPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * and then adds permissions based on the URL of the codesource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * If the protocol of this URL is "jar", then the permission granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * is based on the permission that is required by the URL of the Jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * If the protocol is "file" and there is an authority component, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * permission to connect to and accept connections from that authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * may be granted. If the protocol is "file"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * and the path specifies a file, then permission to read that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * file is granted. If protocol is "file" and the path is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * a directory, permission is granted to read all files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * and (recursively) all files and subdirectories contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * that directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * If the protocol is not "file", then permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * to connect to and accept connections from the URL's host is granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @param codesource the codesource
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
   723
     * @exception NullPointerException if {@code codesource} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @return the permissions granted to the codesource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    protected PermissionCollection getPermissions(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        PermissionCollection perms = super.getPermissions(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        URL url = codesource.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        Permission p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        URLConnection urlConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            urlConnection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            p = urlConnection.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            urlConnection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        if (p instanceof FilePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            // if the permission has a separator char on the end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            // it means the codebase is a directory, and we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            // to add an additional permission to read recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            String path = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if (path.endsWith(File.separator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                path += "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        } else if ((p == null) && (url.getProtocol().equals("file"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            String path = url.getFile().replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            path = ParseUtil.decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            if (path.endsWith(File.separator))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                path += "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            p =  new FilePermission(path, SecurityConstants.FILE_READ_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
             * Not loading from a 'file:' URL so we want to give the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
             * permission to connect to and accept from the remote host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
             * after we've made sure the host is the correct one and is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            URL locUrl = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            if (urlConnection instanceof JarURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            String host = locUrl.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            if (host != null && (host.length() > 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                p = new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                         SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        // make sure the person that created this class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        // would have this permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            final SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                final Permission fp = p;
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26487
diff changeset
   781
                AccessController.doPrivileged(new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   782
                    public Void run() throws SecurityException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        sm.checkPermission(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            perms.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * Creates a new instance of URLClassLoader for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * URLs and parent class loader. If a security manager is
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   796
     * installed, the {@code loadClass} method of the URLClassLoader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * returned by this method will invoke the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   798
     * {@code SecurityManager.checkPackageAccess} method before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * loading the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @param urls the URLs to search for classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @param parent the parent class loader for delegation
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
   803
     * @exception  NullPointerException if {@code urls} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @return the resulting class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    public static URLClassLoader newInstance(final URL[] urls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                             final ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        // Save the caller's context
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   809
        final AccessControlContext acc = AccessController.getContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        // Need a privileged block to create the class loader
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   811
        URLClassLoader ucl = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26487
diff changeset
   812
            new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   813
                public URLClassLoader run() {
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   814
                    return new FactoryURLClassLoader(null, urls, parent, acc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        return ucl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * Creates a new instance of URLClassLoader for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * URLs and default parent class loader. If a security manager is
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   823
     * installed, the {@code loadClass} method of the URLClassLoader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * returned by this method will invoke the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 15682
diff changeset
   825
     * {@code SecurityManager.checkPackageAccess} before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * loading the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @param urls the URLs to search for classes and resources
21328
13dbd66f4c09 7179567: JCK8 tests: api/java_net/URLClassLoader/index.html#Ctor3 failed with NPE
bpb
parents: 19409
diff changeset
   829
     * @exception  NullPointerException if {@code urls} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @return the resulting class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    public static URLClassLoader newInstance(final URL[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // Save the caller's context
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   834
        final AccessControlContext acc = AccessController.getContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        // Need a privileged block to create the class loader
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   836
        URLClassLoader ucl = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26487
diff changeset
   837
            new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   838
                public URLClassLoader run() {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   839
                    return new FactoryURLClassLoader(urls, acc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        return ucl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    static {
41596
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
   846
        SharedSecrets.setJavaNetURLClassLoaderAccess(
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
   847
            new JavaNetURLClassLoaderAccess() {
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
   848
                @Override
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
   849
                public AccessControlContext getAccessControlContext(URLClassLoader u) {
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
   850
                    return u.acc;
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
   851
                }
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
   852
            }
a6382b6ba29d 8165271: Fix use of reflection to gain access to private fields
ddehaven
parents: 41555
diff changeset
   853
        );
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   854
        ClassLoader.registerAsParallelCapable();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
final class FactoryURLClassLoader extends URLClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   860
    static {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   861
        ClassLoader.registerAsParallelCapable();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   864
    FactoryURLClassLoader(String name, URL[] urls, ClassLoader parent,
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   865
                          AccessControlContext acc) {
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41596
diff changeset
   866
        super(name, urls, parent, acc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   869
    FactoryURLClassLoader(URL[] urls, AccessControlContext acc) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   870
        super(urls, acc);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   871
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 1940
diff changeset
   872
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 9035
diff changeset
   873
    public final Class<?> loadClass(String name, boolean resolve)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        // First check if we have permission to access the package. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        // should go away once we've added support for exported packages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                sm.checkPackageAccess(name.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        return super.loadClass(name, resolve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
}