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.tools.jlink.internal;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.PrintWriter;
30 import java.io.UncheckedIOException;
31 import java.lang.module.Configuration;
32 import java.lang.module.ModuleDescriptor;
33 import java.lang.module.ModuleFinder;
34 import java.lang.module.ModuleReference;
35 import java.lang.module.ResolutionException;
36 import java.lang.module.ResolvedModule;
37 import java.net.URI;
38 import java.nio.ByteOrder;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.*;
43 import java.util.stream.Collectors;
44 import java.util.stream.Stream;
45
46 import jdk.tools.jlink.internal.TaskHelper.BadArgs;
47 import static jdk.tools.jlink.internal.TaskHelper.JLINK_BUNDLE;
48 import jdk.tools.jlink.internal.Jlink.JlinkConfiguration;
49 import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;
50 import jdk.tools.jlink.internal.TaskHelper.Option;
51 import jdk.tools.jlink.internal.TaskHelper.OptionsHelper;
215 if (options.version || options.fullVersion) {
216 taskHelper.showVersion(options.fullVersion);
217 return EXIT_OK;
218 }
219
220 if (taskHelper.getExistingImage() == null) {
221 if (options.modulePath.isEmpty()) {
222 throw taskHelper.newBadArgs("err.modulepath.must.be.specified").showUsage(true);
223 }
224 createImage();
225 } else {
226 postProcessOnly(taskHelper.getExistingImage());
227 }
228
229 if (options.saveoptsfile != null) {
230 Files.write(Paths.get(options.saveoptsfile), getSaveOpts().getBytes());
231 }
232
233 return EXIT_OK;
234 } catch (PluginException | IllegalArgumentException |
235 UncheckedIOException |IOException | ResolutionException e) {
236 log.println(taskHelper.getMessage("error.prefix") + " " + e.getMessage());
237 if (DEBUG) {
238 e.printStackTrace(log);
239 }
240 return EXIT_ERROR;
241 } catch (BadArgs e) {
242 taskHelper.reportError(e.key, e.args);
243 if (e.showUsage) {
244 log.println(taskHelper.getMessage("main.usage.summary", PROGNAME));
245 }
246 if (DEBUG) {
247 e.printStackTrace(log);
248 }
249 return EXIT_CMDERR;
250 } catch (Throwable x) {
251 log.println(taskHelper.getMessage("error.prefix") + " " + x.getMessage());
252 x.printStackTrace(log);
253 return EXIT_ABNORMAL;
254 } finally {
255 log.flush();
401 Optional<URI> ouri = m.reference().location();
402 if (!ouri.isPresent())
403 throw new InternalError(m + " does not have a location");
404 URI uri = ouri.get();
405 return Paths.get(uri);
406 }
407
408 private static ImageProvider createImageProvider(ModuleFinder finder,
409 Set<String> roots,
410 ByteOrder order,
411 Path retainModulesPath,
412 boolean ignoreSigning,
413 PrintWriter log)
414 throws IOException
415 {
416 if (roots.isEmpty()) {
417 throw new IllegalArgumentException("empty modules and limitmods");
418 }
419
420 Configuration cf = Configuration.empty()
421 .resolveRequires(finder,
422 ModuleFinder.of(),
423 roots);
424
425 // emit warning for modules that end with a digit
426 cf.modules().stream()
427 .map(ResolvedModule::name)
428 .filter(mn -> !Checks.hasLegalModuleNameLastCharacter(mn))
429 .forEach(mn -> System.err.println("WARNING: Module name \""
430 + mn + "\" may soon be illegal"));
431
432 // emit a warning for any incubating modules in the configuration
433 if (log != null) {
434 String im = cf.modules()
435 .stream()
436 .map(ResolvedModule::reference)
437 .filter(ModuleResolution::hasIncubatingWarning)
438 .map(ModuleReference::descriptor)
439 .map(ModuleDescriptor::name)
440 .collect(Collectors.joining(", "));
441
442 if (!"".equals(im))
443 log.println("WARNING: Using incubator modules: " + im);
444 }
445
446 Map<String, Path> mods = cf.modules().stream()
447 .collect(Collectors.toMap(ResolvedModule::name, JlinkTask::toPathLocation));
448 return new ImageHelper(cf, mods, order, retainModulesPath, ignoreSigning);
449 }
450
451 /*
452 * Returns a ModuleFinder that limits observability to the given root
453 * modules, their transitive dependences, plus a set of other modules.
454 */
455 private static ModuleFinder limitFinder(ModuleFinder finder,
456 Set<String> roots,
457 Set<String> otherMods) {
458
459 // resolve all root modules
460 Configuration cf = Configuration.empty()
461 .resolveRequires(finder,
462 ModuleFinder.of(),
463 roots);
464
465 // module name -> reference
466 Map<String, ModuleReference> map = new HashMap<>();
467 cf.modules().forEach(m -> {
468 ModuleReference mref = m.reference();
469 map.put(mref.descriptor().name(), mref);
470 });
471
472 // add the other modules
473 otherMods.stream()
474 .map(finder::find)
475 .flatMap(Optional::stream)
476 .forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref));
477
478 // set of modules that are observable
479 Set<ModuleReference> mrefs = new HashSet<>(map.values());
480
481 return new ModuleFinder() {
|
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.tools.jlink.internal;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.PrintWriter;
30 import java.io.UncheckedIOException;
31 import java.lang.module.Configuration;
32 import java.lang.module.FindException;
33 import java.lang.module.ModuleDescriptor;
34 import java.lang.module.ModuleFinder;
35 import java.lang.module.ModuleReference;
36 import java.lang.module.ResolutionException;
37 import java.lang.module.ResolvedModule;
38 import java.net.URI;
39 import java.nio.ByteOrder;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.util.*;
44 import java.util.stream.Collectors;
45 import java.util.stream.Stream;
46
47 import jdk.tools.jlink.internal.TaskHelper.BadArgs;
48 import static jdk.tools.jlink.internal.TaskHelper.JLINK_BUNDLE;
49 import jdk.tools.jlink.internal.Jlink.JlinkConfiguration;
50 import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;
51 import jdk.tools.jlink.internal.TaskHelper.Option;
52 import jdk.tools.jlink.internal.TaskHelper.OptionsHelper;
216 if (options.version || options.fullVersion) {
217 taskHelper.showVersion(options.fullVersion);
218 return EXIT_OK;
219 }
220
221 if (taskHelper.getExistingImage() == null) {
222 if (options.modulePath.isEmpty()) {
223 throw taskHelper.newBadArgs("err.modulepath.must.be.specified").showUsage(true);
224 }
225 createImage();
226 } else {
227 postProcessOnly(taskHelper.getExistingImage());
228 }
229
230 if (options.saveoptsfile != null) {
231 Files.write(Paths.get(options.saveoptsfile), getSaveOpts().getBytes());
232 }
233
234 return EXIT_OK;
235 } catch (PluginException | IllegalArgumentException |
236 UncheckedIOException |IOException | FindException | ResolutionException e) {
237 log.println(taskHelper.getMessage("error.prefix") + " " + e.getMessage());
238 if (DEBUG) {
239 e.printStackTrace(log);
240 }
241 return EXIT_ERROR;
242 } catch (BadArgs e) {
243 taskHelper.reportError(e.key, e.args);
244 if (e.showUsage) {
245 log.println(taskHelper.getMessage("main.usage.summary", PROGNAME));
246 }
247 if (DEBUG) {
248 e.printStackTrace(log);
249 }
250 return EXIT_CMDERR;
251 } catch (Throwable x) {
252 log.println(taskHelper.getMessage("error.prefix") + " " + x.getMessage());
253 x.printStackTrace(log);
254 return EXIT_ABNORMAL;
255 } finally {
256 log.flush();
402 Optional<URI> ouri = m.reference().location();
403 if (!ouri.isPresent())
404 throw new InternalError(m + " does not have a location");
405 URI uri = ouri.get();
406 return Paths.get(uri);
407 }
408
409 private static ImageProvider createImageProvider(ModuleFinder finder,
410 Set<String> roots,
411 ByteOrder order,
412 Path retainModulesPath,
413 boolean ignoreSigning,
414 PrintWriter log)
415 throws IOException
416 {
417 if (roots.isEmpty()) {
418 throw new IllegalArgumentException("empty modules and limitmods");
419 }
420
421 Configuration cf = Configuration.empty()
422 .resolve(finder,
423 ModuleFinder.of(),
424 roots);
425
426 // emit warning for modules that end with a digit
427 cf.modules().stream()
428 .map(ResolvedModule::name)
429 .filter(mn -> !Checks.hasLegalModuleNameLastCharacter(mn))
430 .forEach(mn -> System.err.println("WARNING: Module name \""
431 + mn + "\" may soon be illegal"));
432
433 // emit a warning for any incubating modules in the configuration
434 if (log != null) {
435 String im = cf.modules()
436 .stream()
437 .map(ResolvedModule::reference)
438 .filter(ModuleResolution::hasIncubatingWarning)
439 .map(ModuleReference::descriptor)
440 .map(ModuleDescriptor::name)
441 .collect(Collectors.joining(", "));
442
443 if (!"".equals(im))
444 log.println("WARNING: Using incubator modules: " + im);
445 }
446
447 Map<String, Path> mods = cf.modules().stream()
448 .collect(Collectors.toMap(ResolvedModule::name, JlinkTask::toPathLocation));
449 return new ImageHelper(cf, mods, order, retainModulesPath, ignoreSigning);
450 }
451
452 /*
453 * Returns a ModuleFinder that limits observability to the given root
454 * modules, their transitive dependences, plus a set of other modules.
455 */
456 private static ModuleFinder limitFinder(ModuleFinder finder,
457 Set<String> roots,
458 Set<String> otherMods) {
459
460 // resolve all root modules
461 Configuration cf = Configuration.empty()
462 .resolve(finder,
463 ModuleFinder.of(),
464 roots);
465
466 // module name -> reference
467 Map<String, ModuleReference> map = new HashMap<>();
468 cf.modules().forEach(m -> {
469 ModuleReference mref = m.reference();
470 map.put(mref.descriptor().name(), mref);
471 });
472
473 // add the other modules
474 otherMods.stream()
475 .map(finder::find)
476 .flatMap(Optional::stream)
477 .forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref));
478
479 // set of modules that are observable
480 Set<ModuleReference> mrefs = new HashSet<>(map.values());
481
482 return new ModuleFinder() {
|