1 /* 2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. 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.tools.jlink.internal; 26 27 import java.io.BufferedInputStream; 28 import java.io.File; 29 import java.io.IOException; 30 import java.io.PrintWriter; 31 import java.io.UncheckedIOException; 32 import java.lang.module.Configuration; 33 import java.lang.module.FindException; 34 import java.lang.module.ModuleDescriptor; 35 import java.lang.module.ModuleFinder; 36 import java.lang.module.ModuleReference; 37 import java.lang.module.ResolutionException; 38 import java.lang.module.ResolvedModule; 39 import java.net.URI; 40 import java.nio.ByteOrder; 41 import java.nio.file.Files; 42 import java.nio.file.Path; 43 import java.nio.file.Paths; 44 import java.util.ArrayList; 45 import java.util.Arrays; 46 import java.util.Collection; 47 import java.util.Collections; 48 import java.util.Comparator; 49 import java.util.Date; 50 import java.util.HashMap; 51 import java.util.HashSet; 52 import java.util.List; 53 import java.util.Locale; 54 import java.util.Map; 55 import java.util.Objects; 56 import java.util.Optional; 57 import java.util.Set; 58 import java.util.stream.Collectors; 59 import java.util.stream.Stream; 60 61 import jdk.tools.jlink.internal.TaskHelper.BadArgs; 62 import static jdk.tools.jlink.internal.TaskHelper.JLINK_BUNDLE; 63 import jdk.tools.jlink.internal.Jlink.JlinkConfiguration; 64 import jdk.tools.jlink.internal.Jlink.PluginsConfiguration; 65 import jdk.tools.jlink.internal.TaskHelper.Option; 66 import jdk.tools.jlink.internal.TaskHelper.OptionsHelper; 67 import jdk.tools.jlink.internal.ImagePluginStack.ImageProvider; 68 import jdk.tools.jlink.plugin.PluginException; 69 import jdk.tools.jlink.builder.DefaultImageBuilder; 70 import jdk.tools.jlink.plugin.Plugin; 71 import jdk.internal.module.ModulePath; 72 import jdk.internal.module.ModuleResolution; 73 74 /** 75 * Implementation for the jlink tool. 76 * 77 * ## Should use jdk.joptsimple some day. 78 */ 79 public class JlinkTask { 80 static final boolean DEBUG = Boolean.getBoolean("jlink.debug"); 81 82 // jlink API ignores by default. Remove when signing is implemented. 83 static final boolean IGNORE_SIGNING_DEFAULT = true; 84 85 private static final TaskHelper taskHelper 86 = new TaskHelper(JLINK_BUNDLE); 87 88 private static final Option<?>[] recognizedOptions = { 89 new Option<JlinkTask>(false, (task, opt, arg) -> { 90 task.options.help = true; 91 }, "--help", "-h", "-?"), 92 new Option<JlinkTask>(true, (task, opt, arg) -> { 93 // if used multiple times, the last one wins! 94 // So, clear previous values, if any. 95 task.options.modulePath.clear(); 96 String[] dirs = arg.split(File.pathSeparator); 97 int i = 0; 98 Arrays.stream(dirs) 99 .map(Paths::get) 100 .forEach(task.options.modulePath::add); 101 }, "--module-path", "-p"), 102 new Option<JlinkTask>(true, (task, opt, arg) -> { 103 // if used multiple times, the last one wins! 104 // So, clear previous values, if any. 105 task.options.limitMods.clear(); 106 for (String mn : arg.split(",")) { 107 if (mn.isEmpty()) { 108 throw taskHelper.newBadArgs("err.mods.must.be.specified", 109 "--limit-modules"); 110 } 111 task.options.limitMods.add(mn); 112 } 113 }, "--limit-modules"), 114 new Option<JlinkTask>(true, (task, opt, arg) -> { 115 for (String mn : arg.split(",")) { 116 if (mn.isEmpty()) { 117 throw taskHelper.newBadArgs("err.mods.must.be.specified", 118 "--add-modules"); 119 } 120 task.options.addMods.add(mn); 121 } 122 }, "--add-modules"), 123 new Option<JlinkTask>(true, (task, opt, arg) -> { 124 Path path = Paths.get(arg); 125 task.options.output = path; 126 }, "--output"), 127 new Option<JlinkTask>(false, (task, opt, arg) -> { 128 task.options.bindServices = true; 129 }, "--bind-services"), 130 new Option<JlinkTask>(false, (task, opt, arg) -> { 131 task.options.suggestProviders = true; 132 }, "--suggest-providers", "", true), 133 new Option<JlinkTask>(true, (task, opt, arg) -> { 134 String[] values = arg.split("="); 135 // check values 136 if (values.length != 2 || values[0].isEmpty() || values[1].isEmpty()) { 137 throw taskHelper.newBadArgs("err.launcher.value.format", arg); 138 } else { 139 String commandName = values[0]; 140 String moduleAndMain = values[1]; 141 int idx = moduleAndMain.indexOf("/"); 142 if (idx != -1) { 143 if (moduleAndMain.substring(0, idx).isEmpty()) { 144 throw taskHelper.newBadArgs("err.launcher.module.name.empty", arg); 145 } 146 147 if (moduleAndMain.substring(idx + 1).isEmpty()) { 148 throw taskHelper.newBadArgs("err.launcher.main.class.empty", arg); 149 } 150 } 151 task.options.launchers.put(commandName, moduleAndMain); 152 } 153 }, "--launcher"), 154 new Option<JlinkTask>(true, (task, opt, arg) -> { 155 if ("little".equals(arg)) { 156 task.options.endian = ByteOrder.LITTLE_ENDIAN; 157 } else if ("big".equals(arg)) { 158 task.options.endian = ByteOrder.BIG_ENDIAN; 159 } else { 160 throw taskHelper.newBadArgs("err.unknown.byte.order", arg); 161 } 162 }, "--endian"), 163 new Option<JlinkTask>(false, (task, opt, arg) -> { 164 task.options.verbose = true; 165 }, "--verbose", "-v"), 166 new Option<JlinkTask>(false, (task, opt, arg) -> { 167 task.options.version = true; 168 }, "--version"), 169 new Option<JlinkTask>(true, (task, opt, arg) -> { 170 Path path = Paths.get(arg); 171 if (Files.exists(path)) { 172 throw taskHelper.newBadArgs("err.dir.exists", path); 173 } 174 task.options.packagedModulesPath = path; 175 }, true, "--keep-packaged-modules"), 176 new Option<JlinkTask>(true, (task, opt, arg) -> { 177 task.options.saveoptsfile = arg; 178 }, "--save-opts"), 179 new Option<JlinkTask>(false, (task, opt, arg) -> { 180 task.options.fullVersion = true; 181 }, true, "--full-version"), 182 new Option<JlinkTask>(false, (task, opt, arg) -> { 183 task.options.ignoreSigning = true; 184 }, "--ignore-signing-information"),}; 185 186 private static final String PROGNAME = "jlink"; 187 private final OptionsValues options = new OptionsValues(); 188 189 private static final OptionsHelper<JlinkTask> optionsHelper 190 = taskHelper.newOptionsHelper(JlinkTask.class, recognizedOptions); 191 private PrintWriter log; 192 193 void setLog(PrintWriter out, PrintWriter err) { 194 log = out; 195 taskHelper.setLog(log); 196 } 197 198 /** 199 * Result codes. 200 */ 201 static final int 202 EXIT_OK = 0, // Completed with no errors. 203 EXIT_ERROR = 1, // Completed but reported errors. 204 EXIT_CMDERR = 2, // Bad command-line arguments 205 EXIT_SYSERR = 3, // System error or resource exhaustion. 206 EXIT_ABNORMAL = 4;// terminated abnormally 207 208 static class OptionsValues { 209 boolean help; 210 String saveoptsfile; 211 boolean verbose; 212 boolean version; 213 boolean fullVersion; 214 final List<Path> modulePath = new ArrayList<>(); 215 final Set<String> limitMods = new HashSet<>(); 216 final Set<String> addMods = new HashSet<>(); 217 Path output; 218 final Map<String, String> launchers = new HashMap<>(); 219 Path packagedModulesPath; 220 ByteOrder endian = ByteOrder.nativeOrder(); 221 boolean ignoreSigning = false; 222 boolean bindServices = false; 223 boolean suggestProviders = false; 224 } 225 226 int run(String[] args) { 227 if (log == null) { 228 setLog(new PrintWriter(System.out, true), 229 new PrintWriter(System.err, true)); 230 } 231 try { 232 List<String> remaining = optionsHelper.handleOptions(this, args); 233 if (remaining.size() > 0 && !options.suggestProviders) { 234 throw taskHelper.newBadArgs("err.orphan.arguments", toString(remaining)) 235 .showUsage(true); 236 } 237 if (options.help) { 238 optionsHelper.showHelp(PROGNAME); 239 return EXIT_OK; 240 } 241 if (optionsHelper.shouldListPlugins()) { 242 optionsHelper.listPlugins(); 243 return EXIT_OK; 244 } 245 if (options.version || options.fullVersion) { 246 taskHelper.showVersion(options.fullVersion); 247 return EXIT_OK; 248 } 249 250 if (taskHelper.getExistingImage() != null) { 251 postProcessOnly(taskHelper.getExistingImage()); 252 return EXIT_OK; 253 } 254 255 256 if (options.modulePath.isEmpty()) { 257 // no --module-path specified - try to set $JAVA_HOME/jmods if that exists 258 Path jmods = getDefaultModulePath(); 259 if (jmods != null) { 260 options.modulePath.add(jmods); 261 } 262 263 if (options.modulePath.isEmpty()) { 264 throw taskHelper.newBadArgs("err.modulepath.must.be.specified") 265 .showUsage(true); 266 } 267 } 268 269 JlinkConfiguration config = initJlinkConfig(); 270 if (options.suggestProviders) { 271 suggestProviders(config, remaining); 272 } else { 273 createImage(config); 274 if (options.saveoptsfile != null) { 275 Files.write(Paths.get(options.saveoptsfile), getSaveOpts().getBytes()); 276 } 277 } 278 279 return EXIT_OK; 280 } catch (PluginException | IllegalArgumentException | 281 UncheckedIOException |IOException | FindException | ResolutionException e) { 282 log.println(taskHelper.getMessage("error.prefix") + " " + e.getMessage()); 283 if (DEBUG) { 284 e.printStackTrace(log); 285 } 286 return EXIT_ERROR; 287 } catch (BadArgs e) { 288 taskHelper.reportError(e.key, e.args); 289 if (e.showUsage) { 290 log.println(taskHelper.getMessage("main.usage.summary", PROGNAME)); 291 } 292 if (DEBUG) { 293 e.printStackTrace(log); 294 } 295 return EXIT_CMDERR; 296 } catch (Throwable x) { 297 log.println(taskHelper.getMessage("error.prefix") + " " + x.getMessage()); 298 x.printStackTrace(log); 299 return EXIT_ABNORMAL; 300 } finally { 301 log.flush(); 302 } 303 } 304 305 /* 306 * Jlink API entry point. 307 */ 308 public static void createImage(JlinkConfiguration config, 309 PluginsConfiguration plugins) 310 throws Exception { 311 Objects.requireNonNull(config); 312 Objects.requireNonNull(config.getOutput()); 313 plugins = plugins == null ? new PluginsConfiguration() : plugins; 314 315 // First create the image provider 316 ImageProvider imageProvider = 317 createImageProvider(config, 318 null, 319 IGNORE_SIGNING_DEFAULT, 320 false, 321 false, 322 null); 323 324 // Then create the Plugin Stack 325 ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(plugins); 326 327 //Ask the stack to proceed; 328 stack.operate(imageProvider); 329 } 330 331 /* 332 * Jlink API entry point. 333 */ 334 public static void postProcessImage(ExecutableImage image, List<Plugin> postProcessorPlugins) 335 throws Exception { 336 Objects.requireNonNull(image); 337 Objects.requireNonNull(postProcessorPlugins); 338 PluginsConfiguration config = new PluginsConfiguration(postProcessorPlugins); 339 ImagePluginStack stack = ImagePluginConfiguration. 340 parseConfiguration(config); 341 342 stack.operate((ImagePluginStack stack1) -> image); 343 } 344 345 private void postProcessOnly(Path existingImage) throws Exception { 346 PluginsConfiguration config = taskHelper.getPluginsConfig(null, null); 347 ExecutableImage img = DefaultImageBuilder.getExecutableImage(existingImage); 348 if (img == null) { 349 throw taskHelper.newBadArgs("err.existing.image.invalid"); 350 } 351 postProcessImage(img, config.getPlugins()); 352 } 353 354 // the token for "all modules on the module path" 355 private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH"; 356 private JlinkConfiguration initJlinkConfig() throws BadArgs { 357 Set<String> roots = new HashSet<>(); 358 for (String mod : options.addMods) { 359 if (mod.equals(ALL_MODULE_PATH)) { 360 ModuleFinder finder = newModuleFinder(options.modulePath, options.limitMods, Set.of()); 361 // all observable modules are roots 362 finder.findAll() 363 .stream() 364 .map(ModuleReference::descriptor) 365 .map(ModuleDescriptor::name) 366 .forEach(mn -> roots.add(mn)); 367 } else { 368 roots.add(mod); 369 } 370 } 371 372 ModuleFinder finder = newModuleFinder(options.modulePath, options.limitMods, roots); 373 if (!finder.find("java.base").isPresent()) { 374 Path defModPath = getDefaultModulePath(); 375 if (defModPath != null) { 376 options.modulePath.add(defModPath); 377 } 378 finder = newModuleFinder(options.modulePath, options.limitMods, roots); 379 } 380 381 return new JlinkConfiguration(options.output, 382 roots, 383 options.endian, 384 finder); 385 } 386 387 private void createImage(JlinkConfiguration config) throws Exception { 388 if (options.output == null) { 389 throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true); 390 } 391 if (options.addMods.isEmpty()) { 392 throw taskHelper.newBadArgs("err.mods.must.be.specified", "--add-modules") 393 .showUsage(true); 394 } 395 396 // First create the image provider 397 ImageProvider imageProvider = createImageProvider(config, 398 options.packagedModulesPath, 399 options.ignoreSigning, 400 options.bindServices, 401 options.verbose, 402 log); 403 404 // Then create the Plugin Stack 405 ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration( 406 taskHelper.getPluginsConfig(options.output, options.launchers)); 407 408 //Ask the stack to proceed 409 stack.operate(imageProvider); 410 } 411 412 /** 413 * @return the system module path or null 414 */ 415 public static Path getDefaultModulePath() { 416 Path jmods = Paths.get(System.getProperty("java.home"), "jmods"); 417 return Files.isDirectory(jmods)? jmods : null; 418 } 419 420 /* 421 * Returns a module finder of the given module path that limits 422 * the observable modules to those in the transitive closure of 423 * the modules specified in {@code limitMods} plus other modules 424 * specified in the {@code roots} set. 425 * 426 * @throws IllegalArgumentException if java.base module is present 427 * but its descriptor has no version 428 */ 429 public static ModuleFinder newModuleFinder(List<Path> paths, 430 Set<String> limitMods, 431 Set<String> roots) 432 { 433 if (Objects.requireNonNull(paths).isEmpty()) { 434 throw new IllegalArgumentException(taskHelper.getMessage("err.empty.module.path")); 435 } 436 437 Path[] entries = paths.toArray(new Path[0]); 438 Runtime.Version version = Runtime.version(); 439 ModuleFinder finder = ModulePath.of(version, true, entries); 440 441 if (finder.find("java.base").isPresent()) { 442 // use the version of java.base module, if present, as 443 // the release version for multi-release JAR files 444 ModuleDescriptor.Version v = finder.find("java.base").get() 445 .descriptor().version().orElseThrow(() -> 446 new IllegalArgumentException("No version in java.base descriptor") 447 ); 448 449 // java.base version is different than the current runtime version 450 version = Runtime.Version.parse(v.toString()); 451 if (Runtime.version().major() != version.major() || 452 Runtime.version().minor() != version.minor()) { 453 // jlink version and java.base version do not match. 454 // We do not (yet) support this mode. 455 throw new IllegalArgumentException(taskHelper.getMessage("err.jlink.version.mismatch", 456 Runtime.version().major(), Runtime.version().minor(), 457 version.major(), version.minor())); 458 } 459 } 460 461 // if limitmods is specified then limit the universe 462 if (limitMods != null && !limitMods.isEmpty()) { 463 finder = limitFinder(finder, limitMods, Objects.requireNonNull(roots)); 464 } 465 return finder; 466 } 467 468 private static Path toPathLocation(ResolvedModule m) { 469 Optional<URI> ouri = m.reference().location(); 470 if (!ouri.isPresent()) 471 throw new InternalError(m + " does not have a location"); 472 URI uri = ouri.get(); 473 return Paths.get(uri); 474 } 475 476 477 private static ImageProvider createImageProvider(JlinkConfiguration config, 478 Path retainModulesPath, 479 boolean ignoreSigning, 480 boolean bindService, 481 boolean verbose, 482 PrintWriter log) 483 throws IOException 484 { 485 Configuration cf = bindService ? config.resolveAndBind() 486 : config.resolve(); 487 488 cf.modules().stream() 489 .map(ResolvedModule::reference) 490 .filter(mref -> mref.descriptor().isAutomatic()) 491 .findAny() 492 .ifPresent(mref -> { 493 String loc = mref.location().map(URI::toString).orElse("<unknown>"); 494 throw new IllegalArgumentException( 495 taskHelper.getMessage("err.automatic.module", mref.descriptor().name(), loc)); 496 }); 497 498 if (verbose && log != null) { 499 // print modules to be linked in 500 cf.modules().stream() 501 .sorted(Comparator.comparing(ResolvedModule::name)) 502 .forEach(rm -> log.format("%s %s%n", 503 rm.name(), rm.reference().location().get())); 504 505 // print provider info 506 Set<ModuleReference> references = cf.modules().stream() 507 .map(ResolvedModule::reference).collect(Collectors.toSet()); 508 509 String msg = String.format("%n%s:", taskHelper.getMessage("providers.header")); 510 printProviders(log, msg, references); 511 } 512 513 // emit a warning for any incubating modules in the configuration 514 if (log != null) { 515 String im = cf.modules() 516 .stream() 517 .map(ResolvedModule::reference) 518 .filter(ModuleResolution::hasIncubatingWarning) 519 .map(ModuleReference::descriptor) 520 .map(ModuleDescriptor::name) 521 .collect(Collectors.joining(", ")); 522 523 if (!"".equals(im)) 524 log.println("WARNING: Using incubator modules: " + im); 525 } 526 527 Map<String, Path> mods = cf.modules().stream() 528 .collect(Collectors.toMap(ResolvedModule::name, JlinkTask::toPathLocation)); 529 return new ImageHelper(cf, mods, config.getByteOrder(), retainModulesPath, ignoreSigning); 530 } 531 532 /* 533 * Returns a ModuleFinder that limits observability to the given root 534 * modules, their transitive dependences, plus a set of other modules. 535 */ 536 public static ModuleFinder limitFinder(ModuleFinder finder, 537 Set<String> roots, 538 Set<String> otherMods) { 539 540 // resolve all root modules 541 Configuration cf = Configuration.empty() 542 .resolve(finder, 543 ModuleFinder.of(), 544 roots); 545 546 // module name -> reference 547 Map<String, ModuleReference> map = new HashMap<>(); 548 cf.modules().forEach(m -> { 549 ModuleReference mref = m.reference(); 550 map.put(mref.descriptor().name(), mref); 551 }); 552 553 // add the other modules 554 otherMods.stream() 555 .map(finder::find) 556 .flatMap(Optional::stream) 557 .forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref)); 558 559 // set of modules that are observable 560 Set<ModuleReference> mrefs = new HashSet<>(map.values()); 561 562 return new ModuleFinder() { 563 @Override 564 public Optional<ModuleReference> find(String name) { 565 return Optional.ofNullable(map.get(name)); 566 } 567 568 @Override 569 public Set<ModuleReference> findAll() { 570 return mrefs; 571 } 572 }; 573 } 574 575 /* 576 * Returns a map of each service type to the modules that use it 577 * It will include services that are provided by a module but may not used 578 * by any of the observable modules. 579 */ 580 private static Map<String, Set<String>> uses(Set<ModuleReference> modules) { 581 // collects the services used by the modules and print uses 582 Map<String, Set<String>> services = new HashMap<>(); 583 modules.stream() 584 .map(ModuleReference::descriptor) 585 .forEach(md -> { 586 // include services that may not be used by any observable modules 587 md.provides().forEach(p -> 588 services.computeIfAbsent(p.service(), _k -> new HashSet<>())); 589 md.uses().forEach(s -> services.computeIfAbsent(s, _k -> new HashSet<>()) 590 .add(md.name())); 591 }); 592 return services; 593 } 594 595 private static void printProviders(PrintWriter log, 596 String header, 597 Set<ModuleReference> modules) { 598 printProviders(log, header, modules, uses(modules)); 599 } 600 601 /* 602 * Prints the providers that are used by the specified services. 603 * 604 * The specified services maps a service type name to the modules 605 * using the service type which may be empty if no observable module uses 606 * that service. 607 */ 608 private static void printProviders(PrintWriter log, 609 String header, 610 Set<ModuleReference> modules, 611 Map<String, Set<String>> serviceToUses) { 612 if (modules.isEmpty()) 613 return; 614 615 // Build a map of a service type to the provider modules 616 Map<String, Set<ModuleDescriptor>> providers = new HashMap<>(); 617 modules.stream() 618 .map(ModuleReference::descriptor) 619 .forEach(md -> { 620 md.provides().stream() 621 .filter(p -> serviceToUses.containsKey(p.service())) 622 .forEach(p -> providers.computeIfAbsent(p.service(), _k -> new HashSet<>()) 623 .add(md)); 624 }); 625 626 if (!providers.isEmpty()) { 627 log.println(header); 628 } 629 630 // print the providers of the service types used by the specified modules 631 // sorted by the service type name and then provider's module name 632 providers.entrySet().stream() 633 .sorted(Map.Entry.comparingByKey()) 634 .forEach(e -> { 635 String service = e.getKey(); 636 e.getValue().stream() 637 .sorted(Comparator.comparing(ModuleDescriptor::name)) 638 .forEach(md -> 639 md.provides().stream() 640 .filter(p -> p.service().equals(service)) 641 .forEach(p -> { 642 String usedBy; 643 if (serviceToUses.get(p.service()).isEmpty()) { 644 usedBy = "not used by any observable module"; 645 } else { 646 usedBy = serviceToUses.get(p.service()).stream() 647 .sorted() 648 .collect(Collectors.joining(",", "used by ", "")); 649 } 650 log.format(" %s provides %s %s%n", 651 md.name(), p.service(), usedBy); 652 }) 653 ); 654 }); 655 } 656 657 private void suggestProviders(JlinkConfiguration config, List<String> args) 658 throws BadArgs 659 { 660 if (args.size() > 1) { 661 throw taskHelper.newBadArgs("err.orphan.argument", 662 toString(args.subList(1, args.size()))) 663 .showUsage(true); 664 } 665 666 if (options.bindServices) { 667 log.println(taskHelper.getMessage("no.suggested.providers")); 668 return; 669 } 670 671 ModuleFinder finder = config.finder(); 672 if (args.isEmpty()) { 673 // print providers used by the observable modules without service binding 674 Set<ModuleReference> mrefs = finder.findAll(); 675 // print uses of the modules that would be linked into the image 676 mrefs.stream() 677 .sorted(Comparator.comparing(mref -> mref.descriptor().name())) 678 .forEach(mref -> { 679 ModuleDescriptor md = mref.descriptor(); 680 log.format("%s %s%n", md.name(), 681 mref.location().get()); 682 md.uses().stream().sorted() 683 .forEach(s -> log.format(" uses %s%n", s)); 684 }); 685 686 String msg = String.format("%n%s:", taskHelper.getMessage("suggested.providers.header")); 687 printProviders(log, msg, mrefs, uses(mrefs)); 688 689 } else { 690 // comma-separated service types, if specified 691 Set<String> names = Stream.of(args.get(0).split(",")) 692 .collect(Collectors.toSet()); 693 // find the modules that provide the specified service 694 Set<ModuleReference> mrefs = finder.findAll().stream() 695 .filter(mref -> mref.descriptor().provides().stream() 696 .map(ModuleDescriptor.Provides::service) 697 .anyMatch(names::contains)) 698 .collect(Collectors.toSet()); 699 700 // find the modules that uses the specified services 701 Map<String, Set<String>> uses = new HashMap<>(); 702 names.forEach(s -> uses.computeIfAbsent(s, _k -> new HashSet<>())); 703 finder.findAll().stream() 704 .map(ModuleReference::descriptor) 705 .forEach(md -> md.uses().stream() 706 .filter(names::contains) 707 .forEach(s -> uses.get(s).add(md.name()))); 708 709 // check if any name given on the command line are not provided by any module 710 mrefs.stream() 711 .flatMap(mref -> mref.descriptor().provides().stream() 712 .map(ModuleDescriptor.Provides::service)) 713 .forEach(names::remove); 714 if (!names.isEmpty()) { 715 log.println(taskHelper.getMessage("warn.provider.notfound", 716 toString(names))); 717 } 718 719 String msg = String.format("%n%s:", taskHelper.getMessage("suggested.providers.header")); 720 printProviders(log, msg, mrefs, uses); 721 } 722 } 723 724 private static String toString(Collection<String> collection) { 725 return collection.stream().sorted() 726 .collect(Collectors.joining(",")); 727 } 728 729 private String getSaveOpts() { 730 StringBuilder sb = new StringBuilder(); 731 sb.append('#').append(new Date()).append("\n"); 732 for (String c : optionsHelper.getInputCommand()) { 733 sb.append(c).append(" "); 734 } 735 736 return sb.toString(); 737 } 738 739 private static String getBomHeader() { 740 StringBuilder sb = new StringBuilder(); 741 sb.append("#").append(new Date()).append("\n"); 742 sb.append("#Please DO NOT Modify this file").append("\n"); 743 return sb.toString(); 744 } 745 746 private String genBOMContent() throws IOException { 747 StringBuilder sb = new StringBuilder(); 748 sb.append(getBomHeader()); 749 StringBuilder command = new StringBuilder(); 750 for (String c : optionsHelper.getInputCommand()) { 751 command.append(c).append(" "); 752 } 753 sb.append("command").append(" = ").append(command); 754 sb.append("\n"); 755 756 return sb.toString(); 757 } 758 759 private static String genBOMContent(JlinkConfiguration config, 760 PluginsConfiguration plugins) 761 throws IOException { 762 StringBuilder sb = new StringBuilder(); 763 sb.append(getBomHeader()); 764 sb.append(config); 765 sb.append(plugins); 766 return sb.toString(); 767 } 768 769 private static class ImageHelper implements ImageProvider { 770 final ByteOrder order; 771 final Path packagedModulesPath; 772 final boolean ignoreSigning; 773 final Runtime.Version version; 774 final Set<Archive> archives; 775 776 ImageHelper(Configuration cf, 777 Map<String, Path> modsPaths, 778 ByteOrder order, 779 Path packagedModulesPath, 780 boolean ignoreSigning) throws IOException { 781 this.order = order; 782 this.packagedModulesPath = packagedModulesPath; 783 this.ignoreSigning = ignoreSigning; 784 785 // use the version of java.base module, if present, as 786 // the release version for multi-release JAR files 787 this.version = cf.findModule("java.base") 788 .map(ResolvedModule::reference) 789 .map(ModuleReference::descriptor) 790 .flatMap(ModuleDescriptor::version) 791 .map(ModuleDescriptor.Version::toString) 792 .map(Runtime.Version::parse) 793 .orElse(Runtime.version()); 794 795 this.archives = modsPaths.entrySet().stream() 796 .map(e -> newArchive(e.getKey(), e.getValue())) 797 .collect(Collectors.toSet()); 798 } 799 800 private Archive newArchive(String module, Path path) { 801 if (path.toString().endsWith(".jmod")) { 802 return new JmodArchive(module, path); 803 } else if (path.toString().endsWith(".jar")) { 804 ModularJarArchive modularJarArchive = new ModularJarArchive(module, path, version); 805 806 Stream<Archive.Entry> signatures = modularJarArchive.entries().filter((entry) -> { 807 String name = entry.name().toUpperCase(Locale.ENGLISH); 808 809 return name.startsWith("META-INF/") && name.indexOf('/', 9) == -1 && ( 810 name.endsWith(".SF") || 811 name.endsWith(".DSA") || 812 name.endsWith(".RSA") || 813 name.endsWith(".EC") || 814 name.startsWith("META-INF/SIG-") 815 ); 816 }); 817 818 if (signatures.count() != 0) { 819 if (ignoreSigning) { 820 System.err.println(taskHelper.getMessage("warn.signing", path)); 821 } else { 822 throw new IllegalArgumentException(taskHelper.getMessage("err.signing", path)); 823 } 824 } 825 826 return modularJarArchive; 827 } else if (Files.isDirectory(path)) { 828 Path modInfoPath = path.resolve("module-info.class"); 829 if (Files.isRegularFile(modInfoPath)) { 830 return new DirArchive(path, findModuleName(modInfoPath)); 831 } else { 832 throw new IllegalArgumentException( 833 taskHelper.getMessage("err.not.a.module.directory", path)); 834 } 835 } else { 836 throw new IllegalArgumentException( 837 taskHelper.getMessage("err.not.modular.format", module, path)); 838 } 839 } 840 841 private static String findModuleName(Path modInfoPath) { 842 try (BufferedInputStream bis = new BufferedInputStream( 843 Files.newInputStream(modInfoPath))) { 844 return ModuleDescriptor.read(bis).name(); 845 } catch (IOException exp) { 846 throw new IllegalArgumentException(taskHelper.getMessage( 847 "err.cannot.read.module.info", modInfoPath), exp); 848 } 849 } 850 851 @Override 852 public ExecutableImage retrieve(ImagePluginStack stack) throws IOException { 853 ExecutableImage image = ImageFileCreator.create(archives, order, stack); 854 if (packagedModulesPath != null) { 855 // copy the packaged modules to the given path 856 Files.createDirectories(packagedModulesPath); 857 for (Archive a : archives) { 858 Path file = a.getPath(); 859 Path dest = packagedModulesPath.resolve(file.getFileName()); 860 Files.copy(file, dest); 861 } 862 } 863 return image; 864 } 865 } 866 }