87
88 /**
89 * Initialize the module system, returning the boot Layer.
90 *
91 * @see java.lang.System#initPhase2()
92 */
93 public static Layer boot() {
94
95 long t0 = System.nanoTime();
96
97 // system module path
98 ModuleFinder systemModulePath = ModuleFinder.ofSystem();
99
100 // Once we have the system module path then we define the base module.
101 // We do this here so that java.base is defined to the VM as early as
102 // possible and also that resources in the base module can be located
103 // for error messages that may happen from here on.
104 Optional<ModuleReference> obase = systemModulePath.find(JAVA_BASE);
105 if (!obase.isPresent())
106 throw new InternalError(JAVA_BASE + " not found");
107 ModuleReference base = obase.get();
108 BootLoader.loadModule(base);
109 Modules.defineModule(null, base.descriptor(), base.location().orElse(null));
110
111
112 // -upgrademodulepath option specified to launcher
113 ModuleFinder upgradeModulePath
114 = createModulePathFinder("jdk.upgrade.module.path");
115
116 // -modulepath option specified to the launcher
117 ModuleFinder appModulePath = createModulePathFinder("jdk.module.path");
118
119 // The module finder: [-upgrademodulepath] system-module-path [-modulepath]
120 ModuleFinder finder = systemModulePath;
121 if (upgradeModulePath != null)
122 finder = ModuleFinder.compose(upgradeModulePath, finder);
123 if (appModulePath != null)
124 finder = ModuleFinder.compose(finder, appModulePath);
125
126 // launcher -m option to specify the initial module
127 String mainModule = System.getProperty("jdk.module.main");
289 {
290 // resolve all root modules
291 Configuration cf = Configuration.empty()
292 .resolveRequires(finder,
293 ModuleFinder.empty(),
294 roots);
295
296 // module name -> reference
297 Map<String, ModuleReference> map = new HashMap<>();
298 cf.modules().stream()
299 .map(ResolvedModule::reference)
300 .forEach(mref -> map.put(mref.descriptor().name(), mref));
301
302 // set of modules that are observable
303 Set<ModuleReference> mrefs = new HashSet<>(map.values());
304
305 // add the other modules
306 for (String mod : otherMods) {
307 Optional<ModuleReference> omref = finder.find(mod);
308 if (omref.isPresent()) {
309 ModuleReference mref = omref.get();
310 map.putIfAbsent(mod, mref);
311 mrefs.add(mref);
312 } else {
313 // no need to fail
314 }
315 }
316
317 return new ModuleFinder() {
318 @Override
319 public Optional<ModuleReference> find(String name) {
320 return Optional.ofNullable(map.get(name));
321 }
322 @Override
323 public Set<ModuleReference> findAll() {
324 return mrefs;
325 }
326 };
327 }
328
329 /**
345 }
346 }
347
348
349 /**
350 * Process the -XaddReads options to add any additional read edges that
351 * are specified on the command-line.
352 */
353 private static void addExtraReads(Layer bootLayer) {
354
355 // decode the command line options
356 Map<String, Set<String>> map = decode("jdk.launcher.addreads.");
357
358 for (Map.Entry<String, Set<String>> e : map.entrySet()) {
359
360 // the key is $MODULE
361 String mn = e.getKey();
362 Optional<Module> om = bootLayer.findModule(mn);
363 if (!om.isPresent())
364 fail("Unknown module: " + mn);
365 Module m = om.get();
366
367 // the value is the set of other modules (by name)
368 for (String name : e.getValue()) {
369
370 Module other;
371 if (ALL_UNNAMED.equals(name)) {
372 other = null; // loose
373 } else {
374 om = bootLayer.findModule(name);
375 if (!om.isPresent())
376 fail("Unknown module: " + name);
377 other = om.get();
378 }
379
380 Modules.addReads(m, other);
381 }
382 }
383 }
384
385
386 /**
387 * Process the -XaddExports options to add any additional read edges that
388 * are specified on the command-line.
389 */
390 private static void addExtraExports(Layer bootLayer) {
391
392 // decode the command line options
393 Map<String, Set<String>> map = decode("jdk.launcher.addexports.");
394
395 for (Map.Entry<String, Set<String>> e : map.entrySet()) {
396
397 // the key is $MODULE/$PACKAGE
398 String key = e.getKey();
399 String[] s = key.split("/");
400 if (s.length != 2)
401 fail("Unable to parse: " + key);
402
403 String mn = s[0];
404 String pn = s[1];
405
406 // The exporting module is in the boot layer
407 Module m;
408 Optional<Module> om = bootLayer.findModule(mn);
409 if (!om.isPresent())
410 fail("Unknown module: " + mn);
411 m = om.get();
412
413 // the value is the set of modules to export to (by name)
414 for (String name : e.getValue()) {
415 boolean allUnnamed = false;
416 Module other = null;
417 if (ALL_UNNAMED.equals(name)) {
418 allUnnamed = true;
419 } else {
420 om = bootLayer.findModule(name);
421 if (om.isPresent()) {
422 other = om.get();
423 } else {
424 fail("Unknown module: " + name);
425 }
426 }
427
428 if (allUnnamed) {
429 Modules.addExportsToAllUnnamed(m, pn);
430 } else {
431 Modules.addExports(m, pn, other);
432 }
433 }
434 }
435 }
436
437
438 /**
439 * Decodes the values of -XaddReads or -XaddExports options
440 *
441 * The format of the options is: $KEY=$MODULE(,$MODULE)*
442 *
|
87
88 /**
89 * Initialize the module system, returning the boot Layer.
90 *
91 * @see java.lang.System#initPhase2()
92 */
93 public static Layer boot() {
94
95 long t0 = System.nanoTime();
96
97 // system module path
98 ModuleFinder systemModulePath = ModuleFinder.ofSystem();
99
100 // Once we have the system module path then we define the base module.
101 // We do this here so that java.base is defined to the VM as early as
102 // possible and also that resources in the base module can be located
103 // for error messages that may happen from here on.
104 Optional<ModuleReference> obase = systemModulePath.find(JAVA_BASE);
105 if (!obase.isPresent())
106 throw new InternalError(JAVA_BASE + " not found");
107 ModuleReference base = obase.getWhenPresent();
108 BootLoader.loadModule(base);
109 Modules.defineModule(null, base.descriptor(), base.location().orElse(null));
110
111
112 // -upgrademodulepath option specified to launcher
113 ModuleFinder upgradeModulePath
114 = createModulePathFinder("jdk.upgrade.module.path");
115
116 // -modulepath option specified to the launcher
117 ModuleFinder appModulePath = createModulePathFinder("jdk.module.path");
118
119 // The module finder: [-upgrademodulepath] system-module-path [-modulepath]
120 ModuleFinder finder = systemModulePath;
121 if (upgradeModulePath != null)
122 finder = ModuleFinder.compose(upgradeModulePath, finder);
123 if (appModulePath != null)
124 finder = ModuleFinder.compose(finder, appModulePath);
125
126 // launcher -m option to specify the initial module
127 String mainModule = System.getProperty("jdk.module.main");
289 {
290 // resolve all root modules
291 Configuration cf = Configuration.empty()
292 .resolveRequires(finder,
293 ModuleFinder.empty(),
294 roots);
295
296 // module name -> reference
297 Map<String, ModuleReference> map = new HashMap<>();
298 cf.modules().stream()
299 .map(ResolvedModule::reference)
300 .forEach(mref -> map.put(mref.descriptor().name(), mref));
301
302 // set of modules that are observable
303 Set<ModuleReference> mrefs = new HashSet<>(map.values());
304
305 // add the other modules
306 for (String mod : otherMods) {
307 Optional<ModuleReference> omref = finder.find(mod);
308 if (omref.isPresent()) {
309 ModuleReference mref = omref.getWhenPresent();
310 map.putIfAbsent(mod, mref);
311 mrefs.add(mref);
312 } else {
313 // no need to fail
314 }
315 }
316
317 return new ModuleFinder() {
318 @Override
319 public Optional<ModuleReference> find(String name) {
320 return Optional.ofNullable(map.get(name));
321 }
322 @Override
323 public Set<ModuleReference> findAll() {
324 return mrefs;
325 }
326 };
327 }
328
329 /**
345 }
346 }
347
348
349 /**
350 * Process the -XaddReads options to add any additional read edges that
351 * are specified on the command-line.
352 */
353 private static void addExtraReads(Layer bootLayer) {
354
355 // decode the command line options
356 Map<String, Set<String>> map = decode("jdk.launcher.addreads.");
357
358 for (Map.Entry<String, Set<String>> e : map.entrySet()) {
359
360 // the key is $MODULE
361 String mn = e.getKey();
362 Optional<Module> om = bootLayer.findModule(mn);
363 if (!om.isPresent())
364 fail("Unknown module: " + mn);
365 Module m = om.getWhenPresent();
366
367 // the value is the set of other modules (by name)
368 for (String name : e.getValue()) {
369
370 Module other;
371 if (ALL_UNNAMED.equals(name)) {
372 other = null; // loose
373 } else {
374 om = bootLayer.findModule(name);
375 if (!om.isPresent())
376 fail("Unknown module: " + name);
377 other = om.getWhenPresent();
378 }
379
380 Modules.addReads(m, other);
381 }
382 }
383 }
384
385
386 /**
387 * Process the -XaddExports options to add any additional read edges that
388 * are specified on the command-line.
389 */
390 private static void addExtraExports(Layer bootLayer) {
391
392 // decode the command line options
393 Map<String, Set<String>> map = decode("jdk.launcher.addexports.");
394
395 for (Map.Entry<String, Set<String>> e : map.entrySet()) {
396
397 // the key is $MODULE/$PACKAGE
398 String key = e.getKey();
399 String[] s = key.split("/");
400 if (s.length != 2)
401 fail("Unable to parse: " + key);
402
403 String mn = s[0];
404 String pn = s[1];
405
406 // The exporting module is in the boot layer
407 Module m;
408 Optional<Module> om = bootLayer.findModule(mn);
409 if (!om.isPresent())
410 fail("Unknown module: " + mn);
411 m = om.getWhenPresent();
412
413 // the value is the set of modules to export to (by name)
414 for (String name : e.getValue()) {
415 boolean allUnnamed = false;
416 Module other = null;
417 if (ALL_UNNAMED.equals(name)) {
418 allUnnamed = true;
419 } else {
420 om = bootLayer.findModule(name);
421 if (om.isPresent()) {
422 other = om.getWhenPresent();
423 } else {
424 fail("Unknown module: " + name);
425 }
426 }
427
428 if (allUnnamed) {
429 Modules.addExportsToAllUnnamed(m, pn);
430 } else {
431 Modules.addExports(m, pn, other);
432 }
433 }
434 }
435 }
436
437
438 /**
439 * Decodes the values of -XaddReads or -XaddExports options
440 *
441 * The format of the options is: $KEY=$MODULE(,$MODULE)*
442 *
|