author | dholmes |
Mon, 04 Feb 2013 18:08:50 -0500 | |
changeset 15688 | 625addfb4d4c |
parent 15682 | 793a36de151d |
parent 15305 | 45a0eb6ab4f7 |
child 16023 | 58ecc1b8327b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7980
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, 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 sun.misc; |
|
27 |
||
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
28 |
import java.util.*; |
2 | 29 |
import java.util.jar.JarFile; |
30 |
import sun.misc.JarIndex; |
|
31 |
import sun.misc.InvalidJarIndexException; |
|
32 |
import sun.net.www.ParseUtil; |
|
33 |
import java.util.zip.ZipEntry; |
|
34 |
import java.util.jar.JarEntry; |
|
35 |
import java.util.jar.Manifest; |
|
36 |
import java.util.jar.Attributes; |
|
37 |
import java.util.jar.Attributes.Name; |
|
15682
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
38 |
import java.util.jar.UnsupportedProfileException; |
2 | 39 |
import java.net.JarURLConnection; |
40 |
import java.net.MalformedURLException; |
|
41 |
import java.net.URL; |
|
42 |
import java.net.URLConnection; |
|
43 |
import java.net.HttpURLConnection; |
|
44 |
import java.net.URLStreamHandler; |
|
45 |
import java.net.URLStreamHandlerFactory; |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
46 |
import java.io.*; |
2 | 47 |
import java.security.AccessController; |
48 |
import java.security.AccessControlException; |
|
49 |
import java.security.CodeSigner; |
|
50 |
import java.security.Permission; |
|
51 |
import java.security.PrivilegedAction; |
|
52 |
import java.security.PrivilegedExceptionAction; |
|
53 |
import java.security.cert.Certificate; |
|
54 |
import sun.misc.FileURLMapper; |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
55 |
import sun.net.util.URLUtil; |
2 | 56 |
|
57 |
/** |
|
58 |
* This class is used to maintain a search path of URLs for loading classes |
|
59 |
* and resources from both JAR files and directories. |
|
60 |
* |
|
61 |
* @author David Connelly |
|
62 |
*/ |
|
63 |
public class URLClassPath { |
|
64 |
final static String USER_AGENT_JAVA_VERSION = "UA-Java-Version"; |
|
65 |
final static String JAVA_VERSION; |
|
66 |
private static final boolean DEBUG; |
|
67 |
||
15682
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
68 |
/** |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
69 |
* Used by launcher to indicate that checking of the JAR file "Profile" |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
70 |
* attribute has been suppressed. |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
71 |
*/ |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
72 |
private static boolean profileCheckSuppressedByLauncher; |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
73 |
|
2 | 74 |
static { |
75 |
JAVA_VERSION = java.security.AccessController.doPrivileged( |
|
76 |
new sun.security.action.GetPropertyAction("java.version")); |
|
77 |
DEBUG = (java.security.AccessController.doPrivileged( |
|
78 |
new sun.security.action.GetPropertyAction("sun.misc.URLClassPath.debug")) != null); |
|
79 |
} |
|
80 |
||
81 |
/* The original search path of URLs. */ |
|
51 | 82 |
private ArrayList<URL> path = new ArrayList<URL>(); |
2 | 83 |
|
84 |
/* The stack of unopened URLs */ |
|
51 | 85 |
Stack<URL> urls = new Stack<URL>(); |
2 | 86 |
|
87 |
/* The resulting search path of Loaders */ |
|
51 | 88 |
ArrayList<Loader> loaders = new ArrayList<Loader>(); |
2 | 89 |
|
90 |
/* Map of each URL opened to its corresponding Loader */ |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
91 |
HashMap<String, Loader> lmap = new HashMap<String, Loader>(); |
2 | 92 |
|
93 |
/* The jar protocol handler to use when creating new URLs */ |
|
94 |
private URLStreamHandler jarHandler; |
|
95 |
||
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
96 |
/* Whether this URLClassLoader has been closed yet */ |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
97 |
private boolean closed = false; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
98 |
|
2 | 99 |
/** |
100 |
* Creates a new URLClassPath for the given URLs. The URLs will be |
|
101 |
* searched in the order specified for classes and resources. A URL |
|
102 |
* ending with a '/' is assumed to refer to a directory. Otherwise, |
|
103 |
* the URL is assumed to refer to a JAR file. |
|
104 |
* |
|
105 |
* @param urls the directory and JAR file URLs to search for classes |
|
106 |
* and resources |
|
107 |
* @param factory the URLStreamHandlerFactory to use when creating new URLs |
|
108 |
*/ |
|
109 |
public URLClassPath(URL[] urls, URLStreamHandlerFactory factory) { |
|
110 |
for (int i = 0; i < urls.length; i++) { |
|
111 |
path.add(urls[i]); |
|
112 |
} |
|
113 |
push(urls); |
|
114 |
if (factory != null) { |
|
115 |
jarHandler = factory.createURLStreamHandler("jar"); |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
public URLClassPath(URL[] urls) { |
|
120 |
this(urls, null); |
|
121 |
} |
|
122 |
||
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
123 |
public synchronized List<IOException> closeLoaders() { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
124 |
if (closed) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
125 |
return Collections.emptyList(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
126 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
127 |
List<IOException> result = new LinkedList<IOException>(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
128 |
for (Loader loader : loaders) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
129 |
try { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
130 |
loader.close(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
131 |
} catch (IOException e) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
132 |
result.add (e); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
133 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
134 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
135 |
closed = true; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
136 |
return result; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
137 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
138 |
|
2 | 139 |
/** |
140 |
* Appends the specified URL to the search path of directory and JAR |
|
141 |
* file URLs from which to load classes and resources. |
|
142 |
* <p> |
|
143 |
* If the URL specified is null or is already in the list of |
|
144 |
* URLs, then invoking this method has no effect. |
|
145 |
*/ |
|
3073
5e93bd389012
6827999: 6827999: URLClassLoader.addURL(URL) adds URLs to closed class loader
michaelm
parents:
1940
diff
changeset
|
146 |
public synchronized void addURL(URL url) { |
5e93bd389012
6827999: 6827999: URLClassLoader.addURL(URL) adds URLs to closed class loader
michaelm
parents:
1940
diff
changeset
|
147 |
if (closed) |
5e93bd389012
6827999: 6827999: URLClassLoader.addURL(URL) adds URLs to closed class loader
michaelm
parents:
1940
diff
changeset
|
148 |
return; |
2 | 149 |
synchronized (urls) { |
150 |
if (url == null || path.contains(url)) |
|
151 |
return; |
|
152 |
||
153 |
urls.add(0, url); |
|
154 |
path.add(url); |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* Returns the original search path of URLs. |
|
160 |
*/ |
|
161 |
public URL[] getURLs() { |
|
162 |
synchronized (urls) { |
|
51 | 163 |
return path.toArray(new URL[path.size()]); |
2 | 164 |
} |
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* Finds the resource with the specified name on the URL search path |
|
169 |
* or null if not found or security check fails. |
|
170 |
* |
|
171 |
* @param name the name of the resource |
|
172 |
* @param check whether to perform a security check |
|
173 |
* @return a <code>URL</code> for the resource, or <code>null</code> |
|
174 |
* if the resource could not be found. |
|
175 |
*/ |
|
176 |
public URL findResource(String name, boolean check) { |
|
177 |
Loader loader; |
|
178 |
for (int i = 0; (loader = getLoader(i)) != null; i++) { |
|
179 |
URL url = loader.findResource(name, check); |
|
180 |
if (url != null) { |
|
181 |
return url; |
|
182 |
} |
|
183 |
} |
|
184 |
return null; |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Finds the first Resource on the URL search path which has the specified |
|
189 |
* name. Returns null if no Resource could be found. |
|
190 |
* |
|
191 |
* @param name the name of the Resource |
|
192 |
* @param check whether to perform a security check |
|
193 |
* @return the Resource, or null if not found |
|
194 |
*/ |
|
195 |
public Resource getResource(String name, boolean check) { |
|
196 |
if (DEBUG) { |
|
197 |
System.err.println("URLClassPath.getResource(\"" + name + "\")"); |
|
198 |
} |
|
199 |
||
200 |
Loader loader; |
|
201 |
for (int i = 0; (loader = getLoader(i)) != null; i++) { |
|
202 |
Resource res = loader.getResource(name, check); |
|
203 |
if (res != null) { |
|
204 |
return res; |
|
205 |
} |
|
206 |
} |
|
207 |
return null; |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Finds all resources on the URL search path with the given name. |
|
212 |
* Returns an enumeration of the URL objects. |
|
213 |
* |
|
214 |
* @param name the resource name |
|
215 |
* @return an Enumeration of all the urls having the specified name |
|
216 |
*/ |
|
51 | 217 |
public Enumeration<URL> findResources(final String name, |
2 | 218 |
final boolean check) { |
51 | 219 |
return new Enumeration<URL>() { |
2 | 220 |
private int index = 0; |
221 |
private URL url = null; |
|
222 |
||
223 |
private boolean next() { |
|
224 |
if (url != null) { |
|
225 |
return true; |
|
226 |
} else { |
|
227 |
Loader loader; |
|
228 |
while ((loader = getLoader(index++)) != null) { |
|
229 |
url = loader.findResource(name, check); |
|
230 |
if (url != null) { |
|
231 |
return true; |
|
232 |
} |
|
233 |
} |
|
234 |
return false; |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
public boolean hasMoreElements() { |
|
239 |
return next(); |
|
240 |
} |
|
241 |
||
51 | 242 |
public URL nextElement() { |
2 | 243 |
if (!next()) { |
244 |
throw new NoSuchElementException(); |
|
245 |
} |
|
246 |
URL u = url; |
|
247 |
url = null; |
|
248 |
return u; |
|
249 |
} |
|
250 |
}; |
|
251 |
} |
|
252 |
||
253 |
public Resource getResource(String name) { |
|
254 |
return getResource(name, true); |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Finds all resources on the URL search path with the given name. |
|
259 |
* Returns an enumeration of the Resource objects. |
|
260 |
* |
|
261 |
* @param name the resource name |
|
262 |
* @return an Enumeration of all the resources having the specified name |
|
263 |
*/ |
|
51 | 264 |
public Enumeration<Resource> getResources(final String name, |
2 | 265 |
final boolean check) { |
51 | 266 |
return new Enumeration<Resource>() { |
2 | 267 |
private int index = 0; |
268 |
private Resource res = null; |
|
269 |
||
270 |
private boolean next() { |
|
271 |
if (res != null) { |
|
272 |
return true; |
|
273 |
} else { |
|
274 |
Loader loader; |
|
275 |
while ((loader = getLoader(index++)) != null) { |
|
276 |
res = loader.getResource(name, check); |
|
277 |
if (res != null) { |
|
278 |
return true; |
|
279 |
} |
|
280 |
} |
|
281 |
return false; |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
public boolean hasMoreElements() { |
|
286 |
return next(); |
|
287 |
} |
|
288 |
||
51 | 289 |
public Resource nextElement() { |
2 | 290 |
if (!next()) { |
291 |
throw new NoSuchElementException(); |
|
292 |
} |
|
293 |
Resource r = res; |
|
294 |
res = null; |
|
295 |
return r; |
|
296 |
} |
|
297 |
}; |
|
298 |
} |
|
299 |
||
51 | 300 |
public Enumeration<Resource> getResources(final String name) { |
2 | 301 |
return getResources(name, true); |
302 |
} |
|
303 |
||
304 |
/* |
|
305 |
* Returns the Loader at the specified position in the URL search |
|
306 |
* path. The URLs are opened and expanded as needed. Returns null |
|
307 |
* if the specified index is out of range. |
|
308 |
*/ |
|
309 |
private synchronized Loader getLoader(int index) { |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
310 |
if (closed) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
311 |
return null; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
312 |
} |
2 | 313 |
// Expand URL search path until the request can be satisfied |
314 |
// or the URL stack is empty. |
|
315 |
while (loaders.size() < index + 1) { |
|
316 |
// Pop the next URL from the URL stack |
|
317 |
URL url; |
|
318 |
synchronized (urls) { |
|
319 |
if (urls.empty()) { |
|
320 |
return null; |
|
321 |
} else { |
|
51 | 322 |
url = urls.pop(); |
2 | 323 |
} |
324 |
} |
|
325 |
// Skip this URL if it already has a Loader. (Loader |
|
326 |
// may be null in the case where URL has not been opened |
|
327 |
// but is referenced by a JAR index.) |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
328 |
String urlNoFragString = URLUtil.urlNoFragString(url); |
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
329 |
if (lmap.containsKey(urlNoFragString)) { |
2 | 330 |
continue; |
331 |
} |
|
332 |
// Otherwise, create a new Loader for the URL. |
|
333 |
Loader loader; |
|
334 |
try { |
|
335 |
loader = getLoader(url); |
|
336 |
// If the loader defines a local class path then add the |
|
337 |
// URLs to the list of URLs to be opened. |
|
338 |
URL[] urls = loader.getClassPath(); |
|
339 |
if (urls != null) { |
|
340 |
push(urls); |
|
341 |
} |
|
342 |
} catch (IOException e) { |
|
343 |
// Silently ignore for now... |
|
344 |
continue; |
|
345 |
} |
|
346 |
// Finally, add the Loader to the search path. |
|
347 |
loaders.add(loader); |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
348 |
lmap.put(urlNoFragString, loader); |
2 | 349 |
} |
51 | 350 |
return loaders.get(index); |
2 | 351 |
} |
352 |
||
353 |
/* |
|
354 |
* Returns the Loader for the specified base URL. |
|
355 |
*/ |
|
356 |
private Loader getLoader(final URL url) throws IOException { |
|
357 |
try { |
|
51 | 358 |
return java.security.AccessController.doPrivileged( |
359 |
new java.security.PrivilegedExceptionAction<Loader>() { |
|
360 |
public Loader run() throws IOException { |
|
2 | 361 |
String file = url.getFile(); |
362 |
if (file != null && file.endsWith("/")) { |
|
363 |
if ("file".equals(url.getProtocol())) { |
|
364 |
return new FileLoader(url); |
|
365 |
} else { |
|
366 |
return new Loader(url); |
|
367 |
} |
|
368 |
} else { |
|
369 |
return new JarLoader(url, jarHandler, lmap); |
|
370 |
} |
|
371 |
} |
|
372 |
}); |
|
373 |
} catch (java.security.PrivilegedActionException pae) { |
|
374 |
throw (IOException)pae.getException(); |
|
375 |
} |
|
376 |
} |
|
377 |
||
378 |
/* |
|
379 |
* Pushes the specified URLs onto the list of unopened URLs. |
|
380 |
*/ |
|
381 |
private void push(URL[] us) { |
|
382 |
synchronized (urls) { |
|
383 |
for (int i = us.length - 1; i >= 0; --i) { |
|
384 |
urls.push(us[i]); |
|
385 |
} |
|
386 |
} |
|
387 |
} |
|
388 |
||
389 |
/** |
|
390 |
* Convert class path specification into an array of file URLs. |
|
391 |
* |
|
392 |
* The path of the file is encoded before conversion into URL |
|
393 |
* form so that reserved characters can safely appear in the path. |
|
394 |
*/ |
|
395 |
public static URL[] pathToURLs(String path) { |
|
396 |
StringTokenizer st = new StringTokenizer(path, File.pathSeparator); |
|
397 |
URL[] urls = new URL[st.countTokens()]; |
|
398 |
int count = 0; |
|
399 |
while (st.hasMoreTokens()) { |
|
400 |
File f = new File(st.nextToken()); |
|
401 |
try { |
|
402 |
f = new File(f.getCanonicalPath()); |
|
403 |
} catch (IOException x) { |
|
404 |
// use the non-canonicalized filename |
|
405 |
} |
|
406 |
try { |
|
407 |
urls[count++] = ParseUtil.fileToEncodedURL(f); |
|
408 |
} catch (IOException x) { } |
|
409 |
} |
|
410 |
||
411 |
if (urls.length != count) { |
|
412 |
URL[] tmp = new URL[count]; |
|
413 |
System.arraycopy(urls, 0, tmp, 0, count); |
|
414 |
urls = tmp; |
|
415 |
} |
|
416 |
return urls; |
|
417 |
} |
|
418 |
||
419 |
/* |
|
420 |
* Check whether the resource URL should be returned. |
|
421 |
* Return null on security check failure. |
|
422 |
* Called by java.net.URLClassLoader. |
|
423 |
*/ |
|
424 |
public URL checkURL(URL url) { |
|
425 |
try { |
|
426 |
check(url); |
|
427 |
} catch (Exception e) { |
|
428 |
return null; |
|
429 |
} |
|
430 |
||
431 |
return url; |
|
432 |
} |
|
433 |
||
434 |
/* |
|
435 |
* Check whether the resource URL should be returned. |
|
436 |
* Throw exception on failure. |
|
437 |
* Called internally within this file. |
|
438 |
*/ |
|
439 |
static void check(URL url) throws IOException { |
|
440 |
SecurityManager security = System.getSecurityManager(); |
|
441 |
if (security != null) { |
|
442 |
URLConnection urlConnection = url.openConnection(); |
|
443 |
Permission perm = urlConnection.getPermission(); |
|
444 |
if (perm != null) { |
|
445 |
try { |
|
446 |
security.checkPermission(perm); |
|
447 |
} catch (SecurityException se) { |
|
448 |
// fallback to checkRead/checkConnect for pre 1.2 |
|
449 |
// security managers |
|
450 |
if ((perm instanceof java.io.FilePermission) && |
|
451 |
perm.getActions().indexOf("read") != -1) { |
|
452 |
security.checkRead(perm.getName()); |
|
453 |
} else if ((perm instanceof |
|
454 |
java.net.SocketPermission) && |
|
455 |
perm.getActions().indexOf("connect") != -1) { |
|
456 |
URL locUrl = url; |
|
457 |
if (urlConnection instanceof JarURLConnection) { |
|
458 |
locUrl = ((JarURLConnection)urlConnection).getJarFileURL(); |
|
459 |
} |
|
460 |
security.checkConnect(locUrl.getHost(), |
|
461 |
locUrl.getPort()); |
|
462 |
} else { |
|
463 |
throw se; |
|
464 |
} |
|
465 |
} |
|
466 |
} |
|
467 |
} |
|
468 |
} |
|
469 |
||
470 |
/** |
|
471 |
* Inner class used to represent a loader of resources and classes |
|
472 |
* from a base URL. |
|
473 |
*/ |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
474 |
private static class Loader implements Closeable { |
2 | 475 |
private final URL base; |
7980
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
476 |
private JarFile jarfile; // if this points to a jar file |
2 | 477 |
|
478 |
/* |
|
479 |
* Creates a new Loader for the specified URL. |
|
480 |
*/ |
|
481 |
Loader(URL url) { |
|
482 |
base = url; |
|
483 |
} |
|
484 |
||
485 |
/* |
|
486 |
* Returns the base URL for this Loader. |
|
487 |
*/ |
|
488 |
URL getBaseURL() { |
|
489 |
return base; |
|
490 |
} |
|
491 |
||
492 |
URL findResource(final String name, boolean check) { |
|
493 |
URL url; |
|
494 |
try { |
|
495 |
url = new URL(base, ParseUtil.encodePath(name, false)); |
|
496 |
} catch (MalformedURLException e) { |
|
497 |
throw new IllegalArgumentException("name"); |
|
498 |
} |
|
499 |
||
500 |
try { |
|
501 |
if (check) { |
|
502 |
URLClassPath.check(url); |
|
503 |
} |
|
504 |
||
505 |
/* |
|
506 |
* For a HTTP connection we use the HEAD method to |
|
507 |
* check if the resource exists. |
|
508 |
*/ |
|
509 |
URLConnection uc = url.openConnection(); |
|
510 |
if (uc instanceof HttpURLConnection) { |
|
511 |
HttpURLConnection hconn = (HttpURLConnection)uc; |
|
512 |
hconn.setRequestMethod("HEAD"); |
|
513 |
if (hconn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { |
|
514 |
return null; |
|
515 |
} |
|
516 |
} else { |
|
517 |
// our best guess for the other cases |
|
15305
45a0eb6ab4f7
7183373: URLClassloader.close() does not close JAR files whose resources have been loaded via getResource()
dingxmin
parents:
11131
diff
changeset
|
518 |
uc.setUseCaches(false); |
45a0eb6ab4f7
7183373: URLClassloader.close() does not close JAR files whose resources have been loaded via getResource()
dingxmin
parents:
11131
diff
changeset
|
519 |
InputStream is = uc.getInputStream(); |
2 | 520 |
is.close(); |
521 |
} |
|
522 |
return url; |
|
523 |
} catch (Exception e) { |
|
524 |
return null; |
|
525 |
} |
|
526 |
} |
|
527 |
||
528 |
Resource getResource(final String name, boolean check) { |
|
529 |
final URL url; |
|
530 |
try { |
|
531 |
url = new URL(base, ParseUtil.encodePath(name, false)); |
|
532 |
} catch (MalformedURLException e) { |
|
533 |
throw new IllegalArgumentException("name"); |
|
534 |
} |
|
535 |
final URLConnection uc; |
|
536 |
try { |
|
537 |
if (check) { |
|
538 |
URLClassPath.check(url); |
|
539 |
} |
|
540 |
uc = url.openConnection(); |
|
541 |
InputStream in = uc.getInputStream(); |
|
7980
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
542 |
if (uc instanceof JarURLConnection) { |
9823
2bc6b0d4490e
7050028: ISE "zip file closed" from JarURLConnection.getInputStream on JDK 7 when !useCaches
michaelm
parents:
9035
diff
changeset
|
543 |
/* Need to remember the jar file so it can be closed |
7980
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
544 |
* in a hurry. |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
545 |
*/ |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
546 |
JarURLConnection juc = (JarURLConnection)uc; |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
547 |
jarfile = juc.getJarFile(); |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
548 |
} |
2 | 549 |
} catch (Exception e) { |
550 |
return null; |
|
551 |
} |
|
552 |
return new Resource() { |
|
553 |
public String getName() { return name; } |
|
554 |
public URL getURL() { return url; } |
|
555 |
public URL getCodeSourceURL() { return base; } |
|
556 |
public InputStream getInputStream() throws IOException { |
|
557 |
return uc.getInputStream(); |
|
558 |
} |
|
559 |
public int getContentLength() throws IOException { |
|
560 |
return uc.getContentLength(); |
|
561 |
} |
|
562 |
}; |
|
563 |
} |
|
564 |
||
565 |
/* |
|
566 |
* Returns the Resource for the specified name, or null if not |
|
567 |
* found or the caller does not have the permission to get the |
|
568 |
* resource. |
|
569 |
*/ |
|
570 |
Resource getResource(final String name) { |
|
571 |
return getResource(name, true); |
|
572 |
} |
|
573 |
||
574 |
/* |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
575 |
* close this loader and release all resources |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
576 |
* method overridden in sub-classes |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
577 |
*/ |
7980
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
578 |
public void close () throws IOException { |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
579 |
if (jarfile != null) { |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
580 |
jarfile.close(); |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
581 |
} |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
582 |
} |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
583 |
|
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
584 |
/* |
2 | 585 |
* Returns the local class path for this loader, or null if none. |
586 |
*/ |
|
587 |
URL[] getClassPath() throws IOException { |
|
588 |
return null; |
|
589 |
} |
|
590 |
} |
|
591 |
||
15682
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
592 |
/** |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
593 |
* Used by the launcher to suppress further checking of the JAR file Profile |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
594 |
* attribute (necessary when the launcher is aborting as the abort involves |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
595 |
* a resource lookup that may involve opening additional JAR files) |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
596 |
*/ |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
597 |
public static void suppressProfileCheckForLauncher() { |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
598 |
profileCheckSuppressedByLauncher = true; |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
599 |
} |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
600 |
|
2 | 601 |
/* |
602 |
* Inner class used to represent a Loader of resources from a JAR URL. |
|
603 |
*/ |
|
604 |
static class JarLoader extends Loader { |
|
605 |
private JarFile jar; |
|
606 |
private URL csu; |
|
607 |
private JarIndex index; |
|
608 |
private MetaIndex metaIndex; |
|
609 |
private URLStreamHandler handler; |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
610 |
private HashMap<String, Loader> lmap; |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
611 |
private boolean closed = false; |
2 | 612 |
|
613 |
/* |
|
614 |
* Creates a new JarLoader for the specified URL referring to |
|
615 |
* a JAR file. |
|
616 |
*/ |
|
51 | 617 |
JarLoader(URL url, URLStreamHandler jarHandler, |
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
618 |
HashMap<String, Loader> loaderMap) |
2 | 619 |
throws IOException |
620 |
{ |
|
621 |
super(new URL("jar", "", -1, url + "!/", jarHandler)); |
|
622 |
csu = url; |
|
623 |
handler = jarHandler; |
|
624 |
lmap = loaderMap; |
|
625 |
||
626 |
if (!isOptimizable(url)) { |
|
627 |
ensureOpen(); |
|
628 |
} else { |
|
629 |
String fileName = url.getFile(); |
|
630 |
if (fileName != null) { |
|
631 |
fileName = ParseUtil.decode(fileName); |
|
632 |
File f = new File(fileName); |
|
633 |
metaIndex = MetaIndex.forJar(f); |
|
634 |
// If the meta index is found but the file is not |
|
635 |
// installed, set metaIndex to null. A typical |
|
636 |
// senario is charsets.jar which won't be installed |
|
637 |
// when the user is running in certain locale environment. |
|
638 |
// The side effect of null metaIndex will cause |
|
639 |
// ensureOpen get called so that IOException is thrown. |
|
640 |
if (metaIndex != null && !f.exists()) { |
|
641 |
metaIndex = null; |
|
642 |
} |
|
643 |
} |
|
644 |
||
645 |
// metaIndex is null when either there is no such jar file |
|
646 |
// entry recorded in meta-index file or such jar file is |
|
647 |
// missing in JRE. See bug 6340399. |
|
648 |
if (metaIndex == null) { |
|
649 |
ensureOpen(); |
|
650 |
} |
|
651 |
} |
|
652 |
} |
|
653 |
||
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
654 |
@Override |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
655 |
public void close () throws IOException { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
656 |
// closing is synchronized at higher level |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
657 |
if (!closed) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
658 |
closed = true; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
659 |
// in case not already open. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
660 |
ensureOpen(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
661 |
jar.close(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
662 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
663 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
664 |
|
2 | 665 |
JarFile getJarFile () { |
666 |
return jar; |
|
667 |
} |
|
668 |
||
669 |
private boolean isOptimizable(URL url) { |
|
670 |
return "file".equals(url.getProtocol()); |
|
671 |
} |
|
672 |
||
673 |
private void ensureOpen() throws IOException { |
|
674 |
if (jar == null) { |
|
675 |
try { |
|
676 |
java.security.AccessController.doPrivileged( |
|
51 | 677 |
new java.security.PrivilegedExceptionAction<Void>() { |
678 |
public Void run() throws IOException { |
|
2 | 679 |
if (DEBUG) { |
680 |
System.err.println("Opening " + csu); |
|
681 |
Thread.dumpStack(); |
|
682 |
} |
|
683 |
||
684 |
jar = getJarFile(csu); |
|
685 |
index = JarIndex.getJarIndex(jar, metaIndex); |
|
686 |
if (index != null) { |
|
687 |
String[] jarfiles = index.getJarFiles(); |
|
688 |
// Add all the dependent URLs to the lmap so that loaders |
|
689 |
// will not be created for them by URLClassPath.getLoader(int) |
|
690 |
// if the same URL occurs later on the main class path. We set |
|
691 |
// Loader to null here to avoid creating a Loader for each |
|
692 |
// URL until we actually need to try to load something from them. |
|
693 |
for(int i = 0; i < jarfiles.length; i++) { |
|
694 |
try { |
|
695 |
URL jarURL = new URL(csu, jarfiles[i]); |
|
696 |
// If a non-null loader already exists, leave it alone. |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
697 |
String urlNoFragString = URLUtil.urlNoFragString(jarURL); |
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
698 |
if (!lmap.containsKey(urlNoFragString)) { |
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
699 |
lmap.put(urlNoFragString, null); |
2 | 700 |
} |
701 |
} catch (MalformedURLException e) { |
|
702 |
continue; |
|
703 |
} |
|
704 |
} |
|
705 |
} |
|
706 |
return null; |
|
707 |
} |
|
708 |
} |
|
709 |
); |
|
710 |
} catch (java.security.PrivilegedActionException pae) { |
|
711 |
throw (IOException)pae.getException(); |
|
712 |
} |
|
713 |
} |
|
714 |
} |
|
715 |
||
716 |
private JarFile getJarFile(URL url) throws IOException { |
|
717 |
// Optimize case where url refers to a local jar file |
|
718 |
if (isOptimizable(url)) { |
|
719 |
FileURLMapper p = new FileURLMapper (url); |
|
720 |
if (!p.exists()) { |
|
721 |
throw new FileNotFoundException(p.getPath()); |
|
722 |
} |
|
723 |
return new JarFile (p.getPath()); |
|
724 |
} |
|
725 |
URLConnection uc = getBaseURL().openConnection(); |
|
726 |
uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION); |
|
727 |
return ((JarURLConnection)uc).getJarFile(); |
|
728 |
} |
|
729 |
||
730 |
/* |
|
731 |
* Returns the index of this JarLoader if it exists. |
|
732 |
*/ |
|
733 |
JarIndex getIndex() { |
|
734 |
try { |
|
735 |
ensureOpen(); |
|
736 |
} catch (IOException e) { |
|
10355
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
9823
diff
changeset
|
737 |
throw new InternalError(e); |
2 | 738 |
} |
739 |
return index; |
|
740 |
} |
|
741 |
||
742 |
/* |
|
743 |
* Creates the resource and if the check flag is set to true, checks if |
|
744 |
* is its okay to return the resource. |
|
745 |
*/ |
|
746 |
Resource checkResource(final String name, boolean check, |
|
747 |
final JarEntry entry) { |
|
748 |
||
749 |
final URL url; |
|
750 |
try { |
|
751 |
url = new URL(getBaseURL(), ParseUtil.encodePath(name, false)); |
|
752 |
if (check) { |
|
753 |
URLClassPath.check(url); |
|
754 |
} |
|
755 |
} catch (MalformedURLException e) { |
|
756 |
return null; |
|
757 |
// throw new IllegalArgumentException("name"); |
|
758 |
} catch (IOException e) { |
|
759 |
return null; |
|
760 |
} catch (AccessControlException e) { |
|
761 |
return null; |
|
762 |
} |
|
763 |
||
764 |
return new Resource() { |
|
765 |
public String getName() { return name; } |
|
766 |
public URL getURL() { return url; } |
|
767 |
public URL getCodeSourceURL() { return csu; } |
|
768 |
public InputStream getInputStream() throws IOException |
|
769 |
{ return jar.getInputStream(entry); } |
|
770 |
public int getContentLength() |
|
771 |
{ return (int)entry.getSize(); } |
|
772 |
public Manifest getManifest() throws IOException |
|
773 |
{ return jar.getManifest(); }; |
|
774 |
public Certificate[] getCertificates() |
|
775 |
{ return entry.getCertificates(); }; |
|
776 |
public CodeSigner[] getCodeSigners() |
|
777 |
{ return entry.getCodeSigners(); }; |
|
778 |
}; |
|
779 |
} |
|
780 |
||
781 |
||
782 |
/* |
|
783 |
* Returns true iff atleast one resource in the jar file has the same |
|
784 |
* package name as that of the specified resource name. |
|
785 |
*/ |
|
786 |
boolean validIndex(final String name) { |
|
787 |
String packageName = name; |
|
788 |
int pos; |
|
789 |
if((pos = name.lastIndexOf("/")) != -1) { |
|
790 |
packageName = name.substring(0, pos); |
|
791 |
} |
|
792 |
||
793 |
String entryName; |
|
794 |
ZipEntry entry; |
|
51 | 795 |
Enumeration<JarEntry> enum_ = jar.entries(); |
2 | 796 |
while (enum_.hasMoreElements()) { |
51 | 797 |
entry = enum_.nextElement(); |
2 | 798 |
entryName = entry.getName(); |
799 |
if((pos = entryName.lastIndexOf("/")) != -1) |
|
800 |
entryName = entryName.substring(0, pos); |
|
801 |
if (entryName.equals(packageName)) { |
|
802 |
return true; |
|
803 |
} |
|
804 |
} |
|
805 |
return false; |
|
806 |
} |
|
807 |
||
15682
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
808 |
/** |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
809 |
* If the Profile attribute is present then this method checks that the runtime |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
810 |
* supports that profile. |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
811 |
* |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
812 |
* ## Add a fast path like Class-Path to avoid reading the manifest when the attribute |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
813 |
* is not present. |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
814 |
*/ |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
815 |
void checkProfileAttribute() throws IOException { |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
816 |
Manifest man = jar.getManifest(); |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
817 |
if (man != null) { |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
818 |
Attributes attr = man.getMainAttributes(); |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
819 |
if (attr != null) { |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
820 |
String value = attr.getValue(Name.PROFILE); |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
821 |
if (value != null && !Version.supportsProfile(value)) { |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
822 |
String prefix = Version.profileName().length() > 0 ? |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
823 |
"This runtime implements " + Version.profileName() + ", " : ""; |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
824 |
throw new UnsupportedProfileException(prefix + csu + " requires " + value); |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
825 |
} |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
826 |
} |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
827 |
} |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
828 |
} |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
829 |
|
2 | 830 |
/* |
831 |
* Returns the URL for a resource with the specified name |
|
832 |
*/ |
|
833 |
URL findResource(final String name, boolean check) { |
|
834 |
Resource rsc = getResource(name, check); |
|
835 |
if (rsc != null) { |
|
836 |
return rsc.getURL(); |
|
837 |
} |
|
838 |
return null; |
|
839 |
} |
|
840 |
||
841 |
/* |
|
842 |
* Returns the JAR Resource for the specified name. |
|
843 |
*/ |
|
844 |
Resource getResource(final String name, boolean check) { |
|
845 |
if (metaIndex != null) { |
|
846 |
if (!metaIndex.mayContain(name)) { |
|
847 |
return null; |
|
848 |
} |
|
849 |
} |
|
850 |
||
851 |
try { |
|
852 |
ensureOpen(); |
|
853 |
} catch (IOException e) { |
|
10355
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
9823
diff
changeset
|
854 |
throw new InternalError(e); |
2 | 855 |
} |
856 |
final JarEntry entry = jar.getJarEntry(name); |
|
857 |
if (entry != null) |
|
858 |
return checkResource(name, check, entry); |
|
859 |
||
860 |
if (index == null) |
|
861 |
return null; |
|
862 |
||
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
863 |
HashSet<String> visited = new HashSet<String>(); |
2 | 864 |
return getResource(name, check, visited); |
865 |
} |
|
866 |
||
867 |
/* |
|
868 |
* Version of getResource() that tracks the jar files that have been |
|
869 |
* visited by linking through the index files. This helper method uses |
|
870 |
* a HashSet to store the URLs of jar files that have been searched and |
|
871 |
* uses it to avoid going into an infinite loop, looking for a |
|
872 |
* non-existent resource |
|
873 |
*/ |
|
874 |
Resource getResource(final String name, boolean check, |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
875 |
Set<String> visited) { |
2 | 876 |
|
877 |
Resource res; |
|
11131
27747ee5a62a
7116857: Warnings in javax.security and some sun.misc
weijun
parents:
10355
diff
changeset
|
878 |
String[] jarFiles; |
2 | 879 |
int count = 0; |
11131
27747ee5a62a
7116857: Warnings in javax.security and some sun.misc
weijun
parents:
10355
diff
changeset
|
880 |
LinkedList<String> jarFilesList = null; |
2 | 881 |
|
882 |
/* If there no jar files in the index that can potential contain |
|
883 |
* this resource then return immediately. |
|
884 |
*/ |
|
885 |
if((jarFilesList = index.get(name)) == null) |
|
886 |
return null; |
|
887 |
||
888 |
do { |
|
889 |
int size = jarFilesList.size(); |
|
11131
27747ee5a62a
7116857: Warnings in javax.security and some sun.misc
weijun
parents:
10355
diff
changeset
|
890 |
jarFiles = jarFilesList.toArray(new String[size]); |
2 | 891 |
/* loop through the mapped jar file list */ |
892 |
while(count < size) { |
|
11131
27747ee5a62a
7116857: Warnings in javax.security and some sun.misc
weijun
parents:
10355
diff
changeset
|
893 |
String jarName = jarFiles[count++]; |
2 | 894 |
JarLoader newLoader; |
895 |
final URL url; |
|
896 |
||
897 |
try{ |
|
898 |
url = new URL(csu, jarName); |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
899 |
String urlNoFragString = URLUtil.urlNoFragString(url); |
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
900 |
if ((newLoader = (JarLoader)lmap.get(urlNoFragString)) == null) { |
2 | 901 |
/* no loader has been set up for this jar file |
902 |
* before |
|
903 |
*/ |
|
51 | 904 |
newLoader = AccessController.doPrivileged( |
905 |
new PrivilegedExceptionAction<JarLoader>() { |
|
906 |
public JarLoader run() throws IOException { |
|
2 | 907 |
return new JarLoader(url, handler, |
908 |
lmap); |
|
909 |
} |
|
910 |
}); |
|
911 |
||
912 |
/* this newly opened jar file has its own index, |
|
913 |
* merge it into the parent's index, taking into |
|
914 |
* account the relative path. |
|
915 |
*/ |
|
916 |
JarIndex newIndex = newLoader.getIndex(); |
|
917 |
if(newIndex != null) { |
|
918 |
int pos = jarName.lastIndexOf("/"); |
|
919 |
newIndex.merge(this.index, (pos == -1 ? |
|
920 |
null : jarName.substring(0, pos + 1))); |
|
921 |
} |
|
922 |
||
923 |
/* put it in the global hashtable */ |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
924 |
lmap.put(urlNoFragString, newLoader); |
2 | 925 |
} |
926 |
} catch (java.security.PrivilegedActionException pae) { |
|
927 |
continue; |
|
928 |
} catch (MalformedURLException e) { |
|
929 |
continue; |
|
930 |
} |
|
931 |
||
932 |
||
933 |
/* Note that the addition of the url to the list of visited |
|
934 |
* jars incorporates a check for presence in the hashmap |
|
935 |
*/ |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
936 |
boolean visitedURL = !visited.add(URLUtil.urlNoFragString(url)); |
2 | 937 |
if (!visitedURL) { |
938 |
try { |
|
939 |
newLoader.ensureOpen(); |
|
940 |
} catch (IOException e) { |
|
10355
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
9823
diff
changeset
|
941 |
throw new InternalError(e); |
2 | 942 |
} |
943 |
final JarEntry entry = newLoader.jar.getJarEntry(name); |
|
944 |
if (entry != null) { |
|
945 |
return newLoader.checkResource(name, check, entry); |
|
946 |
} |
|
947 |
||
948 |
/* Verify that at least one other resource with the |
|
949 |
* same package name as the lookedup resource is |
|
950 |
* present in the new jar |
|
951 |
*/ |
|
952 |
if (!newLoader.validIndex(name)) { |
|
953 |
/* the mapping is wrong */ |
|
954 |
throw new InvalidJarIndexException("Invalid index"); |
|
955 |
} |
|
956 |
} |
|
957 |
||
958 |
/* If newLoader is the current loader or if it is a |
|
959 |
* loader that has already been searched or if the new |
|
960 |
* loader does not have an index then skip it |
|
961 |
* and move on to the next loader. |
|
962 |
*/ |
|
963 |
if (visitedURL || newLoader == this || |
|
964 |
newLoader.getIndex() == null) { |
|
965 |
continue; |
|
966 |
} |
|
967 |
||
968 |
/* Process the index of the new loader |
|
969 |
*/ |
|
970 |
if((res = newLoader.getResource(name, check, visited)) |
|
971 |
!= null) { |
|
972 |
return res; |
|
973 |
} |
|
974 |
} |
|
975 |
// Get the list of jar files again as the list could have grown |
|
976 |
// due to merging of index files. |
|
977 |
jarFilesList = index.get(name); |
|
978 |
||
979 |
// If the count is unchanged, we are done. |
|
980 |
} while(count < jarFilesList.size()); |
|
981 |
return null; |
|
982 |
} |
|
983 |
||
984 |
||
985 |
/* |
|
986 |
* Returns the JAR file local class path, or null if none. |
|
987 |
*/ |
|
988 |
URL[] getClassPath() throws IOException { |
|
989 |
if (index != null) { |
|
990 |
return null; |
|
991 |
} |
|
992 |
||
993 |
if (metaIndex != null) { |
|
994 |
return null; |
|
995 |
} |
|
996 |
||
997 |
ensureOpen(); |
|
998 |
parseExtensionsDependencies(); |
|
15682
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
999 |
|
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
1000 |
// check Profile attribute if present |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
1001 |
if (!profileCheckSuppressedByLauncher) { |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
1002 |
checkProfileAttribute(); |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
1003 |
} |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
1004 |
|
2 | 1005 |
if (SharedSecrets.javaUtilJarAccess().jarFileHasClassPathAttribute(jar)) { // Only get manifest when necessary |
1006 |
Manifest man = jar.getManifest(); |
|
1007 |
if (man != null) { |
|
1008 |
Attributes attr = man.getMainAttributes(); |
|
1009 |
if (attr != null) { |
|
1010 |
String value = attr.getValue(Name.CLASS_PATH); |
|
1011 |
if (value != null) { |
|
1012 |
return parseClassPath(csu, value); |
|
1013 |
} |
|
1014 |
} |
|
1015 |
} |
|
1016 |
} |
|
1017 |
return null; |
|
1018 |
} |
|
1019 |
||
1020 |
/* |
|
1021 |
* parse the standard extension dependencies |
|
1022 |
*/ |
|
1023 |
private void parseExtensionsDependencies() throws IOException { |
|
1024 |
ExtensionDependency.checkExtensionsDependencies(jar); |
|
1025 |
} |
|
1026 |
||
1027 |
/* |
|
1028 |
* Parses value of the Class-Path manifest attribute and returns |
|
1029 |
* an array of URLs relative to the specified base URL. |
|
1030 |
*/ |
|
1031 |
private URL[] parseClassPath(URL base, String value) |
|
1032 |
throws MalformedURLException |
|
1033 |
{ |
|
1034 |
StringTokenizer st = new StringTokenizer(value); |
|
1035 |
URL[] urls = new URL[st.countTokens()]; |
|
1036 |
int i = 0; |
|
1037 |
while (st.hasMoreTokens()) { |
|
1038 |
String path = st.nextToken(); |
|
1039 |
urls[i] = new URL(base, path); |
|
1040 |
i++; |
|
1041 |
} |
|
1042 |
return urls; |
|
1043 |
} |
|
1044 |
} |
|
1045 |
||
1046 |
/* |
|
1047 |
* Inner class used to represent a loader of classes and resources |
|
1048 |
* from a file URL that refers to a directory. |
|
1049 |
*/ |
|
1050 |
private static class FileLoader extends Loader { |
|
495
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1051 |
/* Canonicalized File */ |
2 | 1052 |
private File dir; |
1053 |
||
1054 |
FileLoader(URL url) throws IOException { |
|
1055 |
super(url); |
|
1056 |
if (!"file".equals(url.getProtocol())) { |
|
1057 |
throw new IllegalArgumentException("url"); |
|
1058 |
} |
|
1059 |
String path = url.getFile().replace('/', File.separatorChar); |
|
1060 |
path = ParseUtil.decode(path); |
|
495
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1061 |
dir = (new File(path)).getCanonicalFile(); |
2 | 1062 |
} |
1063 |
||
1064 |
/* |
|
1065 |
* Returns the URL for a resource with the specified name |
|
1066 |
*/ |
|
1067 |
URL findResource(final String name, boolean check) { |
|
1068 |
Resource rsc = getResource(name, check); |
|
1069 |
if (rsc != null) { |
|
1070 |
return rsc.getURL(); |
|
1071 |
} |
|
1072 |
return null; |
|
1073 |
} |
|
1074 |
||
1075 |
Resource getResource(final String name, boolean check) { |
|
1076 |
final URL url; |
|
1077 |
try { |
|
1078 |
URL normalizedBase = new URL(getBaseURL(), "."); |
|
1079 |
url = new URL(getBaseURL(), ParseUtil.encodePath(name, false)); |
|
1080 |
||
1081 |
if (url.getFile().startsWith(normalizedBase.getFile()) == false) { |
|
1082 |
// requested resource had ../..'s in path |
|
1083 |
return null; |
|
1084 |
} |
|
1085 |
||
1086 |
if (check) |
|
1087 |
URLClassPath.check(url); |
|
495
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1088 |
|
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1089 |
final File file; |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1090 |
if (name.indexOf("..") != -1) { |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1091 |
file = (new File(dir, name.replace('/', File.separatorChar))) |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1092 |
.getCanonicalFile(); |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1093 |
if ( !((file.getPath()).startsWith(dir.getPath())) ) { |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1094 |
/* outside of base dir */ |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1095 |
return null; |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1096 |
} |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1097 |
} else { |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1098 |
file = new File(dir, name.replace('/', File.separatorChar)); |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1099 |
} |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1100 |
|
2 | 1101 |
if (file.exists()) { |
1102 |
return new Resource() { |
|
1103 |
public String getName() { return name; }; |
|
1104 |
public URL getURL() { return url; }; |
|
1105 |
public URL getCodeSourceURL() { return getBaseURL(); }; |
|
1106 |
public InputStream getInputStream() throws IOException |
|
1107 |
{ return new FileInputStream(file); }; |
|
1108 |
public int getContentLength() throws IOException |
|
1109 |
{ return (int)file.length(); }; |
|
1110 |
}; |
|
1111 |
} |
|
1112 |
} catch (Exception e) { |
|
1113 |
return null; |
|
1114 |
} |
|
1115 |
return null; |
|
1116 |
} |
|
1117 |
} |
|
1118 |
} |