author | mchung |
Mon, 14 Sep 2009 13:37:26 -0700 | |
changeset 3849 | 34469964aa98 |
parent 3228 | d7c00dbea781 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
2 |
* Copyright 1997-2009 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.lang.reflect.Method; |
|
29 |
import java.lang.reflect.Modifier; |
|
30 |
import java.io.File; |
|
31 |
import java.io.FilePermission; |
|
32 |
import java.io.InputStream; |
|
33 |
import java.io.IOException; |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
34 |
import java.io.Closeable; |
2 | 35 |
import java.net.URL; |
36 |
import java.net.URLConnection; |
|
37 |
import java.net.URLStreamHandlerFactory; |
|
38 |
import java.util.Enumeration; |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
39 |
import java.util.List; |
2 | 40 |
import java.util.NoSuchElementException; |
41 |
import java.util.StringTokenizer; |
|
42 |
import java.util.jar.Manifest; |
|
43 |
import java.util.jar.Attributes; |
|
44 |
import java.util.jar.Attributes.Name; |
|
45 |
import java.security.CodeSigner; |
|
46 |
import java.security.PrivilegedAction; |
|
47 |
import java.security.PrivilegedExceptionAction; |
|
48 |
import java.security.AccessController; |
|
49 |
import java.security.AccessControlContext; |
|
50 |
import java.security.SecureClassLoader; |
|
51 |
import java.security.CodeSource; |
|
52 |
import java.security.Permission; |
|
53 |
import java.security.PermissionCollection; |
|
54 |
import sun.misc.Resource; |
|
55 |
import sun.misc.URLClassPath; |
|
56 |
import sun.net.www.ParseUtil; |
|
57 |
import sun.security.util.SecurityConstants; |
|
58 |
||
59 |
/** |
|
60 |
* This class loader is used to load classes and resources from a search |
|
61 |
* path of URLs referring to both JAR files and directories. Any URL that |
|
62 |
* ends with a '/' is assumed to refer to a directory. Otherwise, the URL |
|
63 |
* is assumed to refer to a JAR file which will be opened as needed. |
|
64 |
* <p> |
|
65 |
* The AccessControlContext of the thread that created the instance of |
|
66 |
* URLClassLoader will be used when subsequently loading classes and |
|
67 |
* resources. |
|
68 |
* <p> |
|
69 |
* The classes that are loaded are by default granted permission only to |
|
70 |
* access the URLs specified when the URLClassLoader was created. |
|
71 |
* |
|
72 |
* @author David Connelly |
|
73 |
* @since 1.2 |
|
74 |
*/ |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
75 |
public class URLClassLoader extends SecureClassLoader implements Closeable { |
2 | 76 |
/* 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
|
77 |
private final URLClassPath ucp; |
2 | 78 |
|
79 |
/* 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
|
80 |
private final AccessControlContext acc; |
2 | 81 |
|
82 |
/** |
|
83 |
* Constructs a new URLClassLoader for the given URLs. The URLs will be |
|
84 |
* searched in the order specified for classes and resources after first |
|
85 |
* searching in the specified parent class loader. Any URL that ends with |
|
86 |
* a '/' is assumed to refer to a directory. Otherwise, the URL is assumed |
|
87 |
* to refer to a JAR file which will be downloaded and opened as needed. |
|
88 |
* |
|
89 |
* <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
|
90 |
* calls the security manager's {@code checkCreateClassLoader} method |
2 | 91 |
* to ensure creation of a class loader is allowed. |
92 |
* |
|
93 |
* @param urls the URLs from which to load classes and resources |
|
94 |
* @param parent the parent class loader for delegation |
|
95 |
* @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
|
96 |
* {@code checkCreateClassLoader} method doesn't allow |
2 | 97 |
* creation of a class loader. |
98 |
* @see SecurityManager#checkCreateClassLoader |
|
99 |
*/ |
|
100 |
public URLClassLoader(URL[] urls, ClassLoader parent) { |
|
101 |
super(parent); |
|
102 |
// this is to make the stack depth consistent with 1.1 |
|
103 |
SecurityManager security = System.getSecurityManager(); |
|
104 |
if (security != null) { |
|
105 |
security.checkCreateClassLoader(); |
|
106 |
} |
|
107 |
ucp = new URLClassPath(urls); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
108 |
this.acc = AccessController.getContext(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
109 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
110 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
111 |
URLClassLoader(URL[] urls, ClassLoader parent, |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
112 |
AccessControlContext acc) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
113 |
super(parent); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
114 |
// 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
|
115 |
SecurityManager security = System.getSecurityManager(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
116 |
if (security != null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
117 |
security.checkCreateClassLoader(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
118 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
119 |
ucp = new URLClassPath(urls); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
120 |
this.acc = acc; |
2 | 121 |
} |
122 |
||
123 |
/** |
|
124 |
* Constructs a new URLClassLoader for the specified URLs using the |
|
125 |
* default delegation parent <code>ClassLoader</code>. The URLs will |
|
126 |
* be searched in the order specified for classes and resources after |
|
127 |
* first searching in the parent class loader. Any URL that ends with |
|
128 |
* a '/' is assumed to refer to a directory. Otherwise, the URL is |
|
129 |
* assumed to refer to a JAR file which will be downloaded and opened |
|
130 |
* as needed. |
|
131 |
* |
|
132 |
* <p>If there is a security manager, this method first |
|
133 |
* calls the security manager's <code>checkCreateClassLoader</code> method |
|
134 |
* to ensure creation of a class loader is allowed. |
|
135 |
* |
|
136 |
* @param urls the URLs from which to load classes and resources |
|
137 |
* |
|
138 |
* @exception SecurityException if a security manager exists and its |
|
139 |
* <code>checkCreateClassLoader</code> method doesn't allow |
|
140 |
* creation of a class loader. |
|
141 |
* @see SecurityManager#checkCreateClassLoader |
|
142 |
*/ |
|
143 |
public URLClassLoader(URL[] urls) { |
|
144 |
super(); |
|
145 |
// this is to make the stack depth consistent with 1.1 |
|
146 |
SecurityManager security = System.getSecurityManager(); |
|
147 |
if (security != null) { |
|
148 |
security.checkCreateClassLoader(); |
|
149 |
} |
|
150 |
ucp = new URLClassPath(urls); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
151 |
this.acc = AccessController.getContext(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
152 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
153 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
154 |
URLClassLoader(URL[] urls, AccessControlContext acc) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
155 |
super(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
156 |
// 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
|
157 |
SecurityManager security = System.getSecurityManager(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
158 |
if (security != null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
159 |
security.checkCreateClassLoader(); |
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 |
ucp = new URLClassPath(urls); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
162 |
this.acc = acc; |
2 | 163 |
} |
164 |
||
165 |
/** |
|
166 |
* Constructs a new URLClassLoader for the specified URLs, parent |
|
167 |
* class loader, and URLStreamHandlerFactory. The parent argument |
|
168 |
* will be used as the parent class loader for delegation. The |
|
169 |
* factory argument will be used as the stream handler factory to |
|
170 |
* obtain protocol handlers when creating new jar URLs. |
|
171 |
* |
|
172 |
* <p>If there is a security manager, this method first |
|
173 |
* calls the security manager's <code>checkCreateClassLoader</code> method |
|
174 |
* to ensure creation of a class loader is allowed. |
|
175 |
* |
|
176 |
* @param urls the URLs from which to load classes and resources |
|
177 |
* @param parent the parent class loader for delegation |
|
178 |
* @param factory the URLStreamHandlerFactory to use when creating URLs |
|
179 |
* |
|
180 |
* @exception SecurityException if a security manager exists and its |
|
181 |
* <code>checkCreateClassLoader</code> method doesn't allow |
|
182 |
* creation of a class loader. |
|
183 |
* @see SecurityManager#checkCreateClassLoader |
|
184 |
*/ |
|
185 |
public URLClassLoader(URL[] urls, ClassLoader parent, |
|
186 |
URLStreamHandlerFactory factory) { |
|
187 |
super(parent); |
|
188 |
// this is to make the stack depth consistent with 1.1 |
|
189 |
SecurityManager security = System.getSecurityManager(); |
|
190 |
if (security != null) { |
|
191 |
security.checkCreateClassLoader(); |
|
192 |
} |
|
193 |
ucp = new URLClassPath(urls, factory); |
|
194 |
acc = AccessController.getContext(); |
|
195 |
} |
|
196 |
||
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
197 |
|
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
198 |
/** |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
199 |
* 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
|
200 |
* 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
|
201 |
* 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
|
202 |
* 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
|
203 |
* that are already loaded, are still accessible. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
204 |
* <p> |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
205 |
* In the case of jar: and file: URLs, it also closes any class files, |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
206 |
* or JAR files that were opened by it. If another thread is loading a |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
207 |
* 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
|
208 |
* that load is undefined. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
209 |
* <p> |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
210 |
* 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
|
211 |
* 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
|
212 |
* 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
|
213 |
* loader has no effect. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
214 |
* <p> |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
215 |
* @throws IOException if closing any file opened by this class loader |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
216 |
* resulted in an IOException. Any such exceptions are caught, and a |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
217 |
* single IOException is thrown after the last file has been closed. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
218 |
* If only one exception was thrown, it will be set as the <i>cause</i> |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
219 |
* of this IOException. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
220 |
* |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
221 |
* @throws SecurityException if a security manager is set, and it denies |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
222 |
* {@link RuntimePermission}<tt>("closeClassLoader")</tt> |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
223 |
* |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
224 |
* @since 1.7 |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
225 |
*/ |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
226 |
public void close() throws IOException { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
227 |
SecurityManager security = System.getSecurityManager(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
228 |
if (security != null) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
229 |
security.checkPermission(new RuntimePermission("closeClassLoader")); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
230 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
231 |
List<IOException> errors = ucp.closeLoaders(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
232 |
if (errors.isEmpty()) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
233 |
return; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
234 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
235 |
if (errors.size() == 1) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
236 |
throw new IOException ( |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
237 |
"Error closing URLClassLoader resource", |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
238 |
errors.get(0) |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
239 |
); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
240 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
241 |
// Several exceptions. So, just combine the error messages |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
242 |
String errormsg = "Error closing resources: "; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
243 |
for (IOException error: errors) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
244 |
errormsg = errormsg + "[" + error.toString() + "] "; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
245 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
246 |
throw new IOException (errormsg); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
247 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
248 |
|
2 | 249 |
/** |
250 |
* Appends the specified URL to the list of URLs to search for |
|
251 |
* classes and resources. |
|
252 |
* <p> |
|
253 |
* If the URL specified is <code>null</code> or is already in the |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
254 |
* 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
|
255 |
* method has no effect. |
2 | 256 |
* |
257 |
* @param url the URL to be added to the search path of URLs |
|
258 |
*/ |
|
259 |
protected void addURL(URL url) { |
|
260 |
ucp.addURL(url); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Returns the search path of URLs for loading classes and resources. |
|
265 |
* This includes the original list of URLs specified to the constructor, |
|
266 |
* along with any URLs subsequently appended by the addURL() method. |
|
267 |
* @return the search path of URLs for loading classes and resources. |
|
268 |
*/ |
|
269 |
public URL[] getURLs() { |
|
270 |
return ucp.getURLs(); |
|
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Finds and loads the class with the specified name from the URL search |
|
275 |
* path. Any URLs referring to JAR files are loaded and opened as needed |
|
276 |
* until the class is found. |
|
277 |
* |
|
278 |
* @param name the name of the class |
|
279 |
* @return the resulting class |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
280 |
* @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
|
281 |
* or if the loader is closed. |
2 | 282 |
*/ |
283 |
protected Class<?> findClass(final String name) |
|
284 |
throws ClassNotFoundException |
|
285 |
{ |
|
286 |
try { |
|
51 | 287 |
return AccessController.doPrivileged( |
288 |
new PrivilegedExceptionAction<Class>() { |
|
289 |
public Class run() throws ClassNotFoundException { |
|
2 | 290 |
String path = name.replace('.', '/').concat(".class"); |
291 |
Resource res = ucp.getResource(path, false); |
|
292 |
if (res != null) { |
|
293 |
try { |
|
294 |
return defineClass(name, res); |
|
295 |
} catch (IOException e) { |
|
296 |
throw new ClassNotFoundException(name, e); |
|
297 |
} |
|
298 |
} else { |
|
299 |
throw new ClassNotFoundException(name); |
|
300 |
} |
|
301 |
} |
|
302 |
}, acc); |
|
303 |
} catch (java.security.PrivilegedActionException pae) { |
|
304 |
throw (ClassNotFoundException) pae.getException(); |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
/* |
|
3228
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
309 |
* 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
|
310 |
* 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
|
311 |
* source and manifest. |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
312 |
*/ |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
313 |
private Package getAndVerifyPackage(String pkgname, |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
314 |
Manifest man, URL url) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
315 |
Package pkg = getPackage(pkgname); |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
316 |
if (pkg != null) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
317 |
// Package found, so check package sealing. |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
318 |
if (pkg.isSealed()) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
319 |
// 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
|
320 |
if (!pkg.isSealed(url)) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
321 |
throw new SecurityException( |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
322 |
"sealing violation: package " + pkgname + " is sealed"); |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
323 |
} |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
324 |
} else { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
325 |
// 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
|
326 |
// at this code source URL. |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
327 |
if ((man != null) && isSealed(pkgname, man)) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
328 |
throw new SecurityException( |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
329 |
"sealing violation: can't seal package " + pkgname + |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
330 |
": already loaded"); |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
331 |
} |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
332 |
} |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
333 |
} |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
334 |
return pkg; |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
335 |
} |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
336 |
|
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
337 |
/* |
2 | 338 |
* Defines a Class using the class bytes obtained from the specified |
339 |
* Resource. The resulting Class must be resolved before it can be |
|
340 |
* used. |
|
341 |
*/ |
|
342 |
private Class defineClass(String name, Resource res) throws IOException { |
|
3849 | 343 |
long t0 = System.nanoTime(); |
2 | 344 |
int i = name.lastIndexOf('.'); |
345 |
URL url = res.getCodeSourceURL(); |
|
346 |
if (i != -1) { |
|
347 |
String pkgname = name.substring(0, i); |
|
348 |
// Check if package already loaded. |
|
349 |
Manifest man = res.getManifest(); |
|
3228
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
350 |
if (getAndVerifyPackage(pkgname, man, url) == null) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
351 |
try { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
352 |
if (man != null) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
353 |
definePackage(pkgname, man, url); |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
354 |
} else { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
355 |
definePackage(pkgname, null, null, null, null, null, null, null); |
2 | 356 |
} |
3228
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
357 |
} catch (IllegalArgumentException iae) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
358 |
// parallel-capable class loaders: re-verify in case of a |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
359 |
// race condition |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
360 |
if (getAndVerifyPackage(pkgname, man, url) == null) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
361 |
// Should never happen |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
362 |
throw new AssertionError("Cannot find package " + |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
363 |
pkgname); |
2 | 364 |
} |
365 |
} |
|
366 |
} |
|
367 |
} |
|
368 |
// Now read the class bytes and define the class |
|
369 |
java.nio.ByteBuffer bb = res.getByteBuffer(); |
|
370 |
if (bb != null) { |
|
371 |
// Use (direct) ByteBuffer: |
|
372 |
CodeSigner[] signers = res.getCodeSigners(); |
|
373 |
CodeSource cs = new CodeSource(url, signers); |
|
3849 | 374 |
sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0); |
2 | 375 |
return defineClass(name, bb, cs); |
376 |
} else { |
|
377 |
byte[] b = res.getBytes(); |
|
378 |
// must read certificates AFTER reading bytes. |
|
379 |
CodeSigner[] signers = res.getCodeSigners(); |
|
380 |
CodeSource cs = new CodeSource(url, signers); |
|
3849 | 381 |
sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0); |
2 | 382 |
return defineClass(name, b, 0, b.length, cs); |
383 |
} |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* Defines a new package by name in this ClassLoader. The attributes |
|
388 |
* contained in the specified Manifest will be used to obtain package |
|
389 |
* version and sealing information. For sealed packages, the additional |
|
390 |
* URL specifies the code source URL from which the package was loaded. |
|
391 |
* |
|
392 |
* @param name the package name |
|
393 |
* @param man the Manifest containing package version and sealing |
|
394 |
* information |
|
395 |
* @param url the code source url for the package, or null if none |
|
396 |
* @exception IllegalArgumentException if the package name duplicates |
|
397 |
* an existing package either in this class loader or one |
|
398 |
* of its ancestors |
|
399 |
* @return the newly defined Package object |
|
400 |
*/ |
|
401 |
protected Package definePackage(String name, Manifest man, URL url) |
|
402 |
throws IllegalArgumentException |
|
403 |
{ |
|
404 |
String path = name.replace('.', '/').concat("/"); |
|
405 |
String specTitle = null, specVersion = null, specVendor = null; |
|
406 |
String implTitle = null, implVersion = null, implVendor = null; |
|
407 |
String sealed = null; |
|
408 |
URL sealBase = null; |
|
409 |
||
410 |
Attributes attr = man.getAttributes(path); |
|
411 |
if (attr != null) { |
|
412 |
specTitle = attr.getValue(Name.SPECIFICATION_TITLE); |
|
413 |
specVersion = attr.getValue(Name.SPECIFICATION_VERSION); |
|
414 |
specVendor = attr.getValue(Name.SPECIFICATION_VENDOR); |
|
415 |
implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE); |
|
416 |
implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION); |
|
417 |
implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR); |
|
418 |
sealed = attr.getValue(Name.SEALED); |
|
419 |
} |
|
420 |
attr = man.getMainAttributes(); |
|
421 |
if (attr != null) { |
|
422 |
if (specTitle == null) { |
|
423 |
specTitle = attr.getValue(Name.SPECIFICATION_TITLE); |
|
424 |
} |
|
425 |
if (specVersion == null) { |
|
426 |
specVersion = attr.getValue(Name.SPECIFICATION_VERSION); |
|
427 |
} |
|
428 |
if (specVendor == null) { |
|
429 |
specVendor = attr.getValue(Name.SPECIFICATION_VENDOR); |
|
430 |
} |
|
431 |
if (implTitle == null) { |
|
432 |
implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE); |
|
433 |
} |
|
434 |
if (implVersion == null) { |
|
435 |
implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION); |
|
436 |
} |
|
437 |
if (implVendor == null) { |
|
438 |
implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR); |
|
439 |
} |
|
440 |
if (sealed == null) { |
|
441 |
sealed = attr.getValue(Name.SEALED); |
|
442 |
} |
|
443 |
} |
|
444 |
if ("true".equalsIgnoreCase(sealed)) { |
|
445 |
sealBase = url; |
|
446 |
} |
|
447 |
return definePackage(name, specTitle, specVersion, specVendor, |
|
448 |
implTitle, implVersion, implVendor, sealBase); |
|
449 |
} |
|
450 |
||
451 |
/* |
|
452 |
* Returns true if the specified package name is sealed according to the |
|
453 |
* given manifest. |
|
454 |
*/ |
|
455 |
private boolean isSealed(String name, Manifest man) { |
|
456 |
String path = name.replace('.', '/').concat("/"); |
|
457 |
Attributes attr = man.getAttributes(path); |
|
458 |
String sealed = null; |
|
459 |
if (attr != null) { |
|
460 |
sealed = attr.getValue(Name.SEALED); |
|
461 |
} |
|
462 |
if (sealed == null) { |
|
463 |
if ((attr = man.getMainAttributes()) != null) { |
|
464 |
sealed = attr.getValue(Name.SEALED); |
|
465 |
} |
|
466 |
} |
|
467 |
return "true".equalsIgnoreCase(sealed); |
|
468 |
} |
|
469 |
||
470 |
/** |
|
471 |
* Finds the resource with the specified name on the URL search path. |
|
472 |
* |
|
473 |
* @param name the name of the resource |
|
474 |
* @return a <code>URL</code> for the resource, or <code>null</code> |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
475 |
* if the resource could not be found, or if the loader is closed. |
2 | 476 |
*/ |
477 |
public URL findResource(final String name) { |
|
478 |
/* |
|
479 |
* The same restriction to finding classes applies to resources |
|
480 |
*/ |
|
51 | 481 |
URL url = AccessController.doPrivileged( |
482 |
new PrivilegedAction<URL>() { |
|
483 |
public URL run() { |
|
2 | 484 |
return ucp.findResource(name, true); |
485 |
} |
|
486 |
}, acc); |
|
487 |
||
488 |
return url != null ? ucp.checkURL(url) : null; |
|
489 |
} |
|
490 |
||
491 |
/** |
|
492 |
* Returns an Enumeration of URLs representing all of the resources |
|
493 |
* on the URL search path having the specified name. |
|
494 |
* |
|
495 |
* @param name the resource name |
|
496 |
* @exception IOException if an I/O exception occurs |
|
497 |
* @return an <code>Enumeration</code> of <code>URL</code>s |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
498 |
* If the loader is closed, the Enumeration will be empty. |
2 | 499 |
*/ |
500 |
public Enumeration<URL> findResources(final String name) |
|
501 |
throws IOException |
|
502 |
{ |
|
51 | 503 |
final Enumeration<URL> e = ucp.findResources(name, true); |
2 | 504 |
|
505 |
return new Enumeration<URL>() { |
|
506 |
private URL url = null; |
|
507 |
||
508 |
private boolean next() { |
|
509 |
if (url != null) { |
|
510 |
return true; |
|
511 |
} |
|
512 |
do { |
|
51 | 513 |
URL u = AccessController.doPrivileged( |
514 |
new PrivilegedAction<URL>() { |
|
515 |
public URL run() { |
|
2 | 516 |
if (!e.hasMoreElements()) |
517 |
return null; |
|
518 |
return e.nextElement(); |
|
519 |
} |
|
520 |
}, acc); |
|
521 |
if (u == null) |
|
522 |
break; |
|
523 |
url = ucp.checkURL(u); |
|
524 |
} while (url == null); |
|
525 |
return url != null; |
|
526 |
} |
|
527 |
||
528 |
public URL nextElement() { |
|
529 |
if (!next()) { |
|
530 |
throw new NoSuchElementException(); |
|
531 |
} |
|
532 |
URL u = url; |
|
533 |
url = null; |
|
534 |
return u; |
|
535 |
} |
|
536 |
||
537 |
public boolean hasMoreElements() { |
|
538 |
return next(); |
|
539 |
} |
|
540 |
}; |
|
541 |
} |
|
542 |
||
543 |
/** |
|
544 |
* Returns the permissions for the given codesource object. |
|
545 |
* The implementation of this method first calls super.getPermissions |
|
546 |
* and then adds permissions based on the URL of the codesource. |
|
547 |
* <p> |
|
548 |
* If the protocol of this URL is "jar", then the permission granted |
|
549 |
* is based on the permission that is required by the URL of the Jar |
|
550 |
* file. |
|
551 |
* <p> |
|
552 |
* If the protocol is "file" and there is an authority component, then |
|
553 |
* permission to connect to and accept connections from that authority |
|
554 |
* may be granted. If the protocol is "file" |
|
555 |
* and the path specifies a file, then permission to read that |
|
556 |
* file is granted. If protocol is "file" and the path is |
|
557 |
* a directory, permission is granted to read all files |
|
558 |
* and (recursively) all files and subdirectories contained in |
|
559 |
* that directory. |
|
560 |
* <p> |
|
561 |
* If the protocol is not "file", then permission |
|
562 |
* to connect to and accept connections from the URL's host is granted. |
|
563 |
* @param codesource the codesource |
|
564 |
* @return the permissions granted to the codesource |
|
565 |
*/ |
|
566 |
protected PermissionCollection getPermissions(CodeSource codesource) |
|
567 |
{ |
|
568 |
PermissionCollection perms = super.getPermissions(codesource); |
|
569 |
||
570 |
URL url = codesource.getLocation(); |
|
571 |
||
572 |
Permission p; |
|
573 |
URLConnection urlConnection; |
|
574 |
||
575 |
try { |
|
576 |
urlConnection = url.openConnection(); |
|
577 |
p = urlConnection.getPermission(); |
|
578 |
} catch (java.io.IOException ioe) { |
|
579 |
p = null; |
|
580 |
urlConnection = null; |
|
581 |
} |
|
582 |
||
583 |
if (p instanceof FilePermission) { |
|
584 |
// if the permission has a separator char on the end, |
|
585 |
// it means the codebase is a directory, and we need |
|
586 |
// to add an additional permission to read recursively |
|
587 |
String path = p.getName(); |
|
588 |
if (path.endsWith(File.separator)) { |
|
589 |
path += "-"; |
|
590 |
p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION); |
|
591 |
} |
|
592 |
} else if ((p == null) && (url.getProtocol().equals("file"))) { |
|
593 |
String path = url.getFile().replace('/', File.separatorChar); |
|
594 |
path = ParseUtil.decode(path); |
|
595 |
if (path.endsWith(File.separator)) |
|
596 |
path += "-"; |
|
597 |
p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION); |
|
598 |
} else { |
|
599 |
/** |
|
600 |
* Not loading from a 'file:' URL so we want to give the class |
|
601 |
* permission to connect to and accept from the remote host |
|
602 |
* after we've made sure the host is the correct one and is valid. |
|
603 |
*/ |
|
604 |
URL locUrl = url; |
|
605 |
if (urlConnection instanceof JarURLConnection) { |
|
606 |
locUrl = ((JarURLConnection)urlConnection).getJarFileURL(); |
|
607 |
} |
|
608 |
String host = locUrl.getHost(); |
|
609 |
if (host != null && (host.length() > 0)) |
|
610 |
p = new SocketPermission(host, |
|
611 |
SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION); |
|
612 |
} |
|
613 |
||
614 |
// make sure the person that created this class loader |
|
615 |
// would have this permission |
|
616 |
||
617 |
if (p != null) { |
|
618 |
final SecurityManager sm = System.getSecurityManager(); |
|
619 |
if (sm != null) { |
|
620 |
final Permission fp = p; |
|
51 | 621 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
622 |
public Void run() throws SecurityException { |
|
2 | 623 |
sm.checkPermission(fp); |
624 |
return null; |
|
625 |
} |
|
626 |
}, acc); |
|
627 |
} |
|
628 |
perms.add(p); |
|
629 |
} |
|
630 |
return perms; |
|
631 |
} |
|
632 |
||
633 |
/** |
|
634 |
* Creates a new instance of URLClassLoader for the specified |
|
635 |
* URLs and parent class loader. If a security manager is |
|
636 |
* installed, the <code>loadClass</code> method of the URLClassLoader |
|
637 |
* returned by this method will invoke the |
|
638 |
* <code>SecurityManager.checkPackageAccess</code> method before |
|
639 |
* loading the class. |
|
640 |
* |
|
641 |
* @param urls the URLs to search for classes and resources |
|
642 |
* @param parent the parent class loader for delegation |
|
643 |
* @return the resulting class loader |
|
644 |
*/ |
|
645 |
public static URLClassLoader newInstance(final URL[] urls, |
|
646 |
final ClassLoader parent) { |
|
647 |
// Save the caller's context |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
648 |
final AccessControlContext acc = AccessController.getContext(); |
2 | 649 |
// Need a privileged block to create the class loader |
51 | 650 |
URLClassLoader ucl = AccessController.doPrivileged( |
651 |
new PrivilegedAction<URLClassLoader>() { |
|
652 |
public URLClassLoader run() { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
653 |
return new FactoryURLClassLoader(urls, parent, acc); |
2 | 654 |
} |
655 |
}); |
|
656 |
return ucl; |
|
657 |
} |
|
658 |
||
659 |
/** |
|
660 |
* Creates a new instance of URLClassLoader for the specified |
|
661 |
* URLs and default parent class loader. If a security manager is |
|
662 |
* installed, the <code>loadClass</code> method of the URLClassLoader |
|
663 |
* returned by this method will invoke the |
|
664 |
* <code>SecurityManager.checkPackageAccess</code> before |
|
665 |
* loading the class. |
|
666 |
* |
|
667 |
* @param urls the URLs to search for classes and resources |
|
668 |
* @return the resulting class loader |
|
669 |
*/ |
|
670 |
public static URLClassLoader newInstance(final URL[] urls) { |
|
671 |
// Save the caller's context |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
672 |
final AccessControlContext acc = AccessController.getContext(); |
2 | 673 |
// Need a privileged block to create the class loader |
51 | 674 |
URLClassLoader ucl = AccessController.doPrivileged( |
675 |
new PrivilegedAction<URLClassLoader>() { |
|
676 |
public URLClassLoader run() { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
677 |
return new FactoryURLClassLoader(urls, acc); |
2 | 678 |
} |
679 |
}); |
|
680 |
return ucl; |
|
681 |
} |
|
682 |
||
683 |
static { |
|
684 |
sun.misc.SharedSecrets.setJavaNetAccess ( |
|
685 |
new sun.misc.JavaNetAccess() { |
|
686 |
public URLClassPath getURLClassPath (URLClassLoader u) { |
|
687 |
return u.ucp; |
|
688 |
} |
|
689 |
} |
|
690 |
); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
691 |
ClassLoader.registerAsParallelCapable(); |
2 | 692 |
} |
693 |
} |
|
694 |
||
695 |
final class FactoryURLClassLoader extends URLClassLoader { |
|
696 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
697 |
static { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
698 |
ClassLoader.registerAsParallelCapable(); |
2 | 699 |
} |
700 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
701 |
FactoryURLClassLoader(URL[] urls, ClassLoader parent, |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
702 |
AccessControlContext acc) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
703 |
super(urls, parent, acc); |
2 | 704 |
} |
705 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
706 |
FactoryURLClassLoader(URL[] urls, AccessControlContext acc) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
707 |
super(urls, acc); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
708 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
709 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
710 |
public final Class loadClass(String name, boolean resolve) |
2 | 711 |
throws ClassNotFoundException |
712 |
{ |
|
713 |
// First check if we have permission to access the package. This |
|
714 |
// should go away once we've added support for exported packages. |
|
715 |
SecurityManager sm = System.getSecurityManager(); |
|
716 |
if (sm != null) { |
|
717 |
int i = name.lastIndexOf('.'); |
|
718 |
if (i != -1) { |
|
719 |
sm.checkPackageAccess(name.substring(0, i)); |
|
720 |
} |
|
721 |
} |
|
722 |
return super.loadClass(name, resolve); |
|
723 |
} |
|
724 |
} |