92 throw taskHelper.newBadArgs("err.mods.must.be.specified",
93 "--limit-modules");
94 }
95 task.options.limitMods.add(mn);
96 }
97 }, "--limit-modules"),
98 new Option<JlinkTask>(true, (task, opt, arg) -> {
99 for (String mn : arg.split(",")) {
100 if (mn.isEmpty()) {
101 throw taskHelper.newBadArgs("err.mods.must.be.specified",
102 "--add-modules");
103 }
104 task.options.addMods.add(mn);
105 }
106 }, "--add-modules"),
107 new Option<JlinkTask>(true, (task, opt, arg) -> {
108 Path path = Paths.get(arg);
109 task.options.output = path;
110 }, "--output"),
111 new Option<JlinkTask>(true, (task, opt, arg) -> {
112 if ("little".equals(arg)) {
113 task.options.endian = ByteOrder.LITTLE_ENDIAN;
114 } else if ("big".equals(arg)) {
115 task.options.endian = ByteOrder.BIG_ENDIAN;
116 } else {
117 throw taskHelper.newBadArgs("err.unknown.byte.order", arg);
118 }
119 }, "--endian"),
120 new Option<JlinkTask>(false, (task, opt, arg) -> {
121 task.options.version = true;
122 }, "--version"),
123 new Option<JlinkTask>(true, (task, opt, arg) -> {
124 Path path = Paths.get(arg);
125 if (Files.exists(path)) {
126 throw taskHelper.newBadArgs("err.dir.exists", path);
127 }
128 task.options.packagedModulesPath = path;
129 }, true, "--keep-packaged-modules"),
130 new Option<JlinkTask>(true, (task, opt, arg) -> {
131 task.options.saveoptsfile = arg;
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;
190 }
267
268 //Ask the stack to proceed;
269 stack.operate(imageProvider);
270 }
271
272 /*
273 * Jlink API entry point.
274 */
275 public static void postProcessImage(ExecutableImage image, List<Plugin> postProcessorPlugins)
276 throws Exception {
277 Objects.requireNonNull(image);
278 Objects.requireNonNull(postProcessorPlugins);
279 PluginsConfiguration config = new PluginsConfiguration(postProcessorPlugins);
280 ImagePluginStack stack = ImagePluginConfiguration.
281 parseConfiguration(config);
282
283 stack.operate((ImagePluginStack stack1) -> image);
284 }
285
286 private void postProcessOnly(Path existingImage) throws Exception {
287 PluginsConfiguration config = taskHelper.getPluginsConfig(null);
288 ExecutableImage img = DefaultImageBuilder.getExecutableImage(existingImage);
289 if (img == null) {
290 throw taskHelper.newBadArgs("err.existing.image.invalid");
291 }
292 postProcessImage(img, config.getPlugins());
293 }
294
295 // the token for "all modules on the module path"
296 private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
297 private void createImage() throws Exception {
298 if (options.output == null) {
299 throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true);
300 }
301
302 if (options.addMods.isEmpty()) {
303 throw taskHelper.newBadArgs("err.mods.must.be.specified", "--add-modules")
304 .showUsage(true);
305 }
306
307 Set<String> roots = new HashSet<>();
315 .forEach(mn -> roots.add(mn));
316 } else {
317 roots.add(mod);
318 }
319 }
320
321 ModuleFinder finder = newModuleFinder(options.modulePath,
322 options.limitMods,
323 roots);
324
325
326 // First create the image provider
327 ImageProvider imageProvider = createImageProvider(finder,
328 roots,
329 options.endian,
330 options.packagedModulesPath,
331 options.ignoreSigning);
332
333 // Then create the Plugin Stack
334 ImagePluginStack stack = ImagePluginConfiguration.
335 parseConfiguration(taskHelper.getPluginsConfig(options.output));
336
337 //Ask the stack to proceed
338 stack.operate(imageProvider);
339 }
340
341 /**
342 * Returns a module finder to find the observable modules specified in
343 * the --module-path and --limit-modules options
344 */
345 private ModuleFinder modulePathFinder() {
346 Path[] entries = options.modulePath.toArray(new Path[0]);
347 ModuleFinder finder = SharedSecrets.getJavaLangModuleAccess()
348 .newModulePath(Runtime.version(), true, entries);
349
350 if (!options.limitMods.isEmpty()) {
351 finder = limitFinder(finder, options.limitMods, Collections.emptySet());
352 }
353 return finder;
354 }
355
|
92 throw taskHelper.newBadArgs("err.mods.must.be.specified",
93 "--limit-modules");
94 }
95 task.options.limitMods.add(mn);
96 }
97 }, "--limit-modules"),
98 new Option<JlinkTask>(true, (task, opt, arg) -> {
99 for (String mn : arg.split(",")) {
100 if (mn.isEmpty()) {
101 throw taskHelper.newBadArgs("err.mods.must.be.specified",
102 "--add-modules");
103 }
104 task.options.addMods.add(mn);
105 }
106 }, "--add-modules"),
107 new Option<JlinkTask>(true, (task, opt, arg) -> {
108 Path path = Paths.get(arg);
109 task.options.output = path;
110 }, "--output"),
111 new Option<JlinkTask>(true, (task, opt, arg) -> {
112 String[] values = arg.split("=");
113 // check values
114 if (values.length != 2 || values[0].isEmpty() || values[1].isEmpty()) {
115 throw taskHelper.newBadArgs("err.launcher.value.format", arg);
116 } else {
117 String commandName = values[0];
118 String moduleAndMain = values[1];
119 int idx = moduleAndMain.indexOf("/");
120 if (idx != -1) {
121 if (moduleAndMain.substring(0, idx).isEmpty()) {
122 throw taskHelper.newBadArgs("err.launcher.module.name.empty", arg);
123 }
124
125 if (moduleAndMain.substring(idx + 1).isEmpty()) {
126 throw taskHelper.newBadArgs("err.launcher.main.class.empty", arg);
127 }
128 }
129 task.options.launchers.put(commandName, moduleAndMain);
130 }
131 }, "--launcher"),
132 new Option<JlinkTask>(true, (task, opt, arg) -> {
133 if ("little".equals(arg)) {
134 task.options.endian = ByteOrder.LITTLE_ENDIAN;
135 } else if ("big".equals(arg)) {
136 task.options.endian = ByteOrder.BIG_ENDIAN;
137 } else {
138 throw taskHelper.newBadArgs("err.unknown.byte.order", arg);
139 }
140 }, "--endian"),
141 new Option<JlinkTask>(false, (task, opt, arg) -> {
142 task.options.version = true;
143 }, "--version"),
144 new Option<JlinkTask>(true, (task, opt, arg) -> {
145 Path path = Paths.get(arg);
146 if (Files.exists(path)) {
147 throw taskHelper.newBadArgs("err.dir.exists", path);
148 }
149 task.options.packagedModulesPath = path;
150 }, true, "--keep-packaged-modules"),
151 new Option<JlinkTask>(true, (task, opt, arg) -> {
152 task.options.saveoptsfile = arg;
172
173 /**
174 * Result codes.
175 */
176 static final int
177 EXIT_OK = 0, // Completed with no errors.
178 EXIT_ERROR = 1, // Completed but reported errors.
179 EXIT_CMDERR = 2, // Bad command-line arguments
180 EXIT_SYSERR = 3, // System error or resource exhaustion.
181 EXIT_ABNORMAL = 4;// terminated abnormally
182
183 static class OptionsValues {
184 boolean help;
185 String saveoptsfile;
186 boolean version;
187 boolean fullVersion;
188 final List<Path> modulePath = new ArrayList<>();
189 final Set<String> limitMods = new HashSet<>();
190 final Set<String> addMods = new HashSet<>();
191 Path output;
192 final Map<String, String> launchers = new HashMap<>();
193 Path packagedModulesPath;
194 ByteOrder endian = ByteOrder.nativeOrder();
195 boolean ignoreSigning = false;
196 }
197
198 int run(String[] args) {
199 if (log == null) {
200 setLog(new PrintWriter(System.out, true),
201 new PrintWriter(System.err, true));
202 }
203 try {
204 optionsHelper.handleOptionsNoUnhandled(this, args);
205 if (options.help) {
206 optionsHelper.showHelp(PROGNAME);
207 return EXIT_OK;
208 }
209 if (optionsHelper.shouldListPlugins()) {
210 optionsHelper.listPlugins();
211 return EXIT_OK;
212 }
289
290 //Ask the stack to proceed;
291 stack.operate(imageProvider);
292 }
293
294 /*
295 * Jlink API entry point.
296 */
297 public static void postProcessImage(ExecutableImage image, List<Plugin> postProcessorPlugins)
298 throws Exception {
299 Objects.requireNonNull(image);
300 Objects.requireNonNull(postProcessorPlugins);
301 PluginsConfiguration config = new PluginsConfiguration(postProcessorPlugins);
302 ImagePluginStack stack = ImagePluginConfiguration.
303 parseConfiguration(config);
304
305 stack.operate((ImagePluginStack stack1) -> image);
306 }
307
308 private void postProcessOnly(Path existingImage) throws Exception {
309 PluginsConfiguration config = taskHelper.getPluginsConfig(null, null);
310 ExecutableImage img = DefaultImageBuilder.getExecutableImage(existingImage);
311 if (img == null) {
312 throw taskHelper.newBadArgs("err.existing.image.invalid");
313 }
314 postProcessImage(img, config.getPlugins());
315 }
316
317 // the token for "all modules on the module path"
318 private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
319 private void createImage() throws Exception {
320 if (options.output == null) {
321 throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true);
322 }
323
324 if (options.addMods.isEmpty()) {
325 throw taskHelper.newBadArgs("err.mods.must.be.specified", "--add-modules")
326 .showUsage(true);
327 }
328
329 Set<String> roots = new HashSet<>();
337 .forEach(mn -> roots.add(mn));
338 } else {
339 roots.add(mod);
340 }
341 }
342
343 ModuleFinder finder = newModuleFinder(options.modulePath,
344 options.limitMods,
345 roots);
346
347
348 // First create the image provider
349 ImageProvider imageProvider = createImageProvider(finder,
350 roots,
351 options.endian,
352 options.packagedModulesPath,
353 options.ignoreSigning);
354
355 // Then create the Plugin Stack
356 ImagePluginStack stack = ImagePluginConfiguration.
357 parseConfiguration(taskHelper.getPluginsConfig(options.output, options.launchers));
358
359 //Ask the stack to proceed
360 stack.operate(imageProvider);
361 }
362
363 /**
364 * Returns a module finder to find the observable modules specified in
365 * the --module-path and --limit-modules options
366 */
367 private ModuleFinder modulePathFinder() {
368 Path[] entries = options.modulePath.toArray(new Path[0]);
369 ModuleFinder finder = SharedSecrets.getJavaLangModuleAccess()
370 .newModulePath(Runtime.version(), true, entries);
371
372 if (!options.limitMods.isEmpty()) {
373 finder = limitFinder(finder, options.limitMods, Collections.emptySet());
374 }
375 return finder;
376 }
377
|