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