author | pchilanomate |
Fri, 31 Aug 2018 10:22:04 -0400 | |
changeset 51610 | cdef4df6b0e7 |
parent 47464 | 36de9c637393 |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, 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 |
*/ |
|
41559
5b33f33df1fb
8168091: jlink should check security permission early when programmatic access is used
sundar
parents:
39321
diff
changeset
|
25 |
package jdk.tools.jlink.internal; |
36511 | 26 |
|
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
27 |
import java.lang.module.Configuration; |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
28 |
import java.lang.module.ModuleFinder; |
36511 | 29 |
import java.nio.ByteOrder; |
47464
36de9c637393
8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents:
47216
diff
changeset
|
30 |
import java.nio.file.Files; |
36511 | 31 |
import java.nio.file.Path; |
47464
36de9c637393
8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents:
47216
diff
changeset
|
32 |
import java.nio.file.Paths; |
42150
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
33 |
import java.util.ArrayList; |
36511 | 34 |
import java.util.Collections; |
35 |
import java.util.List; |
|
36 |
import java.util.Map; |
|
37 |
import java.util.Objects; |
|
38 |
import java.util.Set; |
|
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
39 |
|
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
40 |
import jdk.internal.module.ModulePath; |
36511 | 41 |
import jdk.tools.jlink.plugin.Plugin; |
42 |
import jdk.tools.jlink.plugin.PluginException; |
|
43 |
import jdk.tools.jlink.builder.ImageBuilder; |
|
44 |
||
45 |
/** |
|
46 |
* API to call jlink. |
|
47 |
*/ |
|
48 |
public final class Jlink { |
|
49 |
||
50 |
/** |
|
51 |
* Create a plugin. |
|
52 |
* |
|
53 |
* @param name Plugin name |
|
54 |
* @param configuration Plugin configuration. |
|
55 |
* @param pluginsLayer Plugins Layer. null means boot layer. |
|
56 |
* @return A new plugin or null if plugin is unknown. |
|
57 |
*/ |
|
58 |
public static Plugin newPlugin(String name, |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44367
diff
changeset
|
59 |
Map<String, String> configuration, ModuleLayer pluginsLayer) { |
36511 | 60 |
Objects.requireNonNull(name); |
61 |
Objects.requireNonNull(configuration); |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44367
diff
changeset
|
62 |
pluginsLayer = pluginsLayer == null ? ModuleLayer.boot() : pluginsLayer; |
36511 | 63 |
return PluginRepository.newPlugin(configuration, name, pluginsLayer); |
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* A complete plugin configuration. Instances of this class are used to |
|
68 |
* configure jlink. |
|
69 |
*/ |
|
70 |
public static final class PluginsConfiguration { |
|
71 |
||
72 |
private final List<Plugin> plugins; |
|
73 |
private final ImageBuilder imageBuilder; |
|
74 |
private final String lastSorterPluginName; |
|
75 |
||
76 |
/** |
|
77 |
* Empty plugins configuration. |
|
78 |
*/ |
|
79 |
public PluginsConfiguration() { |
|
80 |
this(Collections.emptyList()); |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Plugins configuration. |
|
85 |
* |
|
86 |
* @param plugins List of plugins. |
|
87 |
*/ |
|
88 |
public PluginsConfiguration(List<Plugin> plugins) { |
|
38320 | 89 |
this(plugins, null, null); |
36511 | 90 |
} |
91 |
||
92 |
/** |
|
93 |
* Plugins configuration with a last sorter and an ImageBuilder. No |
|
94 |
* sorting can occur after the last sorter plugin. The ImageBuilder is |
|
95 |
* in charge to layout the image content on disk. |
|
96 |
* |
|
97 |
* @param plugins List of transformer plugins. |
|
98 |
* @param imageBuilder Image builder. |
|
99 |
* @param lastSorterPluginName Name of last sorter plugin, no sorting |
|
100 |
* can occur after it. |
|
101 |
*/ |
|
102 |
public PluginsConfiguration(List<Plugin> plugins, |
|
103 |
ImageBuilder imageBuilder, String lastSorterPluginName) { |
|
104 |
this.plugins = plugins == null ? Collections.emptyList() |
|
105 |
: plugins; |
|
106 |
this.imageBuilder = imageBuilder; |
|
107 |
this.lastSorterPluginName = lastSorterPluginName; |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* @return the plugins |
|
112 |
*/ |
|
113 |
public List<Plugin> getPlugins() { |
|
114 |
return plugins; |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* @return the imageBuilder |
|
119 |
*/ |
|
120 |
public ImageBuilder getImageBuilder() { |
|
121 |
return imageBuilder; |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* @return the lastSorterPluginName |
|
126 |
*/ |
|
127 |
public String getLastSorterPluginName() { |
|
128 |
return lastSorterPluginName; |
|
129 |
} |
|
130 |
||
131 |
@Override |
|
132 |
public String toString() { |
|
133 |
StringBuilder builder = new StringBuilder(); |
|
134 |
builder.append("imagebuilder=").append(imageBuilder).append("\n"); |
|
135 |
StringBuilder pluginsBuilder = new StringBuilder(); |
|
136 |
for (Plugin p : plugins) { |
|
137 |
pluginsBuilder.append(p).append(","); |
|
138 |
} |
|
139 |
builder.append("plugins=").append(pluginsBuilder).append("\n"); |
|
140 |
builder.append("lastsorter=").append(lastSorterPluginName).append("\n"); |
|
141 |
||
142 |
return builder.toString(); |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Jlink configuration. Instances of this class are used to configure jlink. |
|
148 |
*/ |
|
149 |
public static final class JlinkConfiguration { |
|
150 |
||
151 |
private final Path output; |
|
152 |
private final Set<String> modules; |
|
153 |
private final ByteOrder endian; |
|
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
154 |
private final ModuleFinder finder; |
36511 | 155 |
|
156 |
/** |
|
157 |
* jlink configuration, |
|
158 |
* |
|
159 |
* @param output Output directory, must not exist. |
|
44747
dc7378149f20
8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents:
44545
diff
changeset
|
160 |
* @param modules The possibly-empty set of root modules to resolve |
36511 | 161 |
* @param endian Jimage byte order. Native order by default |
47464
36de9c637393
8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents:
47216
diff
changeset
|
162 |
* @param finder the ModuleFinder for this configuration |
36511 | 163 |
*/ |
164 |
public JlinkConfiguration(Path output, |
|
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
165 |
Set<String> modules, |
47464
36de9c637393
8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents:
47216
diff
changeset
|
166 |
ByteOrder endian, |
36de9c637393
8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents:
47216
diff
changeset
|
167 |
ModuleFinder finder) { |
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
168 |
this.output = output; |
44747
dc7378149f20
8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents:
44545
diff
changeset
|
169 |
this.modules = Objects.requireNonNull(modules); |
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
170 |
this.endian = Objects.requireNonNull(endian); |
47464
36de9c637393
8189777: jlink --module-path default value and automatic addition of $JAVA_HOME/jmods if java.base is missing
sundar
parents:
47216
diff
changeset
|
171 |
this.finder = finder; |
36511 | 172 |
} |
173 |
||
174 |
/** |
|
175 |
* @return the byte ordering |
|
176 |
*/ |
|
177 |
public ByteOrder getByteOrder() { |
|
178 |
return endian; |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* @return the output |
|
183 |
*/ |
|
184 |
public Path getOutput() { |
|
185 |
return output; |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* @return the modules |
|
190 |
*/ |
|
191 |
public Set<String> getModules() { |
|
192 |
return modules; |
|
193 |
} |
|
194 |
||
195 |
/** |
|
44367
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
196 |
* Returns {@link ModuleFinder} that finds all observable modules |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
197 |
* for this jlink configuration. |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
198 |
*/ |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
199 |
public ModuleFinder finder() { |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
200 |
return finder; |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
201 |
} |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
202 |
|
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
203 |
/** |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
204 |
* Returns a {@link Configuration} of the given module path, |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
205 |
* root modules with full service binding. |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
206 |
*/ |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
207 |
public Configuration resolveAndBind() |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
208 |
{ |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
209 |
return Configuration.empty().resolveAndBind(finder, |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
210 |
ModuleFinder.of(), |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
211 |
modules); |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
212 |
} |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
213 |
|
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
214 |
/** |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
215 |
* Returns a {@link Configuration} of the given module path, |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
216 |
* root modules with no service binding. |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
217 |
*/ |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
218 |
public Configuration resolve() |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
219 |
{ |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
220 |
return Configuration.empty().resolve(finder, |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
221 |
ModuleFinder.of(), |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
222 |
modules); |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
223 |
} |
a81c9c6619fb
8174826: jlink support for linking in service provider modules
mchung
parents:
42330
diff
changeset
|
224 |
|
36511 | 225 |
@Override |
226 |
public String toString() { |
|
227 |
StringBuilder builder = new StringBuilder(); |
|
228 |
||
229 |
builder.append("output=").append(output).append("\n"); |
|
230 |
StringBuilder modsBuilder = new StringBuilder(); |
|
231 |
for (String p : modules) { |
|
232 |
modsBuilder.append(p).append(","); |
|
233 |
} |
|
234 |
builder.append("modules=").append(modsBuilder).append("\n"); |
|
235 |
builder.append("endian=").append(endian).append("\n"); |
|
236 |
return builder.toString(); |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Jlink instance constructor, if a security manager is set, the jlink |
|
242 |
* permission is checked. |
|
243 |
*/ |
|
244 |
public Jlink() { |
|
245 |
if (System.getSecurityManager() != null) { |
|
246 |
System.getSecurityManager(). |
|
247 |
checkPermission(new JlinkPermission("jlink")); |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Build the image. |
|
253 |
* |
|
254 |
* @param config Jlink config, must not be null. |
|
255 |
* @throws PluginException |
|
256 |
*/ |
|
257 |
public void build(JlinkConfiguration config) { |
|
258 |
build(config, null); |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* Build the image with a plugin configuration. |
|
263 |
* |
|
264 |
* @param config Jlink config, must not be null. |
|
265 |
* @param pluginsConfig Plugins config, can be null |
|
266 |
* @throws PluginException |
|
267 |
*/ |
|
268 |
public void build(JlinkConfiguration config, PluginsConfiguration pluginsConfig) { |
|
269 |
Objects.requireNonNull(config); |
|
42150
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
270 |
if (pluginsConfig == null) { |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
271 |
pluginsConfig = new PluginsConfiguration(); |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
272 |
} |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
273 |
|
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
274 |
// add all auto-enabled plugins from boot layer |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
275 |
pluginsConfig = addAutoEnabledPlugins(pluginsConfig); |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
276 |
|
36511 | 277 |
try { |
278 |
JlinkTask.createImage(config, pluginsConfig); |
|
279 |
} catch (Exception ex) { |
|
280 |
throw new PluginException(ex); |
|
281 |
} |
|
282 |
} |
|
283 |
||
42150
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
284 |
private PluginsConfiguration addAutoEnabledPlugins(PluginsConfiguration pluginsConfig) { |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
285 |
List<Plugin> plugins = new ArrayList<>(pluginsConfig.getPlugins()); |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44367
diff
changeset
|
286 |
List<Plugin> bootPlugins = PluginRepository.getPlugins(ModuleLayer.boot()); |
42150
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
287 |
for (Plugin bp : bootPlugins) { |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
288 |
if (Utils.isAutoEnabled(bp)) { |
42330
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42150
diff
changeset
|
289 |
try { |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42150
diff
changeset
|
290 |
bp.configure(Collections.emptyMap()); |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42150
diff
changeset
|
291 |
} catch (IllegalArgumentException e) { |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42150
diff
changeset
|
292 |
if (JlinkTask.DEBUG) { |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42150
diff
changeset
|
293 |
System.err.println("Plugin " + bp.getName() + " threw exception with config: {}"); |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42150
diff
changeset
|
294 |
e.printStackTrace(); |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42150
diff
changeset
|
295 |
} |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42150
diff
changeset
|
296 |
throw e; |
bd999576b78d
8160359: Improve jlink logging for cases when a plugin throws exception
sundar
parents:
42150
diff
changeset
|
297 |
} |
42150
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
298 |
plugins.add(bp); |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
299 |
} |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
300 |
} |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
301 |
return new PluginsConfiguration(plugins, pluginsConfig.getImageBuilder(), |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
302 |
pluginsConfig.getLastSorterPluginName()); |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
303 |
} |
dd0623828f16
8153038: The set of jlink plugins enabled by default should be the same via CLI or jlink API
sundar
parents:
41559
diff
changeset
|
304 |
|
36511 | 305 |
/** |
306 |
* Post process the image with a plugin configuration. |
|
307 |
* |
|
308 |
* @param image Existing image. |
|
309 |
* @param plugins Plugins cannot be null |
|
310 |
*/ |
|
311 |
public void postProcess(ExecutableImage image, List<Plugin> plugins) { |
|
312 |
Objects.requireNonNull(image); |
|
313 |
Objects.requireNonNull(plugins); |
|
314 |
try { |
|
315 |
JlinkTask.postProcessImage(image, plugins); |
|
316 |
} catch (Exception ex) { |
|
317 |
throw new PluginException(ex); |
|
318 |
} |
|
319 |
} |
|
320 |
} |