author | mchung |
Tue, 06 Nov 2018 10:01:16 -0800 | |
changeset 52427 | 3c6aa484536c |
parent 52159 | 42244a052fbb |
child 53660 | 929f0c7e019b |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
52159 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
36511 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact 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. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.internal.loader; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.io.FilePermission; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
32 |
import java.lang.module.ModuleDescriptor; |
36511 | 33 |
import java.lang.module.ModuleReference; |
34 |
import java.lang.module.ModuleReader; |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
35 |
import java.lang.ref.SoftReference; |
36511 | 36 |
import java.net.MalformedURLException; |
37 |
import java.net.URI; |
|
38 |
import java.net.URL; |
|
39 |
import java.nio.ByteBuffer; |
|
40 |
import java.security.AccessController; |
|
41 |
import java.security.CodeSigner; |
|
42 |
import java.security.CodeSource; |
|
43 |
import java.security.Permission; |
|
44 |
import java.security.PermissionCollection; |
|
45 |
import java.security.PrivilegedAction; |
|
46 |
import java.security.PrivilegedActionException; |
|
47 |
import java.security.PrivilegedExceptionAction; |
|
48 |
import java.security.SecureClassLoader; |
|
49 |
import java.util.ArrayList; |
|
50 |
import java.util.Collections; |
|
51 |
import java.util.Enumeration; |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
52 |
import java.util.Iterator; |
36511 | 53 |
import java.util.List; |
54 |
import java.util.Map; |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
55 |
import java.util.NoSuchElementException; |
36511 | 56 |
import java.util.Optional; |
57 |
import java.util.concurrent.ConcurrentHashMap; |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
58 |
import java.util.function.Function; |
36511 | 59 |
import java.util.jar.Attributes; |
60 |
import java.util.jar.Manifest; |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
37779
diff
changeset
|
61 |
import java.util.stream.Stream; |
36511 | 62 |
|
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
52159
diff
changeset
|
63 |
import jdk.internal.access.SharedSecrets; |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
64 |
import jdk.internal.misc.VM; |
36511 | 65 |
import jdk.internal.module.ModulePatcher.PatchedModuleReader; |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
66 |
import jdk.internal.module.Resources; |
36511 | 67 |
|
68 |
||
69 |
/** |
|
70 |
* The platform or application class loader. Resources loaded from modules |
|
71 |
* defined to the boot class loader are also loaded via an instance of this |
|
72 |
* ClassLoader type. |
|
73 |
* |
|
74 |
* <p> This ClassLoader supports loading of classes and resources from modules. |
|
75 |
* Modules are defined to the ClassLoader by invoking the {@link #loadModule} |
|
76 |
* method. Defining a module to this ClassLoader has the effect of making the |
|
77 |
* types in the module visible. </p> |
|
78 |
* |
|
79 |
* <p> This ClassLoader also supports loading of classes and resources from a |
|
80 |
* class path of URLs that are specified to the ClassLoader at construction |
|
81 |
* time. The class path may expand at runtime (the Class-Path attribute in JAR |
|
82 |
* files or via instrumentation agents). </p> |
|
83 |
* |
|
84 |
* <p> The delegation model used by this ClassLoader differs to the regular |
|
85 |
* delegation model. When requested to load a class then this ClassLoader first |
|
86 |
* maps the class name to its package name. If there is a module defined to a |
|
87 |
* BuiltinClassLoader containing this package then the class loader delegates |
|
88 |
* directly to that class loader. If there isn't a module containing the |
|
89 |
* package then it delegates the search to the parent class loader and if not |
|
90 |
* found in the parent then it searches the class path. The main difference |
|
91 |
* between this and the usual delegation model is that it allows the platform |
|
92 |
* class loader to delegate to the application class loader, important with |
|
93 |
* upgraded modules defined to the platform class loader. |
|
94 |
*/ |
|
95 |
||
96 |
public class BuiltinClassLoader |
|
97 |
extends SecureClassLoader |
|
98 |
{ |
|
99 |
static { |
|
100 |
if (!ClassLoader.registerAsParallelCapable()) |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
101 |
throw new InternalError("Unable to register as parallel capable"); |
36511 | 102 |
} |
103 |
||
104 |
// parent ClassLoader |
|
105 |
private final BuiltinClassLoader parent; |
|
106 |
||
48995
58bec53828ba
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader
martin
parents:
47216
diff
changeset
|
107 |
// the URL class path, or null if there is no class path |
36511 | 108 |
private final URLClassPath ucp; |
109 |
||
110 |
||
111 |
/** |
|
112 |
* A module defined/loaded by a built-in class loader. |
|
113 |
* |
|
114 |
* A LoadedModule encapsulates a ModuleReference along with its CodeSource |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36674
diff
changeset
|
115 |
* URL to avoid needing to create this URL when defining classes. |
36511 | 116 |
*/ |
117 |
private static class LoadedModule { |
|
118 |
private final BuiltinClassLoader loader; |
|
119 |
private final ModuleReference mref; |
|
120 |
private final URL codeSourceURL; // may be null |
|
121 |
||
122 |
LoadedModule(BuiltinClassLoader loader, ModuleReference mref) { |
|
123 |
URL url = null; |
|
124 |
if (mref.location().isPresent()) { |
|
125 |
try { |
|
126 |
url = mref.location().get().toURL(); |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
127 |
} catch (MalformedURLException | IllegalArgumentException e) { } |
36511 | 128 |
} |
129 |
this.loader = loader; |
|
130 |
this.mref = mref; |
|
131 |
this.codeSourceURL = url; |
|
132 |
} |
|
133 |
||
134 |
BuiltinClassLoader loader() { return loader; } |
|
135 |
ModuleReference mref() { return mref; } |
|
136 |
String name() { return mref.descriptor().name(); } |
|
137 |
URL codeSourceURL() { return codeSourceURL; } |
|
138 |
} |
|
139 |
||
140 |
||
141 |
// maps package name to loaded module for modules in the boot layer |
|
142 |
private static final Map<String, LoadedModule> packageToModule |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
143 |
= new ConcurrentHashMap<>(1024); |
36511 | 144 |
|
145 |
// maps a module name to a module reference |
|
146 |
private final Map<String, ModuleReference> nameToModule; |
|
147 |
||
148 |
// maps a module reference to a module reader |
|
149 |
private final Map<ModuleReference, ModuleReader> moduleToReader; |
|
150 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
151 |
// cache of resource name -> list of URLs. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
152 |
// used only for resources that are not in module packages |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
153 |
private volatile SoftReference<Map<String, List<URL>>> resourceCache; |
36511 | 154 |
|
155 |
/** |
|
156 |
* Create a new instance. |
|
157 |
*/ |
|
41911 | 158 |
BuiltinClassLoader(String name, BuiltinClassLoader parent, URLClassPath ucp) { |
36511 | 159 |
// ensure getParent() returns null when the parent is the boot loader |
41911 | 160 |
super(name, parent == null || parent == ClassLoaders.bootLoader() ? null : parent); |
36511 | 161 |
|
162 |
this.parent = parent; |
|
163 |
this.ucp = ucp; |
|
164 |
||
165 |
this.nameToModule = new ConcurrentHashMap<>(); |
|
166 |
this.moduleToReader = new ConcurrentHashMap<>(); |
|
167 |
} |
|
168 |
||
169 |
/** |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
170 |
* Returns {@code true} if there is a class path associated with this |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
171 |
* class loader. |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
172 |
*/ |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
173 |
boolean hasClassPath() { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
174 |
return ucp != null; |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
175 |
} |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
176 |
|
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
177 |
/** |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
178 |
* Register a module this class loader. This has the effect of making the |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
179 |
* types in the module visible. |
36511 | 180 |
*/ |
181 |
public void loadModule(ModuleReference mref) { |
|
182 |
String mn = mref.descriptor().name(); |
|
183 |
if (nameToModule.putIfAbsent(mn, mref) != null) { |
|
184 |
throw new InternalError(mn + " already defined to this loader"); |
|
185 |
} |
|
186 |
||
187 |
LoadedModule loadedModule = new LoadedModule(this, mref); |
|
188 |
for (String pn : mref.descriptor().packages()) { |
|
189 |
LoadedModule other = packageToModule.putIfAbsent(pn, loadedModule); |
|
190 |
if (other != null) { |
|
191 |
throw new InternalError(pn + " in modules " + mn + " and " |
|
192 |
+ other.mref().descriptor().name()); |
|
193 |
} |
|
194 |
} |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
195 |
|
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
196 |
// clear resources cache if VM is already initialized |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
197 |
if (VM.isModuleSystemInited() && resourceCache != null) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
198 |
resourceCache = null; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
199 |
} |
36511 | 200 |
} |
201 |
||
202 |
/** |
|
203 |
* Returns the {@code ModuleReference} for the named module defined to |
|
204 |
* this class loader; or {@code null} if not defined. |
|
205 |
* |
|
206 |
* @param name The name of the module to find |
|
207 |
*/ |
|
208 |
protected ModuleReference findModule(String name) { |
|
209 |
return nameToModule.get(name); |
|
210 |
} |
|
211 |
||
212 |
||
213 |
// -- finding resources |
|
214 |
||
215 |
/** |
|
216 |
* Returns a URL to a resource of the given name in a module defined to |
|
217 |
* this class loader. |
|
218 |
*/ |
|
219 |
@Override |
|
220 |
public URL findResource(String mn, String name) throws IOException { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
221 |
URL url = null; |
36511 | 222 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
223 |
if (mn != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
224 |
// find in module |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
225 |
ModuleReference mref = nameToModule.get(mn); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
226 |
if (mref != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
227 |
url = findResource(mref, name); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
228 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
229 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
230 |
// find on class path |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
231 |
url = findResourceOnClassPath(name); |
36511 | 232 |
} |
233 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
234 |
return checkURL(url); // check access before returning |
36511 | 235 |
} |
236 |
||
237 |
/** |
|
238 |
* Returns an input stream to a resource of the given name in a module |
|
239 |
* defined to this class loader. |
|
240 |
*/ |
|
241 |
public InputStream findResourceAsStream(String mn, String name) |
|
242 |
throws IOException |
|
243 |
{ |
|
244 |
// Need URL to resource when running with a security manager so that |
|
245 |
// the right permission check is done. |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
246 |
if (System.getSecurityManager() != null || mn == null) { |
36511 | 247 |
URL url = findResource(mn, name); |
248 |
return (url != null) ? url.openStream() : null; |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
249 |
} |
36511 | 250 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
251 |
// find in module defined to this loader, no security manager |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
252 |
ModuleReference mref = nameToModule.get(mn); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
253 |
if (mref != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
254 |
return moduleReaderFor(mref).open(name).orElse(null); |
36511 | 255 |
} else { |
256 |
return null; |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
261 |
* Finds a resource with the given name in the modules defined to this |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
262 |
* class loader or its class path. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
263 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
264 |
@Override |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
265 |
public URL findResource(String name) { |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
266 |
String pn = Resources.toPackageName(name); |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
267 |
LoadedModule module = packageToModule.get(pn); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
268 |
if (module != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
269 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
270 |
// resource is in a package of a module defined to this loader |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
271 |
if (module.loader() == this) { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
272 |
URL url; |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
273 |
try { |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
274 |
url = findResource(module.name(), name); // checks URL |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
275 |
} catch (IOException ioe) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
276 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
277 |
} |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
278 |
if (url != null |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
279 |
&& (name.endsWith(".class") |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
280 |
|| url.toString().endsWith("/") |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
281 |
|| isOpen(module.mref(), pn))) { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
282 |
return url; |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
283 |
} |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
284 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
285 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
286 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
287 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
288 |
// not in a module package but may be in module defined to this loader |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
289 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
290 |
List<URL> urls = findMiscResource(name); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
291 |
if (!urls.isEmpty()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
292 |
URL url = urls.get(0); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
293 |
if (url != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
294 |
return checkURL(url); // check access before returning |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
295 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
296 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
297 |
} catch (IOException ioe) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
298 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
299 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
300 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
301 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
302 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
303 |
// search class path |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
304 |
URL url = findResourceOnClassPath(name); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
305 |
return checkURL(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
306 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
307 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
308 |
/** |
36511 | 309 |
* Returns an enumeration of URL objects to all the resources with the |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
310 |
* given name in modules defined to this class loader or on the class |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
311 |
* path of this loader. |
36511 | 312 |
*/ |
313 |
@Override |
|
314 |
public Enumeration<URL> findResources(String name) throws IOException { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
315 |
List<URL> checked = new ArrayList<>(); // list of checked URLs |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
316 |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
317 |
String pn = Resources.toPackageName(name); |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
318 |
LoadedModule module = packageToModule.get(pn); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
319 |
if (module != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
320 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
321 |
// resource is in a package of a module defined to this loader |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
322 |
if (module.loader() == this) { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
323 |
URL url = findResource(module.name(), name); // checks URL |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
324 |
if (url != null |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
325 |
&& (name.endsWith(".class") |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
326 |
|| url.toString().endsWith("/") |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
327 |
|| isOpen(module.mref(), pn))) { |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
328 |
checked.add(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
329 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
330 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
331 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
332 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
333 |
// not in a package of a module defined to this loader |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
334 |
for (URL url : findMiscResource(name)) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
335 |
url = checkURL(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
336 |
if (url != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
337 |
checked.add(url); |
36511 | 338 |
} |
339 |
} |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
340 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
341 |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
342 |
// class path (not checked) |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
343 |
Enumeration<URL> e = findResourcesOnClassPath(name); |
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
344 |
|
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
345 |
// concat the checked URLs and the (not checked) class path |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
346 |
return new Enumeration<>() { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
347 |
final Iterator<URL> iterator = checked.iterator(); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
348 |
URL next; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
349 |
private boolean hasNext() { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
350 |
if (next != null) { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
351 |
return true; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
352 |
} else if (iterator.hasNext()) { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
353 |
next = iterator.next(); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
354 |
return true; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
355 |
} else { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
356 |
// need to check each URL |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
357 |
while (e.hasMoreElements() && next == null) { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
358 |
next = checkURL(e.nextElement()); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
359 |
} |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
360 |
return next != null; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
361 |
} |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
362 |
} |
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
363 |
@Override |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
364 |
public boolean hasMoreElements() { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
365 |
return hasNext(); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
366 |
} |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
367 |
@Override |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
368 |
public URL nextElement() { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
369 |
if (hasNext()) { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
370 |
URL result = next; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
371 |
next = null; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
372 |
return result; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
373 |
} else { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
374 |
throw new NoSuchElementException(); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
375 |
} |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
376 |
} |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
377 |
}; |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
378 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
379 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
380 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
381 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
382 |
* Returns the list of URLs to a "miscellaneous" resource in modules |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
383 |
* defined to this loader. A miscellaneous resource is not in a module |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
384 |
* package, e.g. META-INF/services/p.S. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
385 |
* |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
386 |
* The cache used by this method avoids repeated searching of all modules. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
387 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
388 |
private List<URL> findMiscResource(String name) throws IOException { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
389 |
SoftReference<Map<String, List<URL>>> ref = this.resourceCache; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
390 |
Map<String, List<URL>> map = (ref != null) ? ref.get() : null; |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
391 |
if (map == null) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
392 |
map = new ConcurrentHashMap<>(); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
393 |
this.resourceCache = new SoftReference<>(map); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
394 |
} else { |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
395 |
List<URL> urls = map.get(name); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
396 |
if (urls != null) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
397 |
return urls; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
398 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
399 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
400 |
// search all modules for the resource |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
401 |
List<URL> urls; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
402 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
403 |
urls = AccessController.doPrivileged( |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
404 |
new PrivilegedExceptionAction<>() { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
405 |
@Override |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
406 |
public List<URL> run() throws IOException { |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
407 |
List<URL> result = null; |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
408 |
for (ModuleReference mref : nameToModule.values()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
409 |
URI u = moduleReaderFor(mref).find(name).orElse(null); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
410 |
if (u != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
411 |
try { |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
412 |
if (result == null) |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
413 |
result = new ArrayList<>(); |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
414 |
result.add(u.toURL()); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
415 |
} catch (MalformedURLException | |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
416 |
IllegalArgumentException e) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
417 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
418 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
419 |
} |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
420 |
return (result != null) ? result : Collections.emptyList(); |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
421 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
422 |
}); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
423 |
} catch (PrivilegedActionException pae) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
424 |
throw (IOException) pae.getCause(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
425 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
426 |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
427 |
// only cache resources after VM is fully initialized |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
428 |
if (VM.isModuleSystemInited()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
429 |
map.putIfAbsent(name, urls); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
430 |
} |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44359
diff
changeset
|
431 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
432 |
return urls; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
433 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
434 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
435 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
436 |
* Returns the URL to a resource in a module or {@code null} if not found. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
437 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
438 |
private URL findResource(ModuleReference mref, String name) throws IOException { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
439 |
URI u; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
440 |
if (System.getSecurityManager() == null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
441 |
u = moduleReaderFor(mref).find(name).orElse(null); |
36511 | 442 |
} else { |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
443 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
444 |
u = AccessController.doPrivileged(new PrivilegedExceptionAction<> () { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
445 |
@Override |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
446 |
public URI run() throws IOException { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
447 |
return moduleReaderFor(mref).find(name).orElse(null); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
448 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
449 |
}); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
450 |
} catch (PrivilegedActionException pae) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
451 |
throw (IOException) pae.getCause(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
452 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
453 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
454 |
if (u != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
455 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
456 |
return u.toURL(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
457 |
} catch (MalformedURLException | IllegalArgumentException e) { } |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
458 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
459 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
460 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
461 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
462 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
463 |
* Returns the URL to a resource in a module. Returns {@code null} if not found |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
464 |
* or an I/O error occurs. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
465 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
466 |
private URL findResourceOrNull(ModuleReference mref, String name) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
467 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
468 |
return findResource(mref, name); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
469 |
} catch (IOException ignore) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
470 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
471 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
472 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
473 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
474 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
475 |
* Returns a URL to a resource on the class path. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
476 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
477 |
private URL findResourceOnClassPath(String name) { |
44003
dc26a57d2570
8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents:
43712
diff
changeset
|
478 |
if (hasClassPath()) { |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
479 |
if (System.getSecurityManager() == null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
480 |
return ucp.findResource(name, false); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
481 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
482 |
PrivilegedAction<URL> pa = () -> ucp.findResource(name, false); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
483 |
return AccessController.doPrivileged(pa); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
484 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
485 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
486 |
// no class path |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
487 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
488 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
489 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
490 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
491 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
492 |
* Returns the URLs of all resources of the given name on the class path. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
493 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
494 |
private Enumeration<URL> findResourcesOnClassPath(String name) { |
44003
dc26a57d2570
8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents:
43712
diff
changeset
|
495 |
if (hasClassPath()) { |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
496 |
if (System.getSecurityManager() == null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
497 |
return ucp.findResources(name, false); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
498 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
499 |
PrivilegedAction<Enumeration<URL>> pa; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
500 |
pa = () -> ucp.findResources(name, false); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
501 |
return AccessController.doPrivileged(pa); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
502 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
503 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
504 |
// no class path |
36511 | 505 |
return Collections.emptyEnumeration(); |
506 |
} |
|
507 |
} |
|
508 |
||
509 |
// -- finding/loading classes |
|
510 |
||
511 |
/** |
|
512 |
* Finds the class with the specified binary name. |
|
513 |
*/ |
|
514 |
@Override |
|
515 |
protected Class<?> findClass(String cn) throws ClassNotFoundException { |
|
516 |
// no class loading until VM is fully initialized |
|
517 |
if (!VM.isModuleSystemInited()) |
|
518 |
throw new ClassNotFoundException(cn); |
|
519 |
||
520 |
// find the candidate module for this class |
|
521 |
LoadedModule loadedModule = findLoadedModule(cn); |
|
522 |
||
523 |
Class<?> c = null; |
|
524 |
if (loadedModule != null) { |
|
525 |
||
526 |
// attempt to load class in module defined to this loader |
|
527 |
if (loadedModule.loader() == this) { |
|
528 |
c = findClassInModuleOrNull(loadedModule, cn); |
|
529 |
} |
|
530 |
||
531 |
} else { |
|
532 |
||
533 |
// search class path |
|
44003
dc26a57d2570
8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents:
43712
diff
changeset
|
534 |
if (hasClassPath()) { |
36511 | 535 |
c = findClassOnClassPathOrNull(cn); |
536 |
} |
|
537 |
||
538 |
} |
|
539 |
||
540 |
// not found |
|
541 |
if (c == null) |
|
542 |
throw new ClassNotFoundException(cn); |
|
543 |
||
544 |
return c; |
|
545 |
} |
|
546 |
||
547 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
548 |
* Finds the class with the specified binary name in a module. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
549 |
* This method returns {@code null} if the class cannot be found |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
550 |
* or not defined in the specified module. |
36511 | 551 |
*/ |
552 |
@Override |
|
553 |
protected Class<?> findClass(String mn, String cn) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
554 |
if (mn != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
555 |
// find the candidate module for this class |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
556 |
LoadedModule loadedModule = findLoadedModule(mn, cn); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
557 |
if (loadedModule == null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
558 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
559 |
} |
36511 | 560 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
561 |
// attempt to load class in module defined to this loader |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
562 |
assert loadedModule.loader() == this; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
563 |
return findClassInModuleOrNull(loadedModule, cn); |
36511 | 564 |
} |
565 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
566 |
// search class path |
44003
dc26a57d2570
8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents:
43712
diff
changeset
|
567 |
if (hasClassPath()) { |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
568 |
return findClassOnClassPathOrNull(cn); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
569 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
570 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
571 |
return null; |
36511 | 572 |
} |
573 |
||
574 |
/** |
|
575 |
* Loads the class with the specified binary name. |
|
576 |
*/ |
|
577 |
@Override |
|
578 |
protected Class<?> loadClass(String cn, boolean resolve) |
|
579 |
throws ClassNotFoundException |
|
580 |
{ |
|
581 |
Class<?> c = loadClassOrNull(cn, resolve); |
|
582 |
if (c == null) |
|
583 |
throw new ClassNotFoundException(cn); |
|
584 |
return c; |
|
585 |
} |
|
586 |
||
587 |
/** |
|
49528 | 588 |
* A variation of {@code loadClass} to load a class with the specified |
36511 | 589 |
* binary name. This method returns {@code null} when the class is not |
590 |
* found. |
|
591 |
*/ |
|
592 |
protected Class<?> loadClassOrNull(String cn, boolean resolve) { |
|
593 |
synchronized (getClassLoadingLock(cn)) { |
|
594 |
// check if already loaded |
|
595 |
Class<?> c = findLoadedClass(cn); |
|
596 |
||
597 |
if (c == null) { |
|
598 |
||
599 |
// find the candidate module for this class |
|
600 |
LoadedModule loadedModule = findLoadedModule(cn); |
|
601 |
if (loadedModule != null) { |
|
602 |
||
603 |
// package is in a module |
|
604 |
BuiltinClassLoader loader = loadedModule.loader(); |
|
605 |
if (loader == this) { |
|
606 |
if (VM.isModuleSystemInited()) { |
|
607 |
c = findClassInModuleOrNull(loadedModule, cn); |
|
608 |
} |
|
609 |
} else { |
|
610 |
// delegate to the other loader |
|
611 |
c = loader.loadClassOrNull(cn); |
|
612 |
} |
|
613 |
||
614 |
} else { |
|
615 |
||
616 |
// check parent |
|
617 |
if (parent != null) { |
|
618 |
c = parent.loadClassOrNull(cn); |
|
619 |
} |
|
620 |
||
621 |
// check class path |
|
44003
dc26a57d2570
8175385: ServiceLoader$LazyClassPathLookupIterator scans boot and platform modules for services
redestad
parents:
43712
diff
changeset
|
622 |
if (c == null && hasClassPath() && VM.isModuleSystemInited()) { |
36511 | 623 |
c = findClassOnClassPathOrNull(cn); |
624 |
} |
|
625 |
} |
|
626 |
||
627 |
} |
|
628 |
||
629 |
if (resolve && c != null) |
|
630 |
resolveClass(c); |
|
631 |
||
632 |
return c; |
|
633 |
} |
|
634 |
} |
|
635 |
||
636 |
/** |
|
49528 | 637 |
* A variation of {@code loadClass} to load a class with the specified |
36511 | 638 |
* binary name. This method returns {@code null} when the class is not |
639 |
* found. |
|
640 |
*/ |
|
49528 | 641 |
protected Class<?> loadClassOrNull(String cn) { |
36511 | 642 |
return loadClassOrNull(cn, false); |
643 |
} |
|
644 |
||
645 |
/** |
|
49528 | 646 |
* Finds the candidate loaded module for the given class name. |
36511 | 647 |
* Returns {@code null} if none of the modules defined to this |
648 |
* class loader contain the API package for the class. |
|
649 |
*/ |
|
650 |
private LoadedModule findLoadedModule(String cn) { |
|
651 |
int pos = cn.lastIndexOf('.'); |
|
652 |
if (pos < 0) |
|
653 |
return null; // unnamed package |
|
654 |
||
655 |
String pn = cn.substring(0, pos); |
|
656 |
return packageToModule.get(pn); |
|
657 |
} |
|
658 |
||
659 |
/** |
|
49528 | 660 |
* Finds the candidate loaded module for the given class name |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
661 |
* in the named module. Returns {@code null} if the named module |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
662 |
* is not defined to this class loader or does not contain |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
663 |
* the API package for the class. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
664 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
665 |
private LoadedModule findLoadedModule(String mn, String cn) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
666 |
LoadedModule loadedModule = findLoadedModule(cn); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
667 |
if (loadedModule != null && mn.equals(loadedModule.name())) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
668 |
return loadedModule; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
669 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
670 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
671 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
672 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
673 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
674 |
/** |
36511 | 675 |
* Finds the class with the specified binary name if in a module |
676 |
* defined to this ClassLoader. |
|
677 |
* |
|
678 |
* @return the resulting Class or {@code null} if not found |
|
679 |
*/ |
|
680 |
private Class<?> findClassInModuleOrNull(LoadedModule loadedModule, String cn) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
681 |
if (System.getSecurityManager() == null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
682 |
return defineClass(cn, loadedModule); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
683 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
684 |
PrivilegedAction<Class<?>> pa = () -> defineClass(cn, loadedModule); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
685 |
return AccessController.doPrivileged(pa); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
686 |
} |
36511 | 687 |
} |
688 |
||
689 |
/** |
|
690 |
* Finds the class with the specified binary name on the class path. |
|
691 |
* |
|
692 |
* @return the resulting Class or {@code null} if not found |
|
693 |
*/ |
|
694 |
private Class<?> findClassOnClassPathOrNull(String cn) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
695 |
String path = cn.replace('.', '/').concat(".class"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
696 |
if (System.getSecurityManager() == null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
697 |
Resource res = ucp.getResource(path, false); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
698 |
if (res != null) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
699 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
700 |
return defineClass(cn, res); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
701 |
} catch (IOException ioe) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
702 |
// TBD on how I/O errors should be propagated |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
703 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
704 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
705 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
706 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
707 |
// avoid use of lambda here |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
708 |
PrivilegedAction<Class<?>> pa = new PrivilegedAction<>() { |
36511 | 709 |
public Class<?> run() { |
710 |
Resource res = ucp.getResource(path, false); |
|
711 |
if (res != null) { |
|
712 |
try { |
|
713 |
return defineClass(cn, res); |
|
714 |
} catch (IOException ioe) { |
|
715 |
// TBD on how I/O errors should be propagated |
|
716 |
} |
|
717 |
} |
|
718 |
return null; |
|
719 |
} |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
720 |
}; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
721 |
return AccessController.doPrivileged(pa); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
722 |
} |
36511 | 723 |
} |
724 |
||
725 |
/** |
|
726 |
* Defines the given binary class name to the VM, loading the class |
|
727 |
* bytes from the given module. |
|
728 |
* |
|
729 |
* @return the resulting Class or {@code null} if an I/O error occurs |
|
730 |
*/ |
|
731 |
private Class<?> defineClass(String cn, LoadedModule loadedModule) { |
|
732 |
ModuleReference mref = loadedModule.mref(); |
|
733 |
ModuleReader reader = moduleReaderFor(mref); |
|
734 |
||
735 |
try { |
|
736 |
ByteBuffer bb = null; |
|
737 |
URL csURL = null; |
|
738 |
||
739 |
// locate class file, special handling for patched modules to |
|
740 |
// avoid locating the resource twice |
|
741 |
String rn = cn.replace('.', '/').concat(".class"); |
|
742 |
if (reader instanceof PatchedModuleReader) { |
|
743 |
Resource r = ((PatchedModuleReader)reader).findResource(rn); |
|
744 |
if (r != null) { |
|
745 |
bb = r.getByteBuffer(); |
|
746 |
csURL = r.getCodeSourceURL(); |
|
747 |
} |
|
748 |
} else { |
|
749 |
bb = reader.read(rn).orElse(null); |
|
750 |
csURL = loadedModule.codeSourceURL(); |
|
751 |
} |
|
752 |
||
753 |
if (bb == null) { |
|
754 |
// class not found |
|
755 |
return null; |
|
756 |
} |
|
757 |
||
758 |
CodeSource cs = new CodeSource(csURL, (CodeSigner[]) null); |
|
759 |
try { |
|
760 |
// define class to VM |
|
761 |
return defineClass(cn, bb, cs); |
|
762 |
||
763 |
} finally { |
|
764 |
reader.release(bb); |
|
765 |
} |
|
766 |
||
767 |
} catch (IOException ioe) { |
|
768 |
// TBD on how I/O errors should be propagated |
|
769 |
return null; |
|
770 |
} |
|
771 |
} |
|
772 |
||
773 |
/** |
|
774 |
* Defines the given binary class name to the VM, loading the class |
|
775 |
* bytes via the given Resource object. |
|
776 |
* |
|
777 |
* @return the resulting Class |
|
778 |
* @throws IOException if reading the resource fails |
|
779 |
* @throws SecurityException if there is a sealing violation (JAR spec) |
|
780 |
*/ |
|
781 |
private Class<?> defineClass(String cn, Resource res) throws IOException { |
|
782 |
URL url = res.getCodeSourceURL(); |
|
783 |
||
784 |
// if class is in a named package then ensure that the package is defined |
|
785 |
int pos = cn.lastIndexOf('.'); |
|
786 |
if (pos != -1) { |
|
787 |
String pn = cn.substring(0, pos); |
|
788 |
Manifest man = res.getManifest(); |
|
789 |
defineOrCheckPackage(pn, man, url); |
|
790 |
} |
|
791 |
||
792 |
// defines the class to the runtime |
|
793 |
ByteBuffer bb = res.getByteBuffer(); |
|
794 |
if (bb != null) { |
|
795 |
CodeSigner[] signers = res.getCodeSigners(); |
|
796 |
CodeSource cs = new CodeSource(url, signers); |
|
797 |
return defineClass(cn, bb, cs); |
|
798 |
} else { |
|
799 |
byte[] b = res.getBytes(); |
|
800 |
CodeSigner[] signers = res.getCodeSigners(); |
|
801 |
CodeSource cs = new CodeSource(url, signers); |
|
802 |
return defineClass(cn, b, 0, b.length, cs); |
|
803 |
} |
|
804 |
} |
|
805 |
||
806 |
||
807 |
// -- packages |
|
808 |
||
809 |
/** |
|
810 |
* Defines a package in this ClassLoader. If the package is already defined |
|
811 |
* then its sealing needs to be checked if sealed by the legacy sealing |
|
812 |
* mechanism. |
|
813 |
* |
|
814 |
* @throws SecurityException if there is a sealing violation (JAR spec) |
|
815 |
*/ |
|
816 |
protected Package defineOrCheckPackage(String pn, Manifest man, URL url) { |
|
817 |
Package pkg = getAndVerifyPackage(pn, man, url); |
|
818 |
if (pkg == null) { |
|
819 |
try { |
|
820 |
if (man != null) { |
|
821 |
pkg = definePackage(pn, man, url); |
|
822 |
} else { |
|
823 |
pkg = definePackage(pn, null, null, null, null, null, null, null); |
|
824 |
} |
|
825 |
} catch (IllegalArgumentException iae) { |
|
826 |
// defined by another thread so need to re-verify |
|
827 |
pkg = getAndVerifyPackage(pn, man, url); |
|
828 |
if (pkg == null) |
|
829 |
throw new InternalError("Cannot find package: " + pn); |
|
830 |
} |
|
831 |
} |
|
832 |
return pkg; |
|
833 |
} |
|
834 |
||
835 |
/** |
|
49528 | 836 |
* Gets the Package with the specified package name. If defined |
837 |
* then verifies it against the manifest and code source. |
|
36511 | 838 |
* |
839 |
* @throws SecurityException if there is a sealing violation (JAR spec) |
|
840 |
*/ |
|
841 |
private Package getAndVerifyPackage(String pn, Manifest man, URL url) { |
|
842 |
Package pkg = getDefinedPackage(pn); |
|
843 |
if (pkg != null) { |
|
844 |
if (pkg.isSealed()) { |
|
845 |
if (!pkg.isSealed(url)) { |
|
846 |
throw new SecurityException( |
|
847 |
"sealing violation: package " + pn + " is sealed"); |
|
848 |
} |
|
849 |
} else { |
|
850 |
// can't seal package if already defined without sealing |
|
851 |
if ((man != null) && isSealed(pn, man)) { |
|
852 |
throw new SecurityException( |
|
853 |
"sealing violation: can't seal package " + pn + |
|
854 |
": already defined"); |
|
855 |
} |
|
856 |
} |
|
857 |
} |
|
858 |
return pkg; |
|
859 |
} |
|
860 |
||
861 |
/** |
|
862 |
* Defines a new package in this ClassLoader. The attributes in the specified |
|
49528 | 863 |
* Manifest are used to get the package version and sealing information. |
36511 | 864 |
* |
865 |
* @throws IllegalArgumentException if the package name duplicates an |
|
52159 | 866 |
* existing package either in this class loader or one of its ancestors |
867 |
* @throws SecurityException if the package name is untrusted in the manifest |
|
36511 | 868 |
*/ |
869 |
private Package definePackage(String pn, Manifest man, URL url) { |
|
870 |
String specTitle = null; |
|
871 |
String specVersion = null; |
|
872 |
String specVendor = null; |
|
873 |
String implTitle = null; |
|
874 |
String implVersion = null; |
|
875 |
String implVendor = null; |
|
876 |
String sealed = null; |
|
877 |
URL sealBase = null; |
|
878 |
||
879 |
if (man != null) { |
|
52159 | 880 |
Attributes attr = SharedSecrets.javaUtilJarAccess() |
881 |
.getTrustedAttributes(man, pn.replace('.', '/').concat("/")); |
|
36511 | 882 |
if (attr != null) { |
883 |
specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE); |
|
884 |
specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION); |
|
885 |
specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR); |
|
886 |
implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE); |
|
887 |
implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION); |
|
888 |
implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR); |
|
889 |
sealed = attr.getValue(Attributes.Name.SEALED); |
|
890 |
} |
|
891 |
||
892 |
attr = man.getMainAttributes(); |
|
893 |
if (attr != null) { |
|
894 |
if (specTitle == null) |
|
895 |
specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE); |
|
896 |
if (specVersion == null) |
|
897 |
specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION); |
|
898 |
if (specVendor == null) |
|
899 |
specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR); |
|
900 |
if (implTitle == null) |
|
901 |
implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE); |
|
902 |
if (implVersion == null) |
|
903 |
implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION); |
|
904 |
if (implVendor == null) |
|
905 |
implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR); |
|
906 |
if (sealed == null) |
|
907 |
sealed = attr.getValue(Attributes.Name.SEALED); |
|
908 |
} |
|
909 |
||
910 |
// package is sealed |
|
911 |
if ("true".equalsIgnoreCase(sealed)) |
|
912 |
sealBase = url; |
|
913 |
} |
|
914 |
return definePackage(pn, |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
915 |
specTitle, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
916 |
specVersion, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
917 |
specVendor, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
918 |
implTitle, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
919 |
implVersion, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
920 |
implVendor, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
921 |
sealBase); |
36511 | 922 |
} |
923 |
||
924 |
/** |
|
925 |
* Returns {@code true} if the specified package name is sealed according to |
|
926 |
* the given manifest. |
|
52159 | 927 |
* |
928 |
* @throws SecurityException if the package name is untrusted in the manifest |
|
36511 | 929 |
*/ |
930 |
private boolean isSealed(String pn, Manifest man) { |
|
52159 | 931 |
Attributes attr = SharedSecrets.javaUtilJarAccess() |
932 |
.getTrustedAttributes(man, pn.replace('.', '/').concat("/")); |
|
36511 | 933 |
String sealed = null; |
934 |
if (attr != null) |
|
935 |
sealed = attr.getValue(Attributes.Name.SEALED); |
|
936 |
if (sealed == null && (attr = man.getMainAttributes()) != null) |
|
937 |
sealed = attr.getValue(Attributes.Name.SEALED); |
|
938 |
return "true".equalsIgnoreCase(sealed); |
|
939 |
} |
|
940 |
||
941 |
// -- permissions |
|
942 |
||
943 |
/** |
|
944 |
* Returns the permissions for the given CodeSource. |
|
945 |
*/ |
|
946 |
@Override |
|
947 |
protected PermissionCollection getPermissions(CodeSource cs) { |
|
948 |
PermissionCollection perms = super.getPermissions(cs); |
|
949 |
||
950 |
// add the permission to access the resource |
|
951 |
URL url = cs.getLocation(); |
|
952 |
if (url == null) |
|
953 |
return perms; |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
954 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
955 |
// avoid opening connection when URL is to resource in run-time image |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
956 |
if (url.getProtocol().equals("jrt")) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
957 |
perms.add(new RuntimePermission("accessSystemModules")); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
958 |
return perms; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
959 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
960 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
961 |
// open connection to determine the permission needed |
36511 | 962 |
try { |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
963 |
Permission p = url.openConnection().getPermission(); |
36511 | 964 |
if (p != null) { |
965 |
// for directories then need recursive access |
|
966 |
if (p instanceof FilePermission) { |
|
967 |
String path = p.getName(); |
|
968 |
if (path.endsWith(File.separator)) { |
|
969 |
path += "-"; |
|
970 |
p = new FilePermission(path, "read"); |
|
971 |
} |
|
972 |
} |
|
973 |
perms.add(p); |
|
974 |
} |
|
975 |
} catch (IOException ioe) { } |
|
976 |
||
977 |
return perms; |
|
978 |
} |
|
979 |
||
980 |
||
981 |
// -- miscellaneous supporting methods |
|
982 |
||
983 |
/** |
|
49528 | 984 |
* Returns the ModuleReader for the given module, creating it if needed. |
36511 | 985 |
*/ |
986 |
private ModuleReader moduleReaderFor(ModuleReference mref) { |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
987 |
ModuleReader reader = moduleToReader.get(mref); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
988 |
if (reader == null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
989 |
// avoid method reference during startup |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
990 |
Function<ModuleReference, ModuleReader> create = new Function<>() { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
991 |
public ModuleReader apply(ModuleReference moduleReference) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
992 |
try { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
993 |
return mref.open(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
994 |
} catch (IOException e) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
995 |
// Return a null module reader to avoid a future class |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
996 |
// load attempting to open the module again. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
997 |
return new NullModuleReader(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
998 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
999 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
1000 |
}; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
1001 |
reader = moduleToReader.computeIfAbsent(mref, create); |
36511 | 1002 |
} |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45652
diff
changeset
|
1003 |
return reader; |
36511 | 1004 |
} |
1005 |
||
1006 |
/** |
|
1007 |
* A ModuleReader that doesn't read any resources. |
|
1008 |
*/ |
|
1009 |
private static class NullModuleReader implements ModuleReader { |
|
1010 |
@Override |
|
1011 |
public Optional<URI> find(String name) { |
|
1012 |
return Optional.empty(); |
|
1013 |
} |
|
1014 |
@Override |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
37779
diff
changeset
|
1015 |
public Stream<String> list() { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
37779
diff
changeset
|
1016 |
return Stream.empty(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
37779
diff
changeset
|
1017 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
37779
diff
changeset
|
1018 |
@Override |
36511 | 1019 |
public void close() { |
1020 |
throw new InternalError("Should not get here"); |
|
1021 |
} |
|
1022 |
}; |
|
1023 |
||
1024 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1025 |
* Returns true if the given module opens the given package |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1026 |
* unconditionally. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1027 |
* |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1028 |
* @implNote This method currently iterates over each of the open |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1029 |
* packages. This will be replaced once the ModuleDescriptor.Opens |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1030 |
* API is updated. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1031 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1032 |
private boolean isOpen(ModuleReference mref, String pn) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1033 |
ModuleDescriptor descriptor = mref.descriptor(); |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
1034 |
if (descriptor.isOpen() || descriptor.isAutomatic()) |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1035 |
return true; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1036 |
for (ModuleDescriptor.Opens opens : descriptor.opens()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1037 |
String source = opens.source(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1038 |
if (!opens.isQualified() && source.equals(pn)) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1039 |
return true; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1040 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1041 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1042 |
return false; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1043 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1044 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41911
diff
changeset
|
1045 |
/** |
36511 | 1046 |
* Checks access to the given URL. We use URLClassPath for consistent |
1047 |
* checking with java.net.URLClassLoader. |
|
1048 |
*/ |
|
1049 |
private static URL checkURL(URL url) { |
|
1050 |
return URLClassPath.checkURL(url); |
|
1051 |
} |
|
1052 |
} |