53 import jdk.internal.module.ConfigurableModuleFinder;
54 import jdk.internal.module.ConfigurableModuleFinder.Phase;
55 import jdk.tools.jlink.internal.TaskHelper.BadArgs;
56 import static jdk.tools.jlink.internal.TaskHelper.JLINK_BUNDLE;
57 import jdk.tools.jlink.internal.TaskHelper.Option;
58 import jdk.tools.jlink.internal.TaskHelper.OptionsHelper;
59 import jdk.tools.jlink.internal.ImagePluginStack.ImageProvider;
60 import jdk.tools.jlink.plugin.ExecutableImage;
61 import jdk.tools.jlink.Jlink.JlinkConfiguration;
62 import jdk.tools.jlink.Jlink.PluginsConfiguration;
63 import jdk.tools.jlink.plugin.PluginException;
64 import jdk.tools.jlink.builder.DefaultImageBuilder;
65 import jdk.tools.jlink.plugin.Plugin;
66
67 /**
68 * Implementation for the jlink tool.
69 *
70 * ## Should use jdk.joptsimple some day.
71 */
72 public class JlinkTask {
73
74 private static <T extends Throwable> void fail(Class<T> type,
75 String format,
76 Object... args) throws T {
77 String msg = new Formatter().format(format, args).toString();
78 try {
79 T t = type.getConstructor(String.class).newInstance(msg);
80 throw t;
81 } catch (InstantiationException |
82 InvocationTargetException |
83 NoSuchMethodException |
84 IllegalAccessException e) {
85 throw new InternalError("Unable to create an instance of " + type, e);
86 }
87 }
88
89 private static final TaskHelper taskHelper
90 = new TaskHelper(JLINK_BUNDLE);
91
92 static Option<?>[] recognizedOptions = {
198 return EXIT_OK;
199 }
200 if (options.version || options.fullVersion) {
201 taskHelper.showVersion(options.fullVersion);
202 return EXIT_OK;
203 }
204 if (taskHelper.getExistingImage() == null) {
205 if (options.modulePath == null || options.modulePath.length == 0) {
206 throw taskHelper.newBadArgs("err.modulepath.must.be.specified").showUsage(true);
207 }
208 createImage();
209 } else {
210 postProcessOnly(taskHelper.getExistingImage());
211 }
212
213 if (options.saveoptsfile != null) {
214 Files.write(Paths.get(options.saveoptsfile), getSaveOpts().getBytes());
215 }
216
217 return EXIT_OK;
218 } catch (UncheckedIOException | PluginException | IOException | ResolutionException e) {
219 log.println(taskHelper.getMessage("error.prefix") + " " + e.getMessage());
220 log.println(taskHelper.getMessage("main.usage.summary", PROGNAME));
221 return EXIT_ERROR;
222 } catch (BadArgs e) {
223 taskHelper.reportError(e.key, e.args);
224 if (e.showUsage) {
225 log.println(taskHelper.getMessage("main.usage.summary", PROGNAME));
226 }
227 return EXIT_CMDERR;
228 } catch (Throwable x) {
229 log.println(taskHelper.getMessage("main.msg.bug"));
230 x.printStackTrace(log);
231 return EXIT_ABNORMAL;
232 } finally {
233 log.flush();
234 }
235 }
236
237 private static Map<String, Path> modulesToPath(Configuration cf) {
238 Map<String, Path> modPaths = new HashMap<>();
239 for (ResolvedModule resolvedModule : cf.modules()) {
240 ModuleReference mref = resolvedModule.reference();
241 URI uri = mref.location().get();
242 modPaths.put(mref.descriptor().name(), Paths.get(uri));
243 }
244 return modPaths;
245 }
246
247 /*
248 * Jlink API entry point.
249 */
|
53 import jdk.internal.module.ConfigurableModuleFinder;
54 import jdk.internal.module.ConfigurableModuleFinder.Phase;
55 import jdk.tools.jlink.internal.TaskHelper.BadArgs;
56 import static jdk.tools.jlink.internal.TaskHelper.JLINK_BUNDLE;
57 import jdk.tools.jlink.internal.TaskHelper.Option;
58 import jdk.tools.jlink.internal.TaskHelper.OptionsHelper;
59 import jdk.tools.jlink.internal.ImagePluginStack.ImageProvider;
60 import jdk.tools.jlink.plugin.ExecutableImage;
61 import jdk.tools.jlink.Jlink.JlinkConfiguration;
62 import jdk.tools.jlink.Jlink.PluginsConfiguration;
63 import jdk.tools.jlink.plugin.PluginException;
64 import jdk.tools.jlink.builder.DefaultImageBuilder;
65 import jdk.tools.jlink.plugin.Plugin;
66
67 /**
68 * Implementation for the jlink tool.
69 *
70 * ## Should use jdk.joptsimple some day.
71 */
72 public class JlinkTask {
73 private static final boolean DEBUG = Boolean.getBoolean("jlink.debug");
74
75 private static <T extends Throwable> void fail(Class<T> type,
76 String format,
77 Object... args) throws T {
78 String msg = new Formatter().format(format, args).toString();
79 try {
80 T t = type.getConstructor(String.class).newInstance(msg);
81 throw t;
82 } catch (InstantiationException |
83 InvocationTargetException |
84 NoSuchMethodException |
85 IllegalAccessException e) {
86 throw new InternalError("Unable to create an instance of " + type, e);
87 }
88 }
89
90 private static final TaskHelper taskHelper
91 = new TaskHelper(JLINK_BUNDLE);
92
93 static Option<?>[] recognizedOptions = {
199 return EXIT_OK;
200 }
201 if (options.version || options.fullVersion) {
202 taskHelper.showVersion(options.fullVersion);
203 return EXIT_OK;
204 }
205 if (taskHelper.getExistingImage() == null) {
206 if (options.modulePath == null || options.modulePath.length == 0) {
207 throw taskHelper.newBadArgs("err.modulepath.must.be.specified").showUsage(true);
208 }
209 createImage();
210 } else {
211 postProcessOnly(taskHelper.getExistingImage());
212 }
213
214 if (options.saveoptsfile != null) {
215 Files.write(Paths.get(options.saveoptsfile), getSaveOpts().getBytes());
216 }
217
218 return EXIT_OK;
219 } catch (UncheckedIOException | PluginException | IllegalArgumentException |
220 IOException | ResolutionException e) {
221 log.println(taskHelper.getMessage("error.prefix") + " " + e.getMessage());
222 if (DEBUG) {
223 e.printStackTrace(log);
224 }
225 return EXIT_ERROR;
226 } catch (BadArgs e) {
227 taskHelper.reportError(e.key, e.args);
228 if (e.showUsage) {
229 log.println(taskHelper.getMessage("main.usage.summary", PROGNAME));
230 }
231 if (DEBUG) {
232 e.printStackTrace(log);
233 }
234 return EXIT_CMDERR;
235 } catch (Throwable x) {
236 log.println(taskHelper.getMessage("error.prefix") + " " + x.getMessage());
237 x.printStackTrace(log);
238 return EXIT_ABNORMAL;
239 } finally {
240 log.flush();
241 }
242 }
243
244 private static Map<String, Path> modulesToPath(Configuration cf) {
245 Map<String, Path> modPaths = new HashMap<>();
246 for (ResolvedModule resolvedModule : cf.modules()) {
247 ModuleReference mref = resolvedModule.reference();
248 URI uri = mref.location().get();
249 modPaths.put(mref.descriptor().name(), Paths.get(uri));
250 }
251 return modPaths;
252 }
253
254 /*
255 * Jlink API entry point.
256 */
|