author | jlaskey |
Mon, 07 Nov 2016 13:10:42 -0400 | |
changeset 41919 | 612b698f8b8f |
parent 41831 | 2dd91b18195b |
child 41953 | 3e89e6103470 |
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(); |
|
41831
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
164 |
private final List<Plugin> plugins; |
36511 | 165 |
private String lastSorter; |
166 |
private boolean listPlugins; |
|
167 |
private Path existingImage; |
|
168 |
||
169 |
// plugin to args maps. Each plugin may be used more than once in command line. |
|
170 |
// Each such occurrence results in a Map of arguments. So, there could be multiple |
|
171 |
// args maps per plugin instance. |
|
172 |
private final Map<Plugin, List<Map<String, String>>> pluginToMaps = new HashMap<>(); |
|
173 |
private final List<PlugOption> pluginsOptions = new ArrayList<>(); |
|
174 |
private final List<PlugOption> mainOptions = new ArrayList<>(); |
|
175 |
||
176 |
private PluginsOptions(String pp) throws BadArgs { |
|
177 |
||
178 |
if (pp != null) { |
|
179 |
String[] dirs = pp.split(File.pathSeparator); |
|
180 |
List<Path> paths = new ArrayList<>(dirs.length); |
|
181 |
for (String dir : dirs) { |
|
182 |
paths.add(Paths.get(dir)); |
|
183 |
} |
|
184 |
||
185 |
pluginsLayer = createPluginsLayer(paths); |
|
186 |
} |
|
187 |
||
41831
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
188 |
plugins = PluginRepository.getPlugins(pluginsLayer); |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
189 |
|
36511 | 190 |
Set<String> optionsSeen = new HashSet<>(); |
41831
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
191 |
for (Plugin plugin : plugins) { |
36511 | 192 |
if (!Utils.isDisabled(plugin)) { |
193 |
addOrderedPluginOptions(plugin, optionsSeen); |
|
194 |
} |
|
195 |
} |
|
196 |
mainOptions.add(new PlugOption(false, |
|
197 |
(task, opt, arg) -> { |
|
198 |
// This option is handled prior |
|
199 |
// to have the options parsed. |
|
200 |
}, |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39321
diff
changeset
|
201 |
"--plugin-module-path")); |
36511 | 202 |
mainOptions.add(new PlugOption(true, (task, opt, arg) -> { |
41831
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
203 |
for (Plugin plugin : plugins) { |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
204 |
if (plugin.getName().equals(arg)) { |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
205 |
pluginToMaps.remove(plugin); |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
206 |
return; |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
207 |
} |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
208 |
} |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
209 |
throw newBadArgs("err.no.such.plugin", arg); |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
210 |
}, |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
211 |
"--disable-plugin")); |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
212 |
mainOptions.add(new PlugOption(true, (task, opt, arg) -> { |
36511 | 213 |
Path path = Paths.get(arg); |
214 |
if (!Files.exists(path) || !Files.isDirectory(path)) { |
|
41919
612b698f8b8f
8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents:
41831
diff
changeset
|
215 |
throw newBadArgs("err.image.must.exist"); |
36511 | 216 |
} |
217 |
existingImage = path.toAbsolutePath(); |
|
218 |
}, true, POST_PROCESS)); |
|
219 |
mainOptions.add(new PlugOption(true, |
|
220 |
(task, opt, arg) -> { |
|
221 |
lastSorter = arg; |
|
222 |
}, |
|
223 |
true, "--resources-last-sorter")); |
|
224 |
mainOptions.add(new PlugOption(false, |
|
225 |
(task, opt, arg) -> { |
|
226 |
listPlugins = true; |
|
227 |
}, |
|
228 |
"--list-plugins")); |
|
229 |
} |
|
230 |
||
231 |
private List<Map<String, String>> argListFor(Plugin plugin) { |
|
232 |
List<Map<String, String>> mapList = pluginToMaps.get(plugin); |
|
233 |
if (mapList == null) { |
|
234 |
mapList = new ArrayList<>(); |
|
235 |
pluginToMaps.put(plugin, mapList); |
|
236 |
} |
|
237 |
return mapList; |
|
238 |
} |
|
239 |
||
240 |
private void addEmptyArgumentMap(Plugin plugin) { |
|
241 |
argListFor(plugin).add(Collections.emptyMap()); |
|
242 |
} |
|
243 |
||
244 |
private Map<String, String> addArgumentMap(Plugin plugin) { |
|
245 |
Map<String, String> map = new HashMap<>(); |
|
246 |
argListFor(plugin).add(map); |
|
247 |
return map; |
|
248 |
} |
|
249 |
||
250 |
private void addOrderedPluginOptions(Plugin plugin, |
|
251 |
Set<String> optionsSeen) throws BadArgs { |
|
252 |
String option = plugin.getOption(); |
|
253 |
if (option == null) { |
|
254 |
return; |
|
255 |
} |
|
256 |
||
257 |
// make sure that more than one plugin does not use the same option! |
|
258 |
if (optionsSeen.contains(option)) { |
|
259 |
throw new BadArgs("err.plugin.mutiple.options", |
|
260 |
option); |
|
261 |
} |
|
262 |
optionsSeen.add(option); |
|
263 |
||
264 |
PlugOption plugOption |
|
265 |
= new PlugOption(plugin.hasArguments(), |
|
266 |
(task, opt, arg) -> { |
|
267 |
if (!Utils.isFunctional(plugin)) { |
|
268 |
throw newBadArgs("err.provider.not.functional", |
|
269 |
option); |
|
270 |
} |
|
271 |
||
272 |
if (! plugin.hasArguments()) { |
|
273 |
addEmptyArgumentMap(plugin); |
|
274 |
return; |
|
275 |
} |
|
276 |
||
277 |
Map<String, String> m = addArgumentMap(plugin); |
|
278 |
// handle one or more arguments |
|
279 |
if (arg.indexOf(':') == -1) { |
|
280 |
// single argument case |
|
281 |
m.put(option, arg); |
|
282 |
} else { |
|
283 |
// This option can accept more than one arguments |
|
284 |
// like --option_name=arg_value:arg2=value2:arg3=value3 |
|
285 |
||
286 |
// ":" followed by word char condition takes care of args that |
|
287 |
// like Windows absolute paths "C:\foo", "C:/foo" [cygwin] etc. |
|
288 |
// This enforces that key names start with a word character. |
|
289 |
String[] args = arg.split(":(?=\\w)", -1); |
|
290 |
String firstArg = args[0]; |
|
291 |
if (firstArg.isEmpty()) { |
|
292 |
throw newBadArgs("err.provider.additional.arg.error", |
|
293 |
option, arg); |
|
294 |
} |
|
295 |
m.put(option, firstArg); |
|
296 |
// process the additional arguments |
|
297 |
for (int i = 1; i < args.length; i++) { |
|
298 |
String addArg = args[i]; |
|
299 |
int eqIdx = addArg.indexOf('='); |
|
300 |
if (eqIdx == -1) { |
|
301 |
throw newBadArgs("err.provider.additional.arg.error", |
|
302 |
option, arg); |
|
303 |
} |
|
304 |
||
305 |
String addArgName = addArg.substring(0, eqIdx); |
|
306 |
String addArgValue = addArg.substring(eqIdx+1); |
|
307 |
if (addArgName.isEmpty() || addArgValue.isEmpty()) { |
|
308 |
throw newBadArgs("err.provider.additional.arg.error", |
|
309 |
option, arg); |
|
310 |
} |
|
311 |
m.put(addArgName, addArgValue); |
|
312 |
} |
|
313 |
} |
|
314 |
}, |
|
315 |
"--" + option); |
|
316 |
pluginsOptions.add(plugOption); |
|
317 |
||
318 |
if (Utils.isFunctional(plugin)) { |
|
319 |
if (Utils.isAutoEnabled(plugin)) { |
|
320 |
addEmptyArgumentMap(plugin); |
|
321 |
} |
|
322 |
||
323 |
if (plugin instanceof DefaultCompressPlugin) { |
|
324 |
plugOption |
|
325 |
= new PlugOption(false, |
|
326 |
(task, opt, arg) -> { |
|
327 |
Map<String, String> m = addArgumentMap(plugin); |
|
328 |
m.put(DefaultCompressPlugin.NAME, DefaultCompressPlugin.LEVEL_2); |
|
329 |
}, "-c"); |
|
330 |
mainOptions.add(plugOption); |
|
331 |
} else if (plugin instanceof StripDebugPlugin) { |
|
332 |
plugOption |
|
333 |
= new PlugOption(false, |
|
334 |
(task, opt, arg) -> { |
|
335 |
addArgumentMap(plugin); |
|
336 |
}, "-G"); |
|
337 |
mainOptions.add(plugOption); |
|
41561
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
338 |
} else if (plugin instanceof ExcludeJmodSectionPlugin) { |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
339 |
plugOption = new PlugOption(false, (task, opt, arg) -> { |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
340 |
Map<String, String> m = addArgumentMap(plugin); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
341 |
m.put(ExcludeJmodSectionPlugin.NAME, |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
342 |
ExcludeJmodSectionPlugin.MAN_PAGES); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
343 |
}, "--no-man-pages"); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
344 |
mainOptions.add(plugOption); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
345 |
|
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
346 |
plugOption = new PlugOption(false, (task, opt, arg) -> { |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
347 |
Map<String, String> m = addArgumentMap(plugin); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
348 |
m.put(ExcludeJmodSectionPlugin.NAME, |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
349 |
ExcludeJmodSectionPlugin.INCLUDE_HEADER_FILES); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
350 |
}, "--no-header-files"); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
351 |
mainOptions.add(plugOption); |
36511 | 352 |
} |
353 |
} |
|
354 |
} |
|
355 |
||
356 |
private PlugOption getOption(String name) throws BadArgs { |
|
357 |
for (PlugOption o : pluginsOptions) { |
|
358 |
if (o.matches(name)) { |
|
359 |
return o; |
|
360 |
} |
|
361 |
} |
|
362 |
for (PlugOption o : mainOptions) { |
|
363 |
if (o.matches(name)) { |
|
364 |
return o; |
|
365 |
} |
|
366 |
} |
|
367 |
return null; |
|
368 |
} |
|
369 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
370 |
private PluginsConfiguration getPluginsConfig(Path output |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
371 |
) throws IOException, BadArgs { |
36511 | 372 |
if (output != null) { |
373 |
if (Files.exists(output)) { |
|
374 |
throw new PluginException(PluginsResourceBundle. |
|
375 |
getMessage("err.dir.already.exits", output)); |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
List<Plugin> pluginsList = new ArrayList<>(); |
|
380 |
for (Entry<Plugin, List<Map<String, String>>> entry : pluginToMaps.entrySet()) { |
|
381 |
Plugin plugin = entry.getKey(); |
|
382 |
List<Map<String, String>> argsMaps = entry.getValue(); |
|
383 |
||
384 |
// same plugin option may be used multiple times in command line. |
|
385 |
// we call configure once for each occurrence. It is upto the plugin |
|
386 |
// to 'merge' and/or 'override' arguments. |
|
387 |
for (Map<String, String> map : argsMaps) { |
|
38320 | 388 |
plugin.configure(Collections.unmodifiableMap(map)); |
36511 | 389 |
} |
390 |
||
391 |
if (!Utils.isDisabled(plugin)) { |
|
392 |
pluginsList.add(plugin); |
|
393 |
} |
|
394 |
} |
|
395 |
||
396 |
// recreate or postprocessing don't require an output directory. |
|
397 |
ImageBuilder builder = null; |
|
398 |
if (output != null) { |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
399 |
builder = new DefaultImageBuilder(output); |
36511 | 400 |
|
401 |
} |
|
402 |
return new Jlink.PluginsConfiguration(pluginsList, |
|
38320 | 403 |
builder, lastSorter); |
36511 | 404 |
} |
405 |
} |
|
406 |
||
407 |
private static final class ResourceBundleHelper { |
|
408 |
||
409 |
private final ResourceBundle bundle; |
|
410 |
private final ResourceBundle pluginBundle; |
|
411 |
||
412 |
ResourceBundleHelper(String path) { |
|
413 |
Locale locale = Locale.getDefault(); |
|
414 |
try { |
|
415 |
bundle = ResourceBundle.getBundle(path, locale); |
|
416 |
pluginBundle = ResourceBundle.getBundle("jdk.tools.jlink.resources.plugins", locale); |
|
417 |
} catch (MissingResourceException e) { |
|
418 |
throw new InternalError("Cannot find jlink resource bundle for locale " + locale); |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
String getMessage(String key, Object... args) { |
|
423 |
String val; |
|
424 |
try { |
|
425 |
val = bundle.getString(key); |
|
426 |
} catch (MissingResourceException e) { |
|
427 |
// XXX OK, check in plugin bundle |
|
428 |
val = pluginBundle.getString(key); |
|
429 |
} |
|
430 |
return MessageFormat.format(val, args); |
|
431 |
} |
|
432 |
||
433 |
} |
|
434 |
||
435 |
public final class OptionsHelper<T> { |
|
436 |
||
437 |
private final List<Option<T>> options; |
|
438 |
private String[] command; |
|
439 |
private String defaults; |
|
440 |
||
441 |
OptionsHelper(List<Option<T>> options) { |
|
442 |
this.options = options; |
|
443 |
} |
|
444 |
||
445 |
private boolean hasArgument(String optionName) throws BadArgs { |
|
446 |
Option<?> opt = getOption(optionName); |
|
447 |
if (opt == null) { |
|
448 |
opt = pluginOptions.getOption(optionName); |
|
449 |
if (opt == null) { |
|
450 |
throw new BadArgs("err.unknown.option", optionName). |
|
451 |
showUsage(true); |
|
452 |
} |
|
453 |
} |
|
454 |
return opt.hasArg; |
|
455 |
} |
|
456 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
457 |
public boolean shouldListPlugins() { |
36511 | 458 |
return pluginOptions.listPlugins; |
459 |
} |
|
460 |
||
461 |
private String getPluginsPath(String[] args) throws BadArgs { |
|
462 |
String pp = null; |
|
463 |
for (int i = 0; i < args.length; i++) { |
|
464 |
if (args[i].equals(PluginsOptions.PLUGINS_PATH)) { |
|
465 |
if (i == args.length - 1) { |
|
466 |
throw new BadArgs("err.no.plugins.path").showUsage(true); |
|
467 |
} else { |
|
468 |
i += 1; |
|
469 |
pp = args[i]; |
|
470 |
if (!pp.isEmpty() && pp.charAt(0) == '-') { |
|
471 |
throw new BadArgs("err.no.plugins.path").showUsage(true); |
|
472 |
} |
|
473 |
break; |
|
474 |
} |
|
475 |
} |
|
476 |
} |
|
477 |
return pp; |
|
478 |
} |
|
479 |
||
41766 | 480 |
// used by jimage. Return unhandled arguments like "create", "describe". |
36511 | 481 |
public List<String> handleOptions(T task, String[] args) throws BadArgs { |
41766 | 482 |
return handleOptions(task, args, true); |
483 |
} |
|
484 |
||
485 |
// used by jlink. No unhandled arguments like "create", "describe". |
|
486 |
void handleOptionsNoUnhandled(T task, String[] args) throws BadArgs { |
|
487 |
handleOptions(task, args, false); |
|
488 |
} |
|
489 |
||
490 |
// shared code that handles options for both jlink and jimage. jimage uses arguments like |
|
491 |
// "create", "describe" etc. as "task names". Those arguments are unhandled here and returned |
|
492 |
// as "unhandled arguments list". jlink does not want such arguments. "collectUnhandled" flag |
|
493 |
// tells whether to allow for unhandled arguments or not. |
|
494 |
private List<String> handleOptions(T task, String[] args, boolean collectUnhandled) throws BadArgs { |
|
36511 | 495 |
// findbugs warning, copy instead of keeping a reference. |
496 |
command = Arrays.copyOf(args, args.length); |
|
497 |
||
498 |
// Must extract it prior to do any option analysis. |
|
499 |
// Required to interpret custom plugin options. |
|
500 |
// Unit tests can call Task multiple time in same JVM. |
|
501 |
pluginOptions = new PluginsOptions(getPluginsPath(args)); |
|
502 |
||
503 |
// First extract plugins path if any |
|
504 |
String pp = null; |
|
505 |
List<String> filteredArgs = new ArrayList<>(); |
|
506 |
for (int i = 0; i < args.length; i++) { |
|
507 |
if (args[i].equals(PluginsOptions.PLUGINS_PATH)) { |
|
508 |
if (i == args.length - 1) { |
|
509 |
throw new BadArgs("err.no.plugins.path").showUsage(true); |
|
510 |
} else { |
|
511 |
warning("warn.thirdparty.plugins.enabled"); |
|
512 |
log.println(bundleHelper.getMessage("warn.thirdparty.plugins")); |
|
513 |
i += 1; |
|
514 |
String arg = args[i]; |
|
515 |
if (!arg.isEmpty() && arg.charAt(0) == '-') { |
|
516 |
throw new BadArgs("err.no.plugins.path").showUsage(true); |
|
517 |
} |
|
518 |
pp = args[i]; |
|
519 |
} |
|
520 |
} else { |
|
521 |
filteredArgs.add(args[i]); |
|
522 |
} |
|
523 |
} |
|
524 |
String[] arr = new String[filteredArgs.size()]; |
|
525 |
args = filteredArgs.toArray(arr); |
|
526 |
||
41766 | 527 |
List<String> rest = collectUnhandled? new ArrayList<>() : null; |
36511 | 528 |
// process options |
529 |
for (int i = 0; i < args.length; i++) { |
|
41766 | 530 |
if (args[i].charAt(0) == '-') { |
36511 | 531 |
String name = args[i]; |
532 |
PlugOption pluginOption = null; |
|
533 |
Option<T> option = getOption(name); |
|
534 |
if (option == null) { |
|
535 |
pluginOption = pluginOptions.getOption(name); |
|
536 |
if (pluginOption == null) { |
|
537 |
||
538 |
throw new BadArgs("err.unknown.option", name). |
|
539 |
showUsage(true); |
|
540 |
} |
|
541 |
} |
|
542 |
Option<?> opt = pluginOption == null ? option : pluginOption; |
|
543 |
String param = null; |
|
544 |
if (opt.hasArg) { |
|
545 |
if (name.startsWith("--") && name.indexOf('=') > 0) { |
|
546 |
param = name.substring(name.indexOf('=') + 1, |
|
547 |
name.length()); |
|
548 |
} else if (i + 1 < args.length) { |
|
549 |
param = args[++i]; |
|
550 |
} |
|
551 |
if (param == null || param.isEmpty() |
|
552 |
|| (param.length() >= 2 && param.charAt(0) == '-' |
|
553 |
&& param.charAt(1) == '-')) { |
|
554 |
throw new BadArgs("err.missing.arg", name). |
|
555 |
showUsage(true); |
|
556 |
} |
|
557 |
} |
|
558 |
if (pluginOption != null) { |
|
559 |
pluginOption.process(pluginOptions, name, param); |
|
560 |
} else { |
|
561 |
option.process(task, name, param); |
|
562 |
} |
|
563 |
if (opt.ignoreRest()) { |
|
564 |
i = args.length; |
|
565 |
} |
|
566 |
} else { |
|
41766 | 567 |
if (collectUnhandled) { |
568 |
rest.add(args[i]); |
|
569 |
} else { |
|
570 |
throw new BadArgs("err.orphan.argument", args[i]). |
|
571 |
showUsage(true); |
|
572 |
} |
|
36511 | 573 |
} |
574 |
} |
|
575 |
return rest; |
|
576 |
} |
|
577 |
||
578 |
private Option<T> getOption(String name) { |
|
579 |
for (Option<T> o : options) { |
|
580 |
if (o.matches(name)) { |
|
581 |
return o; |
|
582 |
} |
|
583 |
} |
|
584 |
return null; |
|
585 |
} |
|
586 |
||
587 |
public void showHelp(String progName) { |
|
588 |
showHelp(progName, true); |
|
589 |
} |
|
590 |
||
591 |
private void showHelp(String progName, boolean showsImageBuilder) { |
|
592 |
log.println(bundleHelper.getMessage("main.usage", progName)); |
|
593 |
for (Option<?> o : options) { |
|
594 |
String name = o.aliases[0].substring(1); // there must always be at least one name |
|
595 |
name = name.charAt(0) == '-' ? name.substring(1) : name; |
|
596 |
if (o.isHidden() || name.equals("h")) { |
|
597 |
continue; |
|
598 |
} |
|
599 |
log.println(bundleHelper.getMessage("main.opt." + name)); |
|
600 |
} |
|
601 |
||
602 |
for (Option<?> o : pluginOptions.mainOptions) { |
|
603 |
if (o.aliases[0].equals(PluginsOptions.POST_PROCESS) |
|
604 |
&& !showsImageBuilder) { |
|
605 |
continue; |
|
606 |
} |
|
607 |
String name = o.aliases[0].substring(1); // there must always be at least one name |
|
608 |
name = name.charAt(0) == '-' ? name.substring(1) : name; |
|
609 |
if (o.isHidden()) { |
|
610 |
continue; |
|
611 |
} |
|
612 |
log.println(bundleHelper.getMessage("plugin.opt." + name)); |
|
613 |
} |
|
614 |
||
615 |
log.println(bundleHelper.getMessage("main.command.files")); |
|
616 |
} |
|
617 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
618 |
public void listPlugins() { |
36511 | 619 |
log.println("\n" + bundleHelper.getMessage("main.extended.help")); |
620 |
List<Plugin> pluginList = PluginRepository. |
|
621 |
getPlugins(pluginOptions.pluginsLayer); |
|
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
622 |
for (Plugin plugin : Utils.getSortedPlugins(pluginList)) { |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
623 |
showPlugin(plugin, log); |
36511 | 624 |
} |
39150
cb99396625b4
8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents:
38869
diff
changeset
|
625 |
|
cb99396625b4
8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents:
38869
diff
changeset
|
626 |
log.println("\n" + bundleHelper.getMessage("main.extended.help.footer")); |
36511 | 627 |
} |
628 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
629 |
private void showPlugin(Plugin plugin, PrintWriter log) { |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
630 |
if (showsPlugin(plugin)) { |
36511 | 631 |
log.println("\n" + bundleHelper.getMessage("main.plugin.name") |
632 |
+ ": " + plugin.getName()); |
|
633 |
||
634 |
// print verbose details for non-builtin plugins |
|
635 |
if (!Utils.isBuiltin(plugin)) { |
|
636 |
log.println(bundleHelper.getMessage("main.plugin.class") |
|
637 |
+ ": " + plugin.getClass().getName()); |
|
638 |
log.println(bundleHelper.getMessage("main.plugin.module") |
|
639 |
+ ": " + plugin.getClass().getModule().getName()); |
|
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
640 |
Category category = plugin.getType(); |
36511 | 641 |
log.println(bundleHelper.getMessage("main.plugin.category") |
642 |
+ ": " + category.getName()); |
|
643 |
log.println(bundleHelper.getMessage("main.plugin.state") |
|
644 |
+ ": " + plugin.getStateDescription()); |
|
645 |
} |
|
646 |
||
647 |
String option = plugin.getOption(); |
|
648 |
if (option != null) { |
|
649 |
log.println(bundleHelper.getMessage("main.plugin.option") |
|
650 |
+ ": --" + plugin.getOption() |
|
651 |
+ (plugin.hasArguments()? ("=" + plugin.getArgumentsDescription()) : "")); |
|
652 |
} |
|
653 |
||
654 |
// description can be long spanning more than one line and so |
|
655 |
// print a newline after description label. |
|
656 |
log.println(bundleHelper.getMessage("main.plugin.description") |
|
657 |
+ ": " + plugin.getDescription()); |
|
658 |
} |
|
659 |
} |
|
660 |
||
661 |
String[] getInputCommand() { |
|
662 |
return command; |
|
663 |
} |
|
664 |
||
665 |
String getDefaults() { |
|
666 |
return defaults; |
|
667 |
} |
|
668 |
||
669 |
public Layer getPluginsLayer() { |
|
670 |
return pluginOptions.pluginsLayer; |
|
671 |
} |
|
672 |
} |
|
673 |
||
674 |
private PluginsOptions pluginOptions; |
|
675 |
private PrintWriter log; |
|
676 |
private final ResourceBundleHelper bundleHelper; |
|
677 |
||
678 |
public TaskHelper(String path) { |
|
679 |
if (!JLINK_BUNDLE.equals(path) && !JIMAGE_BUNDLE.equals(path)) { |
|
680 |
throw new IllegalArgumentException("Invalid Bundle"); |
|
681 |
} |
|
682 |
this.bundleHelper = new ResourceBundleHelper(path); |
|
683 |
} |
|
684 |
||
685 |
public <T> OptionsHelper<T> newOptionsHelper(Class<T> clazz, |
|
686 |
Option<?>[] options) { |
|
687 |
List<Option<T>> optionsList = new ArrayList<>(); |
|
688 |
for (Option<?> o : options) { |
|
689 |
@SuppressWarnings("unchecked") |
|
690 |
Option<T> opt = (Option<T>) o; |
|
691 |
optionsList.add(opt); |
|
692 |
} |
|
693 |
return new OptionsHelper<>(optionsList); |
|
694 |
} |
|
695 |
||
696 |
public BadArgs newBadArgs(String key, Object... args) { |
|
697 |
return new BadArgs(key, args); |
|
698 |
} |
|
699 |
||
700 |
public String getMessage(String key, Object... args) { |
|
701 |
return bundleHelper.getMessage(key, args); |
|
702 |
} |
|
703 |
||
704 |
public void setLog(PrintWriter log) { |
|
705 |
this.log = log; |
|
706 |
} |
|
707 |
||
708 |
public void reportError(String key, Object... args) { |
|
709 |
log.println(bundleHelper.getMessage("error.prefix") + " " |
|
710 |
+ bundleHelper.getMessage(key, args)); |
|
711 |
} |
|
712 |
||
713 |
public void reportUnknownError(String message) { |
|
714 |
log.println(bundleHelper.getMessage("error.prefix") + " " + message); |
|
715 |
} |
|
716 |
||
717 |
public void warning(String key, Object... args) { |
|
718 |
log.println(bundleHelper.getMessage("warn.prefix") + " " |
|
719 |
+ bundleHelper.getMessage(key, args)); |
|
720 |
} |
|
721 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
722 |
public PluginsConfiguration getPluginsConfig(Path output) |
36511 | 723 |
throws IOException, BadArgs { |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
724 |
return pluginOptions.getPluginsConfig(output); |
36511 | 725 |
} |
726 |
||
727 |
public Path getExistingImage() { |
|
728 |
return pluginOptions.existingImage; |
|
729 |
} |
|
730 |
||
731 |
public void showVersion(boolean full) { |
|
732 |
log.println(version(full ? "full" : "release")); |
|
733 |
} |
|
734 |
||
735 |
public String version(String key) { |
|
736 |
return System.getProperty("java.version"); |
|
737 |
} |
|
738 |
||
739 |
static Layer createPluginsLayer(List<Path> paths) { |
|
740 |
||
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41766
diff
changeset
|
741 |
Path[] dirs = paths.toArray(new Path[0]); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41766
diff
changeset
|
742 |
ModuleFinder finder = SharedSecrets.getJavaLangModuleAccess() |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41766
diff
changeset
|
743 |
.newModulePath(Runtime.version(), true, dirs); |
36511 | 744 |
|
745 |
Configuration bootConfiguration = Layer.boot().configuration(); |
|
746 |
try { |
|
747 |
Configuration cf = bootConfiguration |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
38320
diff
changeset
|
748 |
.resolveRequiresAndUses(ModuleFinder.of(), |
36511 | 749 |
finder, |
750 |
Collections.emptySet()); |
|
751 |
ClassLoader scl = ClassLoader.getSystemClassLoader(); |
|
752 |
return Layer.boot().defineModulesWithOneLoader(cf, scl); |
|
753 |
} catch (Exception ex) { |
|
754 |
// Malformed plugin modules (e.g.: same package in multiple modules). |
|
755 |
throw new PluginException("Invalid modules in the plugins path: " + ex); |
|
756 |
} |
|
757 |
} |
|
758 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
759 |
// Display all plugins |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
760 |
private static boolean showsPlugin(Plugin plugin) { |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
761 |
return (!Utils.isDisabled(plugin) && plugin.getOption() != null); |
36511 | 762 |
} |
763 |
} |