author | mr |
Tue, 29 Oct 2019 08:26:55 -0700 | |
changeset 58842 | 6c255334120d |
parent 54824 | adb3a3aa2e52 |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
58842
6c255334120d
8232080: jlink plugins for vendor information and run-time options
mr
parents:
54824
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
36511 | 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.IOException; |
|
28 |
import java.io.PrintWriter; |
|
29 |
import java.nio.file.Files; |
|
30 |
import java.nio.file.Path; |
|
31 |
import java.nio.file.Paths; |
|
32 |
import java.text.MessageFormat; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.Arrays; |
|
35 |
import java.util.Collections; |
|
36 |
import java.util.HashMap; |
|
37 |
import java.util.HashSet; |
|
38 |
import java.util.List; |
|
39 |
import java.util.Locale; |
|
40 |
import java.util.Map; |
|
41 |
import java.util.Map.Entry; |
|
42 |
import java.util.MissingResourceException; |
|
43 |
import java.util.ResourceBundle; |
|
44 |
import java.util.Set; |
|
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
45 |
import java.util.stream.Collectors; |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
46 |
import java.util.stream.Stream; |
36511 | 47 |
|
48 |
import jdk.tools.jlink.builder.DefaultImageBuilder; |
|
49 |
import jdk.tools.jlink.builder.ImageBuilder; |
|
41559
5b33f33df1fb
8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
40261
diff
changeset
|
50 |
import jdk.tools.jlink.internal.Jlink.PluginsConfiguration; |
53548 | 51 |
import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin; |
53923 | 52 |
import jdk.tools.jlink.internal.plugins.DefaultStripDebugPlugin; |
53548 | 53 |
import jdk.tools.jlink.internal.plugins.ExcludeJmodSectionPlugin; |
36511 | 54 |
import jdk.tools.jlink.internal.plugins.PluginsResourceBundle; |
53548 | 55 |
import jdk.tools.jlink.plugin.Plugin; |
56 |
import jdk.tools.jlink.plugin.Plugin.Category; |
|
57 |
import jdk.tools.jlink.plugin.PluginException; |
|
36511 | 58 |
|
59 |
/** |
|
60 |
* |
|
61 |
* JLink and JImage tools shared helper. |
|
62 |
*/ |
|
63 |
public final class TaskHelper { |
|
64 |
||
65 |
public static final String JLINK_BUNDLE = "jdk.tools.jlink.resources.jlink"; |
|
66 |
public static final String JIMAGE_BUNDLE = "jdk.tools.jimage.resources.jimage"; |
|
67 |
||
68 |
private static final String DEFAULTS_PROPERTY = "jdk.jlink.defaults"; |
|
69 |
||
70 |
public final class BadArgs extends Exception { |
|
71 |
||
72 |
static final long serialVersionUID = 8765093759964640721L; |
|
73 |
||
74 |
private BadArgs(String key, Object... args) { |
|
75 |
super(bundleHelper.getMessage(key, args)); |
|
76 |
this.key = key; |
|
77 |
this.args = args; |
|
78 |
} |
|
79 |
||
80 |
public BadArgs showUsage(boolean b) { |
|
81 |
showUsage = b; |
|
82 |
return this; |
|
83 |
} |
|
84 |
public final String key; |
|
85 |
public final Object[] args; |
|
86 |
public boolean showUsage; |
|
87 |
} |
|
88 |
||
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
89 |
public static class Option<T> implements Comparable<T> { |
36511 | 90 |
public interface Processing<T> { |
91 |
||
92 |
void process(T task, String opt, String arg) throws BadArgs; |
|
93 |
} |
|
94 |
||
95 |
final boolean hasArg; |
|
96 |
final Processing<T> processing; |
|
97 |
final boolean hidden; |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
98 |
final String name; |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
99 |
final String shortname; |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
100 |
final String shortname2; |
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
101 |
final boolean terminalOption; |
36511 | 102 |
|
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
103 |
public Option(boolean hasArg, |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
104 |
Processing<T> processing, |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
105 |
boolean hidden, |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
106 |
String name, |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
107 |
String shortname, |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
108 |
String shortname2, |
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
109 |
boolean isTerminal) |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
110 |
{ |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
111 |
if (!name.startsWith("--")) { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
112 |
throw new RuntimeException("option name missing --, " + name); |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
113 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
114 |
if (!shortname.isEmpty() && !shortname.startsWith("-")) { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
115 |
throw new RuntimeException("short name missing -, " + shortname); |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
116 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
117 |
|
36511 | 118 |
this.hasArg = hasArg; |
119 |
this.processing = processing; |
|
120 |
this.hidden = hidden; |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
121 |
this.name = name; |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
122 |
this.shortname = shortname; |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
123 |
this.shortname2 = shortname2; |
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
124 |
this.terminalOption = isTerminal; |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
125 |
} |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
126 |
public Option(boolean hasArg, |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
127 |
Processing<T> processing, |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
128 |
boolean hidden, |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
129 |
String name, |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
130 |
String shortname, |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
131 |
boolean isTerminal) |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
132 |
{ |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
133 |
this(hasArg, processing, false, name, shortname, "", isTerminal); |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
134 |
} |
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
135 |
|
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
136 |
public Option(boolean hasArg, Processing<T> processing, String name, String shortname, boolean isTerminal) { |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
137 |
this(hasArg, processing, false, name, shortname, "", isTerminal); |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
138 |
} |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
139 |
|
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
140 |
public Option(boolean hasArg, Processing<T> processing, String name, String shortname, String shortname2) { |
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
141 |
this(hasArg, processing, false, name, shortname, shortname2, false); |
36511 | 142 |
} |
143 |
||
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
144 |
public Option(boolean hasArg, Processing<T> processing, String name, String shortname) { |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
145 |
this(hasArg, processing, false, name, shortname, "", false); |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
146 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
147 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
148 |
public Option(boolean hasArg, Processing<T> processing, boolean hidden, String name) { |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
149 |
this(hasArg, processing, hidden, name, "", "", false); |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
150 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
151 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
152 |
public Option(boolean hasArg, Processing<T> processing, String name) { |
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
153 |
this(hasArg, processing, false, name, "", false); |
36511 | 154 |
} |
155 |
||
156 |
public boolean isHidden() { |
|
157 |
return hidden; |
|
158 |
} |
|
159 |
||
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
160 |
public boolean isTerminal() { |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
161 |
return terminalOption; |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
162 |
} |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
163 |
|
36511 | 164 |
public boolean matches(String opt) { |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
165 |
return opt.equals(name) || |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
166 |
opt.equals(shortname) || |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
167 |
opt.equals(shortname2) || |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
168 |
hasArg && opt.startsWith("--") && opt.startsWith(name + "="); |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
169 |
} |
36511 | 170 |
|
171 |
public boolean ignoreRest() { |
|
172 |
return false; |
|
173 |
} |
|
174 |
||
175 |
void process(T task, String opt, String arg) throws BadArgs { |
|
176 |
processing.process(task, opt, arg); |
|
177 |
} |
|
38869 | 178 |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
179 |
public String getName() { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
180 |
return name; |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
181 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
182 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
183 |
public String resourceName() { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
184 |
return resourcePrefix() + name.substring(2); |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
185 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
186 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
187 |
public String getShortname() { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
188 |
return shortname; |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
189 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
190 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
191 |
public String resourcePrefix() { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
192 |
return "main.opt."; |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
193 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
194 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
195 |
@Override |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
196 |
public int compareTo(Object object) { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
197 |
if (!(object instanceof Option<?>)) { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
198 |
throw new RuntimeException("comparing non-Option"); |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
199 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
200 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
201 |
Option<?> option = (Option<?>)object; |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
202 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
203 |
return name.compareTo(option.name); |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
204 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
205 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
206 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
207 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
208 |
private static class PluginOption extends Option<PluginsHelper> { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
209 |
public PluginOption(boolean hasArg, |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
210 |
Processing<PluginsHelper> processing, boolean hidden, String name, String shortname) { |
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
211 |
super(hasArg, processing, hidden, name, shortname, false); |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
212 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
213 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
214 |
public PluginOption(boolean hasArg, |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
215 |
Processing<PluginsHelper> processing, boolean hidden, String name) { |
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
216 |
super(hasArg, processing, hidden, name, "", false); |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
217 |
} |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
218 |
|
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
219 |
public String resourcePrefix() { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
220 |
return "plugin.opt."; |
38869 | 221 |
} |
36511 | 222 |
} |
223 |
||
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
224 |
private final class PluginsHelper { |
36511 | 225 |
|
54824
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
226 |
// Duplicated here so as to avoid a direct dependency on platform specific plugin |
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
227 |
private static final String STRIP_NATIVE_DEBUG_SYMBOLS_NAME = "strip-native-debug-symbols"; |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44367
diff
changeset
|
228 |
private ModuleLayer pluginsLayer = ModuleLayer.boot(); |
41831
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
229 |
private final List<Plugin> plugins; |
36511 | 230 |
private String lastSorter; |
231 |
private boolean listPlugins; |
|
232 |
private Path existingImage; |
|
233 |
||
234 |
// plugin to args maps. Each plugin may be used more than once in command line. |
|
235 |
// Each such occurrence results in a Map of arguments. So, there could be multiple |
|
236 |
// args maps per plugin instance. |
|
237 |
private final Map<Plugin, List<Map<String, String>>> pluginToMaps = new HashMap<>(); |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
238 |
private final List<PluginOption> pluginsOptions = new ArrayList<>(); |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
239 |
private final List<PluginOption> mainOptions = new ArrayList<>(); |
36511 | 240 |
|
53548 | 241 |
private PluginsHelper() throws BadArgs { |
36511 | 242 |
|
41831
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
243 |
plugins = PluginRepository.getPlugins(pluginsLayer); |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
244 |
|
36511 | 245 |
Set<String> optionsSeen = new HashSet<>(); |
41831
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
246 |
for (Plugin plugin : plugins) { |
36511 | 247 |
if (!Utils.isDisabled(plugin)) { |
248 |
addOrderedPluginOptions(plugin, optionsSeen); |
|
249 |
} |
|
250 |
} |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
251 |
mainOptions.add(new PluginOption(true, (task, opt, arg) -> { |
41831
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
252 |
for (Plugin plugin : plugins) { |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
253 |
if (plugin.getName().equals(arg)) { |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
254 |
pluginToMaps.remove(plugin); |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
255 |
return; |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
256 |
} |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
257 |
} |
2dd91b18195b
8160063: Provide a means to disable a plugin via the command line
sundar
parents:
41817
diff
changeset
|
258 |
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
|
259 |
}, |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
260 |
false, "--disable-plugin")); |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
261 |
mainOptions.add(new PluginOption(true, (task, opt, arg) -> { |
36511 | 262 |
Path path = Paths.get(arg); |
263 |
if (!Files.exists(path) || !Files.isDirectory(path)) { |
|
41953
3e89e6103470
8155756: Better context for some jlink exceptions
jlaskey
parents:
41919
diff
changeset
|
264 |
throw newBadArgs("err.image.must.exist", path); |
36511 | 265 |
} |
266 |
existingImage = path.toAbsolutePath(); |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
267 |
}, true, "--post-process-path")); |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
268 |
mainOptions.add(new PluginOption(true, |
36511 | 269 |
(task, opt, arg) -> { |
270 |
lastSorter = arg; |
|
271 |
}, |
|
272 |
true, "--resources-last-sorter")); |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
273 |
mainOptions.add(new PluginOption(false, |
36511 | 274 |
(task, opt, arg) -> { |
275 |
listPlugins = true; |
|
276 |
}, |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
277 |
false, "--list-plugins")); |
36511 | 278 |
} |
279 |
||
280 |
private List<Map<String, String>> argListFor(Plugin plugin) { |
|
281 |
List<Map<String, String>> mapList = pluginToMaps.get(plugin); |
|
282 |
if (mapList == null) { |
|
283 |
mapList = new ArrayList<>(); |
|
284 |
pluginToMaps.put(plugin, mapList); |
|
285 |
} |
|
286 |
return mapList; |
|
287 |
} |
|
288 |
||
289 |
private void addEmptyArgumentMap(Plugin plugin) { |
|
290 |
argListFor(plugin).add(Collections.emptyMap()); |
|
291 |
} |
|
292 |
||
293 |
private Map<String, String> addArgumentMap(Plugin plugin) { |
|
294 |
Map<String, String> map = new HashMap<>(); |
|
295 |
argListFor(plugin).add(map); |
|
296 |
return map; |
|
297 |
} |
|
298 |
||
299 |
private void addOrderedPluginOptions(Plugin plugin, |
|
300 |
Set<String> optionsSeen) throws BadArgs { |
|
301 |
String option = plugin.getOption(); |
|
302 |
if (option == null) { |
|
303 |
return; |
|
304 |
} |
|
305 |
||
306 |
// make sure that more than one plugin does not use the same option! |
|
307 |
if (optionsSeen.contains(option)) { |
|
308 |
throw new BadArgs("err.plugin.mutiple.options", |
|
309 |
option); |
|
310 |
} |
|
311 |
optionsSeen.add(option); |
|
312 |
||
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
313 |
PluginOption plugOption |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
314 |
= new PluginOption(plugin.hasArguments(), |
36511 | 315 |
(task, opt, arg) -> { |
316 |
if (!Utils.isFunctional(plugin)) { |
|
317 |
throw newBadArgs("err.provider.not.functional", |
|
318 |
option); |
|
319 |
} |
|
320 |
||
321 |
if (! plugin.hasArguments()) { |
|
322 |
addEmptyArgumentMap(plugin); |
|
323 |
return; |
|
324 |
} |
|
325 |
||
326 |
Map<String, String> m = addArgumentMap(plugin); |
|
327 |
// handle one or more arguments |
|
58842
6c255334120d
8232080: jlink plugins for vendor information and run-time options
mr
parents:
54824
diff
changeset
|
328 |
if (plugin.hasRawArgument() || arg.indexOf(':') == -1) { |
36511 | 329 |
// single argument case |
330 |
m.put(option, arg); |
|
331 |
} else { |
|
332 |
// This option can accept more than one arguments |
|
333 |
// like --option_name=arg_value:arg2=value2:arg3=value3 |
|
334 |
||
335 |
// ":" followed by word char condition takes care of args that |
|
336 |
// like Windows absolute paths "C:\foo", "C:/foo" [cygwin] etc. |
|
337 |
// This enforces that key names start with a word character. |
|
338 |
String[] args = arg.split(":(?=\\w)", -1); |
|
339 |
String firstArg = args[0]; |
|
340 |
if (firstArg.isEmpty()) { |
|
341 |
throw newBadArgs("err.provider.additional.arg.error", |
|
342 |
option, arg); |
|
343 |
} |
|
344 |
m.put(option, firstArg); |
|
345 |
// process the additional arguments |
|
346 |
for (int i = 1; i < args.length; i++) { |
|
347 |
String addArg = args[i]; |
|
348 |
int eqIdx = addArg.indexOf('='); |
|
349 |
if (eqIdx == -1) { |
|
350 |
throw newBadArgs("err.provider.additional.arg.error", |
|
351 |
option, arg); |
|
352 |
} |
|
353 |
||
354 |
String addArgName = addArg.substring(0, eqIdx); |
|
355 |
String addArgValue = addArg.substring(eqIdx+1); |
|
356 |
if (addArgName.isEmpty() || addArgValue.isEmpty()) { |
|
357 |
throw newBadArgs("err.provider.additional.arg.error", |
|
358 |
option, arg); |
|
359 |
} |
|
360 |
m.put(addArgName, addArgValue); |
|
361 |
} |
|
362 |
} |
|
363 |
}, |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
364 |
false, "--" + option); |
36511 | 365 |
pluginsOptions.add(plugOption); |
366 |
||
367 |
if (Utils.isFunctional(plugin)) { |
|
368 |
if (Utils.isAutoEnabled(plugin)) { |
|
369 |
addEmptyArgumentMap(plugin); |
|
370 |
} |
|
371 |
||
372 |
if (plugin instanceof DefaultCompressPlugin) { |
|
373 |
plugOption |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
374 |
= new PluginOption(false, |
36511 | 375 |
(task, opt, arg) -> { |
376 |
Map<String, String> m = addArgumentMap(plugin); |
|
377 |
m.put(DefaultCompressPlugin.NAME, DefaultCompressPlugin.LEVEL_2); |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
378 |
}, false, "--compress", "-c"); |
36511 | 379 |
mainOptions.add(plugOption); |
53923 | 380 |
} else if (plugin instanceof DefaultStripDebugPlugin) { |
36511 | 381 |
plugOption |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
382 |
= new PluginOption(false, |
36511 | 383 |
(task, opt, arg) -> { |
384 |
addArgumentMap(plugin); |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
385 |
}, false, "--strip-debug", "-G"); |
36511 | 386 |
mainOptions.add(plugOption); |
41561
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
387 |
} else if (plugin instanceof ExcludeJmodSectionPlugin) { |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
388 |
plugOption = new PluginOption(false, (task, opt, arg) -> { |
41561
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
389 |
Map<String, String> m = addArgumentMap(plugin); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
390 |
m.put(ExcludeJmodSectionPlugin.NAME, |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
391 |
ExcludeJmodSectionPlugin.MAN_PAGES); |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
392 |
}, false, "--no-man-pages"); |
41561
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
393 |
mainOptions.add(plugOption); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
394 |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
395 |
plugOption = new PluginOption(false, (task, opt, arg) -> { |
41561
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
396 |
Map<String, String> m = addArgumentMap(plugin); |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
397 |
m.put(ExcludeJmodSectionPlugin.NAME, |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
398 |
ExcludeJmodSectionPlugin.INCLUDE_HEADER_FILES); |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
399 |
}, false, "--no-header-files"); |
41561
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41559
diff
changeset
|
400 |
mainOptions.add(plugOption); |
36511 | 401 |
} |
402 |
} |
|
403 |
} |
|
404 |
||
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
405 |
private PluginOption getOption(String name) throws BadArgs { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
406 |
for (PluginOption o : pluginsOptions) { |
36511 | 407 |
if (o.matches(name)) { |
408 |
return o; |
|
409 |
} |
|
410 |
} |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
411 |
for (PluginOption o : mainOptions) { |
36511 | 412 |
if (o.matches(name)) { |
413 |
return o; |
|
414 |
} |
|
415 |
} |
|
416 |
return null; |
|
417 |
} |
|
418 |
||
42762 | 419 |
private PluginsConfiguration getPluginsConfig(Path output, Map<String, String> launchers |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
420 |
) throws IOException, BadArgs { |
36511 | 421 |
if (output != null) { |
422 |
if (Files.exists(output)) { |
|
423 |
throw new PluginException(PluginsResourceBundle. |
|
424 |
getMessage("err.dir.already.exits", output)); |
|
425 |
} |
|
426 |
} |
|
427 |
||
428 |
List<Plugin> pluginsList = new ArrayList<>(); |
|
54824
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
429 |
Set<String> seenPlugins = new HashSet<>(); |
36511 | 430 |
for (Entry<Plugin, List<Map<String, String>>> entry : pluginToMaps.entrySet()) { |
431 |
Plugin plugin = entry.getKey(); |
|
432 |
List<Map<String, String>> argsMaps = entry.getValue(); |
|
433 |
||
434 |
// same plugin option may be used multiple times in command line. |
|
435 |
// we call configure once for each occurrence. It is upto the plugin |
|
436 |
// to 'merge' and/or 'override' arguments. |
|
437 |
for (Map<String, String> map : argsMaps) { |
|
42330
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42174
diff
changeset
|
438 |
try { |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42174
diff
changeset
|
439 |
plugin.configure(Collections.unmodifiableMap(map)); |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42174
diff
changeset
|
440 |
} catch (IllegalArgumentException e) { |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42174
diff
changeset
|
441 |
if (JlinkTask.DEBUG) { |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42174
diff
changeset
|
442 |
System.err.println("Plugin " + plugin.getName() + " threw exception with config: " + map); |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42174
diff
changeset
|
443 |
e.printStackTrace(); |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42174
diff
changeset
|
444 |
} |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42174
diff
changeset
|
445 |
throw e; |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42174
diff
changeset
|
446 |
} |
36511 | 447 |
} |
448 |
||
449 |
if (!Utils.isDisabled(plugin)) { |
|
54824
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
450 |
// make sure that --strip-debug and --strip-native-debug-symbols |
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
451 |
// aren't being used at the same time. --strip-debug invokes --strip-native-debug-symbols on |
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
452 |
// platforms that support it, so it makes little sense to allow both at the same time. |
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
453 |
if ((plugin instanceof DefaultStripDebugPlugin && seenPlugins.contains(STRIP_NATIVE_DEBUG_SYMBOLS_NAME)) || |
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
454 |
(STRIP_NATIVE_DEBUG_SYMBOLS_NAME.equals(plugin.getName()) && seenPlugins.contains(DefaultStripDebugPlugin.NAME))) { |
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
455 |
throw new BadArgs("err.plugin.conflicts", "--" + DefaultStripDebugPlugin.NAME, |
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
456 |
"-G", |
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
457 |
"--" + STRIP_NATIVE_DEBUG_SYMBOLS_NAME); |
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
458 |
} |
36511 | 459 |
pluginsList.add(plugin); |
54824
adb3a3aa2e52
8214796: Create a jlink plugin for stripping debug info symbols from native libraries
sgehwolf
parents:
53923
diff
changeset
|
460 |
seenPlugins.add(plugin.getName()); |
36511 | 461 |
} |
462 |
} |
|
463 |
||
464 |
// recreate or postprocessing don't require an output directory. |
|
465 |
ImageBuilder builder = null; |
|
466 |
if (output != null) { |
|
42762 | 467 |
builder = new DefaultImageBuilder(output, launchers); |
468 |
} |
|
36511 | 469 |
|
470 |
return new Jlink.PluginsConfiguration(pluginsList, |
|
38320 | 471 |
builder, lastSorter); |
36511 | 472 |
} |
473 |
} |
|
474 |
||
475 |
private static final class ResourceBundleHelper { |
|
476 |
||
477 |
private final ResourceBundle bundle; |
|
478 |
private final ResourceBundle pluginBundle; |
|
479 |
||
480 |
ResourceBundleHelper(String path) { |
|
481 |
Locale locale = Locale.getDefault(); |
|
482 |
try { |
|
483 |
bundle = ResourceBundle.getBundle(path, locale); |
|
484 |
pluginBundle = ResourceBundle.getBundle("jdk.tools.jlink.resources.plugins", locale); |
|
485 |
} catch (MissingResourceException e) { |
|
486 |
throw new InternalError("Cannot find jlink resource bundle for locale " + locale); |
|
487 |
} |
|
488 |
} |
|
489 |
||
490 |
String getMessage(String key, Object... args) { |
|
491 |
String val; |
|
492 |
try { |
|
493 |
val = bundle.getString(key); |
|
494 |
} catch (MissingResourceException e) { |
|
495 |
// XXX OK, check in plugin bundle |
|
496 |
val = pluginBundle.getString(key); |
|
497 |
} |
|
498 |
return MessageFormat.format(val, args); |
|
499 |
} |
|
500 |
||
501 |
} |
|
502 |
||
503 |
public final class OptionsHelper<T> { |
|
504 |
||
505 |
private final List<Option<T>> options; |
|
506 |
private String[] command; |
|
507 |
private String defaults; |
|
508 |
||
509 |
OptionsHelper(List<Option<T>> options) { |
|
510 |
this.options = options; |
|
511 |
} |
|
512 |
||
513 |
private boolean hasArgument(String optionName) throws BadArgs { |
|
514 |
Option<?> opt = getOption(optionName); |
|
515 |
if (opt == null) { |
|
516 |
opt = pluginOptions.getOption(optionName); |
|
517 |
if (opt == null) { |
|
518 |
throw new BadArgs("err.unknown.option", optionName). |
|
519 |
showUsage(true); |
|
520 |
} |
|
521 |
} |
|
522 |
return opt.hasArg; |
|
523 |
} |
|
524 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
525 |
public boolean shouldListPlugins() { |
36511 | 526 |
return pluginOptions.listPlugins; |
527 |
} |
|
528 |
||
529 |
private String getPluginsPath(String[] args) throws BadArgs { |
|
43328 | 530 |
return null; |
36511 | 531 |
} |
532 |
||
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
533 |
/** |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
534 |
* Handles all options. This method stops processing the argument |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
535 |
* at the first non-option argument i.e. not starts with `-`, or |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
536 |
* at the first terminal option and returns the remaining arguments, |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
537 |
* if any. |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
538 |
*/ |
36511 | 539 |
public List<String> handleOptions(T task, String[] args) throws BadArgs { |
540 |
// findbugs warning, copy instead of keeping a reference. |
|
541 |
command = Arrays.copyOf(args, args.length); |
|
542 |
||
543 |
// Must extract it prior to do any option analysis. |
|
544 |
// Required to interpret custom plugin options. |
|
545 |
// Unit tests can call Task multiple time in same JVM. |
|
53548 | 546 |
pluginOptions = new PluginsHelper(); |
36511 | 547 |
|
548 |
// process options |
|
549 |
for (int i = 0; i < args.length; i++) { |
|
43987
068a2419b5a7
8169713: jimage fails with StringIndexOutOfBoundsException when path to the inspected image is an empty string
redestad
parents:
43712
diff
changeset
|
550 |
if (args[i].startsWith("-")) { |
36511 | 551 |
String name = args[i]; |
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
552 |
PluginOption pluginOption = null; |
36511 | 553 |
Option<T> option = getOption(name); |
554 |
if (option == null) { |
|
555 |
pluginOption = pluginOptions.getOption(name); |
|
556 |
if (pluginOption == null) { |
|
557 |
throw new BadArgs("err.unknown.option", name). |
|
558 |
showUsage(true); |
|
559 |
} |
|
560 |
} |
|
561 |
Option<?> opt = pluginOption == null ? option : pluginOption; |
|
562 |
String param = null; |
|
563 |
if (opt.hasArg) { |
|
564 |
if (name.startsWith("--") && name.indexOf('=') > 0) { |
|
565 |
param = name.substring(name.indexOf('=') + 1, |
|
566 |
name.length()); |
|
567 |
} else if (i + 1 < args.length) { |
|
568 |
param = args[++i]; |
|
569 |
} |
|
570 |
if (param == null || param.isEmpty() |
|
571 |
|| (param.length() >= 2 && param.charAt(0) == '-' |
|
572 |
&& param.charAt(1) == '-')) { |
|
573 |
throw new BadArgs("err.missing.arg", name). |
|
574 |
showUsage(true); |
|
575 |
} |
|
576 |
} |
|
577 |
if (pluginOption != null) { |
|
578 |
pluginOption.process(pluginOptions, name, param); |
|
579 |
} else { |
|
580 |
option.process(task, name, param); |
|
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
581 |
if (option.isTerminal()) { |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
582 |
return ++i < args.length |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
583 |
? Stream.of(Arrays.copyOfRange(args, i, args.length)) |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
584 |
.collect(Collectors.toList()) |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
585 |
: Collections.emptyList(); |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
586 |
|
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
587 |
} |
36511 | 588 |
} |
589 |
if (opt.ignoreRest()) { |
|
590 |
i = args.length; |
|
591 |
} |
|
592 |
} else { |
|
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
593 |
return Stream.of(Arrays.copyOfRange(args, i, args.length)) |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
594 |
.collect(Collectors.toList()); |
36511 | 595 |
} |
596 |
} |
|
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
43987
diff
changeset
|
597 |
return Collections.emptyList(); |
36511 | 598 |
} |
599 |
||
600 |
private Option<T> getOption(String name) { |
|
601 |
for (Option<T> o : options) { |
|
602 |
if (o.matches(name)) { |
|
603 |
return o; |
|
604 |
} |
|
605 |
} |
|
606 |
return null; |
|
607 |
} |
|
608 |
||
609 |
public void showHelp(String progName) { |
|
610 |
log.println(bundleHelper.getMessage("main.usage", progName)); |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
611 |
Stream.concat(options.stream(), pluginOptions.mainOptions.stream()) |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
612 |
.filter(option -> !option.isHidden()) |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
613 |
.sorted() |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
614 |
.forEach(option -> { |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
615 |
log.println(bundleHelper.getMessage(option.resourceName())); |
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
616 |
}); |
36511 | 617 |
|
618 |
log.println(bundleHelper.getMessage("main.command.files")); |
|
619 |
} |
|
620 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
621 |
public void listPlugins() { |
36511 | 622 |
log.println("\n" + bundleHelper.getMessage("main.extended.help")); |
623 |
List<Plugin> pluginList = PluginRepository. |
|
624 |
getPlugins(pluginOptions.pluginsLayer); |
|
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
625 |
|
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
626 |
for (Plugin plugin : Utils.getSortedPlugins(pluginList)) { |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
627 |
showPlugin(plugin, log); |
36511 | 628 |
} |
39150
cb99396625b4
8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents:
38869
diff
changeset
|
629 |
|
cb99396625b4
8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents:
38869
diff
changeset
|
630 |
log.println("\n" + bundleHelper.getMessage("main.extended.help.footer")); |
36511 | 631 |
} |
632 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
633 |
private void showPlugin(Plugin plugin, PrintWriter log) { |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
634 |
if (showsPlugin(plugin)) { |
36511 | 635 |
log.println("\n" + bundleHelper.getMessage("main.plugin.name") |
636 |
+ ": " + plugin.getName()); |
|
637 |
||
638 |
// print verbose details for non-builtin plugins |
|
639 |
if (!Utils.isBuiltin(plugin)) { |
|
640 |
log.println(bundleHelper.getMessage("main.plugin.class") |
|
641 |
+ ": " + plugin.getClass().getName()); |
|
642 |
log.println(bundleHelper.getMessage("main.plugin.module") |
|
643 |
+ ": " + plugin.getClass().getModule().getName()); |
|
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
644 |
Category category = plugin.getType(); |
36511 | 645 |
log.println(bundleHelper.getMessage("main.plugin.category") |
646 |
+ ": " + category.getName()); |
|
647 |
log.println(bundleHelper.getMessage("main.plugin.state") |
|
648 |
+ ": " + plugin.getStateDescription()); |
|
649 |
} |
|
650 |
||
651 |
String option = plugin.getOption(); |
|
652 |
if (option != null) { |
|
653 |
log.println(bundleHelper.getMessage("main.plugin.option") |
|
654 |
+ ": --" + plugin.getOption() |
|
655 |
+ (plugin.hasArguments()? ("=" + plugin.getArgumentsDescription()) : "")); |
|
656 |
} |
|
657 |
||
658 |
// description can be long spanning more than one line and so |
|
659 |
// print a newline after description label. |
|
660 |
log.println(bundleHelper.getMessage("main.plugin.description") |
|
661 |
+ ": " + plugin.getDescription()); |
|
662 |
} |
|
663 |
} |
|
664 |
||
665 |
String[] getInputCommand() { |
|
666 |
return command; |
|
667 |
} |
|
668 |
||
669 |
String getDefaults() { |
|
670 |
return defaults; |
|
671 |
} |
|
672 |
||
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44367
diff
changeset
|
673 |
public ModuleLayer getPluginsLayer() { |
36511 | 674 |
return pluginOptions.pluginsLayer; |
675 |
} |
|
676 |
} |
|
677 |
||
42174
fa763b059501
8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents:
41953
diff
changeset
|
678 |
private PluginsHelper pluginOptions; |
36511 | 679 |
private PrintWriter log; |
680 |
private final ResourceBundleHelper bundleHelper; |
|
681 |
||
682 |
public TaskHelper(String path) { |
|
683 |
if (!JLINK_BUNDLE.equals(path) && !JIMAGE_BUNDLE.equals(path)) { |
|
684 |
throw new IllegalArgumentException("Invalid Bundle"); |
|
685 |
} |
|
686 |
this.bundleHelper = new ResourceBundleHelper(path); |
|
687 |
} |
|
688 |
||
689 |
public <T> OptionsHelper<T> newOptionsHelper(Class<T> clazz, |
|
690 |
Option<?>[] options) { |
|
691 |
List<Option<T>> optionsList = new ArrayList<>(); |
|
692 |
for (Option<?> o : options) { |
|
693 |
@SuppressWarnings("unchecked") |
|
694 |
Option<T> opt = (Option<T>) o; |
|
695 |
optionsList.add(opt); |
|
696 |
} |
|
697 |
return new OptionsHelper<>(optionsList); |
|
698 |
} |
|
699 |
||
700 |
public BadArgs newBadArgs(String key, Object... args) { |
|
701 |
return new BadArgs(key, args); |
|
702 |
} |
|
703 |
||
704 |
public String getMessage(String key, Object... args) { |
|
705 |
return bundleHelper.getMessage(key, args); |
|
706 |
} |
|
707 |
||
708 |
public void setLog(PrintWriter log) { |
|
709 |
this.log = log; |
|
710 |
} |
|
711 |
||
712 |
public void reportError(String key, Object... args) { |
|
713 |
log.println(bundleHelper.getMessage("error.prefix") + " " |
|
714 |
+ bundleHelper.getMessage(key, args)); |
|
715 |
} |
|
716 |
||
717 |
public void reportUnknownError(String message) { |
|
718 |
log.println(bundleHelper.getMessage("error.prefix") + " " + message); |
|
719 |
} |
|
720 |
||
721 |
public void warning(String key, Object... args) { |
|
722 |
log.println(bundleHelper.getMessage("warn.prefix") + " " |
|
723 |
+ bundleHelper.getMessage(key, args)); |
|
724 |
} |
|
725 |
||
42762 | 726 |
public PluginsConfiguration getPluginsConfig(Path output, Map<String, String> launchers) |
36511 | 727 |
throws IOException, BadArgs { |
42762 | 728 |
return pluginOptions.getPluginsConfig(output, launchers); |
36511 | 729 |
} |
730 |
||
731 |
public Path getExistingImage() { |
|
732 |
return pluginOptions.existingImage; |
|
733 |
} |
|
734 |
||
735 |
public void showVersion(boolean full) { |
|
736 |
log.println(version(full ? "full" : "release")); |
|
737 |
} |
|
738 |
||
739 |
public String version(String key) { |
|
740 |
return System.getProperty("java.version"); |
|
741 |
} |
|
742 |
||
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
743 |
// Display all plugins |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
744 |
private static boolean showsPlugin(Plugin plugin) { |
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39150
diff
changeset
|
745 |
return (!Utils.isDisabled(plugin) && plugin.getOption() != null); |
36511 | 746 |
} |
747 |
} |