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;
31 import java.net.URI;
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;
41 import java.util.concurrent.ConcurrentHashMap;
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 {
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());
244 } else if (location.startsWith("file:/")) {
245 // named module in exploded image
246 Path path = Paths.get(URI.create(location));
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
255 Optional<Module> om = ModuleLayer.boot().findModule(mn);
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.
|
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;
31 import java.net.URI;
32 import java.net.URL;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.security.AccessController;
36 import java.security.PrivilegedAction;
37 import java.util.Arrays;
38 import java.util.Enumeration;
39 import java.util.Optional;
40 import java.util.concurrent.ConcurrentHashMap;
41 import java.util.jar.JarInputStream;
42 import java.util.jar.Manifest;
43 import java.util.stream.Stream;
44
45 import jdk.internal.misc.JavaLangAccess;
46 import jdk.internal.misc.SharedSecrets;
47 import jdk.internal.module.ServicesCatalog;
48
49 /**
50 * Find resources and packages in modules defined to the boot class loader or
51 * resources and packages on the "boot class path" specified via -Xbootclasspath/a.
52 */
53
54 public class BootLoader {
225
226 // package in unnamed module (-Xbootclasspath/a)
227 URL url = toFileURL(location);
228 Manifest man = url != null ? getManifest(location) : null;
229
230 return ClassLoaders.bootLoader().defineOrCheckPackage(name, man, url);
231 }
232
233 /**
234 * Finds the module at the given location defined to the boot loader.
235 * The module is either in runtime image or exploded image.
236 * Otherwise this method returns null.
237 */
238 private static Module findModule(String location) {
239 String mn = null;
240 if (location.startsWith("jrt:/")) {
241 // named module in runtime image ("jrt:/".length() == 5)
242 mn = location.substring(5, location.length());
243 } else if (location.startsWith("file:/")) {
244 // named module in exploded image
245 Path path = Path.get(URI.create(location));
246 Path modulesDir = Path.get(JAVA_HOME, "modules");
247 if (path.startsWith(modulesDir)) {
248 mn = path.getFileName().toString();
249 }
250 }
251
252 if (mn != null) {
253 // named module from runtime image or exploded module
254 Optional<Module> om = ModuleLayer.boot().findModule(mn);
255 if (!om.isPresent())
256 throw new InternalError(mn + " not in boot layer");
257 return om.get();
258 }
259
260 return null;
261 }
262
263 /**
264 * Returns URL if the given location is a regular file path.
265 */
266 private static URL toFileURL(String location) {
267 return AccessController.doPrivileged(new PrivilegedAction<>() {
268 public URL run() {
269 Path path = Path.get(location);
270 if (Files.isRegularFile(path)) {
271 try {
272 return path.toUri().toURL();
273 } catch (MalformedURLException e) {}
274 }
275 return null;
276 }
277 });
278 }
279
280 /**
281 * Returns the Manifest if the given location is a JAR file
282 * containing a manifest.
283 */
284 private static Manifest getManifest(String location) {
285 return AccessController.doPrivileged(new PrivilegedAction<>() {
286 public Manifest run() {
287 Path jar = Path.get(location);
288 try (InputStream in = Files.newInputStream(jar);
289 JarInputStream jis = new JarInputStream(in, false)) {
290 return jis.getManifest();
291 } catch (IOException e) {
292 return null;
293 }
294 }
295 });
296 }
297 }
298
299 /**
300 * Returns an array of the binary name of the packages defined by
301 * the boot loader, in VM internal form (forward slashes instead of dot).
302 */
303 private static native String[] getSystemPackageNames();
304
305 /**
306 * Returns the location of the package of the given name, if
307 * defined by the boot loader; otherwise {@code null} is returned.
|