< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java

Print this page




 149         taskHelper.setLog(log);
 150     }
 151 
 152     /**
 153      * Result codes.
 154      */
 155     static final int
 156             EXIT_OK = 0, // Completed with no errors.
 157             EXIT_ERROR = 1, // Completed but reported errors.
 158             EXIT_CMDERR = 2, // Bad command-line arguments
 159             EXIT_SYSERR = 3, // System error or resource exhaustion.
 160             EXIT_ABNORMAL = 4;// terminated abnormally
 161 
 162     static class OptionsValues {
 163         boolean help;
 164         String  saveoptsfile;
 165         boolean version;
 166         boolean fullVersion;
 167         final List<Path> modulePath = new ArrayList<>();
 168         final Set<String> limitMods = new HashSet<>();
 169         final Set<String> addMods = new HashSet<>();
 170         Path output;
 171         Path packagedModulesPath;
 172         ByteOrder endian = ByteOrder.nativeOrder();
 173         boolean ignoreSigning = false;
 174     }
 175 
 176     int run(String[] args) {
 177         if (log == null) {
 178             setLog(new PrintWriter(System.out, true),
 179                    new PrintWriter(System.err, true));
 180         }
 181         try {
 182             optionsHelper.handleOptionsNoUnhandled(this, args);
 183             if (options.help) {
 184                 optionsHelper.showHelp(PROGNAME);
 185                 return EXIT_OK;
 186             }
 187             if (optionsHelper.shouldListPlugins()) {
 188                 optionsHelper.listPlugins();
 189                 return EXIT_OK;


 383     }
 384 
 385     private static ImageProvider createImageProvider(ModuleFinder finder,
 386                                                      Set<String> roots,
 387                                                      ByteOrder order,
 388                                                      Path retainModulesPath,
 389                                                      boolean ignoreSigning)
 390             throws IOException
 391     {
 392         if (roots.isEmpty()) {
 393             throw new IllegalArgumentException("empty modules and limitmods");
 394         }
 395 
 396         Configuration cf = Configuration.empty()
 397                 .resolveRequires(finder,
 398                                  ModuleFinder.of(),
 399                                  roots);
 400 
 401         Map<String, Path> mods = cf.modules().stream()
 402             .collect(Collectors.toMap(ResolvedModule::name, JlinkTask::toPathLocation));
 403         return new ImageHelper(cf, mods, order, retainModulesPath, ignoreSigning);
 404     }
 405 
 406     /*
 407      * Returns a ModuleFinder that limits observability to the given root
 408      * modules, their transitive dependences, plus a set of other modules.
 409      */
 410     private static ModuleFinder limitFinder(ModuleFinder finder,
 411                                             Set<String> roots,
 412                                             Set<String> otherMods) {
 413 
 414         // resolve all root modules
 415         Configuration cf = Configuration.empty()
 416                 .resolveRequires(finder,
 417                                  ModuleFinder.of(),
 418                                  roots);
 419 
 420         // module name -> reference
 421         Map<String, ModuleReference> map = new HashMap<>();
 422         cf.modules().forEach(m -> {
 423             ModuleReference mref = m.reference();


 470         for (String c : optionsHelper.getInputCommand()) {
 471             command.append(c).append(" ");
 472         }
 473         sb.append("command").append(" = ").append(command);
 474         sb.append("\n");
 475 
 476         return sb.toString();
 477     }
 478 
 479     private static String genBOMContent(JlinkConfiguration config,
 480             PluginsConfiguration plugins)
 481             throws IOException {
 482         StringBuilder sb = new StringBuilder();
 483         sb.append(getBomHeader());
 484         sb.append(config);
 485         sb.append(plugins);
 486         return sb.toString();
 487     }
 488 
 489     private static class ImageHelper implements ImageProvider {

 490         final ByteOrder order;
 491         final Path packagedModulesPath;
 492         final boolean ignoreSigning;
 493         final Set<Archive> archives;
 494 
 495         ImageHelper(Configuration cf,

 496                     Map<String, Path> modsPaths,
 497                     ByteOrder order,
 498                     Path packagedModulesPath,
 499                     boolean ignoreSigning) throws IOException {

 500             this.order = order;
 501             this.packagedModulesPath = packagedModulesPath;
 502             this.ignoreSigning = ignoreSigning;
 503             this.archives = modsPaths.entrySet().stream()
 504                                 .map(e -> newArchive(e.getKey(), e.getValue()))
 505                                 .collect(Collectors.toSet());
 506         }
 507 
 508         private Archive newArchive(String module, Path path) {
 509             if (path.toString().endsWith(".jmod")) {
 510                 return new JmodArchive(module, path);
 511             } else if (path.toString().endsWith(".jar")) {
 512                 ModularJarArchive modularJarArchive = new ModularJarArchive(module, path);
 513 
 514                 Stream<Archive.Entry> signatures = modularJarArchive.entries().filter((entry) -> {
 515                     String name = entry.name().toUpperCase(Locale.ENGLISH);
 516 
 517                     return name.startsWith("META-INF/") && name.indexOf('/', 9) == -1 && (
 518                                 name.endsWith(".SF") ||
 519                                 name.endsWith(".DSA") ||


 525 
 526                 if (signatures.count() != 0) {
 527                     if (ignoreSigning) {
 528                         System.err.println(taskHelper.getMessage("warn.signing", path));
 529                     } else {
 530                         throw new IllegalArgumentException(taskHelper.getMessage("err.signing", path));
 531                     }
 532                 }
 533 
 534                 return modularJarArchive;
 535             } else if (Files.isDirectory(path)) {
 536                 return new DirArchive(path);
 537             } else {
 538                 throw new IllegalArgumentException(
 539                     taskHelper.getMessage("err.not.modular.format", module, path));
 540             }
 541         }
 542 
 543         @Override
 544         public ExecutableImage retrieve(ImagePluginStack stack) throws IOException {
 545             ExecutableImage image = ImageFileCreator.create(archives, order, stack);
 546             if (packagedModulesPath != null) {
 547                 // copy the packaged modules to the given path
 548                 Files.createDirectories(packagedModulesPath);
 549                 for (Archive a : archives) {
 550                     Path file = a.getPath();
 551                     Path dest = packagedModulesPath.resolve(file.getFileName());
 552                     Files.copy(file, dest);
 553                 }
 554             }
 555             return image;
 556         }
 557     }
 558 }


 149         taskHelper.setLog(log);
 150     }
 151 
 152     /**
 153      * Result codes.
 154      */
 155     static final int
 156             EXIT_OK = 0, // Completed with no errors.
 157             EXIT_ERROR = 1, // Completed but reported errors.
 158             EXIT_CMDERR = 2, // Bad command-line arguments
 159             EXIT_SYSERR = 3, // System error or resource exhaustion.
 160             EXIT_ABNORMAL = 4;// terminated abnormally
 161 
 162     static class OptionsValues {
 163         boolean help;
 164         String  saveoptsfile;
 165         boolean version;
 166         boolean fullVersion;
 167         final List<Path> modulePath = new ArrayList<>();
 168         final Set<String> limitMods = new HashSet<>();
 169         final Set<String> addMods = new LinkedHashSet<>();
 170         Path output;
 171         Path packagedModulesPath;
 172         ByteOrder endian = ByteOrder.nativeOrder();
 173         boolean ignoreSigning = false;
 174     }
 175 
 176     int run(String[] args) {
 177         if (log == null) {
 178             setLog(new PrintWriter(System.out, true),
 179                    new PrintWriter(System.err, true));
 180         }
 181         try {
 182             optionsHelper.handleOptionsNoUnhandled(this, args);
 183             if (options.help) {
 184                 optionsHelper.showHelp(PROGNAME);
 185                 return EXIT_OK;
 186             }
 187             if (optionsHelper.shouldListPlugins()) {
 188                 optionsHelper.listPlugins();
 189                 return EXIT_OK;


 383     }
 384 
 385     private static ImageProvider createImageProvider(ModuleFinder finder,
 386                                                      Set<String> roots,
 387                                                      ByteOrder order,
 388                                                      Path retainModulesPath,
 389                                                      boolean ignoreSigning)
 390             throws IOException
 391     {
 392         if (roots.isEmpty()) {
 393             throw new IllegalArgumentException("empty modules and limitmods");
 394         }
 395 
 396         Configuration cf = Configuration.empty()
 397                 .resolveRequires(finder,
 398                                  ModuleFinder.of(),
 399                                  roots);
 400 
 401         Map<String, Path> mods = cf.modules().stream()
 402             .collect(Collectors.toMap(ResolvedModule::name, JlinkTask::toPathLocation));
 403         return new ImageHelper(cf, roots, mods, order, retainModulesPath, ignoreSigning);
 404     }
 405 
 406     /*
 407      * Returns a ModuleFinder that limits observability to the given root
 408      * modules, their transitive dependences, plus a set of other modules.
 409      */
 410     private static ModuleFinder limitFinder(ModuleFinder finder,
 411                                             Set<String> roots,
 412                                             Set<String> otherMods) {
 413 
 414         // resolve all root modules
 415         Configuration cf = Configuration.empty()
 416                 .resolveRequires(finder,
 417                                  ModuleFinder.of(),
 418                                  roots);
 419 
 420         // module name -> reference
 421         Map<String, ModuleReference> map = new HashMap<>();
 422         cf.modules().forEach(m -> {
 423             ModuleReference mref = m.reference();


 470         for (String c : optionsHelper.getInputCommand()) {
 471             command.append(c).append(" ");
 472         }
 473         sb.append("command").append(" = ").append(command);
 474         sb.append("\n");
 475 
 476         return sb.toString();
 477     }
 478 
 479     private static String genBOMContent(JlinkConfiguration config,
 480             PluginsConfiguration plugins)
 481             throws IOException {
 482         StringBuilder sb = new StringBuilder();
 483         sb.append(getBomHeader());
 484         sb.append(config);
 485         sb.append(plugins);
 486         return sb.toString();
 487     }
 488 
 489     private static class ImageHelper implements ImageProvider {
 490         final Set<String> rootModules;
 491         final ByteOrder order;
 492         final Path packagedModulesPath;
 493         final boolean ignoreSigning;
 494         final Set<Archive> archives;
 495 
 496         ImageHelper(Configuration cf,
 497                     Set<String> rootMods,
 498                     Map<String, Path> modsPaths,
 499                     ByteOrder order,
 500                     Path packagedModulesPath,
 501                     boolean ignoreSigning) throws IOException {
 502             this.rootModules = rootMods;
 503             this.order = order;
 504             this.packagedModulesPath = packagedModulesPath;
 505             this.ignoreSigning = ignoreSigning;
 506             this.archives = modsPaths.entrySet().stream()
 507                                 .map(e -> newArchive(e.getKey(), e.getValue()))
 508                                 .collect(Collectors.toSet());
 509         }
 510 
 511         private Archive newArchive(String module, Path path) {
 512             if (path.toString().endsWith(".jmod")) {
 513                 return new JmodArchive(module, path);
 514             } else if (path.toString().endsWith(".jar")) {
 515                 ModularJarArchive modularJarArchive = new ModularJarArchive(module, path);
 516 
 517                 Stream<Archive.Entry> signatures = modularJarArchive.entries().filter((entry) -> {
 518                     String name = entry.name().toUpperCase(Locale.ENGLISH);
 519 
 520                     return name.startsWith("META-INF/") && name.indexOf('/', 9) == -1 && (
 521                                 name.endsWith(".SF") ||
 522                                 name.endsWith(".DSA") ||


 528 
 529                 if (signatures.count() != 0) {
 530                     if (ignoreSigning) {
 531                         System.err.println(taskHelper.getMessage("warn.signing", path));
 532                     } else {
 533                         throw new IllegalArgumentException(taskHelper.getMessage("err.signing", path));
 534                     }
 535                 }
 536 
 537                 return modularJarArchive;
 538             } else if (Files.isDirectory(path)) {
 539                 return new DirArchive(path);
 540             } else {
 541                 throw new IllegalArgumentException(
 542                     taskHelper.getMessage("err.not.modular.format", module, path));
 543             }
 544         }
 545 
 546         @Override
 547         public ExecutableImage retrieve(ImagePluginStack stack) throws IOException {
 548             ExecutableImage image = ImageFileCreator.create(archives, rootModules, order, stack);
 549             if (packagedModulesPath != null) {
 550                 // copy the packaged modules to the given path
 551                 Files.createDirectories(packagedModulesPath);
 552                 for (Archive a : archives) {
 553                     Path file = a.getPath();
 554                     Path dest = packagedModulesPath.resolve(file.getFileName());
 555                     Files.copy(file, dest);
 556                 }
 557             }
 558             return image;
 559         }
 560     }
 561 }
< prev index next >