author | mchung |
Sun, 19 Jun 2016 16:46:49 -0700 | |
changeset 39125 | e97953d38291 |
parent 38869 | 4bac8f3bce74 |
child 39156 | 92844b32bcec |
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.io.UncheckedIOException; |
|
31 |
import java.lang.module.Configuration; |
|
32 |
import java.lang.module.ModuleFinder; |
|
33 |
import java.lang.module.ModuleReference; |
|
34 |
import java.lang.module.ResolutionException; |
|
35 |
import java.lang.module.ResolvedModule; |
|
36 |
import java.lang.reflect.InvocationTargetException; |
|
37 |
import java.net.URI; |
|
38 |
import java.nio.ByteOrder; |
|
39 |
import java.nio.file.Files; |
|
40 |
import java.nio.file.Path; |
|
41 |
import java.nio.file.Paths; |
|
39125
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
42 |
import java.util.*; |
36511 | 43 |
import java.util.stream.Collectors; |
44 |
||
45 |
import jdk.internal.module.ConfigurableModuleFinder; |
|
46 |
import jdk.internal.module.ConfigurableModuleFinder.Phase; |
|
47 |
import jdk.tools.jlink.internal.TaskHelper.BadArgs; |
|
48 |
import static jdk.tools.jlink.internal.TaskHelper.JLINK_BUNDLE; |
|
49 |
import jdk.tools.jlink.internal.TaskHelper.Option; |
|
50 |
import jdk.tools.jlink.internal.TaskHelper.OptionsHelper; |
|
51 |
import jdk.tools.jlink.internal.ImagePluginStack.ImageProvider; |
|
52 |
import jdk.tools.jlink.plugin.ExecutableImage; |
|
53 |
import jdk.tools.jlink.Jlink.JlinkConfiguration; |
|
54 |
import jdk.tools.jlink.Jlink.PluginsConfiguration; |
|
55 |
import jdk.tools.jlink.plugin.PluginException; |
|
56 |
import jdk.tools.jlink.builder.DefaultImageBuilder; |
|
57 |
import jdk.tools.jlink.plugin.Plugin; |
|
58 |
||
59 |
/** |
|
60 |
* Implementation for the jlink tool. |
|
61 |
* |
|
62 |
* ## Should use jdk.joptsimple some day. |
|
63 |
*/ |
|
64 |
public class JlinkTask { |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
65 |
private static final boolean DEBUG = Boolean.getBoolean("jlink.debug"); |
36511 | 66 |
|
67 |
private static <T extends Throwable> void fail(Class<T> type, |
|
68 |
String format, |
|
69 |
Object... args) throws T { |
|
70 |
String msg = new Formatter().format(format, args).toString(); |
|
71 |
try { |
|
72 |
T t = type.getConstructor(String.class).newInstance(msg); |
|
73 |
throw t; |
|
74 |
} catch (InstantiationException | |
|
75 |
InvocationTargetException | |
|
76 |
NoSuchMethodException | |
|
77 |
IllegalAccessException e) { |
|
78 |
throw new InternalError("Unable to create an instance of " + type, e); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
private static final TaskHelper taskHelper |
|
83 |
= new TaskHelper(JLINK_BUNDLE); |
|
84 |
||
85 |
static Option<?>[] recognizedOptions = { |
|
86 |
new Option<JlinkTask>(false, (task, opt, arg) -> { |
|
87 |
task.options.help = true; |
|
88 |
}, "--help"), |
|
89 |
new Option<JlinkTask>(true, (task, opt, arg) -> { |
|
90 |
String[] dirs = arg.split(File.pathSeparator); |
|
39125
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
91 |
Arrays.stream(dirs) |
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
92 |
.map(Paths::get) |
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
93 |
.forEach(task.options.modulePath::add); |
36511 | 94 |
}, "--modulepath", "--mp"), |
95 |
new Option<JlinkTask>(true, (task, opt, arg) -> { |
|
96 |
for (String mn : arg.split(",")) { |
|
97 |
if (mn.isEmpty()) { |
|
98 |
throw taskHelper.newBadArgs("err.mods.must.be.specified", |
|
99 |
"--limitmods"); |
|
100 |
} |
|
101 |
task.options.limitMods.add(mn); |
|
102 |
} |
|
103 |
}, "--limitmods"), |
|
104 |
new Option<JlinkTask>(true, (task, opt, arg) -> { |
|
105 |
for (String mn : arg.split(",")) { |
|
106 |
if (mn.isEmpty()) { |
|
107 |
throw taskHelper.newBadArgs("err.mods.must.be.specified", |
|
108 |
"--addmods"); |
|
109 |
} |
|
110 |
task.options.addMods.add(mn); |
|
111 |
} |
|
112 |
}, "--addmods"), |
|
113 |
new Option<JlinkTask>(true, (task, opt, arg) -> { |
|
114 |
Path path = Paths.get(arg); |
|
115 |
task.options.output = path; |
|
116 |
}, "--output"), |
|
117 |
new Option<JlinkTask>(true, (task, opt, arg) -> { |
|
118 |
if ("little".equals(arg)) { |
|
119 |
task.options.endian = ByteOrder.LITTLE_ENDIAN; |
|
120 |
} else if ("big".equals(arg)) { |
|
121 |
task.options.endian = ByteOrder.BIG_ENDIAN; |
|
122 |
} else { |
|
123 |
throw taskHelper.newBadArgs("err.unknown.byte.order", arg); |
|
124 |
} |
|
125 |
}, "--endian"), |
|
126 |
new Option<JlinkTask>(false, (task, opt, arg) -> { |
|
127 |
task.options.version = true; |
|
128 |
}, "--version"), |
|
129 |
new Option<JlinkTask>(true, (task, opt, arg) -> { |
|
130 |
Path path = Paths.get(arg); |
|
131 |
if (Files.exists(path)) { |
|
132 |
throw taskHelper.newBadArgs("err.dir.exists", path); |
|
133 |
} |
|
134 |
task.options.packagedModulesPath = path; |
|
135 |
}, true, "--keep-packaged-modules"), |
|
136 |
new Option<JlinkTask>(true, (task, opt, arg) -> { |
|
137 |
task.options.saveoptsfile = arg; |
|
138 |
}, "--saveopts"), |
|
139 |
new Option<JlinkTask>(false, (task, opt, arg) -> { |
|
140 |
task.options.fullVersion = true; |
|
141 |
}, true, "--fullversion"),}; |
|
142 |
||
143 |
private static final String PROGNAME = "jlink"; |
|
144 |
private final OptionsValues options = new OptionsValues(); |
|
145 |
||
146 |
private static final OptionsHelper<JlinkTask> optionsHelper |
|
147 |
= taskHelper.newOptionsHelper(JlinkTask.class, recognizedOptions); |
|
148 |
private PrintWriter log; |
|
149 |
||
150 |
void setLog(PrintWriter out) { |
|
151 |
log = out; |
|
152 |
taskHelper.setLog(log); |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Result codes. |
|
157 |
*/ |
|
158 |
static final int EXIT_OK = 0, // Completed with no errors. |
|
159 |
EXIT_ERROR = 1, // Completed but reported errors. |
|
160 |
EXIT_CMDERR = 2, // Bad command-line arguments |
|
161 |
EXIT_SYSERR = 3, // System error or resource exhaustion. |
|
162 |
EXIT_ABNORMAL = 4;// terminated abnormally |
|
163 |
||
164 |
static class OptionsValues { |
|
165 |
boolean help; |
|
166 |
String saveoptsfile; |
|
167 |
boolean version; |
|
168 |
boolean fullVersion; |
|
39125
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
169 |
List<Path> modulePath = new ArrayList<>(); |
36511 | 170 |
Set<String> limitMods = new HashSet<>(); |
171 |
Set<String> addMods = new HashSet<>(); |
|
172 |
Path output; |
|
173 |
Path packagedModulesPath; |
|
174 |
ByteOrder endian = ByteOrder.nativeOrder(); |
|
175 |
} |
|
176 |
||
177 |
int run(String[] args) { |
|
178 |
if (log == null) { |
|
38869 | 179 |
setLog(new PrintWriter(System.err, true)); |
36511 | 180 |
} |
181 |
try { |
|
182 |
optionsHelper.handleOptions(this, args); |
|
183 |
if (options.help) { |
|
184 |
optionsHelper.showHelp(PROGNAME); |
|
185 |
return EXIT_OK; |
|
186 |
} |
|
187 |
if (optionsHelper.listPlugins()) { |
|
188 |
optionsHelper.listPlugins(true); |
|
189 |
return EXIT_OK; |
|
190 |
} |
|
191 |
if (options.version || options.fullVersion) { |
|
192 |
taskHelper.showVersion(options.fullVersion); |
|
193 |
return EXIT_OK; |
|
194 |
} |
|
195 |
if (taskHelper.getExistingImage() == null) { |
|
39125
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
196 |
if (options.modulePath == null || options.modulePath.isEmpty()) { |
36511 | 197 |
throw taskHelper.newBadArgs("err.modulepath.must.be.specified").showUsage(true); |
198 |
} |
|
199 |
createImage(); |
|
200 |
} else { |
|
201 |
postProcessOnly(taskHelper.getExistingImage()); |
|
202 |
} |
|
203 |
||
204 |
if (options.saveoptsfile != null) { |
|
205 |
Files.write(Paths.get(options.saveoptsfile), getSaveOpts().getBytes()); |
|
206 |
} |
|
207 |
||
208 |
return EXIT_OK; |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
209 |
} catch (UncheckedIOException | PluginException | IllegalArgumentException | |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
210 |
IOException | ResolutionException e) { |
36511 | 211 |
log.println(taskHelper.getMessage("error.prefix") + " " + e.getMessage()); |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
212 |
if (DEBUG) { |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
213 |
e.printStackTrace(log); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
214 |
} |
36511 | 215 |
return EXIT_ERROR; |
216 |
} catch (BadArgs e) { |
|
217 |
taskHelper.reportError(e.key, e.args); |
|
218 |
if (e.showUsage) { |
|
219 |
log.println(taskHelper.getMessage("main.usage.summary", PROGNAME)); |
|
220 |
} |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
221 |
if (DEBUG) { |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
222 |
e.printStackTrace(log); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
223 |
} |
36511 | 224 |
return EXIT_CMDERR; |
225 |
} catch (Throwable x) { |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
226 |
log.println(taskHelper.getMessage("error.prefix") + " " + x.getMessage()); |
36511 | 227 |
x.printStackTrace(log); |
228 |
return EXIT_ABNORMAL; |
|
229 |
} finally { |
|
230 |
log.flush(); |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
/* |
|
235 |
* Jlink API entry point. |
|
236 |
*/ |
|
237 |
public static void createImage(JlinkConfiguration config, |
|
39125
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
238 |
PluginsConfiguration plugins) |
36511 | 239 |
throws Exception { |
240 |
Objects.requireNonNull(config); |
|
241 |
Objects.requireNonNull(config.getOutput()); |
|
242 |
plugins = plugins == null ? new PluginsConfiguration() : plugins; |
|
243 |
||
244 |
if (config.getModulepaths().isEmpty()) { |
|
245 |
throw new Exception("Empty module paths"); |
|
246 |
} |
|
39125
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
247 |
|
36511 | 248 |
ModuleFinder finder |
39125
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
249 |
= newModuleFinder(config.getModulepaths(), config.getLimitmods(), config.getModules()); |
36511 | 250 |
|
251 |
// First create the image provider |
|
252 |
ImageProvider imageProvider |
|
253 |
= createImageProvider(finder, |
|
254 |
checkAddMods(config.getModules()), |
|
255 |
config.getLimitmods(), |
|
256 |
config.getByteOrder(), |
|
257 |
null); |
|
258 |
||
259 |
// Then create the Plugin Stack |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
260 |
ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(plugins); |
36511 | 261 |
|
262 |
//Ask the stack to proceed; |
|
263 |
stack.operate(imageProvider); |
|
264 |
} |
|
265 |
||
266 |
/* |
|
267 |
* Jlink API entry point. |
|
268 |
*/ |
|
269 |
public static void postProcessImage(ExecutableImage image, List<Plugin> postProcessorPlugins) |
|
270 |
throws Exception { |
|
271 |
Objects.requireNonNull(image); |
|
272 |
Objects.requireNonNull(postProcessorPlugins); |
|
273 |
PluginsConfiguration config = new PluginsConfiguration(postProcessorPlugins); |
|
274 |
ImagePluginStack stack = ImagePluginConfiguration. |
|
275 |
parseConfiguration(config); |
|
276 |
||
277 |
stack.operate((ImagePluginStack stack1) -> image); |
|
278 |
} |
|
279 |
||
280 |
private void postProcessOnly(Path existingImage) throws Exception { |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
281 |
PluginsConfiguration config = taskHelper.getPluginsConfig(null); |
36511 | 282 |
ExecutableImage img = DefaultImageBuilder.getExecutableImage(existingImage); |
283 |
if (img == null) { |
|
284 |
throw taskHelper.newBadArgs("err.existing.image.invalid"); |
|
285 |
} |
|
286 |
postProcessImage(img, config.getPlugins()); |
|
287 |
} |
|
288 |
||
289 |
private void createImage() throws Exception { |
|
290 |
if (options.output == null) { |
|
291 |
throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true); |
|
292 |
} |
|
293 |
ModuleFinder finder |
|
294 |
= newModuleFinder(options.modulePath, options.limitMods, options.addMods); |
|
295 |
try { |
|
296 |
options.addMods = checkAddMods(options.addMods); |
|
297 |
} catch (IllegalArgumentException ex) { |
|
298 |
throw taskHelper.newBadArgs("err.mods.must.be.specified", "--addmods") |
|
299 |
.showUsage(true); |
|
300 |
} |
|
301 |
// First create the image provider |
|
302 |
ImageProvider imageProvider |
|
303 |
= createImageProvider(finder, |
|
304 |
options.addMods, |
|
305 |
options.limitMods, |
|
306 |
options.endian, |
|
307 |
options.packagedModulesPath); |
|
308 |
||
309 |
// Then create the Plugin Stack |
|
310 |
ImagePluginStack stack = ImagePluginConfiguration. |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
311 |
parseConfiguration(taskHelper.getPluginsConfig(options.output)); |
36511 | 312 |
|
313 |
//Ask the stack to proceed |
|
314 |
stack.operate(imageProvider); |
|
315 |
} |
|
316 |
||
317 |
private static Set<String> checkAddMods(Set<String> addMods) { |
|
318 |
if (addMods.isEmpty()) { |
|
319 |
throw new IllegalArgumentException("no modules to add"); |
|
320 |
} |
|
321 |
return addMods; |
|
322 |
} |
|
323 |
||
39125
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
324 |
public static ModuleFinder newModuleFinder(List<Path> paths, |
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
325 |
Set<String> limitMods, |
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
326 |
Set<String> addMods) |
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
327 |
{ |
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
328 |
|
e97953d38291
8155955: packager needs to determine the root modules to create JRE image
mchung
parents:
38869
diff
changeset
|
329 |
ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[0])); |
36511 | 330 |
|
331 |
// jmods are located at link-time |
|
332 |
if (finder instanceof ConfigurableModuleFinder) { |
|
333 |
((ConfigurableModuleFinder) finder).configurePhase(Phase.LINK_TIME); |
|
334 |
} |
|
335 |
||
336 |
// if limitmods is specified then limit the universe |
|
337 |
if (!limitMods.isEmpty()) { |
|
338 |
finder = limitFinder(finder, limitMods, addMods); |
|
339 |
} |
|
340 |
return finder; |
|
341 |
} |
|
342 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
343 |
|
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
344 |
private static Path toPathLocation(ResolvedModule m) { |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
345 |
Optional<URI> ouri = m.reference().location(); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
346 |
if (!ouri.isPresent()) |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
347 |
throw new InternalError(m + " does not have a location"); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
348 |
URI uri = ouri.get(); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
349 |
return Paths.get(uri); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
350 |
} |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
351 |
|
36511 | 352 |
private static ImageProvider createImageProvider(ModuleFinder finder, |
353 |
Set<String> addMods, |
|
354 |
Set<String> limitMods, |
|
355 |
ByteOrder order, |
|
356 |
Path retainModulesPath) |
|
357 |
throws IOException |
|
358 |
{ |
|
359 |
if (addMods.isEmpty()) { |
|
360 |
throw new IllegalArgumentException("empty modules and limitmods"); |
|
361 |
} |
|
362 |
||
363 |
Configuration cf = Configuration.empty() |
|
364 |
.resolveRequires(finder, |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37779
diff
changeset
|
365 |
ModuleFinder.of(), |
36511 | 366 |
addMods); |
367 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
368 |
Map<String, Path> mods = cf.modules().stream() |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
369 |
.collect(Collectors.toMap(ResolvedModule::name, JlinkTask::toPathLocation)); |
36511 | 370 |
return new ImageHelper(cf, mods, order, retainModulesPath); |
371 |
} |
|
372 |
||
373 |
/** |
|
374 |
* Returns a ModuleFinder that limits observability to the given root |
|
375 |
* modules, their transitive dependences, plus a set of other modules. |
|
376 |
*/ |
|
377 |
private static ModuleFinder limitFinder(ModuleFinder finder, |
|
378 |
Set<String> roots, |
|
379 |
Set<String> otherMods) { |
|
380 |
||
381 |
// resolve all root modules |
|
382 |
Configuration cf = Configuration.empty() |
|
383 |
.resolveRequires(finder, |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37779
diff
changeset
|
384 |
ModuleFinder.of(), |
36511 | 385 |
roots); |
386 |
||
387 |
// module name -> reference |
|
388 |
Map<String, ModuleReference> map = new HashMap<>(); |
|
389 |
cf.modules().forEach(m -> { |
|
390 |
ModuleReference mref = m.reference(); |
|
391 |
map.put(mref.descriptor().name(), mref); |
|
392 |
}); |
|
393 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
394 |
// add the other modules |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
395 |
otherMods.stream() |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
396 |
.map(finder::find) |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
397 |
.flatMap(Optional::stream) |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
398 |
.forEach(mref -> map.putIfAbsent(mref.descriptor().name(), mref)); |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
399 |
|
36511 | 400 |
// set of modules that are observable |
401 |
Set<ModuleReference> mrefs = new HashSet<>(map.values()); |
|
402 |
||
403 |
return new ModuleFinder() { |
|
404 |
@Override |
|
405 |
public Optional<ModuleReference> find(String name) { |
|
406 |
return Optional.ofNullable(map.get(name)); |
|
407 |
} |
|
408 |
||
409 |
@Override |
|
410 |
public Set<ModuleReference> findAll() { |
|
411 |
return mrefs; |
|
412 |
} |
|
413 |
}; |
|
414 |
} |
|
415 |
||
416 |
private String getSaveOpts() { |
|
417 |
StringBuilder sb = new StringBuilder(); |
|
418 |
sb.append('#').append(new Date()).append("\n"); |
|
419 |
for (String c : optionsHelper.getInputCommand()) { |
|
420 |
sb.append(c).append(" "); |
|
421 |
} |
|
422 |
||
423 |
return sb.toString(); |
|
424 |
} |
|
425 |
||
426 |
private static String getBomHeader() { |
|
427 |
StringBuilder sb = new StringBuilder(); |
|
428 |
sb.append("#").append(new Date()).append("\n"); |
|
429 |
sb.append("#Please DO NOT Modify this file").append("\n"); |
|
430 |
return sb.toString(); |
|
431 |
} |
|
432 |
||
433 |
private String genBOMContent() throws IOException { |
|
434 |
StringBuilder sb = new StringBuilder(); |
|
435 |
sb.append(getBomHeader()); |
|
436 |
StringBuilder command = new StringBuilder(); |
|
437 |
for (String c : optionsHelper.getInputCommand()) { |
|
438 |
command.append(c).append(" "); |
|
439 |
} |
|
440 |
sb.append("command").append(" = ").append(command); |
|
441 |
sb.append("\n"); |
|
442 |
||
443 |
return sb.toString(); |
|
444 |
} |
|
445 |
||
446 |
private static String genBOMContent(JlinkConfiguration config, |
|
447 |
PluginsConfiguration plugins) |
|
448 |
throws IOException { |
|
449 |
StringBuilder sb = new StringBuilder(); |
|
450 |
sb.append(getBomHeader()); |
|
451 |
sb.append(config); |
|
452 |
sb.append(plugins); |
|
453 |
return sb.toString(); |
|
454 |
} |
|
455 |
||
456 |
private static class ImageHelper implements ImageProvider { |
|
457 |
||
458 |
final Set<Archive> archives; |
|
459 |
final ByteOrder order; |
|
460 |
final Path packagedModulesPath; |
|
461 |
||
462 |
ImageHelper(Configuration cf, |
|
463 |
Map<String, Path> modsPaths, |
|
464 |
ByteOrder order, |
|
465 |
Path packagedModulesPath) throws IOException { |
|
466 |
archives = modsPaths.entrySet().stream() |
|
467 |
.map(e -> newArchive(e.getKey(), e.getValue())) |
|
468 |
.collect(Collectors.toSet()); |
|
469 |
this.order = order; |
|
470 |
this.packagedModulesPath = packagedModulesPath; |
|
471 |
} |
|
472 |
||
473 |
private Archive newArchive(String module, Path path) { |
|
474 |
if (path.toString().endsWith(".jmod")) { |
|
475 |
return new JmodArchive(module, path); |
|
476 |
} else if (path.toString().endsWith(".jar")) { |
|
477 |
return new ModularJarArchive(module, path); |
|
478 |
} else if (Files.isDirectory(path)) { |
|
479 |
return new DirArchive(path); |
|
480 |
} else { |
|
481 |
fail(RuntimeException.class, |
|
482 |
"Selected module %s (%s) not in jmod or modular jar format", |
|
483 |
module, |
|
484 |
path); |
|
485 |
} |
|
486 |
return null; |
|
487 |
} |
|
488 |
||
489 |
@Override |
|
490 |
public ExecutableImage retrieve(ImagePluginStack stack) throws IOException { |
|
491 |
ExecutableImage image = ImageFileCreator.create(archives, order, stack); |
|
492 |
if (packagedModulesPath != null) { |
|
493 |
// copy the packaged modules to the given path |
|
494 |
Files.createDirectories(packagedModulesPath); |
|
495 |
for (Archive a : archives) { |
|
496 |
Path file = a.getPath(); |
|
497 |
Path dest = packagedModulesPath.resolve(file.getFileName()); |
|
498 |
Files.copy(file, dest); |
|
499 |
} |
|
500 |
} |
|
501 |
return image; |
|
502 |
} |
|
503 |
} |
|
504 |
||
505 |
private static enum Section { |
|
506 |
NATIVE_LIBS("native", nativeDir()), |
|
507 |
NATIVE_CMDS("bin", "bin"), |
|
508 |
CLASSES("classes", "classes"), |
|
509 |
CONFIG("conf", "conf"), |
|
510 |
UNKNOWN("unknown", "unknown"); |
|
511 |
||
512 |
private static String nativeDir() { |
|
513 |
if (System.getProperty("os.name").startsWith("Windows")) { |
|
514 |
return "bin"; |
|
515 |
} else { |
|
516 |
return "lib"; |
|
517 |
} |
|
518 |
} |
|
519 |
||
520 |
private final String jmodDir; |
|
521 |
private final String imageDir; |
|
522 |
||
523 |
Section(String jmodDir, String imageDir) { |
|
524 |
this.jmodDir = jmodDir; |
|
525 |
this.imageDir = imageDir; |
|
526 |
} |
|
527 |
||
528 |
String imageDir() { |
|
529 |
return imageDir; |
|
530 |
} |
|
531 |
||
532 |
String jmodDir() { |
|
533 |
return jmodDir; |
|
534 |
} |
|
535 |
||
536 |
boolean matches(String path) { |
|
537 |
return path.startsWith(jmodDir); |
|
538 |
} |
|
539 |
||
540 |
static Section getSectionFromName(String dir) { |
|
541 |
if (Section.NATIVE_LIBS.matches(dir)) { |
|
542 |
return Section.NATIVE_LIBS; |
|
543 |
} else if (Section.NATIVE_CMDS.matches(dir)) { |
|
544 |
return Section.NATIVE_CMDS; |
|
545 |
} else if (Section.CLASSES.matches(dir)) { |
|
546 |
return Section.CLASSES; |
|
547 |
} else if (Section.CONFIG.matches(dir)) { |
|
548 |
return Section.CONFIG; |
|
549 |
} else { |
|
550 |
return Section.UNKNOWN; |
|
551 |
} |
|
552 |
} |
|
553 |
} |
|
554 |
} |