37 import java.nio.ByteBuffer;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.util.ArrayDeque;
41 import java.util.Collections;
42 import java.util.Deque;
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.Iterator;
46 import java.util.Map;
47 import java.util.Objects;
48 import java.util.Optional;
49 import java.util.Set;
50 import java.util.Spliterator;
51 import java.util.function.Consumer;
52 import java.util.function.Supplier;
53 import java.util.stream.Stream;
54 import java.util.stream.StreamSupport;
55
56 import jdk.internal.jimage.ImageReader;
57 import jdk.internal.jimage.ImageReaderFactory;
58 import jdk.internal.access.JavaNetUriAccess;
59 import jdk.internal.access.SharedSecrets;
60 import jdk.internal.util.StaticProperty;
61 import jdk.internal.module.ModuleHashes.HashSupplier;
62
63 /**
64 * The factory for SystemModules objects and for creating ModuleFinder objects
65 * that find modules in the runtime image.
66 *
67 * This class supports initializing the module system when the runtime is an
68 * images build, an exploded build, or an images build with java.base patched
69 * by an exploded java.base. It also supports a testing mode that re-parses
70 * the module-info.class resources in the run-time image.
71 */
72
73 public final class SystemModuleFinders {
74 private static final JavaNetUriAccess JNUA = SharedSecrets.getJavaNetUriAccess();
75
76 private static final boolean USE_FAST_PATH;
77 static {
78 String value = System.getProperty("jdk.system.module.finder.disableFastPath");
79 if (value == null) {
187 SystemModules systemModules = allSystemModules();
188 if (systemModules != null) {
189 finder = of(systemModules);
190 }
191 }
192
193 // fall back to parsing the module-info.class files in image
194 if (finder == null) {
195 finder = ofModuleInfos();
196 }
197
198 cachedSystemModuleFinder = finder;
199 return finder;
200
201 }
202
203 // exploded build (do not cache module finder)
204 Path dir = Path.of(home, "modules");
205 if (!Files.isDirectory(dir))
206 throw new InternalError("Unable to detect the run-time image");
207 return ModulePath.of(ModuleBootstrap.patcher(), dir);
208 }
209
210 /**
211 * Parses the {@code module-info.class} of all modules in the runtime image and
212 * returns a ModuleFinder to find the modules.
213 *
214 * @apiNote The returned ModuleFinder is thread safe.
215 */
216 private static ModuleFinder ofModuleInfos() {
217 // parse the module-info.class in every module
218 Map<String, ModuleInfo.Attributes> nameToAttributes = new HashMap<>();
219 Map<String, byte[]> nameToHash = new HashMap<>();
220
221 allModuleAttributes().forEach(attrs -> {
222 nameToAttributes.put(attrs.descriptor().name(), attrs);
223 ModuleHashes hashes = attrs.recordedHashes();
224 if (hashes != null) {
225 for (String name : hashes.names()) {
226 nameToHash.computeIfAbsent(name, k -> hashes.hashFor(name));
227 }
375 */
376 static HashSupplier hashSupplier(Map<String, byte[]> nameToHash, String name) {
377 byte[] hash = nameToHash.get(name);
378 if (hash != null) {
379 // avoid lambda here
380 return new HashSupplier() {
381 @Override
382 public byte[] generate(String algorithm) {
383 return hash;
384 }
385 };
386 } else {
387 return null;
388 }
389 }
390
391 /**
392 * Holder class for the ImageReader.
393 */
394 private static class SystemImage {
395 static final ImageReader READER = ImageReaderFactory.getImageReader();
396 static ImageReader reader() {
397 return READER;
398 }
399 }
400
401 /**
402 * A ModuleReader for reading resources from a module linked into the
403 * run-time image.
404 */
405 private static class SystemModuleReader implements ModuleReader {
406 private final String module;
407 private volatile boolean closed;
408
409 SystemModuleReader(String module) {
410 this.module = module;
411 }
412
413 /**
414 * Returns {@code true} if the given resource exists, {@code false}
415 * if not found.
|
37 import java.nio.ByteBuffer;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.util.ArrayDeque;
41 import java.util.Collections;
42 import java.util.Deque;
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.Iterator;
46 import java.util.Map;
47 import java.util.Objects;
48 import java.util.Optional;
49 import java.util.Set;
50 import java.util.Spliterator;
51 import java.util.function.Consumer;
52 import java.util.function.Supplier;
53 import java.util.stream.Stream;
54 import java.util.stream.StreamSupport;
55
56 import jdk.internal.jimage.ImageReader;
57 import jdk.internal.jimage.SystemImageReader;
58 import jdk.internal.access.JavaNetUriAccess;
59 import jdk.internal.access.SharedSecrets;
60 import jdk.internal.misc.PreviewFeatures;
61 import jdk.internal.util.StaticProperty;
62 import jdk.internal.module.ModuleHashes.HashSupplier;
63
64 /**
65 * The factory for SystemModules objects and for creating ModuleFinder objects
66 * that find modules in the runtime image.
67 *
68 * This class supports initializing the module system when the runtime is an
69 * images build, an exploded build, or an images build with java.base patched
70 * by an exploded java.base. It also supports a testing mode that re-parses
71 * the module-info.class resources in the run-time image.
72 */
73
74 public final class SystemModuleFinders {
75 private static final JavaNetUriAccess JNUA = SharedSecrets.getJavaNetUriAccess();
76
77 private static final boolean USE_FAST_PATH;
78 static {
79 String value = System.getProperty("jdk.system.module.finder.disableFastPath");
80 if (value == null) {
188 SystemModules systemModules = allSystemModules();
189 if (systemModules != null) {
190 finder = of(systemModules);
191 }
192 }
193
194 // fall back to parsing the module-info.class files in image
195 if (finder == null) {
196 finder = ofModuleInfos();
197 }
198
199 cachedSystemModuleFinder = finder;
200 return finder;
201
202 }
203
204 // exploded build (do not cache module finder)
205 Path dir = Path.of(home, "modules");
206 if (!Files.isDirectory(dir))
207 throw new InternalError("Unable to detect the run-time image");
208 return ModulePath.of(ModuleBootstrap.patcher(), PreviewFeatures.isEnabled(), dir);
209 }
210
211 /**
212 * Parses the {@code module-info.class} of all modules in the runtime image and
213 * returns a ModuleFinder to find the modules.
214 *
215 * @apiNote The returned ModuleFinder is thread safe.
216 */
217 private static ModuleFinder ofModuleInfos() {
218 // parse the module-info.class in every module
219 Map<String, ModuleInfo.Attributes> nameToAttributes = new HashMap<>();
220 Map<String, byte[]> nameToHash = new HashMap<>();
221
222 allModuleAttributes().forEach(attrs -> {
223 nameToAttributes.put(attrs.descriptor().name(), attrs);
224 ModuleHashes hashes = attrs.recordedHashes();
225 if (hashes != null) {
226 for (String name : hashes.names()) {
227 nameToHash.computeIfAbsent(name, k -> hashes.hashFor(name));
228 }
376 */
377 static HashSupplier hashSupplier(Map<String, byte[]> nameToHash, String name) {
378 byte[] hash = nameToHash.get(name);
379 if (hash != null) {
380 // avoid lambda here
381 return new HashSupplier() {
382 @Override
383 public byte[] generate(String algorithm) {
384 return hash;
385 }
386 };
387 } else {
388 return null;
389 }
390 }
391
392 /**
393 * Holder class for the ImageReader.
394 */
395 private static class SystemImage {
396 static final ImageReader READER = SystemImageReader.get();
397 static ImageReader reader() {
398 return READER;
399 }
400 }
401
402 /**
403 * A ModuleReader for reading resources from a module linked into the
404 * run-time image.
405 */
406 private static class SystemModuleReader implements ModuleReader {
407 private final String module;
408 private volatile boolean closed;
409
410 SystemModuleReader(String module) {
411 this.module = module;
412 }
413
414 /**
415 * Returns {@code true} if the given resource exists, {@code false}
416 * if not found.
|