author | mchung |
Tue, 06 Nov 2018 10:01:16 -0800 | |
changeset 52427 | 3c6aa484536c |
parent 52172 | f54dcfc5a5f8 |
child 52902 | e3398b2e1ab0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
52164 | 2 |
* Copyright (c) 1997, 2018, 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 |
||
36674
7ab530dd6f10
8149122: Move sun.misc.URLClassPath and Resouce to an internal package
chegar
parents:
36670
diff
changeset
|
26 |
package jdk.internal.loader; |
2 | 27 |
|
27565 | 28 |
import java.io.Closeable; |
29 |
import java.io.File; |
|
30 |
import java.io.FileInputStream; |
|
31 |
import java.io.FileNotFoundException; |
|
32 |
import java.io.IOException; |
|
33 |
import java.io.InputStream; |
|
34 |
import java.net.HttpURLConnection; |
|
35 |
import java.net.JarURLConnection; |
|
36 |
import java.net.MalformedURLException; |
|
52164 | 37 |
import java.net.URI; |
27565 | 38 |
import java.net.URL; |
39 |
import java.net.URLConnection; |
|
40 |
import java.net.URLStreamHandler; |
|
41 |
import java.net.URLStreamHandlerFactory; |
|
43196 | 42 |
import java.security.AccessControlContext; |
27565 | 43 |
import java.security.AccessControlException; |
44 |
import java.security.AccessController; |
|
45 |
import java.security.CodeSigner; |
|
46 |
import java.security.Permission; |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
47 |
import java.security.PrivilegedActionException; |
27565 | 48 |
import java.security.PrivilegedExceptionAction; |
49 |
import java.security.cert.Certificate; |
|
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
50 |
import java.util.ArrayDeque; |
27565 | 51 |
import java.util.ArrayList; |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
52 |
import java.util.Arrays; |
27565 | 53 |
import java.util.Collections; |
54 |
import java.util.Enumeration; |
|
55 |
import java.util.HashMap; |
|
56 |
import java.util.HashSet; |
|
57 |
import java.util.LinkedList; |
|
58 |
import java.util.List; |
|
59 |
import java.util.NoSuchElementException; |
|
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
36745
diff
changeset
|
60 |
import java.util.Properties; |
27565 | 61 |
import java.util.Set; |
62 |
import java.util.StringTokenizer; |
|
2 | 63 |
import java.util.jar.JarFile; |
64 |
import java.util.zip.ZipEntry; |
|
65 |
import java.util.jar.JarEntry; |
|
66 |
import java.util.jar.Manifest; |
|
67 |
import java.util.jar.Attributes; |
|
68 |
import java.util.jar.Attributes.Name; |
|
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
36000
diff
changeset
|
69 |
import java.util.zip.ZipFile; |
27565 | 70 |
|
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
52172
diff
changeset
|
71 |
import jdk.internal.access.JavaNetURLAccess; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
52172
diff
changeset
|
72 |
import jdk.internal.access.JavaUtilZipFileAccess; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
52172
diff
changeset
|
73 |
import jdk.internal.access.SharedSecrets; |
36745
51effd3e92d0
8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents:
36674
diff
changeset
|
74 |
import jdk.internal.util.jar.InvalidJarIndexError; |
51effd3e92d0
8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents:
36674
diff
changeset
|
75 |
import jdk.internal.util.jar.JarIndex; |
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
76 |
import sun.net.util.URLUtil; |
27565 | 77 |
import sun.net.www.ParseUtil; |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
36745
diff
changeset
|
78 |
import sun.security.action.GetPropertyAction; |
2 | 79 |
|
80 |
/** |
|
81 |
* This class is used to maintain a search path of URLs for loading classes |
|
82 |
* and resources from both JAR files and directories. |
|
83 |
* |
|
84 |
* @author David Connelly |
|
85 |
*/ |
|
86 |
public class URLClassPath { |
|
27565 | 87 |
private static final String USER_AGENT_JAVA_VERSION = "UA-Java-Version"; |
88 |
private static final String JAVA_VERSION; |
|
2 | 89 |
private static final boolean DEBUG; |
18223
35a5c2462991
8008593: Better URLClassLoader resource management
chegar
parents:
16023
diff
changeset
|
90 |
private static final boolean DISABLE_JAR_CHECKING; |
43196 | 91 |
private static final boolean DISABLE_ACC_CHECKING; |
52164 | 92 |
private static final boolean DISABLE_CP_URL_CHECK; |
93 |
private static final boolean DEBUG_CP_URL_CHECK; |
|
2 | 94 |
|
95 |
static { |
|
37781
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
96 |
Properties props = GetPropertyAction.privilegedGetProperties(); |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
36745
diff
changeset
|
97 |
JAVA_VERSION = props.getProperty("java.version"); |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
36745
diff
changeset
|
98 |
DEBUG = (props.getProperty("sun.misc.URLClassPath.debug") != null); |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
36745
diff
changeset
|
99 |
String p = props.getProperty("sun.misc.URLClassPath.disableJarChecking"); |
18223
35a5c2462991
8008593: Better URLClassLoader resource management
chegar
parents:
16023
diff
changeset
|
100 |
DISABLE_JAR_CHECKING = p != null ? p.equals("true") || p.equals("") : false; |
43196 | 101 |
|
102 |
p = props.getProperty("jdk.net.URLClassPath.disableRestrictedPermissions"); |
|
103 |
DISABLE_ACC_CHECKING = p != null ? p.equals("true") || p.equals("") : false; |
|
52164 | 104 |
|
105 |
// This property will be removed in a later release |
|
52172
f54dcfc5a5f8
8211731: Reconsider default option for ClassPathURLCheck change done in JDK-8195874
bchristi
parents:
52171
diff
changeset
|
106 |
p = props.getProperty("jdk.net.URLClassPath.disableClassPathURLCheck", "true"); |
52164 | 107 |
|
108 |
DISABLE_CP_URL_CHECK = p != null ? p.equals("true") || p.isEmpty() : false; |
|
109 |
DEBUG_CP_URL_CHECK = "debug".equals(p); |
|
2 | 110 |
} |
111 |
||
112 |
/* The original search path of URLs. */ |
|
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
113 |
private final ArrayList<URL> path; |
2 | 114 |
|
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
115 |
/* The deque of unopened URLs */ |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
116 |
private final ArrayDeque<URL> unopenedUrls; |
2 | 117 |
|
118 |
/* The resulting search path of Loaders */ |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
119 |
private final ArrayList<Loader> loaders = new ArrayList<>(); |
2 | 120 |
|
121 |
/* Map of each URL opened to its corresponding Loader */ |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
122 |
private final HashMap<String, Loader> lmap = new HashMap<>(); |
2 | 123 |
|
124 |
/* The jar protocol handler to use when creating new URLs */ |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
125 |
private final URLStreamHandler jarHandler; |
2 | 126 |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
127 |
/* Whether this URLClassLoader has been closed yet */ |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
128 |
private boolean closed = false; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
129 |
|
43196 | 130 |
/* The context to be used when loading classes and resources. If non-null |
131 |
* this is the context that was captured during the creation of the |
|
132 |
* URLClassLoader. null implies no additional security restrictions. */ |
|
133 |
private final AccessControlContext acc; |
|
134 |
||
2 | 135 |
/** |
136 |
* Creates a new URLClassPath for the given URLs. The URLs will be |
|
137 |
* searched in the order specified for classes and resources. A URL |
|
138 |
* ending with a '/' is assumed to refer to a directory. Otherwise, |
|
139 |
* the URL is assumed to refer to a JAR file. |
|
140 |
* |
|
141 |
* @param urls the directory and JAR file URLs to search for classes |
|
142 |
* and resources |
|
143 |
* @param factory the URLStreamHandlerFactory to use when creating new URLs |
|
43196 | 144 |
* @param acc the context to be used when loading classes and resources, may |
145 |
* be null |
|
2 | 146 |
*/ |
43196 | 147 |
public URLClassPath(URL[] urls, |
148 |
URLStreamHandlerFactory factory, |
|
149 |
AccessControlContext acc) { |
|
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
150 |
ArrayList<URL> path = new ArrayList<>(urls.length); |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
151 |
ArrayDeque<URL> unopenedUrls = new ArrayDeque<>(urls.length); |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
152 |
for (URL url : urls) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
153 |
path.add(url); |
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
154 |
unopenedUrls.add(url); |
2 | 155 |
} |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
156 |
this.path = path; |
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
157 |
this.unopenedUrls = unopenedUrls; |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
158 |
|
2 | 159 |
if (factory != null) { |
160 |
jarHandler = factory.createURLStreamHandler("jar"); |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
161 |
} else { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
162 |
jarHandler = null; |
2 | 163 |
} |
43196 | 164 |
if (DISABLE_ACC_CHECKING) |
165 |
this.acc = null; |
|
166 |
else |
|
167 |
this.acc = acc; |
|
2 | 168 |
} |
169 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
170 |
public URLClassPath(URL[] urls, AccessControlContext acc) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
171 |
this(urls, null, acc); |
43196 | 172 |
} |
173 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
174 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
175 |
* Constructs a URLClassPath from a class path string. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
176 |
* |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
177 |
* @param cp the class path string |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
178 |
* @param skipEmptyElements indicates if empty elements are ignored or |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
179 |
* treated as the current working directory |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
180 |
* |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
181 |
* @apiNote Used to create the application class path. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
182 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
183 |
URLClassPath(String cp, boolean skipEmptyElements) { |
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
184 |
ArrayList<URL> path = new ArrayList<>(); |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
185 |
if (cp != null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
186 |
// map each element of class path to a file URL |
48998 | 187 |
int off = 0, next; |
188 |
do { |
|
189 |
next = cp.indexOf(File.pathSeparator, off); |
|
190 |
String element = (next == -1) |
|
191 |
? cp.substring(off) |
|
192 |
: cp.substring(off, next); |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
193 |
if (element.length() > 0 || !skipEmptyElements) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
194 |
URL url = toFileURL(element); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
195 |
if (url != null) path.add(url); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
196 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
197 |
off = next + 1; |
48998 | 198 |
} while (next != -1); |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
199 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
200 |
|
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
201 |
// can't use ArrayDeque#addAll or new ArrayDeque(Collection); |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
202 |
// it's too early in the bootstrap to trigger use of lambdas |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
203 |
int size = path.size(); |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
204 |
ArrayDeque<URL> unopenedUrls = new ArrayDeque<>(size); |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
205 |
for (int i = 0; i < size; i++) |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
206 |
unopenedUrls.add(path.get(i)); |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
207 |
|
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
208 |
this.unopenedUrls = unopenedUrls; |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
209 |
this.path = path; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
210 |
this.jarHandler = null; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
211 |
this.acc = null; |
2 | 212 |
} |
213 |
||
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
214 |
public synchronized List<IOException> closeLoaders() { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
215 |
if (closed) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
216 |
return Collections.emptyList(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
217 |
} |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
27565
diff
changeset
|
218 |
List<IOException> result = new LinkedList<>(); |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
219 |
for (Loader loader : loaders) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
220 |
try { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
221 |
loader.close(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
222 |
} catch (IOException e) { |
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
223 |
result.add(e); |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
224 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
225 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
226 |
closed = true; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
227 |
return result; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
228 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
229 |
|
2 | 230 |
/** |
231 |
* Appends the specified URL to the search path of directory and JAR |
|
232 |
* file URLs from which to load classes and resources. |
|
233 |
* <p> |
|
234 |
* If the URL specified is null or is already in the list of |
|
235 |
* URLs, then invoking this method has no effect. |
|
236 |
*/ |
|
3073
5e93bd389012
6827999: 6827999: URLClassLoader.addURL(URL) adds URLs to closed class loader
michaelm
parents:
1940
diff
changeset
|
237 |
public synchronized void addURL(URL url) { |
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
238 |
if (closed || url == null) |
3073
5e93bd389012
6827999: 6827999: URLClassLoader.addURL(URL) adds URLs to closed class loader
michaelm
parents:
1940
diff
changeset
|
239 |
return; |
48996
1c28a76198da
8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents:
48995
diff
changeset
|
240 |
synchronized (unopenedUrls) { |
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
241 |
if (! path.contains(url)) { |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
242 |
unopenedUrls.addLast(url); |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
243 |
path.add(url); |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
244 |
} |
2 | 245 |
} |
246 |
} |
|
247 |
||
248 |
/** |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
249 |
* Appends the specified file path as a file URL to the search path. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
250 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
251 |
public void addFile(String s) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
252 |
URL url = toFileURL(s); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
253 |
if (url != null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
254 |
addURL(url); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
255 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
256 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
257 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
258 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
259 |
* Returns a file URL for the given file path. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
260 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
261 |
private static URL toFileURL(String s) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
262 |
try { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
263 |
File f = new File(s).getCanonicalFile(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
264 |
return ParseUtil.fileToEncodedURL(f); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
265 |
} catch (IOException e) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
266 |
return null; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
267 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
268 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
269 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
270 |
/** |
2 | 271 |
* Returns the original search path of URLs. |
272 |
*/ |
|
273 |
public URL[] getURLs() { |
|
48996
1c28a76198da
8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents:
48995
diff
changeset
|
274 |
synchronized (unopenedUrls) { |
1c28a76198da
8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents:
48995
diff
changeset
|
275 |
return path.toArray(new URL[0]); |
2 | 276 |
} |
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Finds the resource with the specified name on the URL search path |
|
281 |
* or null if not found or security check fails. |
|
282 |
* |
|
283 |
* @param name the name of the resource |
|
284 |
* @param check whether to perform a security check |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
285 |
* @return a {@code URL} for the resource, or {@code null} |
2 | 286 |
* if the resource could not be found. |
287 |
*/ |
|
288 |
public URL findResource(String name, boolean check) { |
|
289 |
Loader loader; |
|
290 |
for (int i = 0; (loader = getLoader(i)) != null; i++) { |
|
291 |
URL url = loader.findResource(name, check); |
|
292 |
if (url != null) { |
|
293 |
return url; |
|
294 |
} |
|
295 |
} |
|
296 |
return null; |
|
297 |
} |
|
298 |
||
299 |
/** |
|
300 |
* Finds the first Resource on the URL search path which has the specified |
|
301 |
* name. Returns null if no Resource could be found. |
|
302 |
* |
|
303 |
* @param name the name of the Resource |
|
304 |
* @param check whether to perform a security check |
|
305 |
* @return the Resource, or null if not found |
|
306 |
*/ |
|
307 |
public Resource getResource(String name, boolean check) { |
|
308 |
if (DEBUG) { |
|
309 |
System.err.println("URLClassPath.getResource(\"" + name + "\")"); |
|
310 |
} |
|
311 |
||
312 |
Loader loader; |
|
313 |
for (int i = 0; (loader = getLoader(i)) != null; i++) { |
|
314 |
Resource res = loader.getResource(name, check); |
|
315 |
if (res != null) { |
|
316 |
return res; |
|
317 |
} |
|
318 |
} |
|
319 |
return null; |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* Finds all resources on the URL search path with the given name. |
|
324 |
* Returns an enumeration of the URL objects. |
|
325 |
* |
|
326 |
* @param name the resource name |
|
327 |
* @return an Enumeration of all the urls having the specified name |
|
328 |
*/ |
|
51 | 329 |
public Enumeration<URL> findResources(final String name, |
2 | 330 |
final boolean check) { |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
27565
diff
changeset
|
331 |
return new Enumeration<>() { |
2 | 332 |
private int index = 0; |
333 |
private URL url = null; |
|
334 |
||
335 |
private boolean next() { |
|
336 |
if (url != null) { |
|
337 |
return true; |
|
338 |
} else { |
|
339 |
Loader loader; |
|
340 |
while ((loader = getLoader(index++)) != null) { |
|
341 |
url = loader.findResource(name, check); |
|
342 |
if (url != null) { |
|
343 |
return true; |
|
344 |
} |
|
345 |
} |
|
346 |
return false; |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
public boolean hasMoreElements() { |
|
351 |
return next(); |
|
352 |
} |
|
353 |
||
51 | 354 |
public URL nextElement() { |
2 | 355 |
if (!next()) { |
356 |
throw new NoSuchElementException(); |
|
357 |
} |
|
358 |
URL u = url; |
|
359 |
url = null; |
|
360 |
return u; |
|
361 |
} |
|
362 |
}; |
|
363 |
} |
|
364 |
||
365 |
public Resource getResource(String name) { |
|
366 |
return getResource(name, true); |
|
367 |
} |
|
368 |
||
369 |
/** |
|
370 |
* Finds all resources on the URL search path with the given name. |
|
371 |
* Returns an enumeration of the Resource objects. |
|
372 |
* |
|
373 |
* @param name the resource name |
|
374 |
* @return an Enumeration of all the resources having the specified name |
|
375 |
*/ |
|
51 | 376 |
public Enumeration<Resource> getResources(final String name, |
2 | 377 |
final boolean check) { |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
27565
diff
changeset
|
378 |
return new Enumeration<>() { |
2 | 379 |
private int index = 0; |
380 |
private Resource res = null; |
|
381 |
||
382 |
private boolean next() { |
|
383 |
if (res != null) { |
|
384 |
return true; |
|
385 |
} else { |
|
386 |
Loader loader; |
|
387 |
while ((loader = getLoader(index++)) != null) { |
|
388 |
res = loader.getResource(name, check); |
|
389 |
if (res != null) { |
|
390 |
return true; |
|
391 |
} |
|
392 |
} |
|
393 |
return false; |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
public boolean hasMoreElements() { |
|
398 |
return next(); |
|
399 |
} |
|
400 |
||
51 | 401 |
public Resource nextElement() { |
2 | 402 |
if (!next()) { |
403 |
throw new NoSuchElementException(); |
|
404 |
} |
|
405 |
Resource r = res; |
|
406 |
res = null; |
|
407 |
return r; |
|
408 |
} |
|
409 |
}; |
|
410 |
} |
|
411 |
||
51 | 412 |
public Enumeration<Resource> getResources(final String name) { |
2 | 413 |
return getResources(name, true); |
414 |
} |
|
415 |
||
416 |
/* |
|
417 |
* Returns the Loader at the specified position in the URL search |
|
418 |
* path. The URLs are opened and expanded as needed. Returns null |
|
419 |
* if the specified index is out of range. |
|
420 |
*/ |
|
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
421 |
private synchronized Loader getLoader(int index) { |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
422 |
if (closed) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
423 |
return null; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
424 |
} |
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
425 |
// Expand URL search path until the request can be satisfied |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
426 |
// or unopenedUrls is exhausted. |
2 | 427 |
while (loaders.size() < index + 1) { |
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
428 |
final URL url; |
48996
1c28a76198da
8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents:
48995
diff
changeset
|
429 |
synchronized (unopenedUrls) { |
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
430 |
url = unopenedUrls.pollFirst(); |
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
431 |
if (url == null) |
2 | 432 |
return null; |
433 |
} |
|
434 |
// Skip this URL if it already has a Loader. (Loader |
|
435 |
// may be null in the case where URL has not been opened |
|
436 |
// 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
|
437 |
String urlNoFragString = URLUtil.urlNoFragString(url); |
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
438 |
if (lmap.containsKey(urlNoFragString)) { |
2 | 439 |
continue; |
440 |
} |
|
441 |
// Otherwise, create a new Loader for the URL. |
|
442 |
Loader loader; |
|
443 |
try { |
|
444 |
loader = getLoader(url); |
|
445 |
// If the loader defines a local class path then add the |
|
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
446 |
// URLs as the next URLs to be opened. |
2 | 447 |
URL[] urls = loader.getClassPath(); |
448 |
if (urls != null) { |
|
449 |
push(urls); |
|
450 |
} |
|
451 |
} catch (IOException e) { |
|
452 |
// Silently ignore for now... |
|
453 |
continue; |
|
43196 | 454 |
} catch (SecurityException se) { |
455 |
// Always silently ignore. The context, if there is one, that |
|
456 |
// this URLClassPath was given during construction will never |
|
457 |
// have permission to access the URL. |
|
458 |
if (DEBUG) { |
|
459 |
System.err.println("Failed to access " + url + ", " + se ); |
|
460 |
} |
|
461 |
continue; |
|
2 | 462 |
} |
463 |
// Finally, add the Loader to the search path. |
|
464 |
loaders.add(loader); |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
465 |
lmap.put(urlNoFragString, loader); |
2 | 466 |
} |
51 | 467 |
return loaders.get(index); |
2 | 468 |
} |
469 |
||
470 |
/* |
|
471 |
* Returns the Loader for the specified base URL. |
|
472 |
*/ |
|
473 |
private Loader getLoader(final URL url) throws IOException { |
|
474 |
try { |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
475 |
return AccessController.doPrivileged( |
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
476 |
new PrivilegedExceptionAction<>() { |
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
477 |
public Loader run() throws IOException { |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
478 |
String protocol = url.getProtocol(); // lower cased in URL |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
479 |
String file = url.getFile(); |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
480 |
if (file != null && file.endsWith("/")) { |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
481 |
if ("file".equals(protocol)) { |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
482 |
return new FileLoader(url); |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
483 |
} else if ("jar".equals(protocol) && |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
484 |
isDefaultJarHandler(url) && |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
485 |
file.endsWith("!/")) { |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
486 |
// extract the nested URL |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
487 |
URL nestedUrl = new URL(file.substring(0, file.length() - 2)); |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
488 |
return new JarLoader(nestedUrl, jarHandler, lmap, acc); |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
489 |
} else { |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
490 |
return new Loader(url); |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
491 |
} |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
492 |
} else { |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
493 |
return new JarLoader(url, jarHandler, lmap, acc); |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
494 |
} |
2 | 495 |
} |
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
496 |
}, acc); |
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
497 |
} catch (PrivilegedActionException pae) { |
2 | 498 |
throw (IOException)pae.getException(); |
499 |
} |
|
500 |
} |
|
501 |
||
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
502 |
private static final JavaNetURLAccess JNUA |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
503 |
= SharedSecrets.getJavaNetURLAccess(); |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
504 |
|
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
505 |
private static boolean isDefaultJarHandler(URL u) { |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
506 |
URLStreamHandler h = JNUA.getHandler(u); |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
507 |
return h instanceof sun.net.www.protocol.jar.Handler; |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
508 |
} |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
509 |
|
2 | 510 |
/* |
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
511 |
* Pushes the specified URLs onto the head of unopened URLs. |
2 | 512 |
*/ |
48996
1c28a76198da
8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents:
48995
diff
changeset
|
513 |
private void push(URL[] urls) { |
1c28a76198da
8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents:
48995
diff
changeset
|
514 |
synchronized (unopenedUrls) { |
1c28a76198da
8198482: The URLClassPath field "urls" should be renamed to "unopenedUrls"
martin
parents:
48995
diff
changeset
|
515 |
for (int i = urls.length - 1; i >= 0; --i) { |
48997
12c61ec06d2a
8198484: URLClassPath should use an ArrayDeque instead of a Stack
martin
parents:
48996
diff
changeset
|
516 |
unopenedUrls.addFirst(urls[i]); |
2 | 517 |
} |
518 |
} |
|
519 |
} |
|
520 |
||
521 |
/* |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
522 |
* Checks whether the resource URL should be returned. |
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
523 |
* Returns null on security check failure. |
2 | 524 |
* Called by java.net.URLClassLoader. |
525 |
*/ |
|
36511 | 526 |
public static URL checkURL(URL url) { |
527 |
if (url != null) { |
|
528 |
try { |
|
529 |
check(url); |
|
530 |
} catch (Exception e) { |
|
531 |
return null; |
|
532 |
} |
|
2 | 533 |
} |
534 |
return url; |
|
535 |
} |
|
536 |
||
537 |
/* |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
538 |
* Checks whether the resource URL should be returned. |
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
539 |
* Throws exception on failure. |
2 | 540 |
* Called internally within this file. |
541 |
*/ |
|
36511 | 542 |
public static void check(URL url) throws IOException { |
2 | 543 |
SecurityManager security = System.getSecurityManager(); |
544 |
if (security != null) { |
|
545 |
URLConnection urlConnection = url.openConnection(); |
|
546 |
Permission perm = urlConnection.getPermission(); |
|
547 |
if (perm != null) { |
|
548 |
try { |
|
549 |
security.checkPermission(perm); |
|
550 |
} catch (SecurityException se) { |
|
551 |
// fallback to checkRead/checkConnect for pre 1.2 |
|
552 |
// security managers |
|
553 |
if ((perm instanceof java.io.FilePermission) && |
|
554 |
perm.getActions().indexOf("read") != -1) { |
|
555 |
security.checkRead(perm.getName()); |
|
556 |
} else if ((perm instanceof |
|
557 |
java.net.SocketPermission) && |
|
558 |
perm.getActions().indexOf("connect") != -1) { |
|
559 |
URL locUrl = url; |
|
560 |
if (urlConnection instanceof JarURLConnection) { |
|
561 |
locUrl = ((JarURLConnection)urlConnection).getJarFileURL(); |
|
562 |
} |
|
563 |
security.checkConnect(locUrl.getHost(), |
|
564 |
locUrl.getPort()); |
|
565 |
} else { |
|
566 |
throw se; |
|
567 |
} |
|
568 |
} |
|
569 |
} |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
/** |
|
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
574 |
* Nested class used to represent a loader of resources and classes |
2 | 575 |
* from a base URL. |
576 |
*/ |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
577 |
private static class Loader implements Closeable { |
2 | 578 |
private final URL base; |
7980
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
579 |
private JarFile jarfile; // if this points to a jar file |
2 | 580 |
|
581 |
/* |
|
582 |
* Creates a new Loader for the specified URL. |
|
583 |
*/ |
|
584 |
Loader(URL url) { |
|
585 |
base = url; |
|
586 |
} |
|
587 |
||
588 |
/* |
|
589 |
* Returns the base URL for this Loader. |
|
590 |
*/ |
|
591 |
URL getBaseURL() { |
|
592 |
return base; |
|
593 |
} |
|
594 |
||
595 |
URL findResource(final String name, boolean check) { |
|
596 |
URL url; |
|
597 |
try { |
|
598 |
url = new URL(base, ParseUtil.encodePath(name, false)); |
|
599 |
} catch (MalformedURLException e) { |
|
600 |
throw new IllegalArgumentException("name"); |
|
601 |
} |
|
602 |
||
603 |
try { |
|
604 |
if (check) { |
|
605 |
URLClassPath.check(url); |
|
606 |
} |
|
607 |
||
608 |
/* |
|
609 |
* For a HTTP connection we use the HEAD method to |
|
610 |
* check if the resource exists. |
|
611 |
*/ |
|
612 |
URLConnection uc = url.openConnection(); |
|
613 |
if (uc instanceof HttpURLConnection) { |
|
614 |
HttpURLConnection hconn = (HttpURLConnection)uc; |
|
615 |
hconn.setRequestMethod("HEAD"); |
|
616 |
if (hconn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { |
|
617 |
return null; |
|
618 |
} |
|
619 |
} else { |
|
620 |
// 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
|
621 |
uc.setUseCaches(false); |
45a0eb6ab4f7
7183373: URLClassloader.close() does not close JAR files whose resources have been loaded via getResource()
dingxmin
parents:
11131
diff
changeset
|
622 |
InputStream is = uc.getInputStream(); |
2 | 623 |
is.close(); |
624 |
} |
|
625 |
return url; |
|
626 |
} catch (Exception e) { |
|
627 |
return null; |
|
628 |
} |
|
629 |
} |
|
630 |
||
631 |
Resource getResource(final String name, boolean check) { |
|
632 |
final URL url; |
|
633 |
try { |
|
634 |
url = new URL(base, ParseUtil.encodePath(name, false)); |
|
635 |
} catch (MalformedURLException e) { |
|
636 |
throw new IllegalArgumentException("name"); |
|
637 |
} |
|
638 |
final URLConnection uc; |
|
639 |
try { |
|
640 |
if (check) { |
|
641 |
URLClassPath.check(url); |
|
642 |
} |
|
643 |
uc = url.openConnection(); |
|
644 |
InputStream in = uc.getInputStream(); |
|
7980
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
645 |
if (uc instanceof JarURLConnection) { |
9823
2bc6b0d4490e
7050028: ISE "zip file closed" from JarURLConnection.getInputStream on JDK 7 when !useCaches
michaelm
parents:
9035
diff
changeset
|
646 |
/* 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
|
647 |
* in a hurry. |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
648 |
*/ |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
649 |
JarURLConnection juc = (JarURLConnection)uc; |
18223
35a5c2462991
8008593: Better URLClassLoader resource management
chegar
parents:
16023
diff
changeset
|
650 |
jarfile = JarLoader.checkJar(juc.getJarFile()); |
7980
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
651 |
} |
2 | 652 |
} catch (Exception e) { |
653 |
return null; |
|
654 |
} |
|
655 |
return new Resource() { |
|
656 |
public String getName() { return name; } |
|
657 |
public URL getURL() { return url; } |
|
658 |
public URL getCodeSourceURL() { return base; } |
|
659 |
public InputStream getInputStream() throws IOException { |
|
660 |
return uc.getInputStream(); |
|
661 |
} |
|
662 |
public int getContentLength() throws IOException { |
|
663 |
return uc.getContentLength(); |
|
664 |
} |
|
665 |
}; |
|
666 |
} |
|
667 |
||
668 |
/* |
|
669 |
* Returns the Resource for the specified name, or null if not |
|
670 |
* found or the caller does not have the permission to get the |
|
671 |
* resource. |
|
672 |
*/ |
|
673 |
Resource getResource(final String name) { |
|
674 |
return getResource(name, true); |
|
675 |
} |
|
676 |
||
677 |
/* |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
678 |
* Closes this loader and release all resources. |
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
679 |
* Method overridden in sub-classes. |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
680 |
*/ |
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
681 |
@Override |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
682 |
public void close() throws IOException { |
7980
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
683 |
if (jarfile != null) { |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
684 |
jarfile.close(); |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
685 |
} |
3af394bb6f59
6896088: URLClassLoader.close() apparently not working for JAR URLs on Windows
michaelm
parents:
5506
diff
changeset
|
686 |
} |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
687 |
|
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
688 |
/* |
2 | 689 |
* Returns the local class path for this loader, or null if none. |
690 |
*/ |
|
691 |
URL[] getClassPath() throws IOException { |
|
692 |
return null; |
|
693 |
} |
|
694 |
} |
|
695 |
||
696 |
/* |
|
47866
39db80b32b69
8191632: Typos in comments due to duplicating words
igerasim
parents:
47216
diff
changeset
|
697 |
* Nested class used to represent a Loader of resources from a JAR URL. |
2 | 698 |
*/ |
699 |
static class JarLoader extends Loader { |
|
700 |
private JarFile jar; |
|
43196 | 701 |
private final URL csu; |
2 | 702 |
private JarIndex index; |
703 |
private URLStreamHandler handler; |
|
43196 | 704 |
private final HashMap<String, Loader> lmap; |
705 |
private final AccessControlContext acc; |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
706 |
private boolean closed = false; |
32834
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
29986
diff
changeset
|
707 |
private static final JavaUtilZipFileAccess zipAccess = |
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
29986
diff
changeset
|
708 |
SharedSecrets.getJavaUtilZipFileAccess(); |
2 | 709 |
|
710 |
/* |
|
711 |
* Creates a new JarLoader for the specified URL referring to |
|
712 |
* a JAR file. |
|
713 |
*/ |
|
51 | 714 |
JarLoader(URL url, URLStreamHandler jarHandler, |
43196 | 715 |
HashMap<String, Loader> loaderMap, |
716 |
AccessControlContext acc) |
|
2 | 717 |
throws IOException |
718 |
{ |
|
719 |
super(new URL("jar", "", -1, url + "!/", jarHandler)); |
|
720 |
csu = url; |
|
721 |
handler = jarHandler; |
|
722 |
lmap = loaderMap; |
|
43196 | 723 |
this.acc = acc; |
2 | 724 |
|
36000 | 725 |
ensureOpen(); |
2 | 726 |
} |
727 |
||
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
728 |
@Override |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
729 |
public void close () throws IOException { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
730 |
// closing is synchronized at higher level |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
731 |
if (!closed) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
732 |
closed = true; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
733 |
// in case not already open. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
734 |
ensureOpen(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
735 |
jar.close(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
736 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
737 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
738 |
|
2 | 739 |
JarFile getJarFile () { |
740 |
return jar; |
|
741 |
} |
|
742 |
||
743 |
private boolean isOptimizable(URL url) { |
|
744 |
return "file".equals(url.getProtocol()); |
|
745 |
} |
|
746 |
||
747 |
private void ensureOpen() throws IOException { |
|
748 |
if (jar == null) { |
|
749 |
try { |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
750 |
AccessController.doPrivileged( |
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
751 |
new PrivilegedExceptionAction<>() { |
51 | 752 |
public Void run() throws IOException { |
2 | 753 |
if (DEBUG) { |
754 |
System.err.println("Opening " + csu); |
|
755 |
Thread.dumpStack(); |
|
756 |
} |
|
757 |
||
758 |
jar = getJarFile(csu); |
|
36000 | 759 |
index = JarIndex.getJarIndex(jar); |
2 | 760 |
if (index != null) { |
761 |
String[] jarfiles = index.getJarFiles(); |
|
762 |
// Add all the dependent URLs to the lmap so that loaders |
|
763 |
// will not be created for them by URLClassPath.getLoader(int) |
|
764 |
// if the same URL occurs later on the main class path. We set |
|
765 |
// Loader to null here to avoid creating a Loader for each |
|
766 |
// URL until we actually need to try to load something from them. |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
767 |
for (int i = 0; i < jarfiles.length; i++) { |
2 | 768 |
try { |
769 |
URL jarURL = new URL(csu, jarfiles[i]); |
|
770 |
// 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
|
771 |
String urlNoFragString = URLUtil.urlNoFragString(jarURL); |
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
772 |
if (!lmap.containsKey(urlNoFragString)) { |
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
773 |
lmap.put(urlNoFragString, null); |
2 | 774 |
} |
775 |
} catch (MalformedURLException e) { |
|
776 |
continue; |
|
777 |
} |
|
778 |
} |
|
779 |
} |
|
780 |
return null; |
|
781 |
} |
|
43196 | 782 |
}, acc); |
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
783 |
} catch (PrivilegedActionException pae) { |
2 | 784 |
throw (IOException)pae.getException(); |
785 |
} |
|
786 |
} |
|
787 |
} |
|
788 |
||
18223
35a5c2462991
8008593: Better URLClassLoader resource management
chegar
parents:
16023
diff
changeset
|
789 |
/* 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
|
790 |
static JarFile checkJar(JarFile jar) throws IOException { |
35a5c2462991
8008593: Better URLClassLoader resource management
chegar
parents:
16023
diff
changeset
|
791 |
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
|
792 |
&& !zipAccess.startsWithLocHeader(jar)) { |
407ff7067681
8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents:
18223
diff
changeset
|
793 |
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
|
794 |
try { |
407ff7067681
8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents:
18223
diff
changeset
|
795 |
jar.close(); |
407ff7067681
8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents:
18223
diff
changeset
|
796 |
} catch (IOException ex) { |
407ff7067681
8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents:
18223
diff
changeset
|
797 |
x.addSuppressed(ex); |
407ff7067681
8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents:
18223
diff
changeset
|
798 |
} |
407ff7067681
8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents:
18223
diff
changeset
|
799 |
throw x; |
407ff7067681
8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents:
18223
diff
changeset
|
800 |
} |
407ff7067681
8012156: tools/javac/file/zip/T6865530.java fails for win32/64 in 7u25 nightly runs
chegar
parents:
18223
diff
changeset
|
801 |
|
18223
35a5c2462991
8008593: Better URLClassLoader resource management
chegar
parents:
16023
diff
changeset
|
802 |
return jar; |
35a5c2462991
8008593: Better URLClassLoader resource management
chegar
parents:
16023
diff
changeset
|
803 |
} |
35a5c2462991
8008593: Better URLClassLoader resource management
chegar
parents:
16023
diff
changeset
|
804 |
|
2 | 805 |
private JarFile getJarFile(URL url) throws IOException { |
806 |
// Optimize case where url refers to a local jar file |
|
807 |
if (isOptimizable(url)) { |
|
808 |
FileURLMapper p = new FileURLMapper (url); |
|
809 |
if (!p.exists()) { |
|
810 |
throw new FileNotFoundException(p.getPath()); |
|
811 |
} |
|
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
36000
diff
changeset
|
812 |
return checkJar(new JarFile(new File(p.getPath()), true, ZipFile.OPEN_READ, |
39646
2bf99fe5023b
8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents:
37785
diff
changeset
|
813 |
JarFile.runtimeVersion())); |
2 | 814 |
} |
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
36000
diff
changeset
|
815 |
URLConnection uc = (new URL(getBaseURL(), "#runtime")).openConnection(); |
2 | 816 |
uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION); |
18223
35a5c2462991
8008593: Better URLClassLoader resource management
chegar
parents:
16023
diff
changeset
|
817 |
JarFile jarFile = ((JarURLConnection)uc).getJarFile(); |
35a5c2462991
8008593: Better URLClassLoader resource management
chegar
parents:
16023
diff
changeset
|
818 |
return checkJar(jarFile); |
2 | 819 |
} |
820 |
||
821 |
/* |
|
822 |
* Returns the index of this JarLoader if it exists. |
|
823 |
*/ |
|
824 |
JarIndex getIndex() { |
|
825 |
try { |
|
826 |
ensureOpen(); |
|
827 |
} catch (IOException e) { |
|
10355
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
9823
diff
changeset
|
828 |
throw new InternalError(e); |
2 | 829 |
} |
830 |
return index; |
|
831 |
} |
|
832 |
||
833 |
/* |
|
834 |
* Creates the resource and if the check flag is set to true, checks if |
|
835 |
* is its okay to return the resource. |
|
836 |
*/ |
|
837 |
Resource checkResource(final String name, boolean check, |
|
838 |
final JarEntry entry) { |
|
839 |
||
840 |
final URL url; |
|
841 |
try { |
|
37785
daf6dcc73e9d
8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents:
37781
diff
changeset
|
842 |
String nm; |
36251 | 843 |
if (jar.isMultiRelease()) { |
47987
85ea7e83af30
8189611: JarFile versioned stream and real name support
sherman
parents:
47866
diff
changeset
|
844 |
nm = entry.getRealName(); |
36251 | 845 |
} else { |
37785
daf6dcc73e9d
8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents:
37781
diff
changeset
|
846 |
nm = name; |
36251 | 847 |
} |
37785
daf6dcc73e9d
8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents:
37781
diff
changeset
|
848 |
url = new URL(getBaseURL(), ParseUtil.encodePath(nm, false)); |
2 | 849 |
if (check) { |
850 |
URLClassPath.check(url); |
|
851 |
} |
|
852 |
} catch (MalformedURLException e) { |
|
853 |
return null; |
|
854 |
// throw new IllegalArgumentException("name"); |
|
855 |
} catch (IOException e) { |
|
856 |
return null; |
|
857 |
} catch (AccessControlException e) { |
|
858 |
return null; |
|
859 |
} |
|
860 |
||
861 |
return new Resource() { |
|
862 |
public String getName() { return name; } |
|
863 |
public URL getURL() { return url; } |
|
864 |
public URL getCodeSourceURL() { return csu; } |
|
865 |
public InputStream getInputStream() throws IOException |
|
866 |
{ return jar.getInputStream(entry); } |
|
867 |
public int getContentLength() |
|
868 |
{ return (int)entry.getSize(); } |
|
52171 | 869 |
public Manifest getManifest() throws IOException { |
870 |
SharedSecrets.javaUtilJarAccess().ensureInitialization(jar); |
|
871 |
return jar.getManifest(); |
|
872 |
} |
|
2 | 873 |
public Certificate[] getCertificates() |
874 |
{ return entry.getCertificates(); }; |
|
875 |
public CodeSigner[] getCodeSigners() |
|
876 |
{ return entry.getCodeSigners(); }; |
|
877 |
}; |
|
878 |
} |
|
879 |
||
880 |
||
881 |
/* |
|
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
882 |
* Returns true iff at least one resource in the jar file has the same |
2 | 883 |
* package name as that of the specified resource name. |
884 |
*/ |
|
885 |
boolean validIndex(final String name) { |
|
886 |
String packageName = name; |
|
887 |
int pos; |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
888 |
if ((pos = name.lastIndexOf('/')) != -1) { |
2 | 889 |
packageName = name.substring(0, pos); |
890 |
} |
|
891 |
||
892 |
String entryName; |
|
893 |
ZipEntry entry; |
|
51 | 894 |
Enumeration<JarEntry> enum_ = jar.entries(); |
2 | 895 |
while (enum_.hasMoreElements()) { |
51 | 896 |
entry = enum_.nextElement(); |
2 | 897 |
entryName = entry.getName(); |
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
898 |
if ((pos = entryName.lastIndexOf('/')) != -1) |
2 | 899 |
entryName = entryName.substring(0, pos); |
900 |
if (entryName.equals(packageName)) { |
|
901 |
return true; |
|
902 |
} |
|
903 |
} |
|
904 |
return false; |
|
905 |
} |
|
906 |
||
907 |
/* |
|
908 |
* Returns the URL for a resource with the specified name |
|
909 |
*/ |
|
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
910 |
@Override |
2 | 911 |
URL findResource(final String name, boolean check) { |
912 |
Resource rsc = getResource(name, check); |
|
913 |
if (rsc != null) { |
|
914 |
return rsc.getURL(); |
|
915 |
} |
|
916 |
return null; |
|
917 |
} |
|
918 |
||
919 |
/* |
|
920 |
* Returns the JAR Resource for the specified name. |
|
921 |
*/ |
|
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
922 |
@Override |
2 | 923 |
Resource getResource(final String name, boolean check) { |
924 |
try { |
|
925 |
ensureOpen(); |
|
926 |
} catch (IOException e) { |
|
10355
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
9823
diff
changeset
|
927 |
throw new InternalError(e); |
2 | 928 |
} |
929 |
final JarEntry entry = jar.getJarEntry(name); |
|
930 |
if (entry != null) |
|
931 |
return checkResource(name, check, entry); |
|
932 |
||
933 |
if (index == null) |
|
934 |
return null; |
|
935 |
||
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
27565
diff
changeset
|
936 |
HashSet<String> visited = new HashSet<>(); |
2 | 937 |
return getResource(name, check, visited); |
938 |
} |
|
939 |
||
940 |
/* |
|
941 |
* Version of getResource() that tracks the jar files that have been |
|
942 |
* visited by linking through the index files. This helper method uses |
|
943 |
* a HashSet to store the URLs of jar files that have been searched and |
|
944 |
* uses it to avoid going into an infinite loop, looking for a |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
945 |
* non-existent resource. |
2 | 946 |
*/ |
947 |
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
|
948 |
Set<String> visited) { |
2 | 949 |
Resource res; |
11131
27747ee5a62a
7116857: Warnings in javax.security and some sun.misc
weijun
parents:
10355
diff
changeset
|
950 |
String[] jarFiles; |
2 | 951 |
int count = 0; |
11131
27747ee5a62a
7116857: Warnings in javax.security and some sun.misc
weijun
parents:
10355
diff
changeset
|
952 |
LinkedList<String> jarFilesList = null; |
2 | 953 |
|
954 |
/* If there no jar files in the index that can potential contain |
|
955 |
* this resource then return immediately. |
|
956 |
*/ |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
957 |
if ((jarFilesList = index.get(name)) == null) |
2 | 958 |
return null; |
959 |
||
960 |
do { |
|
961 |
int size = jarFilesList.size(); |
|
11131
27747ee5a62a
7116857: Warnings in javax.security and some sun.misc
weijun
parents:
10355
diff
changeset
|
962 |
jarFiles = jarFilesList.toArray(new String[size]); |
2 | 963 |
/* loop through the mapped jar file list */ |
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
964 |
while (count < size) { |
11131
27747ee5a62a
7116857: Warnings in javax.security and some sun.misc
weijun
parents:
10355
diff
changeset
|
965 |
String jarName = jarFiles[count++]; |
2 | 966 |
JarLoader newLoader; |
967 |
final URL url; |
|
968 |
||
969 |
try{ |
|
970 |
url = new URL(csu, jarName); |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
971 |
String urlNoFragString = URLUtil.urlNoFragString(url); |
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
972 |
if ((newLoader = (JarLoader)lmap.get(urlNoFragString)) == null) { |
2 | 973 |
/* no loader has been set up for this jar file |
974 |
* before |
|
975 |
*/ |
|
51 | 976 |
newLoader = AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
27565
diff
changeset
|
977 |
new PrivilegedExceptionAction<>() { |
51 | 978 |
public JarLoader run() throws IOException { |
2 | 979 |
return new JarLoader(url, handler, |
43196 | 980 |
lmap, acc); |
2 | 981 |
} |
43196 | 982 |
}, acc); |
2 | 983 |
|
984 |
/* this newly opened jar file has its own index, |
|
985 |
* merge it into the parent's index, taking into |
|
986 |
* account the relative path. |
|
987 |
*/ |
|
988 |
JarIndex newIndex = newLoader.getIndex(); |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
989 |
if (newIndex != null) { |
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
23010
diff
changeset
|
990 |
int pos = jarName.lastIndexOf('/'); |
2 | 991 |
newIndex.merge(this.index, (pos == -1 ? |
992 |
null : jarName.substring(0, pos + 1))); |
|
993 |
} |
|
994 |
||
995 |
/* 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
|
996 |
lmap.put(urlNoFragString, newLoader); |
2 | 997 |
} |
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
998 |
} catch (PrivilegedActionException pae) { |
2 | 999 |
continue; |
1000 |
} catch (MalformedURLException e) { |
|
1001 |
continue; |
|
1002 |
} |
|
1003 |
||
1004 |
/* Note that the addition of the url to the list of visited |
|
1005 |
* jars incorporates a check for presence in the hashmap |
|
1006 |
*/ |
|
3436
c7d276696c5b
6826780: URLClassPath should use HashMap<String, XXX> instead of HashMap<URL, XXX>
chegar
parents:
3073
diff
changeset
|
1007 |
boolean visitedURL = !visited.add(URLUtil.urlNoFragString(url)); |
2 | 1008 |
if (!visitedURL) { |
1009 |
try { |
|
1010 |
newLoader.ensureOpen(); |
|
1011 |
} catch (IOException e) { |
|
10355
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
9823
diff
changeset
|
1012 |
throw new InternalError(e); |
2 | 1013 |
} |
1014 |
final JarEntry entry = newLoader.jar.getJarEntry(name); |
|
1015 |
if (entry != null) { |
|
1016 |
return newLoader.checkResource(name, check, entry); |
|
1017 |
} |
|
1018 |
||
1019 |
/* Verify that at least one other resource with the |
|
1020 |
* same package name as the lookedup resource is |
|
1021 |
* present in the new jar |
|
1022 |
*/ |
|
1023 |
if (!newLoader.validIndex(name)) { |
|
1024 |
/* the mapping is wrong */ |
|
36745
51effd3e92d0
8152190: Move sun.misc.JarIndex and InvalidJarIndexException to an internal package
chegar
parents:
36674
diff
changeset
|
1025 |
throw new InvalidJarIndexError("Invalid index"); |
2 | 1026 |
} |
1027 |
} |
|
1028 |
||
1029 |
/* If newLoader is the current loader or if it is a |
|
1030 |
* loader that has already been searched or if the new |
|
1031 |
* loader does not have an index then skip it |
|
1032 |
* and move on to the next loader. |
|
1033 |
*/ |
|
1034 |
if (visitedURL || newLoader == this || |
|
1035 |
newLoader.getIndex() == null) { |
|
1036 |
continue; |
|
1037 |
} |
|
1038 |
||
1039 |
/* Process the index of the new loader |
|
1040 |
*/ |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
1041 |
if ((res = newLoader.getResource(name, check, visited)) |
2 | 1042 |
!= null) { |
1043 |
return res; |
|
1044 |
} |
|
1045 |
} |
|
1046 |
// Get the list of jar files again as the list could have grown |
|
1047 |
// due to merging of index files. |
|
1048 |
jarFilesList = index.get(name); |
|
1049 |
||
1050 |
// If the count is unchanged, we are done. |
|
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47987
diff
changeset
|
1051 |
} while (count < jarFilesList.size()); |
2 | 1052 |
return null; |
1053 |
} |
|
1054 |
||
1055 |
||
1056 |
/* |
|
1057 |
* Returns the JAR file local class path, or null if none. |
|
1058 |
*/ |
|
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
1059 |
@Override |
2 | 1060 |
URL[] getClassPath() throws IOException { |
1061 |
if (index != null) { |
|
1062 |
return null; |
|
1063 |
} |
|
1064 |
||
1065 |
ensureOpen(); |
|
15682
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
11131
diff
changeset
|
1066 |
|
37785
daf6dcc73e9d
8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents:
37781
diff
changeset
|
1067 |
// Only get manifest when necessary |
daf6dcc73e9d
8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents:
37781
diff
changeset
|
1068 |
if (SharedSecrets.javaUtilJarAccess().jarFileHasClassPathAttribute(jar)) { |
2 | 1069 |
Manifest man = jar.getManifest(); |
1070 |
if (man != null) { |
|
1071 |
Attributes attr = man.getMainAttributes(); |
|
1072 |
if (attr != null) { |
|
1073 |
String value = attr.getValue(Name.CLASS_PATH); |
|
1074 |
if (value != null) { |
|
1075 |
return parseClassPath(csu, value); |
|
1076 |
} |
|
1077 |
} |
|
1078 |
} |
|
1079 |
} |
|
1080 |
return null; |
|
1081 |
} |
|
1082 |
||
1083 |
/* |
|
1084 |
* Parses value of the Class-Path manifest attribute and returns |
|
1085 |
* an array of URLs relative to the specified base URL. |
|
1086 |
*/ |
|
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
1087 |
private static URL[] parseClassPath(URL base, String value) |
2 | 1088 |
throws MalformedURLException |
1089 |
{ |
|
1090 |
StringTokenizer st = new StringTokenizer(value); |
|
1091 |
URL[] urls = new URL[st.countTokens()]; |
|
1092 |
int i = 0; |
|
1093 |
while (st.hasMoreTokens()) { |
|
1094 |
String path = st.nextToken(); |
|
52164 | 1095 |
URL url = DISABLE_CP_URL_CHECK ? new URL(base, path) : safeResolve(base, path); |
1096 |
if (url != null) { |
|
1097 |
urls[i] = url; |
|
1098 |
i++; |
|
1099 |
} |
|
1100 |
} |
|
1101 |
if (i == 0) { |
|
1102 |
urls = null; |
|
1103 |
} else if (i != urls.length) { |
|
1104 |
// Truncate nulls from end of array |
|
1105 |
urls = Arrays.copyOf(urls, i); |
|
2 | 1106 |
} |
1107 |
return urls; |
|
1108 |
} |
|
52164 | 1109 |
|
1110 |
/* |
|
1111 |
* Return a URL for the given path resolved against the base URL, or |
|
1112 |
* null if the resulting URL is invalid. |
|
1113 |
*/ |
|
1114 |
static URL safeResolve(URL base, String path) { |
|
1115 |
String child = path.replace(File.separatorChar, '/'); |
|
1116 |
try { |
|
1117 |
if (!URI.create(child).isAbsolute()) { |
|
1118 |
URL url = new URL(base, child); |
|
1119 |
if (base.getProtocol().equalsIgnoreCase("file")) { |
|
1120 |
return url; |
|
1121 |
} else { |
|
1122 |
String bp = base.getPath(); |
|
1123 |
String urlp = url.getPath(); |
|
1124 |
int pos = bp.lastIndexOf('/'); |
|
1125 |
if (pos == -1) { |
|
1126 |
pos = bp.length() - 1; |
|
1127 |
} |
|
1128 |
if (urlp.regionMatches(0, bp, 0, pos + 1) |
|
1129 |
&& urlp.indexOf("..", pos) == -1) { |
|
1130 |
return url; |
|
1131 |
} |
|
1132 |
} |
|
1133 |
} |
|
1134 |
} catch (MalformedURLException | IllegalArgumentException e) {} |
|
1135 |
if (DEBUG_CP_URL_CHECK) { |
|
1136 |
System.err.println("Class-Path entry: \"" + path + "\" ignored in JAR file " + base); |
|
1137 |
} |
|
1138 |
return null; |
|
1139 |
} |
|
2 | 1140 |
} |
1141 |
||
1142 |
/* |
|
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
1143 |
* Nested class used to represent a loader of classes and resources |
2 | 1144 |
* from a file URL that refers to a directory. |
1145 |
*/ |
|
1146 |
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
|
1147 |
/* Canonicalized File */ |
2 | 1148 |
private File dir; |
1149 |
||
1150 |
FileLoader(URL url) throws IOException { |
|
1151 |
super(url); |
|
1152 |
if (!"file".equals(url.getProtocol())) { |
|
1153 |
throw new IllegalArgumentException("url"); |
|
1154 |
} |
|
1155 |
String path = url.getFile().replace('/', File.separatorChar); |
|
1156 |
path = ParseUtil.decode(path); |
|
495
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1157 |
dir = (new File(path)).getCanonicalFile(); |
2 | 1158 |
} |
1159 |
||
1160 |
/* |
|
1161 |
* Returns the URL for a resource with the specified name |
|
1162 |
*/ |
|
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
1163 |
@Override |
2 | 1164 |
URL findResource(final String name, boolean check) { |
1165 |
Resource rsc = getResource(name, check); |
|
1166 |
if (rsc != null) { |
|
1167 |
return rsc.getURL(); |
|
1168 |
} |
|
1169 |
return null; |
|
1170 |
} |
|
1171 |
||
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43196
diff
changeset
|
1172 |
@Override |
2 | 1173 |
Resource getResource(final String name, boolean check) { |
1174 |
final URL url; |
|
1175 |
try { |
|
1176 |
URL normalizedBase = new URL(getBaseURL(), "."); |
|
1177 |
url = new URL(getBaseURL(), ParseUtil.encodePath(name, false)); |
|
1178 |
||
1179 |
if (url.getFile().startsWith(normalizedBase.getFile()) == false) { |
|
1180 |
// requested resource had ../..'s in path |
|
1181 |
return null; |
|
1182 |
} |
|
1183 |
||
1184 |
if (check) |
|
1185 |
URLClassPath.check(url); |
|
495
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1186 |
|
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1187 |
final File file; |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1188 |
if (name.indexOf("..") != -1) { |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1189 |
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
|
1190 |
.getCanonicalFile(); |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1191 |
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
|
1192 |
/* outside of base dir */ |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1193 |
return null; |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1194 |
} |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1195 |
} else { |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1196 |
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
|
1197 |
} |
d612e90c3ebc
6687919: REGRESSION : Classloader can handle any resource which is not included in classpath
chegar
parents:
51
diff
changeset
|
1198 |
|
2 | 1199 |
if (file.exists()) { |
1200 |
return new Resource() { |
|
1201 |
public String getName() { return name; }; |
|
1202 |
public URL getURL() { return url; }; |
|
1203 |
public URL getCodeSourceURL() { return getBaseURL(); }; |
|
1204 |
public InputStream getInputStream() throws IOException |
|
1205 |
{ return new FileInputStream(file); }; |
|
1206 |
public int getContentLength() throws IOException |
|
1207 |
{ return (int)file.length(); }; |
|
1208 |
}; |
|
1209 |
} |
|
1210 |
} catch (Exception e) { |
|
1211 |
return null; |
|
1212 |
} |
|
1213 |
return null; |
|
1214 |
} |
|
1215 |
} |
|
1216 |
} |