< prev index next >

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

Print this page




 125         }, "--output"),
 126         new Option<JlinkTask>(true, (task, opt, arg) -> {
 127             if ("little".equals(arg)) {
 128                 task.options.endian = ByteOrder.LITTLE_ENDIAN;
 129             } else if ("big".equals(arg)) {
 130                 task.options.endian = ByteOrder.BIG_ENDIAN;
 131             } else {
 132                 throw taskHelper.newBadArgs("err.unknown.byte.order", arg);
 133             }
 134         }, "--endian"),
 135         new Option<JlinkTask>(false, (task, opt, arg) -> {
 136             task.options.version = true;
 137         }, "--version"),
 138         new Option<JlinkTask>(true, (task, opt, arg) -> {
 139             Path path = Paths.get(arg);
 140             if (Files.exists(path)) {
 141                 throw taskHelper.newBadArgs("err.dir.exists", path);
 142             }
 143             task.options.packagedModulesPath = path;
 144         }, true, "--keep-packaged-modules"),
 145         new Option<JlinkTask>(false, (task, opt, arg) -> {
 146             task.options.genbom = true;
 147         }, true, "--genbom"),
 148         new Option<JlinkTask>(true, (task, opt, arg) -> {
 149             task.options.saveoptsfile = arg;
 150         }, "--saveopts"),
 151         new Option<JlinkTask>(false, (task, opt, arg) -> {
 152             task.options.fullVersion = true;
 153         }, true, "--fullversion"),};
 154 
 155     private static final String PROGNAME = "jlink";
 156     private final OptionsValues options = new OptionsValues();
 157 
 158     private static final OptionsHelper<JlinkTask> optionsHelper
 159             = taskHelper.newOptionsHelper(JlinkTask.class, recognizedOptions);
 160     private PrintWriter log;
 161 
 162     void setLog(PrintWriter out) {
 163         log = out;
 164         taskHelper.setLog(log);
 165     }
 166 
 167     /**
 168      * Result codes.
 169      */
 170     static final int EXIT_OK = 0, // Completed with no errors.
 171             EXIT_ERROR = 1, // Completed but reported errors.
 172             EXIT_CMDERR = 2, // Bad command-line arguments
 173             EXIT_SYSERR = 3, // System error or resource exhaustion.
 174             EXIT_ABNORMAL = 4;// terminated abnormally
 175 
 176     static class OptionsValues {
 177         boolean help;
 178         boolean genbom;
 179         String  saveoptsfile;
 180         boolean version;
 181         boolean fullVersion;
 182         Path[] modulePath;
 183         Set<String> limitMods = new HashSet<>();
 184         Set<String> addMods = new HashSet<>();
 185         Path output;
 186         Path packagedModulesPath;
 187         ByteOrder endian = ByteOrder.nativeOrder();
 188     }
 189 
 190     int run(String[] args) {
 191         if (log == null) {
 192             setLog(new PrintWriter(System.err));
 193         }
 194         try {
 195             optionsHelper.handleOptions(this, args);
 196             if (options.help) {
 197                 optionsHelper.showHelp(PROGNAME);
 198                 return EXIT_OK;


 258         Objects.requireNonNull(config.getOutput());
 259         plugins = plugins == null ? new PluginsConfiguration() : plugins;
 260 
 261         if (config.getModulepaths().isEmpty()) {
 262             throw new Exception("Empty module paths");
 263         }
 264         Path[] arr = new Path[config.getModulepaths().size()];
 265         arr = config.getModulepaths().toArray(arr);
 266         ModuleFinder finder
 267                 = newModuleFinder(arr, config.getLimitmods(), config.getModules());
 268 
 269         // First create the image provider
 270         ImageProvider imageProvider
 271                 = createImageProvider(finder,
 272                                       checkAddMods(config.getModules()),
 273                                       config.getLimitmods(),
 274                                       config.getByteOrder(),
 275                                       null);
 276 
 277         // Then create the Plugin Stack
 278         ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(plugins,
 279                 genBOMContent(config, plugins));
 280 
 281         //Ask the stack to proceed;
 282         stack.operate(imageProvider);
 283     }
 284 
 285     /*
 286      * Jlink API entry point.
 287      */
 288     public static void postProcessImage(ExecutableImage image, List<Plugin> postProcessorPlugins)
 289             throws Exception {
 290         Objects.requireNonNull(image);
 291         Objects.requireNonNull(postProcessorPlugins);
 292         PluginsConfiguration config = new PluginsConfiguration(postProcessorPlugins);
 293         ImagePluginStack stack = ImagePluginConfiguration.
 294                 parseConfiguration(config);
 295 
 296         stack.operate((ImagePluginStack stack1) -> image);
 297     }
 298 
 299     private void postProcessOnly(Path existingImage) throws Exception {
 300         PluginsConfiguration config = taskHelper.getPluginsConfig(null, false);
 301         ExecutableImage img = DefaultImageBuilder.getExecutableImage(existingImage);
 302         if (img == null) {
 303             throw taskHelper.newBadArgs("err.existing.image.invalid");
 304         }
 305         postProcessImage(img, config.getPlugins());
 306     }
 307 
 308     private void createImage() throws Exception {
 309         if (options.output == null) {
 310             throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true);
 311         }
 312         ModuleFinder finder
 313                 = newModuleFinder(options.modulePath, options.limitMods, options.addMods);
 314         try {
 315             options.addMods = checkAddMods(options.addMods);
 316         } catch (IllegalArgumentException ex) {
 317             throw taskHelper.newBadArgs("err.mods.must.be.specified", "--addmods")
 318                     .showUsage(true);
 319         }
 320         // First create the image provider
 321         ImageProvider imageProvider
 322                 = createImageProvider(finder,
 323                         options.addMods,
 324                         options.limitMods,
 325                         options.endian,
 326                         options.packagedModulesPath);
 327 
 328         // Then create the Plugin Stack
 329         ImagePluginStack stack = ImagePluginConfiguration.
 330                 parseConfiguration(taskHelper.getPluginsConfig(options.output, options.genbom),
 331                         genBOMContent());
 332 
 333         //Ask the stack to proceed
 334         stack.operate(imageProvider);
 335     }
 336 
 337     private static Set<String> checkAddMods(Set<String> addMods) {
 338         if (addMods.isEmpty()) {
 339             throw new IllegalArgumentException("no modules to add");
 340         }
 341         return addMods;
 342     }
 343 
 344     private static ModuleFinder newModuleFinder(Path[] paths,
 345             Set<String> limitMods,
 346             Set<String> addMods) {
 347         ModuleFinder finder = ModuleFinder.of(paths);
 348 
 349         // jmods are located at link-time
 350         if (finder instanceof ConfigurableModuleFinder) {
 351             ((ConfigurableModuleFinder) finder).configurePhase(Phase.LINK_TIME);




 125         }, "--output"),
 126         new Option<JlinkTask>(true, (task, opt, arg) -> {
 127             if ("little".equals(arg)) {
 128                 task.options.endian = ByteOrder.LITTLE_ENDIAN;
 129             } else if ("big".equals(arg)) {
 130                 task.options.endian = ByteOrder.BIG_ENDIAN;
 131             } else {
 132                 throw taskHelper.newBadArgs("err.unknown.byte.order", arg);
 133             }
 134         }, "--endian"),
 135         new Option<JlinkTask>(false, (task, opt, arg) -> {
 136             task.options.version = true;
 137         }, "--version"),
 138         new Option<JlinkTask>(true, (task, opt, arg) -> {
 139             Path path = Paths.get(arg);
 140             if (Files.exists(path)) {
 141                 throw taskHelper.newBadArgs("err.dir.exists", path);
 142             }
 143             task.options.packagedModulesPath = path;
 144         }, true, "--keep-packaged-modules"),



 145         new Option<JlinkTask>(true, (task, opt, arg) -> {
 146             task.options.saveoptsfile = arg;
 147         }, "--saveopts"),
 148         new Option<JlinkTask>(false, (task, opt, arg) -> {
 149             task.options.fullVersion = true;
 150         }, true, "--fullversion"),};
 151 
 152     private static final String PROGNAME = "jlink";
 153     private final OptionsValues options = new OptionsValues();
 154 
 155     private static final OptionsHelper<JlinkTask> optionsHelper
 156             = taskHelper.newOptionsHelper(JlinkTask.class, recognizedOptions);
 157     private PrintWriter log;
 158 
 159     void setLog(PrintWriter out) {
 160         log = out;
 161         taskHelper.setLog(log);
 162     }
 163 
 164     /**
 165      * Result codes.
 166      */
 167     static final int EXIT_OK = 0, // Completed with no errors.
 168             EXIT_ERROR = 1, // Completed but reported errors.
 169             EXIT_CMDERR = 2, // Bad command-line arguments
 170             EXIT_SYSERR = 3, // System error or resource exhaustion.
 171             EXIT_ABNORMAL = 4;// terminated abnormally
 172 
 173     static class OptionsValues {
 174         boolean help;

 175         String  saveoptsfile;
 176         boolean version;
 177         boolean fullVersion;
 178         Path[] modulePath;
 179         Set<String> limitMods = new HashSet<>();
 180         Set<String> addMods = new HashSet<>();
 181         Path output;
 182         Path packagedModulesPath;
 183         ByteOrder endian = ByteOrder.nativeOrder();
 184     }
 185 
 186     int run(String[] args) {
 187         if (log == null) {
 188             setLog(new PrintWriter(System.err));
 189         }
 190         try {
 191             optionsHelper.handleOptions(this, args);
 192             if (options.help) {
 193                 optionsHelper.showHelp(PROGNAME);
 194                 return EXIT_OK;


 254         Objects.requireNonNull(config.getOutput());
 255         plugins = plugins == null ? new PluginsConfiguration() : plugins;
 256 
 257         if (config.getModulepaths().isEmpty()) {
 258             throw new Exception("Empty module paths");
 259         }
 260         Path[] arr = new Path[config.getModulepaths().size()];
 261         arr = config.getModulepaths().toArray(arr);
 262         ModuleFinder finder
 263                 = newModuleFinder(arr, config.getLimitmods(), config.getModules());
 264 
 265         // First create the image provider
 266         ImageProvider imageProvider
 267                 = createImageProvider(finder,
 268                                       checkAddMods(config.getModules()),
 269                                       config.getLimitmods(),
 270                                       config.getByteOrder(),
 271                                       null);
 272 
 273         // Then create the Plugin Stack
 274         ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(plugins);

 275 
 276         //Ask the stack to proceed;
 277         stack.operate(imageProvider);
 278     }
 279 
 280     /*
 281      * Jlink API entry point.
 282      */
 283     public static void postProcessImage(ExecutableImage image, List<Plugin> postProcessorPlugins)
 284             throws Exception {
 285         Objects.requireNonNull(image);
 286         Objects.requireNonNull(postProcessorPlugins);
 287         PluginsConfiguration config = new PluginsConfiguration(postProcessorPlugins);
 288         ImagePluginStack stack = ImagePluginConfiguration.
 289                 parseConfiguration(config);
 290 
 291         stack.operate((ImagePluginStack stack1) -> image);
 292     }
 293 
 294     private void postProcessOnly(Path existingImage) throws Exception {
 295         PluginsConfiguration config = taskHelper.getPluginsConfig(null);
 296         ExecutableImage img = DefaultImageBuilder.getExecutableImage(existingImage);
 297         if (img == null) {
 298             throw taskHelper.newBadArgs("err.existing.image.invalid");
 299         }
 300         postProcessImage(img, config.getPlugins());
 301     }
 302 
 303     private void createImage() throws Exception {
 304         if (options.output == null) {
 305             throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true);
 306         }
 307         ModuleFinder finder
 308                 = newModuleFinder(options.modulePath, options.limitMods, options.addMods);
 309         try {
 310             options.addMods = checkAddMods(options.addMods);
 311         } catch (IllegalArgumentException ex) {
 312             throw taskHelper.newBadArgs("err.mods.must.be.specified", "--addmods")
 313                     .showUsage(true);
 314         }
 315         // First create the image provider
 316         ImageProvider imageProvider
 317                 = createImageProvider(finder,
 318                         options.addMods,
 319                         options.limitMods,
 320                         options.endian,
 321                         options.packagedModulesPath);
 322 
 323         // Then create the Plugin Stack
 324         ImagePluginStack stack = ImagePluginConfiguration.
 325                 parseConfiguration(taskHelper.getPluginsConfig(options.output));

 326 
 327         //Ask the stack to proceed
 328         stack.operate(imageProvider);
 329     }
 330 
 331     private static Set<String> checkAddMods(Set<String> addMods) {
 332         if (addMods.isEmpty()) {
 333             throw new IllegalArgumentException("no modules to add");
 334         }
 335         return addMods;
 336     }
 337 
 338     private static ModuleFinder newModuleFinder(Path[] paths,
 339             Set<String> limitMods,
 340             Set<String> addMods) {
 341         ModuleFinder finder = ModuleFinder.of(paths);
 342 
 343         // jmods are located at link-time
 344         if (finder instanceof ConfigurableModuleFinder) {
 345             ((ConfigurableModuleFinder) finder).configurePhase(Phase.LINK_TIME);


< prev index next >