author | igerasim |
Mon, 20 Nov 2017 18:46:52 -0800 | |
changeset 47866 | 39db80b32b69 |
parent 47216 | 71c04702a3d5 |
child 48995 | 58bec53828ba |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37605
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, 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 |
package jdk.internal.loader; |
|
26 |
||
27 |
import java.io.IOException; |
|
28 |
import java.io.InputStream; |
|
29 |
import java.lang.module.ModuleReference; |
|
30 |
import java.net.MalformedURLException; |
|
37605
6f6cd967ec11
8154837: Class::getPackage with exploded modules when classes in modules defined to the boot loader
mchung
parents:
36972
diff
changeset
|
31 |
import java.net.URI; |
36511 | 32 |
import java.net.URL; |
33 |
import java.nio.file.Files; |
|
34 |
import java.nio.file.Path; |
|
35 |
import java.nio.file.Paths; |
|
36 |
import java.security.AccessController; |
|
37 |
import java.security.PrivilegedAction; |
|
38 |
import java.util.Arrays; |
|
39 |
import java.util.Enumeration; |
|
40 |
import java.util.Optional; |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36511
diff
changeset
|
41 |
import java.util.concurrent.ConcurrentHashMap; |
36511 | 42 |
import java.util.jar.JarInputStream; |
43 |
import java.util.jar.Manifest; |
|
44 |
import java.util.stream.Stream; |
|
45 |
||
46 |
import jdk.internal.misc.JavaLangAccess; |
|
47 |
import jdk.internal.misc.SharedSecrets; |
|
48 |
import jdk.internal.module.ServicesCatalog; |
|
49 |
||
50 |
/** |
|
51 |
* Find resources and packages in modules defined to the boot class loader or |
|
52 |
* resources and packages on the "boot class path" specified via -Xbootclasspath/a. |
|
53 |
*/ |
|
54 |
||
55 |
public class BootLoader { |
|
56 |
private BootLoader() { } |
|
57 |
||
58 |
// The unnamed module for the boot loader |
|
59 |
private static final Module UNNAMED_MODULE; |
|
60 |
private static final String JAVA_HOME = System.getProperty("java.home"); |
|
61 |
||
62 |
static { |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
63 |
UNNAMED_MODULE = SharedSecrets.getJavaLangAccess().defineUnnamedModule(null); |
36511 | 64 |
setBootLoaderUnnamedModule0(UNNAMED_MODULE); |
65 |
} |
|
66 |
||
67 |
// ServiceCatalog for the boot class loader |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37605
diff
changeset
|
68 |
private static final ServicesCatalog SERVICES_CATALOG = ServicesCatalog.create(); |
36511 | 69 |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36511
diff
changeset
|
70 |
// ClassLoaderValue map for boot class loader |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
71 |
private static final ConcurrentHashMap<?, ?> CLASS_LOADER_VALUE_MAP |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
72 |
= new ConcurrentHashMap<>(); |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36511
diff
changeset
|
73 |
|
36511 | 74 |
/** |
75 |
* Returns the unnamed module for the boot loader. |
|
76 |
*/ |
|
77 |
public static Module getUnnamedModule() { |
|
78 |
return UNNAMED_MODULE; |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Returns the ServiceCatalog for modules defined to the boot class loader. |
|
83 |
*/ |
|
84 |
public static ServicesCatalog getServicesCatalog() { |
|
85 |
return SERVICES_CATALOG; |
|
86 |
} |
|
87 |
||
88 |
/** |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36511
diff
changeset
|
89 |
* Returns the ClassLoaderValue map for the boot class loader. |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36511
diff
changeset
|
90 |
*/ |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36511
diff
changeset
|
91 |
public static ConcurrentHashMap<?, ?> getClassLoaderValueMap() { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36511
diff
changeset
|
92 |
return CLASS_LOADER_VALUE_MAP; |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36511
diff
changeset
|
93 |
} |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36511
diff
changeset
|
94 |
|
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36511
diff
changeset
|
95 |
/** |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
96 |
* Returns {@code true} if there is a class path associated with the |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
97 |
* BootLoader. |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
98 |
*/ |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
99 |
public static boolean hasClassPath() { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
100 |
return ClassLoaders.bootLoader().hasClassPath(); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
101 |
} |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
102 |
|
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
44003
diff
changeset
|
103 |
/** |
36511 | 104 |
* Register a module with this class loader so that its classes (and |
105 |
* resources) become visible via this class loader. |
|
106 |
*/ |
|
107 |
public static void loadModule(ModuleReference mref) { |
|
108 |
ClassLoaders.bootLoader().loadModule(mref); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Loads the Class object with the given name defined to the boot loader. |
|
113 |
*/ |
|
114 |
public static Class<?> loadClassOrNull(String name) { |
|
115 |
return ClassLoaders.bootLoader().loadClassOrNull(name); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
119 |
* Loads the Class object with the given name in the given module |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
120 |
* defined to the boot loader. Returns {@code null} if not found. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
121 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
122 |
public static Class<?> loadClass(Module module, String name) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
123 |
Class<?> c = loadClassOrNull(name); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
124 |
if (c != null && c.getModule() == module) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
125 |
return c; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
126 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
127 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
128 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
129 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
130 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
131 |
/** |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
132 |
* Returns a URL to a resource in a module defined to the boot loader. |
36511 | 133 |
*/ |
134 |
public static URL findResource(String mn, String name) throws IOException { |
|
135 |
return ClassLoaders.bootLoader().findResource(mn, name); |
|
136 |
} |
|
137 |
||
138 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
139 |
* Returns an input stream to a resource in a module defined to the |
36511 | 140 |
* boot loader. |
141 |
*/ |
|
142 |
public static InputStream findResourceAsStream(String mn, String name) |
|
143 |
throws IOException |
|
144 |
{ |
|
145 |
return ClassLoaders.bootLoader().findResourceAsStream(mn, name); |
|
146 |
} |
|
147 |
||
148 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
149 |
* Returns the URL to the given resource in any of the modules |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
150 |
* defined to the boot loader and the boot class path. |
36511 | 151 |
*/ |
152 |
public static URL findResource(String name) { |
|
153 |
return ClassLoaders.bootLoader().findResource(name); |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Returns an Iterator to iterate over the resources of the given name |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37779
diff
changeset
|
158 |
* in any of the modules defined to the boot loader. |
36511 | 159 |
*/ |
160 |
public static Enumeration<URL> findResources(String name) throws IOException { |
|
161 |
return ClassLoaders.bootLoader().findResources(name); |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Define a package for the given class to the boot loader, if not already |
|
166 |
* defined. |
|
167 |
*/ |
|
168 |
public static Package definePackage(Class<?> c) { |
|
169 |
return getDefinedPackage(c.getPackageName()); |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Returns the Package of the given name defined to the boot loader or null |
|
174 |
* if the package has not been defined. |
|
175 |
*/ |
|
176 |
public static Package getDefinedPackage(String pn) { |
|
177 |
Package pkg = ClassLoaders.bootLoader().getDefinedPackage(pn); |
|
178 |
if (pkg == null) { |
|
179 |
String location = getSystemPackageLocation(pn.replace('.', '/')); |
|
180 |
if (location != null) { |
|
181 |
pkg = PackageHelper.definePackage(pn.intern(), location); |
|
182 |
} |
|
183 |
} |
|
184 |
return pkg; |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Returns a stream of the packages defined to the boot loader. |
|
189 |
*/ |
|
190 |
public static Stream<Package> packages() { |
|
191 |
return Arrays.stream(getSystemPackageNames()) |
|
192 |
.map(name -> getDefinedPackage(name.replace('/', '.'))); |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* Helper class to define {@code Package} objects for packages in modules |
|
197 |
* defined to the boot loader. |
|
198 |
*/ |
|
199 |
static class PackageHelper { |
|
200 |
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess(); |
|
201 |
||
202 |
/** |
|
203 |
* Define the {@code Package} with the given name. The specified |
|
37605
6f6cd967ec11
8154837: Class::getPackage with exploded modules when classes in modules defined to the boot loader
mchung
parents:
36972
diff
changeset
|
204 |
* location is a jrt URL to a named module in the run-time image, |
6f6cd967ec11
8154837: Class::getPackage with exploded modules when classes in modules defined to the boot loader
mchung
parents:
36972
diff
changeset
|
205 |
* a file URL to a module in an exploded run-time image, or a file |
6f6cd967ec11
8154837: Class::getPackage with exploded modules when classes in modules defined to the boot loader
mchung
parents:
36972
diff
changeset
|
206 |
* path to an entry on the boot class path (java agent Boot-Class-Path |
36511 | 207 |
* or -Xbootclasspath/a. |
208 |
* |
|
209 |
* <p> If the given location is a JAR file containing a manifest, |
|
210 |
* the defined Package contains the versioning information from |
|
211 |
* the manifest, if present. |
|
212 |
* |
|
213 |
* @param name package name |
|
37605
6f6cd967ec11
8154837: Class::getPackage with exploded modules when classes in modules defined to the boot loader
mchung
parents:
36972
diff
changeset
|
214 |
* @param location location where the package is (jrt URL or file URL |
6f6cd967ec11
8154837: Class::getPackage with exploded modules when classes in modules defined to the boot loader
mchung
parents:
36972
diff
changeset
|
215 |
* for a named module in the run-time or exploded image; |
6f6cd967ec11
8154837: Class::getPackage with exploded modules when classes in modules defined to the boot loader
mchung
parents:
36972
diff
changeset
|
216 |
* a file path for a package from -Xbootclasspath/a) |
36511 | 217 |
*/ |
218 |
static Package definePackage(String name, String location) { |
|
219 |
Module module = findModule(location); |
|
220 |
if (module != null) { |
|
221 |
// named module from runtime image or exploded module |
|
222 |
if (name.isEmpty()) |
|
223 |
throw new InternalError("empty package in " + location); |
|
224 |
return JLA.definePackage(ClassLoaders.bootLoader(), name, module); |
|
225 |
} |
|
226 |
||
227 |
// package in unnamed module (-Xbootclasspath/a) |
|
228 |
URL url = toFileURL(location); |
|
229 |
Manifest man = url != null ? getManifest(location) : null; |
|
230 |
||
231 |
return ClassLoaders.bootLoader().defineOrCheckPackage(name, man, url); |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* Finds the module at the given location defined to the boot loader. |
|
236 |
* The module is either in runtime image or exploded image. |
|
237 |
* Otherwise this method returns null. |
|
238 |
*/ |
|
239 |
private static Module findModule(String location) { |
|
240 |
String mn = null; |
|
241 |
if (location.startsWith("jrt:/")) { |
|
242 |
// named module in runtime image ("jrt:/".length() == 5) |
|
243 |
mn = location.substring(5, location.length()); |
|
37605
6f6cd967ec11
8154837: Class::getPackage with exploded modules when classes in modules defined to the boot loader
mchung
parents:
36972
diff
changeset
|
244 |
} else if (location.startsWith("file:/")) { |
36511 | 245 |
// named module in exploded image |
37605
6f6cd967ec11
8154837: Class::getPackage with exploded modules when classes in modules defined to the boot loader
mchung
parents:
36972
diff
changeset
|
246 |
Path path = Paths.get(URI.create(location)); |
36511 | 247 |
Path modulesDir = Paths.get(JAVA_HOME, "modules"); |
248 |
if (path.startsWith(modulesDir)) { |
|
249 |
mn = path.getFileName().toString(); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
if (mn != null) { |
|
254 |
// named module from runtime image or exploded module |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
255 |
Optional<Module> om = ModuleLayer.boot().findModule(mn); |
36511 | 256 |
if (!om.isPresent()) |
257 |
throw new InternalError(mn + " not in boot layer"); |
|
258 |
return om.get(); |
|
259 |
} |
|
260 |
||
261 |
return null; |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* Returns URL if the given location is a regular file path. |
|
266 |
*/ |
|
267 |
private static URL toFileURL(String location) { |
|
268 |
return AccessController.doPrivileged(new PrivilegedAction<>() { |
|
269 |
public URL run() { |
|
270 |
Path path = Paths.get(location); |
|
271 |
if (Files.isRegularFile(path)) { |
|
272 |
try { |
|
273 |
return path.toUri().toURL(); |
|
274 |
} catch (MalformedURLException e) {} |
|
275 |
} |
|
276 |
return null; |
|
277 |
} |
|
278 |
}); |
|
279 |
} |
|
280 |
||
281 |
/** |
|
282 |
* Returns the Manifest if the given location is a JAR file |
|
283 |
* containing a manifest. |
|
284 |
*/ |
|
285 |
private static Manifest getManifest(String location) { |
|
286 |
return AccessController.doPrivileged(new PrivilegedAction<>() { |
|
287 |
public Manifest run() { |
|
288 |
Path jar = Paths.get(location); |
|
289 |
try (InputStream in = Files.newInputStream(jar); |
|
290 |
JarInputStream jis = new JarInputStream(in, false)) { |
|
291 |
return jis.getManifest(); |
|
292 |
} catch (IOException e) { |
|
293 |
return null; |
|
294 |
} |
|
295 |
} |
|
296 |
}); |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* Returns an array of the binary name of the packages defined by |
|
302 |
* the boot loader, in VM internal form (forward slashes instead of dot). |
|
303 |
*/ |
|
304 |
private static native String[] getSystemPackageNames(); |
|
305 |
||
306 |
/** |
|
307 |
* Returns the location of the package of the given name, if |
|
308 |
* defined by the boot loader; otherwise {@code null} is returned. |
|
309 |
* |
|
310 |
* The location may be a module from the runtime image or exploded image, |
|
311 |
* or from the boot class append path (i.e. -Xbootclasspath/a or |
|
312 |
* BOOT-CLASS-PATH attribute specified in java agent). |
|
313 |
*/ |
|
314 |
private static native String getSystemPackageLocation(String name); |
|
315 |
private static native void setBootLoaderUnnamedModule0(Module module); |
|
316 |
} |