67 String format,
68 Object... args) throws T {
69 String msg = new Formatter().format(format, args).toString();
70 try {
71 T t = type.getConstructor(String.class).newInstance(msg);
72 throw t;
73 } catch (InstantiationException |
74 InvocationTargetException |
75 NoSuchMethodException |
76 IllegalAccessException e) {
77 throw new InternalError("Unable to create an instance of " + type, e);
78 }
79 }
80
81 private static final TaskHelper taskHelper
82 = new TaskHelper(JLINK_BUNDLE);
83
84 private static final Option<?>[] recognizedOptions = {
85 new Option<JlinkTask>(false, (task, opt, arg) -> {
86 task.options.help = true;
87 }, "--help"),
88 new Option<JlinkTask>(true, (task, opt, arg) -> {
89 String[] dirs = arg.split(File.pathSeparator);
90 Arrays.stream(dirs)
91 .map(Paths::get)
92 .forEach(task.options.modulePath::add);
93 }, "--modulepath", "--mp"),
94 new Option<JlinkTask>(true, (task, opt, arg) -> {
95 for (String mn : arg.split(",")) {
96 if (mn.isEmpty()) {
97 throw taskHelper.newBadArgs("err.mods.must.be.specified",
98 "--limitmods");
99 }
100 task.options.limitMods.add(mn);
101 }
102 }, "--limitmods"),
103 new Option<JlinkTask>(true, (task, opt, arg) -> {
104 for (String mn : arg.split(",")) {
105 if (mn.isEmpty()) {
106 throw taskHelper.newBadArgs("err.mods.must.be.specified",
107 "--addmods");
108 }
109 task.options.addMods.add(mn);
110 }
111 }, "--addmods"),
112 new Option<JlinkTask>(true, (task, opt, arg) -> {
113 Path path = Paths.get(arg);
114 task.options.output = path;
115 }, "--output"),
116 new Option<JlinkTask>(true, (task, opt, arg) -> {
117 if ("little".equals(arg)) {
118 task.options.endian = ByteOrder.LITTLE_ENDIAN;
119 } else if ("big".equals(arg)) {
120 task.options.endian = ByteOrder.BIG_ENDIAN;
121 } else {
122 throw taskHelper.newBadArgs("err.unknown.byte.order", arg);
123 }
124 }, "--endian"),
125 new Option<JlinkTask>(false, (task, opt, arg) -> {
126 task.options.version = true;
127 }, "--version"),
128 new Option<JlinkTask>(true, (task, opt, arg) -> {
129 Path path = Paths.get(arg);
130 if (Files.exists(path)) {
131 throw taskHelper.newBadArgs("err.dir.exists", path);
132 }
133 task.options.packagedModulesPath = path;
134 }, true, "--keep-packaged-modules"),
135 new Option<JlinkTask>(true, (task, opt, arg) -> {
136 task.options.saveoptsfile = arg;
137 }, "--saveopts"),
138 new Option<JlinkTask>(false, (task, opt, arg) -> {
139 task.options.fullVersion = true;
140 }, true, "--fullversion"),};
141
142 private static final String PROGNAME = "jlink";
143 private final OptionsValues options = new OptionsValues();
144
145 private static final OptionsHelper<JlinkTask> optionsHelper
146 = taskHelper.newOptionsHelper(JlinkTask.class, recognizedOptions);
147 private PrintWriter log;
148
149 void setLog(PrintWriter out) {
150 log = out;
151 taskHelper.setLog(log);
152 }
153
154 /**
155 * Result codes.
156 */
157 static final int EXIT_OK = 0, // Completed with no errors.
158 EXIT_ERROR = 1, // Completed but reported errors.
159 EXIT_CMDERR = 2, // Bad command-line arguments
160 EXIT_SYSERR = 3, // System error or resource exhaustion.
277 }
278
279 private void postProcessOnly(Path existingImage) throws Exception {
280 PluginsConfiguration config = taskHelper.getPluginsConfig(null);
281 ExecutableImage img = DefaultImageBuilder.getExecutableImage(existingImage);
282 if (img == null) {
283 throw taskHelper.newBadArgs("err.existing.image.invalid");
284 }
285 postProcessImage(img, config.getPlugins());
286 }
287
288 private void createImage() throws Exception {
289 if (options.output == null) {
290 throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true);
291 }
292 ModuleFinder finder
293 = newModuleFinder(options.modulePath, options.limitMods, options.addMods);
294 try {
295 options.addMods = checkAddMods(options.addMods);
296 } catch (IllegalArgumentException ex) {
297 throw taskHelper.newBadArgs("err.mods.must.be.specified", "--addmods")
298 .showUsage(true);
299 }
300 // First create the image provider
301 ImageProvider imageProvider
302 = createImageProvider(finder,
303 options.addMods,
304 options.limitMods,
305 options.endian,
306 options.packagedModulesPath);
307
308 // Then create the Plugin Stack
309 ImagePluginStack stack = ImagePluginConfiguration.
310 parseConfiguration(taskHelper.getPluginsConfig(options.output));
311
312 //Ask the stack to proceed
313 stack.operate(imageProvider);
314 }
315
316 private static Set<String> checkAddMods(Set<String> addMods) {
317 if (addMods.isEmpty()) {
|
67 String format,
68 Object... args) throws T {
69 String msg = new Formatter().format(format, args).toString();
70 try {
71 T t = type.getConstructor(String.class).newInstance(msg);
72 throw t;
73 } catch (InstantiationException |
74 InvocationTargetException |
75 NoSuchMethodException |
76 IllegalAccessException e) {
77 throw new InternalError("Unable to create an instance of " + type, e);
78 }
79 }
80
81 private static final TaskHelper taskHelper
82 = new TaskHelper(JLINK_BUNDLE);
83
84 private static final Option<?>[] recognizedOptions = {
85 new Option<JlinkTask>(false, (task, opt, arg) -> {
86 task.options.help = true;
87 }, "--help", "-h"),
88 new Option<JlinkTask>(true, (task, opt, arg) -> {
89 String[] dirs = arg.split(File.pathSeparator);
90 int i = 0;
91 Arrays.stream(dirs)
92 .map(Paths::get)
93 .forEach(task.options.modulePath::add);
94 }, "--module-path", "-p"),
95 new Option<JlinkTask>(true, (task, opt, arg) -> {
96 for (String mn : arg.split(",")) {
97 if (mn.isEmpty()) {
98 throw taskHelper.newBadArgs("err.mods.must.be.specified",
99 "--limit-modules");
100 }
101 task.options.limitMods.add(mn);
102 }
103 }, "--limit-modules"),
104 new Option<JlinkTask>(true, (task, opt, arg) -> {
105 for (String mn : arg.split(",")) {
106 if (mn.isEmpty()) {
107 throw taskHelper.newBadArgs("err.mods.must.be.specified",
108 "--add-modules");
109 }
110 task.options.addMods.add(mn);
111 }
112 }, "--add-modules"),
113 new Option<JlinkTask>(true, (task, opt, arg) -> {
114 Path path = Paths.get(arg);
115 task.options.output = path;
116 }, "--output"),
117 new Option<JlinkTask>(true, (task, opt, arg) -> {
118 if ("little".equals(arg)) {
119 task.options.endian = ByteOrder.LITTLE_ENDIAN;
120 } else if ("big".equals(arg)) {
121 task.options.endian = ByteOrder.BIG_ENDIAN;
122 } else {
123 throw taskHelper.newBadArgs("err.unknown.byte.order", arg);
124 }
125 }, "--endian"),
126 new Option<JlinkTask>(false, (task, opt, arg) -> {
127 task.options.version = true;
128 }, "--version"),
129 new Option<JlinkTask>(true, (task, opt, arg) -> {
130 Path path = Paths.get(arg);
131 if (Files.exists(path)) {
132 throw taskHelper.newBadArgs("err.dir.exists", path);
133 }
134 task.options.packagedModulesPath = path;
135 }, true, "--keep-packaged-modules"),
136 new Option<JlinkTask>(true, (task, opt, arg) -> {
137 task.options.saveoptsfile = arg;
138 }, "--save-opts"),
139 new Option<JlinkTask>(false, (task, opt, arg) -> {
140 task.options.fullVersion = true;
141 }, true, "--full-version"),};
142
143 private static final String PROGNAME = "jlink";
144 private final OptionsValues options = new OptionsValues();
145
146 private static final OptionsHelper<JlinkTask> optionsHelper
147 = taskHelper.newOptionsHelper(JlinkTask.class, recognizedOptions);
148 private PrintWriter log;
149
150 void setLog(PrintWriter out) {
151 log = out;
152 taskHelper.setLog(log);
153 }
154
155 /**
156 * Result codes.
157 */
158 static final int EXIT_OK = 0, // Completed with no errors.
159 EXIT_ERROR = 1, // Completed but reported errors.
160 EXIT_CMDERR = 2, // Bad command-line arguments
161 EXIT_SYSERR = 3, // System error or resource exhaustion.
278 }
279
280 private void postProcessOnly(Path existingImage) throws Exception {
281 PluginsConfiguration config = taskHelper.getPluginsConfig(null);
282 ExecutableImage img = DefaultImageBuilder.getExecutableImage(existingImage);
283 if (img == null) {
284 throw taskHelper.newBadArgs("err.existing.image.invalid");
285 }
286 postProcessImage(img, config.getPlugins());
287 }
288
289 private void createImage() throws Exception {
290 if (options.output == null) {
291 throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true);
292 }
293 ModuleFinder finder
294 = newModuleFinder(options.modulePath, options.limitMods, options.addMods);
295 try {
296 options.addMods = checkAddMods(options.addMods);
297 } catch (IllegalArgumentException ex) {
298 throw taskHelper.newBadArgs("err.mods.must.be.specified", "--add-modules")
299 .showUsage(true);
300 }
301 // First create the image provider
302 ImageProvider imageProvider
303 = createImageProvider(finder,
304 options.addMods,
305 options.limitMods,
306 options.endian,
307 options.packagedModulesPath);
308
309 // Then create the Plugin Stack
310 ImagePluginStack stack = ImagePluginConfiguration.
311 parseConfiguration(taskHelper.getPluginsConfig(options.output));
312
313 //Ask the stack to proceed
314 stack.operate(imageProvider);
315 }
316
317 private static Set<String> checkAddMods(Set<String> addMods) {
318 if (addMods.isEmpty()) {
|