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