author | alanb |
Fri, 28 Oct 2016 10:18:07 +0100 | |
changeset 41817 | b90ad1de93ea |
parent 41766 | b5eab76a23a9 |
child 41831 | 2dd91b18195b |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package jdk.tools.jlink.internal; |
|
26 |
||
27 |
import java.io.File; |
|
28 |
import java.io.IOException; |
|
29 |
import java.io.PrintWriter; |
|
30 |
import java.lang.module.Configuration; |
|
31 |
import java.lang.module.ModuleFinder; |
|
32 |
import java.lang.reflect.Layer; |
|
33 |
import java.nio.file.Files; |
|
34 |
import java.nio.file.Path; |
|
35 |
import java.nio.file.Paths; |
|
36 |
import java.text.MessageFormat; |
|
37 |
import java.util.ArrayList; |
|
38 |
import java.util.Arrays; |
|
39 |
import java.util.Collections; |
|
40 |
import java.util.HashMap; |
|
41 |
import java.util.HashSet; |
|
42 |
import java.util.List; |
|
43 |
import java.util.Locale; |
|
44 |
import java.util.Map; |
|
45 |
import java.util.Map.Entry; |
|
46 |
import java.util.MissingResourceException; |
|
47 |
import java.util.ResourceBundle; |
|
48 |
import java.util.Set; |
|
49 |
||
41561
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
50 |
import jdk.tools.jlink.internal.plugins.ExcludeFilesPlugin; |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
51 |
import jdk.tools.jlink.internal.plugins.ExcludeJmodSectionPlugin; |
36511 | 52 |
import jdk.tools.jlink.plugin.Plugin; |
38320 | 53 |
import jdk.tools.jlink.plugin.Plugin.Category; |
36511 | 54 |
import jdk.tools.jlink.builder.DefaultImageBuilder; |
55 |
import jdk.tools.jlink.builder.ImageBuilder; |
|
56 |
import jdk.tools.jlink.plugin.PluginException; |
|
41559
5b33f33df1fb
8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
40261
diff
changeset
|
57 |
import jdk.tools.jlink.internal.Jlink.PluginsConfiguration; |
36511 | 58 |
import jdk.tools.jlink.internal.plugins.PluginsResourceBundle; |
59 |
import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin; |
|
60 |
import jdk.tools.jlink.internal.plugins.StripDebugPlugin; |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41766
diff
changeset
|
61 |
import jdk.internal.misc.SharedSecrets; |
36511 | 62 |
|
63 |
/** |
|
64 |
* |
|
65 |
* JLink and JImage tools shared helper. |
|
66 |
*/ |
|
67 |
public final class TaskHelper { |
|
68 |
||
69 |
public static final String JLINK_BUNDLE = "jdk.tools.jlink.resources.jlink"; |
|
70 |
public static final String JIMAGE_BUNDLE = "jdk.tools.jimage.resources.jimage"; |
|
71 |
||
72 |
private static final String DEFAULTS_PROPERTY = "jdk.jlink.defaults"; |
|
73 |
||
74 |
public final class BadArgs extends Exception { |
|
75 |
||
76 |
static final long serialVersionUID = 8765093759964640721L; |
|
77 |
||
78 |
private BadArgs(String key, Object... args) { |
|
79 |
super(bundleHelper.getMessage(key, args)); |
|
80 |
this.key = key; |
|
81 |
this.args = args; |
|
82 |
} |
|
83 |
||
84 |
public BadArgs showUsage(boolean b) { |
|
85 |
showUsage = b; |
|
86 |
return this; |
|
87 |
} |
|
88 |
public final String key; |
|
89 |
public final Object[] args; |
|
90 |
public boolean showUsage; |
|
91 |
} |
|
92 |
||
93 |
public static class Option<T> { |
|
94 |
||
95 |
public interface Processing<T> { |
|
96 |
||
97 |
void process(T task, String opt, String arg) throws BadArgs; |
|
98 |
} |
|
99 |
||
100 |
final boolean hasArg; |
|
101 |
final Processing<T> processing; |
|
102 |
final boolean hidden; |
|
103 |
final String[] aliases; |
|
104 |
||
105 |
public Option(boolean hasArg, Processing<T> processing, boolean hidden, String... aliases) { |
|
106 |
this.hasArg = hasArg; |
|
107 |
this.processing = processing; |
|
108 |
this.aliases = aliases; |
|
109 |
this.hidden = hidden; |
|
110 |
} |
|
111 |
||
112 |
public Option(boolean hasArg, Processing<T> processing, String... aliases) { |
|
113 |
this(hasArg, processing, false, aliases); |
|
114 |
} |
|
115 |
||
116 |
public boolean isHidden() { |
|
117 |
return hidden; |
|
118 |
} |
|
119 |
||
120 |
public boolean matches(String opt) { |
|
121 |
for (String a : aliases) { |
|
122 |
if (a.equals(opt)) { |
|
123 |
return true; |
|
124 |
} else if (opt.startsWith("--") |
|
125 |
&& (hasArg && opt.startsWith(a + "="))) { |
|
126 |
return true; |
|
127 |
} |
|
128 |
} |
|
129 |
return false; |
|
130 |
} |
|
131 |
||
132 |
public boolean ignoreRest() { |
|
133 |
return false; |
|
134 |
} |
|
135 |
||
136 |
void process(T task, String opt, String arg) throws BadArgs { |
|
137 |
processing.process(task, opt, arg); |
|
138 |
} |
|
38869 | 139 |
|
140 |
public String[] aliases() { |
|
141 |
return aliases; |
|
142 |
} |
|
36511 | 143 |
} |
144 |
||
145 |
private static class PlugOption extends Option<PluginsOptions> { |
|
146 |
||
147 |
public PlugOption(boolean hasArg, |
|
148 |
Processing<PluginsOptions> processing, boolean hidden, String... aliases) { |
|
149 |
super(hasArg, processing, hidden, aliases); |
|
150 |
} |
|
151 |
||
152 |
public PlugOption(boolean hasArg, |
|
153 |
Processing<PluginsOptions> processing, String... aliases) { |
|
154 |
super(hasArg, processing, aliases); |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
private final class PluginsOptions { |
|
159 |
||
160 |
private static final String PLUGINS_PATH = "--plugin-module-path"; |
|
161 |
private static final String POST_PROCESS = "--post-process-path"; |
|
162 |
||
163 |
private Layer pluginsLayer = Layer.boot(); |
|
164 |
private String lastSorter; |
|
165 |
private boolean listPlugins; |
|
166 |
private Path existingImage; |
|
167 |
||
168 |
// plugin to args maps. Each plugin may be used more than once in command line. |
|
169 |
// Each such occurrence results in a Map of arguments. So, there could be multiple |
|
170 |
// args maps per plugin instance. |
|
171 |
private final Map<Plugin, List<Map<String, String>>> pluginToMaps = new HashMap<>(); |
|
172 |
private final List<PlugOption> pluginsOptions = new ArrayList<>(); |
|
173 |
private final List<PlugOption> mainOptions = new ArrayList<>(); |
|
174 |
||
175 |
private PluginsOptions(String pp) throws BadArgs { |
|
176 |
||
177 |
if (pp != null) { |
|
178 |
String[] dirs = pp.split(File.pathSeparator); |
|
179 |
List<Path> paths = new ArrayList<>(dirs.length); |
|
180 |
for (String dir : dirs) { |
|
181 |
paths.add(Paths.get(dir)); |
|
182 |
} |
|
183 |
||
184 |
pluginsLayer = createPluginsLayer(paths); |
|
185 |
} |
|
186 |
||
187 |
Set<String> optionsSeen = new HashSet<>(); |
|
188 |
for (Plugin plugin : PluginRepository. |
|
189 |
getPlugins(pluginsLayer)) { |
|
190 |
if (!Utils.isDisabled(plugin)) { |
|
191 |
addOrderedPluginOptions(plugin, optionsSeen); |
|
192 |
} |
|
193 |
} |
|
194 |
mainOptions.add(new PlugOption(false, |
|
195 |
(task, opt, arg) -> { |
|
196 |
// This option is handled prior |
|
197 |
// to have the options parsed. |
|
198 |
}, |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39321
diff
changeset
|
199 |
"--plugin-module-path")); |
36511 | 200 |
mainOptions.add(new PlugOption(true, (task, opt, arg) -> { |
201 |
Path path = Paths.get(arg); |
|
202 |
if (!Files.exists(path) || !Files.isDirectory(path)) { |
|
203 |
throw newBadArgs("err.existing.image.must.exist"); |
|
204 |
} |
|
205 |
existingImage = path.toAbsolutePath(); |
|
206 |
}, true, POST_PROCESS)); |
|
207 |
mainOptions.add(new PlugOption(true, |
|
208 |
(task, opt, arg) -> { |
|
209 |
lastSorter = arg; |
|
210 |
}, |
|
211 |
true, "--resources-last-sorter")); |
|
212 |
mainOptions.add(new PlugOption(false, |
|
213 |
(task, opt, arg) -> { |
|
214 |
listPlugins = true; |
|
215 |
}, |
|
216 |
"--list-plugins")); |
|
217 |
} |
|
218 |
||
219 |
private List<Map<String, String>> argListFor(Plugin plugin) { |
|
220 |
List<Map<String, String>> mapList = pluginToMaps.get(plugin); |
|
221 |
if (mapList == null) { |
|
222 |
mapList = new ArrayList<>(); |
|
223 |
pluginToMaps.put(plugin, mapList); |
|
224 |
} |
|
225 |
return mapList; |
|
226 |
} |
|
227 |
||
228 |
private void addEmptyArgumentMap(Plugin plugin) { |
|
229 |
argListFor(plugin).add(Collections.emptyMap()); |
|
230 |
} |
|
231 |
||
232 |
private Map<String, String> addArgumentMap(Plugin plugin) { |
|
233 |
Map<String, String> map = new HashMap<>(); |
|
234 |
argListFor(plugin).add(map); |
|
235 |
return map; |
|
236 |
} |
|
237 |
||
238 |
private void addOrderedPluginOptions(Plugin plugin, |
|
239 |
Set<String> optionsSeen) throws BadArgs { |
|
240 |
String option = plugin.getOption(); |
|
241 |
if (option == null) { |
|
242 |
return; |
|
243 |
} |
|
244 |
||
245 |
// make sure that more than one plugin does not use the same option! |
|
246 |
if (optionsSeen.contains(option)) { |
|
247 |
throw new BadArgs("err.plugin.mutiple.options", |
|
248 |
option); |
|
249 |
} |
|
250 |
optionsSeen.add(option); |
|
251 |
||
252 |
PlugOption plugOption |
|
253 |
= new PlugOption(plugin.hasArguments(), |
|
254 |
(task, opt, arg) -> { |
|
255 |
if (!Utils.isFunctional(plugin)) { |
|
256 |
throw newBadArgs("err.provider.not.functional", |
|
257 |
option); |
|
258 |
} |
|
259 |
||
260 |
if (! plugin.hasArguments()) { |
|
261 |
addEmptyArgumentMap(plugin); |
|
262 |
return; |
|
263 |
} |
|
264 |
||
265 |
Map<String, String> m = addArgumentMap(plugin); |
|
266 |
// handle one or more arguments |
|
267 |
if (arg.indexOf(':') == -1) { |
|
268 |
// single argument case |
|
269 |
m.put(option, arg); |
|
270 |
} else { |
|
271 |
// This option can accept more than one arguments |
|
272 |
// like --option_name=arg_value:arg2=value2:arg3=value3 |
|
273 |
||
274 |
// ":" followed by word char condition takes care of args that |
|
275 |
// like Windows absolute paths "C:\foo", "C:/foo" [cygwin] etc. |
|
276 |
// This enforces that key names start with a word character. |
|
277 |
String[] args = arg.split(":(?=\\w)", -1); |
|
278 |
String firstArg = args[0]; |
|
279 |
if (firstArg.isEmpty()) { |
|
280 |
throw newBadArgs("err.provider.additional.arg.error", |
|
281 |
option, arg); |
|
282 |
} |
|
283 |
m.put(option, firstArg); |
|
284 |
// process the additional arguments |
|
285 |
for (int i = 1; i < args.length; i++) { |
|
286 |
String addArg = args[i]; |
|
287 |
int eqIdx = addArg.indexOf('='); |
|
288 |
if (eqIdx == -1) { |
|
289 |
throw newBadArgs("err.provider.additional.arg.error", |
|
290 |
option, arg); |
|
291 |
} |
|
292 |
||
293 |
String addArgName = addArg.substring(0, eqIdx); |
|
294 |
String addArgValue = addArg.substring(eqIdx+1); |
|
295 |
if (addArgName.isEmpty() || addArgValue.isEmpty()) { |
|
296 |
throw newBadArgs("err.provider.additional.arg.error", |
|
297 |
option, arg); |
|
298 |
} |
|
299 |
m.put(addArgName, addArgValue); |
|
300 |
} |
|
301 |
} |
|
302 |
}, |
|
303 |
"--" + option); |
|
304 |
pluginsOptions.add(plugOption); |
|
305 |
||
306 |
if (Utils.isFunctional(plugin)) { |
|
307 |
if (Utils.isAutoEnabled(plugin)) { |
|
308 |
addEmptyArgumentMap(plugin); |
|
309 |
} |
|
310 |
||
311 |
if (plugin instanceof DefaultCompressPlugin) { |
|
312 |
plugOption |
|
313 |
= new PlugOption(false, |
|
314 |
(task, opt, arg) -> { |
|
315 |
Map<String, String> m = addArgumentMap(plugin); |
|
316 |
m.put(DefaultCompressPlugin.NAME, DefaultCompressPlugin.LEVEL_2); |
|
317 |
}, "-c"); |
|
318 |
mainOptions.add(plugOption); |
|
319 |
} else if (plugin instanceof StripDebugPlugin) { |
|
320 |
plugOption |
|
321 |
= new PlugOption(false, |
|
322 |
(task, opt, arg) -> { |
|
323 |
addArgumentMap(plugin); |
|
324 |
}, "-G"); |
|
325 |
mainOptions.add(plugOption); |
|
41561
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
326 |
} else if (plugin instanceof ExcludeJmodSectionPlugin) { |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
327 |
plugOption = new PlugOption(false, (task, opt, arg) -> { |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
328 |
Map<String, String> m = addArgumentMap(plugin); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
329 |
m.put(ExcludeJmodSectionPlugin.NAME, |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
330 |
ExcludeJmodSectionPlugin.MAN_PAGES); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
331 |
}, "--no-man-pages"); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
332 |
mainOptions.add(plugOption); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
333 |
|
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
334 |
plugOption = new PlugOption(false, (task, opt, arg) -> { |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
335 |
Map<String, String> m = addArgumentMap(plugin); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
336 |
m.put(ExcludeJmodSectionPlugin.NAME, |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
337 |
ExcludeJmodSectionPlugin.INCLUDE_HEADER_FILES); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
338 |
}, "--no-header-files"); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
339 |
mainOptions.add(plugOption); |
36511 | 340 |
} |
341 |
} |
|
342 |
} |
|
343 |
||
344 |
private PlugOption getOption(String name) throws BadArgs { |
|
345 |
for (PlugOption o : pluginsOptions) { |
|
346 |
if (o.matches(name)) { |
|
347 |
return o; |
|
348 |
} |
|
349 |
} |
|
350 |
for (PlugOption o : mainOptions) { |
|
351 |
if (o.matches(name)) { |
|
352 |
return o; |
|
353 |
} |
|
354 |
} |
|
355 |
return null; |
|
356 |
} |
|
357 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
358 |
private PluginsConfiguration getPluginsConfig(Path output |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
359 |
) throws IOException, BadArgs { |
36511 | 360 |
if (output != null) { |
361 |
if (Files.exists(output)) { |
|
362 |
throw new PluginException(PluginsResourceBundle. |
|
363 |
getMessage("err.dir.already.exits", output)); |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
List<Plugin> pluginsList = new ArrayList<>(); |
|
368 |
for (Entry<Plugin, List<Map<String, String>>> entry : pluginToMaps.entrySet()) { |
|
369 |
Plugin plugin = entry.getKey(); |
|
370 |
List<Map<String, String>> argsMaps = entry.getValue(); |
|
371 |
||
372 |
// same plugin option may be used multiple times in command line. |
|
373 |
// we call configure once for each occurrence. It is upto the plugin |
|
374 |
// to 'merge' and/or 'override' arguments. |
|
375 |
for (Map<String, String> map : argsMaps) { |
|
38320 | 376 |
plugin.configure(Collections.unmodifiableMap(map)); |
36511 | 377 |
} |
378 |
||
379 |
if (!Utils.isDisabled(plugin)) { |
|
380 |
pluginsList.add(plugin); |
|
381 |
} |
|
382 |
} |
|
383 |
||
384 |
// recreate or postprocessing don't require an output directory. |
|
385 |
ImageBuilder builder = null; |
|
386 |
if (output != null) { |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
387 |
builder = new DefaultImageBuilder(output); |
36511 | 388 |
|
389 |
} |
|
390 |
return new Jlink.PluginsConfiguration(pluginsList, |
|
38320 | 391 |
builder, lastSorter); |
36511 | 392 |
} |
393 |
} |
|
394 |
||
395 |
private static final class ResourceBundleHelper { |
|
396 |
||
397 |
private final ResourceBundle bundle; |
|
398 |
private final ResourceBundle pluginBundle; |
|
399 |
||
400 |
ResourceBundleHelper(String path) { |
|
401 |
Locale locale = Locale.getDefault(); |
|
402 |
try { |
|
403 |
bundle = ResourceBundle.getBundle(path, locale); |
|
404 |
pluginBundle = ResourceBundle.getBundle("jdk.tools.jlink.resources.plugins", locale); |
|
405 |
} catch (MissingResourceException e) { |
|
406 |
throw new InternalError("Cannot find jlink resource bundle for locale " + locale); |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
String getMessage(String key, Object... args) { |
|
411 |
String val; |
|
412 |
try { |
|
413 |
val = bundle.getString(key); |
|
414 |
} catch (MissingResourceException e) { |
|
415 |
// XXX OK, check in plugin bundle |
|
416 |
val = pluginBundle.getString(key); |
|
417 |
} |
|
418 |
return MessageFormat.format(val, args); |
|
419 |
} |
|
420 |
||
421 |
} |
|
422 |
||
423 |
public final class OptionsHelper<T> { |
|
424 |
||
425 |
private final List<Option<T>> options; |
|
426 |
private String[] command; |
|
427 |
private String defaults; |
|
428 |
||
429 |
OptionsHelper(List<Option<T>> options) { |
|
430 |
this.options = options; |
|
431 |
} |
|
432 |
||
433 |
private boolean hasArgument(String optionName) throws BadArgs { |
|
434 |
Option<?> opt = getOption(optionName); |
|
435 |
if (opt == null) { |
|
436 |
opt = pluginOptions.getOption(optionName); |
|
437 |
if (opt == null) { |
|
438 |
throw new BadArgs("err.unknown.option", optionName). |
|
439 |
showUsage(true); |
|
440 |
} |
|
441 |
} |
|
442 |
return opt.hasArg; |
|
443 |
} |
|
444 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
445 |
public boolean shouldListPlugins() { |
36511 | 446 |
return pluginOptions.listPlugins; |
447 |
} |
|
448 |
||
449 |
private String getPluginsPath(String[] args) throws BadArgs { |
|
450 |
String pp = null; |
|
451 |
for (int i = 0; i < args.length; i++) { |
|
452 |
if (args[i].equals(PluginsOptions.PLUGINS_PATH)) { |
|
453 |
if (i == args.length - 1) { |
|
454 |
throw new BadArgs("err.no.plugins.path").showUsage(true); |
|
455 |
} else { |
|
456 |
i += 1; |
|
457 |
pp = args[i]; |
|
458 |
if (!pp.isEmpty() && pp.charAt(0) == '-') { |
|
459 |
throw new BadArgs("err.no.plugins.path").showUsage(true); |
|
460 |
} |
|
461 |
break; |
|
462 |
} |
|
463 |
} |
|
464 |
} |
|
465 |
return pp; |
|
466 |
} |
|
467 |
||
41766 | 468 |
// used by jimage. Return unhandled arguments like "create", "describe". |
36511 | 469 |
public List<String> handleOptions(T task, String[] args) throws BadArgs { |
41766 | 470 |
return handleOptions(task, args, true); |
471 |
} |
|
472 |
||
473 |
// used by jlink. No unhandled arguments like "create", "describe". |
|
474 |
void handleOptionsNoUnhandled(T task, String[] args) throws BadArgs { |
|
475 |
handleOptions(task, args, false); |
|
476 |
} |
|
477 |
||
478 |
// shared code that handles options for both jlink and jimage. jimage uses arguments like |
|
479 |
// "create", "describe" etc. as "task names". Those arguments are unhandled here and returned |
|
480 |
// as "unhandled arguments list". jlink does not want such arguments. "collectUnhandled" flag |
|
481 |
// tells whether to allow for unhandled arguments or not. |
|
482 |
private List<String> handleOptions(T task, String[] args, boolean collectUnhandled) throws BadArgs { |
|
36511 | 483 |
// findbugs warning, copy instead of keeping a reference. |
484 |
command = Arrays.copyOf(args, args.length); |
|
485 |
||
486 |
// Must extract it prior to do any option analysis. |
|
487 |
// Required to interpret custom plugin options. |
|
488 |
// Unit tests can call Task multiple time in same JVM. |
|
489 |
pluginOptions = new PluginsOptions(getPluginsPath(args)); |
|
490 |
||
491 |
// First extract plugins path if any |
|
492 |
String pp = null; |
|
493 |
List<String> filteredArgs = new ArrayList<>(); |
|
494 |
for (int i = 0; i < args.length; i++) { |
|
495 |
if (args[i].equals(PluginsOptions.PLUGINS_PATH)) { |
|
496 |
if (i == args.length - 1) { |
|
497 |
throw new BadArgs("err.no.plugins.path").showUsage(true); |
|
498 |
} else { |
|
499 |
warning("warn.thirdparty.plugins.enabled"); |
|
500 |
log.println(bundleHelper.getMessage("warn.thirdparty.plugins")); |
|
501 |
i += 1; |
|
502 |
String arg = args[i]; |
|
503 |
if (!arg.isEmpty() && arg.charAt(0) == '-') { |
|
504 |
throw new BadArgs("err.no.plugins.path").showUsage(true); |
|
505 |
} |
|
506 |
pp = args[i]; |
|
507 |
} |
|
508 |
} else { |
|
509 |
filteredArgs.add(args[i]); |
|
510 |
} |
|
511 |
} |
|
512 |
String[] arr = new String[filteredArgs.size()]; |
|
513 |
args = filteredArgs.toArray(arr); |
|
514 |
||
41766 | 515 |
List<String> rest = collectUnhandled? new ArrayList<>() : null; |
36511 | 516 |
// process options |
517 |
for (int i = 0; i < args.length; i++) { |
|
41766 | 518 |
if (args[i].charAt(0) == '-') { |
36511 | 519 |
String name = args[i]; |
520 |
PlugOption pluginOption = null; |
|
521 |
Option<T> option = getOption(name); |
|
522 |
if (option == null) { |
|
523 |
pluginOption = pluginOptions.getOption(name); |
|
524 |
if (pluginOption == null) { |
|
525 |
||
526 |
throw new BadArgs("err.unknown.option", name). |
|
527 |
showUsage(true); |
|
528 |
} |
|
529 |
} |
|
530 |
Option<?> opt = pluginOption == null ? option : pluginOption; |
|
531 |
String param = null; |
|
532 |
if (opt.hasArg) { |
|
533 |
if (name.startsWith("--") && name.indexOf('=') > 0) { |
|
534 |
param = name.substring(name.indexOf('=') + 1, |
|
535 |
name.length()); |
|
536 |
} else if (i + 1 < args.length) { |
|
537 |
param = args[++i]; |
|
538 |
} |
|
539 |
if (param == null || param.isEmpty() |
|
540 |
|| (param.length() >= 2 && param.charAt(0) == '-' |
|
541 |
&& param.charAt(1) == '-')) { |
|
542 |
throw new BadArgs("err.missing.arg", name). |
|
543 |
showUsage(true); |
|
544 |
} |
|
545 |
} |
|
546 |
if (pluginOption != null) { |
|
547 |
pluginOption.process(pluginOptions, name, param); |
|
548 |
} else { |
|
549 |
option.process(task, name, param); |
|
550 |
} |
|
551 |
if (opt.ignoreRest()) { |
|
552 |
i = args.length; |
|
553 |
} |
|
554 |
} else { |
|
41766 | 555 |
if (collectUnhandled) { |
556 |
rest.add(args[i]); |
|
557 |
} else { |
|
558 |
throw new BadArgs("err.orphan.argument", args[i]). |
|
559 |
showUsage(true); |
|
560 |
} |
|
36511 | 561 |
} |
562 |
} |
|
563 |
return rest; |
|
564 |
} |
|
565 |
||
566 |
private Option<T> getOption(String name) { |
|
567 |
for (Option<T> o : options) { |
|
568 |
if (o.matches(name)) { |
|
569 |
return o; |
|
570 |
} |
|
571 |
} |
|
572 |
return null; |
|
573 |
} |
|
574 |
||
575 |
public void showHelp(String progName) { |
|
576 |
showHelp(progName, true); |
|
577 |
} |
|
578 |
||
579 |
private void showHelp(String progName, boolean showsImageBuilder) { |
|
580 |
log.println(bundleHelper.getMessage("main.usage", progName)); |
|
581 |
for (Option<?> o : options) { |
|
582 |
String name = o.aliases[0].substring(1); // there must always be at least one name |
|
583 |
name = name.charAt(0) == '-' ? name.substring(1) : name; |
|
584 |
if (o.isHidden() || name.equals("h")) { |
|
585 |
continue; |
|
586 |
} |
|
587 |
log.println(bundleHelper.getMessage("main.opt." + name)); |
|
588 |
} |
|
589 |
||
590 |
for (Option<?> o : pluginOptions.mainOptions) { |
|
591 |
if (o.aliases[0].equals(PluginsOptions.POST_PROCESS) |
|
592 |
&& !showsImageBuilder) { |
|
593 |
continue; |
|
594 |
} |
|
595 |
String name = o.aliases[0].substring(1); // there must always be at least one name |
|
596 |
name = name.charAt(0) == '-' ? name.substring(1) : name; |
|
597 |
if (o.isHidden()) { |
|
598 |
continue; |
|
599 |
} |
|
600 |
log.println(bundleHelper.getMessage("plugin.opt." + name)); |
|
601 |
} |
|
602 |
||
603 |
log.println(bundleHelper.getMessage("main.command.files")); |
|
604 |
} |
|
605 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
606 |
public void listPlugins() { |
36511 | 607 |
log.println("\n" + bundleHelper.getMessage("main.extended.help")); |
608 |
List<Plugin> pluginList = PluginRepository. |
|
609 |
getPlugins(pluginOptions.pluginsLayer); |
|
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
610 |
for (Plugin plugin : Utils.getSortedPlugins(pluginList)) { |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
611 |
showPlugin(plugin, log); |
36511 | 612 |
} |
39150
cb99396625b4
8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents:
38869
diff
changeset
|
613 |
|
cb99396625b4
8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents:
38869
diff
changeset
|
614 |
log.println("\n" + bundleHelper.getMessage("main.extended.help.footer")); |
36511 | 615 |
} |
616 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
617 |
private void showPlugin(Plugin plugin, PrintWriter log) { |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
618 |
if (showsPlugin(plugin)) { |
36511 | 619 |
log.println("\n" + bundleHelper.getMessage("main.plugin.name") |
620 |
+ ": " + plugin.getName()); |
|
621 |
||
622 |
// print verbose details for non-builtin plugins |
|
623 |
if (!Utils.isBuiltin(plugin)) { |
|
624 |
log.println(bundleHelper.getMessage("main.plugin.class") |
|
625 |
+ ": " + plugin.getClass().getName()); |
|
626 |
log.println(bundleHelper.getMessage("main.plugin.module") |
|
627 |
+ ": " + plugin.getClass().getModule().getName()); |
|
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
628 |
Category category = plugin.getType(); |
36511 | 629 |
log.println(bundleHelper.getMessage("main.plugin.category") |
630 |
+ ": " + category.getName()); |
|
631 |
log.println(bundleHelper.getMessage("main.plugin.state") |
|
632 |
+ ": " + plugin.getStateDescription()); |
|
633 |
} |
|
634 |
||
635 |
String option = plugin.getOption(); |
|
636 |
if (option != null) { |
|
637 |
log.println(bundleHelper.getMessage("main.plugin.option") |
|
638 |
+ ": --" + plugin.getOption() |
|
639 |
+ (plugin.hasArguments()? ("=" + plugin.getArgumentsDescription()) : "")); |
|
640 |
} |
|
641 |
||
642 |
// description can be long spanning more than one line and so |
|
643 |
// print a newline after description label. |
|
644 |
log.println(bundleHelper.getMessage("main.plugin.description") |
|
645 |
+ ": " + plugin.getDescription()); |
|
646 |
} |
|
647 |
} |
|
648 |
||
649 |
String[] getInputCommand() { |
|
650 |
return command; |
|
651 |
} |
|
652 |
||
653 |
String getDefaults() { |
|
654 |
return defaults; |
|
655 |
} |
|
656 |
||
657 |
public Layer getPluginsLayer() { |
|
658 |
return pluginOptions.pluginsLayer; |
|
659 |
} |
|
660 |
} |
|
661 |
||
662 |
private PluginsOptions pluginOptions; |
|
663 |
private PrintWriter log; |
|
664 |
private final ResourceBundleHelper bundleHelper; |
|
665 |
||
666 |
public TaskHelper(String path) { |
|
667 |
if (!JLINK_BUNDLE.equals(path) && !JIMAGE_BUNDLE.equals(path)) { |
|
668 |
throw new IllegalArgumentException("Invalid Bundle"); |
|
669 |
} |
|
670 |
this.bundleHelper = new ResourceBundleHelper(path); |
|
671 |
} |
|
672 |
||
673 |
public <T> OptionsHelper<T> newOptionsHelper(Class<T> clazz, |
|
674 |
Option<?>[] options) { |
|
675 |
List<Option<T>> optionsList = new ArrayList<>(); |
|
676 |
for (Option<?> o : options) { |
|
677 |
@SuppressWarnings("unchecked") |
|
678 |
Option<T> opt = (Option<T>) o; |
|
679 |
optionsList.add(opt); |
|
680 |
} |
|
681 |
return new OptionsHelper<>(optionsList); |
|
682 |
} |
|
683 |
||
684 |
public BadArgs newBadArgs(String key, Object... args) { |
|
685 |
return new BadArgs(key, args); |
|
686 |
} |
|
687 |
||
688 |
public String getMessage(String key, Object... args) { |
|
689 |
return bundleHelper.getMessage(key, args); |
|
690 |
} |
|
691 |
||
692 |
public void setLog(PrintWriter log) { |
|
693 |
this.log = log; |
|
694 |
} |
|
695 |
||
696 |
public void reportError(String key, Object... args) { |
|
697 |
log.println(bundleHelper.getMessage("error.prefix") + " " |
|
698 |
+ bundleHelper.getMessage(key, args)); |
|
699 |
} |
|
700 |
||
701 |
public void reportUnknownError(String message) { |
|
702 |
log.println(bundleHelper.getMessage("error.prefix") + " " + message); |
|
703 |
} |
|
704 |
||
705 |
public void warning(String key, Object... args) { |
|
706 |
log.println(bundleHelper.getMessage("warn.prefix") + " " |
|
707 |
+ bundleHelper.getMessage(key, args)); |
|
708 |
} |
|
709 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
710 |
public PluginsConfiguration getPluginsConfig(Path output) |
36511 | 711 |
throws IOException, BadArgs { |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
712 |
return pluginOptions.getPluginsConfig(output); |
36511 | 713 |
} |
714 |
||
715 |
public Path getExistingImage() { |
|
716 |
return pluginOptions.existingImage; |
|
717 |
} |
|
718 |
||
719 |
public void showVersion(boolean full) { |
|
720 |
log.println(version(full ? "full" : "release")); |
|
721 |
} |
|
722 |
||
723 |
public String version(String key) { |
|
724 |
return System.getProperty("java.version"); |
|
725 |
} |
|
726 |
||
727 |
static Layer createPluginsLayer(List<Path> paths) { |
|
728 |
||
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41766
diff
changeset
|
729 |
Path[] dirs = paths.toArray(new Path[0]); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41766
diff
changeset
|
730 |
ModuleFinder finder = SharedSecrets.getJavaLangModuleAccess() |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41766
diff
changeset
|
731 |
.newModulePath(Runtime.version(), true, dirs); |
36511 | 732 |
|
733 |
Configuration bootConfiguration = Layer.boot().configuration(); |
|
734 |
try { |
|
735 |
Configuration cf = bootConfiguration |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
38320
diff
changeset
|
736 |
.resolveRequiresAndUses(ModuleFinder.of(), |
36511 | 737 |
finder, |
738 |
Collections.emptySet()); |
|
739 |
ClassLoader scl = ClassLoader.getSystemClassLoader(); |
|
740 |
return Layer.boot().defineModulesWithOneLoader(cf, scl); |
|
741 |
} catch (Exception ex) { |
|
742 |
// Malformed plugin modules (e.g.: same package in multiple modules). |
|
743 |
throw new PluginException("Invalid modules in the plugins path: " + ex); |
|
744 |
} |
|
745 |
} |
|
746 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
747 |
// Display all plugins |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
748 |
private static boolean showsPlugin(Plugin plugin) { |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
749 |
return (!Utils.isDisabled(plugin) && plugin.getOption() != null); |
36511 | 750 |
} |
751 |
} |