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