author | jlaskey |
Tue, 28 Jun 2016 16:07:23 -0300 | |
changeset 39308 | f6517755057f |
parent 39151 | 34455cc82f5e |
child 39635 | 07c4b195280d |
permissions | -rw-r--r-- |
36511 | 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.internal; |
|
26 |
||
38320 | 27 |
import java.lang.module.ModuleDescriptor; |
36511 | 28 |
import java.nio.ByteBuffer; |
29 |
import java.nio.ByteOrder; |
|
38320 | 30 |
import java.util.Collections; |
31 |
import java.util.HashMap; |
|
32 |
import java.util.HashSet; |
|
33 |
import java.util.LinkedHashMap; |
|
34 |
import java.util.Map; |
|
36511 | 35 |
import java.util.Objects; |
38320 | 36 |
import java.util.Optional; |
37 |
import java.util.Set; |
|
38 |
import java.util.function.Function; |
|
39 |
import java.util.stream.Stream; |
|
36511 | 40 |
import jdk.internal.jimage.decompressor.CompressedResourceHeader; |
38320 | 41 |
import jdk.tools.jlink.plugin.ModulePool; |
42 |
import jdk.tools.jlink.plugin.LinkModule; |
|
43 |
import jdk.tools.jlink.plugin.ModuleEntry; |
|
44 |
import jdk.tools.jlink.plugin.PluginException; |
|
45 |
import jdk.tools.jlink.internal.plugins.FileCopierPlugin; |
|
36511 | 46 |
|
47 |
/** |
|
48 |
* Pool of module data. |
|
49 |
*/ |
|
38320 | 50 |
public class ModulePoolImpl implements ModulePool { |
51 |
||
52 |
private class ModuleImpl implements LinkModule { |
|
36511 | 53 |
|
38320 | 54 |
final Map<String, ModuleEntry> moduleContent = new LinkedHashMap<>(); |
55 |
private ModuleDescriptor descriptor; |
|
56 |
final String name; |
|
36511 | 57 |
|
38320 | 58 |
private ModuleImpl(String name) { |
59 |
this.name = name; |
|
60 |
} |
|
36511 | 61 |
|
38320 | 62 |
@Override |
63 |
public String getName() { |
|
64 |
return name; |
|
36511 | 65 |
} |
66 |
||
38320 | 67 |
@Override |
68 |
public Optional<ModuleEntry> findEntry(String path) { |
|
69 |
if (!path.startsWith("/")) { |
|
70 |
path = "/" + path; |
|
71 |
} |
|
72 |
if (!path.startsWith("/" + name)) { |
|
73 |
path = "/" + name + path; |
|
74 |
} |
|
75 |
return Optional.ofNullable(moduleContent.get(path)); |
|
76 |
} |
|
77 |
||
78 |
@Override |
|
79 |
public ModuleDescriptor getDescriptor() { |
|
80 |
if (descriptor == null) { |
|
81 |
String p = "/" + name + "/module-info.class"; |
|
82 |
Optional<ModuleEntry> content = findEntry(p); |
|
83 |
if (!content.isPresent()) { |
|
84 |
throw new PluginException("No module-info for " + name |
|
85 |
+ " module"); |
|
86 |
} |
|
87 |
ByteBuffer bb = ByteBuffer.wrap(content.get().getBytes()); |
|
88 |
descriptor = ModuleDescriptor.read(bb); |
|
89 |
} |
|
90 |
return descriptor; |
|
36511 | 91 |
} |
92 |
||
93 |
@Override |
|
38320 | 94 |
public void add(ModuleEntry data) { |
95 |
if (isReadOnly()) { |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
96 |
throw new PluginException("ModulePool is readonly"); |
36511 | 97 |
} |
38320 | 98 |
Objects.requireNonNull(data); |
99 |
if (!data.getModule().equals(name)) { |
|
100 |
throw new PluginException("Can't add resource " + data.getPath() |
|
101 |
+ " to module " + name); |
|
102 |
} |
|
103 |
ModulePoolImpl.this.add(data); |
|
36511 | 104 |
} |
105 |
||
106 |
@Override |
|
38320 | 107 |
public Set<String> getAllPackages() { |
108 |
Set<String> pkgs = new HashSet<>(); |
|
109 |
moduleContent.values().stream().filter(m -> m.getType(). |
|
110 |
equals(ModuleEntry.Type.CLASS_OR_RESOURCE)).forEach(res -> { |
|
111 |
// Module metadata only contains packages with .class files |
|
112 |
if (ImageFileCreator.isClassPackage(res.getPath())) { |
|
113 |
String[] split = ImageFileCreator.splitPath(res.getPath()); |
|
114 |
String pkg = split[1]; |
|
115 |
if (pkg != null && !pkg.isEmpty()) { |
|
116 |
pkgs.add(pkg); |
|
117 |
} |
|
118 |
} |
|
119 |
}); |
|
120 |
return pkgs; |
|
121 |
} |
|
122 |
||
123 |
@Override |
|
124 |
public String toString() { |
|
125 |
return getName(); |
|
126 |
} |
|
127 |
||
128 |
@Override |
|
129 |
public Stream<? extends ModuleEntry> entries() { |
|
130 |
return moduleContent.values().stream(); |
|
131 |
} |
|
132 |
||
133 |
@Override |
|
134 |
public int getEntryCount() { |
|
135 |
return moduleContent.values().size(); |
|
36511 | 136 |
} |
137 |
} |
|
138 |
||
38320 | 139 |
private final Map<String, ModuleEntry> resources = new LinkedHashMap<>(); |
140 |
private final Map<String, ModuleImpl> modules = new LinkedHashMap<>(); |
|
141 |
private final ModuleImpl fileCopierModule = new ModuleImpl(FileCopierPlugin.FAKE_MODULE); |
|
142 |
private Map<String, String> releaseProps = new HashMap<>(); |
|
143 |
||
144 |
private final ByteOrder order; |
|
145 |
||
36511 | 146 |
private boolean isReadOnly; |
147 |
private final StringTable table; |
|
148 |
||
38320 | 149 |
public ModulePoolImpl() { |
150 |
this(ByteOrder.nativeOrder()); |
|
151 |
} |
|
152 |
||
153 |
public ModulePoolImpl(ByteOrder order) { |
|
154 |
this(order, new StringTable() { |
|
36511 | 155 |
|
156 |
@Override |
|
157 |
public int addString(String str) { |
|
158 |
return -1; |
|
159 |
} |
|
38320 | 160 |
|
36511 | 161 |
@Override |
162 |
public String getString(int id) { |
|
163 |
return null; |
|
164 |
} |
|
165 |
}); |
|
166 |
} |
|
167 |
||
38320 | 168 |
public ModulePoolImpl(ByteOrder order, StringTable table) { |
39308 | 169 |
this.order = Objects.requireNonNull(order); |
170 |
this.table = Objects.requireNonNull(table); |
|
38320 | 171 |
} |
172 |
||
173 |
/** |
|
174 |
* Add a ModuleEntry. |
|
175 |
* |
|
176 |
* @param data The ModuleEntry to add. |
|
177 |
*/ |
|
178 |
@Override |
|
179 |
public void add(ModuleEntry data) { |
|
180 |
if (isReadOnly()) { |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
181 |
throw new PluginException("ModulePool is readonly"); |
38320 | 182 |
} |
183 |
Objects.requireNonNull(data); |
|
184 |
if (resources.get(data.getPath()) != null) { |
|
185 |
throw new PluginException("Resource " + data.getPath() |
|
186 |
+ " already present"); |
|
187 |
} |
|
188 |
String modulename = data.getModule(); |
|
189 |
ModuleImpl m = modules.get(modulename); |
|
190 |
// ## TODO: FileCopierPlugin should not add content to a module |
|
191 |
// FAKE_MODULE is not really a module to be added in the image |
|
192 |
if (FileCopierPlugin.FAKE_MODULE.equals(modulename)) { |
|
193 |
m = fileCopierModule; |
|
194 |
} |
|
195 |
if (m == null) { |
|
196 |
m = new ModuleImpl(modulename); |
|
197 |
modules.put(modulename, m); |
|
198 |
} |
|
199 |
resources.put(data.getPath(), data); |
|
200 |
m.moduleContent.put(data.getPath(), data); |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Retrieves the module for the provided name. |
|
205 |
* |
|
206 |
* @param name The module name |
|
207 |
* @return the module of matching name, if found |
|
208 |
*/ |
|
209 |
@Override |
|
210 |
public Optional<LinkModule> findModule(String name) { |
|
211 |
Objects.requireNonNull(name); |
|
212 |
return Optional.ofNullable(modules.get(name)); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
216 |
* The stream of modules contained in this ModulePool. |
38320 | 217 |
* |
218 |
* @return The stream of modules. |
|
219 |
*/ |
|
220 |
@Override |
|
221 |
public Stream<? extends LinkModule> modules() { |
|
222 |
return modules.values().stream(); |
|
223 |
} |
|
224 |
||
225 |
/** |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
226 |
* Return the number of LinkModule count in this ModulePool. |
38320 | 227 |
* |
228 |
* @return the module count. |
|
229 |
*/ |
|
230 |
@Override |
|
231 |
public int getModuleCount() { |
|
232 |
return modules.size(); |
|
233 |
} |
|
36511 | 234 |
|
38320 | 235 |
/** |
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
236 |
* Get all ModuleEntry contained in this ModulePool instance. |
38320 | 237 |
* |
238 |
* @return The stream of LinkModuleEntries. |
|
239 |
*/ |
|
240 |
@Override |
|
241 |
public Stream<? extends ModuleEntry> entries() { |
|
242 |
return resources.values().stream(); |
|
243 |
} |
|
244 |
||
245 |
/** |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
246 |
* Return the number of ModuleEntry count in this ModulePool. |
38320 | 247 |
* |
248 |
* @return the entry count. |
|
249 |
*/ |
|
250 |
@Override |
|
251 |
public int getEntryCount() { |
|
252 |
return resources.values().size(); |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* Get the ModuleEntry for the passed path. |
|
257 |
* |
|
258 |
* @param path A data path |
|
259 |
* @return A ModuleEntry instance or null if the data is not found |
|
260 |
*/ |
|
261 |
@Override |
|
262 |
public Optional<ModuleEntry> findEntry(String path) { |
|
263 |
Objects.requireNonNull(path); |
|
264 |
return Optional.ofNullable(resources.get(path)); |
|
265 |
} |
|
266 |
||
267 |
/** |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
268 |
* Check if the ModulePool contains the given ModuleEntry. |
38320 | 269 |
* |
270 |
* @param data The module data to check existence for. |
|
271 |
* @return The module data or null if not found. |
|
272 |
*/ |
|
273 |
@Override |
|
274 |
public boolean contains(ModuleEntry data) { |
|
275 |
Objects.requireNonNull(data); |
|
276 |
return findEntry(data.getPath()).isPresent(); |
|
277 |
} |
|
278 |
||
279 |
/** |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
280 |
* Check if the ModulePool contains some content at all. |
38320 | 281 |
* |
282 |
* @return True, no content, false otherwise. |
|
283 |
*/ |
|
284 |
@Override |
|
285 |
public boolean isEmpty() { |
|
286 |
return resources.isEmpty(); |
|
287 |
} |
|
288 |
||
289 |
/** |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
290 |
* Visit each ModuleEntry in this ModulePool to transform it and |
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
291 |
* copy the transformed ModuleEntry to the output ModulePool. |
38320 | 292 |
* |
293 |
* @param transform The function called for each ModuleEntry found in |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
294 |
* the ModulePool. The transform function should return a |
38320 | 295 |
* ModuleEntry instance which will be added to the output or it should |
296 |
* return null if the passed ModuleEntry is to be ignored for the |
|
297 |
* output. |
|
298 |
* |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
299 |
* @param output The ModulePool to be filled with Visitor returned |
38320 | 300 |
* ModuleEntry. |
301 |
*/ |
|
302 |
@Override |
|
303 |
public void transformAndCopy(Function<ModuleEntry, ModuleEntry> transform, |
|
304 |
ModulePool output) { |
|
305 |
entries().forEach(resource -> { |
|
306 |
ModuleEntry res = transform.apply(resource); |
|
307 |
if (res != null) { |
|
308 |
output.add(res); |
|
36511 | 309 |
} |
310 |
}); |
|
311 |
} |
|
312 |
||
38320 | 313 |
/** |
314 |
* The ByteOrder currently in use when generating the jimage file. |
|
315 |
* |
|
316 |
* @return The ByteOrder. |
|
317 |
*/ |
|
318 |
@Override |
|
319 |
public ByteOrder getByteOrder() { |
|
320 |
return order; |
|
321 |
} |
|
322 |
||
323 |
@Override |
|
324 |
public Map<String, String> getReleaseProperties() { |
|
325 |
return isReadOnly()? Collections.unmodifiableMap(releaseProps) : releaseProps; |
|
36511 | 326 |
} |
327 |
||
328 |
public StringTable getStringTable() { |
|
329 |
return table; |
|
330 |
} |
|
331 |
||
332 |
/** |
|
333 |
* Make this Resources instance read-only. No resource can be added. |
|
334 |
*/ |
|
335 |
public void setReadOnly() { |
|
336 |
isReadOnly = true; |
|
337 |
} |
|
338 |
||
339 |
/** |
|
340 |
* Read only state. |
|
341 |
* |
|
342 |
* @return true if readonly false otherwise. |
|
343 |
*/ |
|
344 |
@Override |
|
345 |
public boolean isReadOnly() { |
|
346 |
return isReadOnly; |
|
347 |
} |
|
348 |
||
38320 | 349 |
/** |
350 |
* A resource that has been compressed. |
|
351 |
*/ |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
352 |
public static final class CompressedModuleData extends ByteArrayModuleEntry { |
38320 | 353 |
|
354 |
final long uncompressed_size; |
|
355 |
||
356 |
private CompressedModuleData(String module, String path, |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
357 |
byte[] content, long uncompressed_size) { |
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
358 |
super(module, path, ModuleEntry.Type.CLASS_OR_RESOURCE, content); |
38320 | 359 |
this.uncompressed_size = uncompressed_size; |
360 |
} |
|
361 |
||
362 |
public long getUncompressedSize() { |
|
363 |
return uncompressed_size; |
|
364 |
} |
|
365 |
||
366 |
@Override |
|
367 |
public boolean equals(Object other) { |
|
368 |
if (!(other instanceof CompressedModuleData)) { |
|
369 |
return false; |
|
370 |
} |
|
371 |
CompressedModuleData f = (CompressedModuleData) other; |
|
372 |
return f.getPath().equals(getPath()); |
|
373 |
} |
|
374 |
||
375 |
@Override |
|
376 |
public int hashCode() { |
|
377 |
return super.hashCode(); |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
public static CompressedModuleData newCompressedResource(ModuleEntry original, |
|
36511 | 382 |
ByteBuffer compressed, |
383 |
String plugin, String pluginConfig, StringTable strings, |
|
384 |
ByteOrder order) { |
|
385 |
Objects.requireNonNull(original); |
|
386 |
Objects.requireNonNull(compressed); |
|
387 |
Objects.requireNonNull(plugin); |
|
388 |
||
389 |
boolean isTerminal = !(original instanceof CompressedModuleData); |
|
390 |
long uncompressed_size = original.getLength(); |
|
391 |
if (original instanceof CompressedModuleData) { |
|
392 |
CompressedModuleData comp = (CompressedModuleData) original; |
|
393 |
uncompressed_size = comp.getUncompressedSize(); |
|
394 |
} |
|
395 |
int nameOffset = strings.addString(plugin); |
|
396 |
int configOffset = -1; |
|
397 |
if (pluginConfig != null) { |
|
398 |
configOffset = strings.addString(plugin); |
|
399 |
} |
|
400 |
CompressedResourceHeader rh |
|
401 |
= new CompressedResourceHeader(compressed.limit(), original.getLength(), |
|
402 |
nameOffset, configOffset, isTerminal); |
|
403 |
// Merge header with content; |
|
404 |
byte[] h = rh.getBytes(order); |
|
405 |
ByteBuffer bb = ByteBuffer.allocate(compressed.limit() + h.length); |
|
406 |
bb.order(order); |
|
407 |
bb.put(h); |
|
408 |
bb.put(compressed); |
|
409 |
byte[] contentWithHeader = bb.array(); |
|
410 |
||
411 |
CompressedModuleData compressedResource |
|
412 |
= new CompressedModuleData(original.getModule(), original.getPath(), |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
413 |
contentWithHeader, uncompressed_size); |
36511 | 414 |
return compressedResource; |
415 |
} |
|
416 |
||
417 |
} |