1 /* |
|
2 * Copyright (c) 2015, 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; |
|
26 |
|
27 import java.lang.reflect.Layer; |
|
28 import java.nio.ByteOrder; |
|
29 import java.nio.file.Path; |
|
30 import java.util.Collections; |
|
31 import java.util.List; |
|
32 import java.util.Map; |
|
33 import java.util.Objects; |
|
34 import java.util.Set; |
|
35 import jdk.tools.jlink.internal.ExecutableImage; |
|
36 import jdk.tools.jlink.internal.JlinkTask; |
|
37 import jdk.tools.jlink.plugin.Plugin; |
|
38 import jdk.tools.jlink.plugin.PluginException; |
|
39 import jdk.tools.jlink.builder.ImageBuilder; |
|
40 import jdk.tools.jlink.internal.PluginRepository; |
|
41 |
|
42 /** |
|
43 * API to call jlink. |
|
44 */ |
|
45 public final class Jlink { |
|
46 |
|
47 /** |
|
48 * Create a plugin. |
|
49 * |
|
50 * @param name Plugin name |
|
51 * @param configuration Plugin configuration. |
|
52 * @param pluginsLayer Plugins Layer. null means boot layer. |
|
53 * @return A new plugin or null if plugin is unknown. |
|
54 */ |
|
55 public static Plugin newPlugin(String name, |
|
56 Map<String, String> configuration, Layer pluginsLayer) { |
|
57 Objects.requireNonNull(name); |
|
58 Objects.requireNonNull(configuration); |
|
59 pluginsLayer = pluginsLayer == null ? Layer.boot() : pluginsLayer; |
|
60 return PluginRepository.newPlugin(configuration, name, pluginsLayer); |
|
61 } |
|
62 |
|
63 /** |
|
64 * A complete plugin configuration. Instances of this class are used to |
|
65 * configure jlink. |
|
66 */ |
|
67 public static final class PluginsConfiguration { |
|
68 |
|
69 private final List<Plugin> plugins; |
|
70 private final ImageBuilder imageBuilder; |
|
71 private final String lastSorterPluginName; |
|
72 |
|
73 /** |
|
74 * Empty plugins configuration. |
|
75 */ |
|
76 public PluginsConfiguration() { |
|
77 this(Collections.emptyList()); |
|
78 } |
|
79 |
|
80 /** |
|
81 * Plugins configuration. |
|
82 * |
|
83 * @param plugins List of plugins. |
|
84 */ |
|
85 public PluginsConfiguration(List<Plugin> plugins) { |
|
86 this(plugins, null, null); |
|
87 } |
|
88 |
|
89 /** |
|
90 * Plugins configuration with a last sorter and an ImageBuilder. No |
|
91 * sorting can occur after the last sorter plugin. The ImageBuilder is |
|
92 * in charge to layout the image content on disk. |
|
93 * |
|
94 * @param plugins List of transformer plugins. |
|
95 * @param imageBuilder Image builder. |
|
96 * @param lastSorterPluginName Name of last sorter plugin, no sorting |
|
97 * can occur after it. |
|
98 */ |
|
99 public PluginsConfiguration(List<Plugin> plugins, |
|
100 ImageBuilder imageBuilder, String lastSorterPluginName) { |
|
101 this.plugins = plugins == null ? Collections.emptyList() |
|
102 : plugins; |
|
103 this.imageBuilder = imageBuilder; |
|
104 this.lastSorterPluginName = lastSorterPluginName; |
|
105 } |
|
106 |
|
107 /** |
|
108 * @return the plugins |
|
109 */ |
|
110 public List<Plugin> getPlugins() { |
|
111 return plugins; |
|
112 } |
|
113 |
|
114 /** |
|
115 * @return the imageBuilder |
|
116 */ |
|
117 public ImageBuilder getImageBuilder() { |
|
118 return imageBuilder; |
|
119 } |
|
120 |
|
121 /** |
|
122 * @return the lastSorterPluginName |
|
123 */ |
|
124 public String getLastSorterPluginName() { |
|
125 return lastSorterPluginName; |
|
126 } |
|
127 |
|
128 @Override |
|
129 public String toString() { |
|
130 StringBuilder builder = new StringBuilder(); |
|
131 builder.append("imagebuilder=").append(imageBuilder).append("\n"); |
|
132 StringBuilder pluginsBuilder = new StringBuilder(); |
|
133 for (Plugin p : plugins) { |
|
134 pluginsBuilder.append(p).append(","); |
|
135 } |
|
136 builder.append("plugins=").append(pluginsBuilder).append("\n"); |
|
137 builder.append("lastsorter=").append(lastSorterPluginName).append("\n"); |
|
138 |
|
139 return builder.toString(); |
|
140 } |
|
141 } |
|
142 |
|
143 /** |
|
144 * Jlink configuration. Instances of this class are used to configure jlink. |
|
145 */ |
|
146 public static final class JlinkConfiguration { |
|
147 |
|
148 private final List<Path> modulepaths; |
|
149 private final Path output; |
|
150 private final Set<String> modules; |
|
151 private final Set<String> limitmods; |
|
152 |
|
153 private final ByteOrder endian; |
|
154 |
|
155 /** |
|
156 * jlink configuration, |
|
157 * |
|
158 * @param output Output directory, must not exist. |
|
159 * @param modulepaths Modules paths |
|
160 * @param modules Root modules to resolve |
|
161 * @param limitmods Limit the universe of observable modules |
|
162 * @param endian Jimage byte order. Native order by default |
|
163 */ |
|
164 public JlinkConfiguration(Path output, |
|
165 List<Path> modulepaths, |
|
166 Set<String> modules, |
|
167 Set<String> limitmods, |
|
168 ByteOrder endian) { |
|
169 this.output = output; |
|
170 this.modulepaths = modulepaths == null ? Collections.emptyList() : modulepaths; |
|
171 this.modules = modules == null ? Collections.emptySet() : modules; |
|
172 this.limitmods = limitmods == null ? Collections.emptySet() : limitmods; |
|
173 this.endian = endian == null ? ByteOrder.nativeOrder() : endian; |
|
174 } |
|
175 |
|
176 /** |
|
177 * jlink configuration, |
|
178 * |
|
179 * @param output Output directory, must not exist. |
|
180 * @param modulepaths Modules paths |
|
181 * @param modules Root modules to resolve |
|
182 * @param limitmods Limit the universe of observable modules |
|
183 */ |
|
184 public JlinkConfiguration(Path output, |
|
185 List<Path> modulepaths, |
|
186 Set<String> modules, |
|
187 Set<String> limitmods) { |
|
188 this(output, modulepaths, modules, limitmods, |
|
189 ByteOrder.nativeOrder()); |
|
190 } |
|
191 |
|
192 /** |
|
193 * @return the modulepaths |
|
194 */ |
|
195 public List<Path> getModulepaths() { |
|
196 return modulepaths; |
|
197 } |
|
198 |
|
199 /** |
|
200 * @return the byte ordering |
|
201 */ |
|
202 public ByteOrder getByteOrder() { |
|
203 return endian; |
|
204 } |
|
205 |
|
206 /** |
|
207 * @return the output |
|
208 */ |
|
209 public Path getOutput() { |
|
210 return output; |
|
211 } |
|
212 |
|
213 /** |
|
214 * @return the modules |
|
215 */ |
|
216 public Set<String> getModules() { |
|
217 return modules; |
|
218 } |
|
219 |
|
220 /** |
|
221 * @return the limitmods |
|
222 */ |
|
223 public Set<String> getLimitmods() { |
|
224 return limitmods; |
|
225 } |
|
226 |
|
227 @Override |
|
228 public String toString() { |
|
229 StringBuilder builder = new StringBuilder(); |
|
230 |
|
231 builder.append("output=").append(output).append("\n"); |
|
232 StringBuilder pathsBuilder = new StringBuilder(); |
|
233 for (Path p : modulepaths) { |
|
234 pathsBuilder.append(p).append(","); |
|
235 } |
|
236 builder.append("modulepaths=").append(pathsBuilder).append("\n"); |
|
237 |
|
238 StringBuilder modsBuilder = new StringBuilder(); |
|
239 for (String p : modules) { |
|
240 modsBuilder.append(p).append(","); |
|
241 } |
|
242 builder.append("modules=").append(modsBuilder).append("\n"); |
|
243 |
|
244 StringBuilder limitsBuilder = new StringBuilder(); |
|
245 for (String p : limitmods) { |
|
246 limitsBuilder.append(p).append(","); |
|
247 } |
|
248 builder.append("limitmodules=").append(limitsBuilder).append("\n"); |
|
249 builder.append("endian=").append(endian).append("\n"); |
|
250 return builder.toString(); |
|
251 } |
|
252 } |
|
253 |
|
254 /** |
|
255 * Jlink instance constructor, if a security manager is set, the jlink |
|
256 * permission is checked. |
|
257 */ |
|
258 public Jlink() { |
|
259 if (System.getSecurityManager() != null) { |
|
260 System.getSecurityManager(). |
|
261 checkPermission(new JlinkPermission("jlink")); |
|
262 } |
|
263 } |
|
264 |
|
265 /** |
|
266 * Build the image. |
|
267 * |
|
268 * @param config Jlink config, must not be null. |
|
269 * @throws PluginException |
|
270 */ |
|
271 public void build(JlinkConfiguration config) { |
|
272 build(config, null); |
|
273 } |
|
274 |
|
275 /** |
|
276 * Build the image with a plugin configuration. |
|
277 * |
|
278 * @param config Jlink config, must not be null. |
|
279 * @param pluginsConfig Plugins config, can be null |
|
280 * @throws PluginException |
|
281 */ |
|
282 public void build(JlinkConfiguration config, PluginsConfiguration pluginsConfig) { |
|
283 Objects.requireNonNull(config); |
|
284 try { |
|
285 JlinkTask.createImage(config, pluginsConfig); |
|
286 } catch (Exception ex) { |
|
287 throw new PluginException(ex); |
|
288 } |
|
289 } |
|
290 |
|
291 /** |
|
292 * Post process the image with a plugin configuration. |
|
293 * |
|
294 * @param image Existing image. |
|
295 * @param plugins Plugins cannot be null |
|
296 */ |
|
297 public void postProcess(ExecutableImage image, List<Plugin> plugins) { |
|
298 Objects.requireNonNull(image); |
|
299 Objects.requireNonNull(plugins); |
|
300 try { |
|
301 JlinkTask.postProcessImage(image, plugins); |
|
302 } catch (Exception ex) { |
|
303 throw new PluginException(ex); |
|
304 } |
|
305 } |
|
306 } |
|