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.internal.module;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.UncheckedIOException;
31 import java.lang.module.ModuleDescriptor;
32 import java.lang.module.ModuleFinder;
33 import java.lang.module.ModuleReader;
34 import java.lang.module.ModuleReference;
35 import java.lang.reflect.Constructor;
36 import java.net.URI;
37 import java.net.URLConnection;
38 import java.nio.ByteBuffer;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.security.AccessController;
43 import java.security.PrivilegedAction;
44 import java.util.ArrayDeque;
45 import java.util.Collections;
46 import java.util.Deque;
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.Iterator;
50 import java.util.Map;
51 import java.util.Objects;
52 import java.util.Optional;
53 import java.util.Set;
54 import java.util.Spliterator;
55 import java.util.function.Consumer;
56 import java.util.function.Supplier;
57 import java.util.stream.Stream;
58 import java.util.stream.StreamSupport;
59
60 import jdk.internal.jimage.ImageLocation;
61 import jdk.internal.jimage.ImageReader;
168 map[i] = Map.entry(name, mref);
169 }
170
171 return new SystemModuleFinder(mrefs, map);
172 }
173
174 /**
175 * Returns the ModuleFinder to find all system modules. Supports both
176 * images and exploded builds.
177 *
178 * @apiNote Used by ModuleFinder.ofSystem()
179 */
180 public static ModuleFinder ofSystem() {
181 ModuleFinder finder = cachedSystemModuleFinder;
182 if (finder != null) {
183 return finder;
184 }
185
186 // probe to see if this is an images build
187 String home = System.getProperty("java.home");
188 Path modules = Paths.get(home, "lib", "modules");
189 if (Files.isRegularFile(modules)) {
190 if (USE_FAST_PATH) {
191 SystemModules systemModules = allSystemModules();
192 if (systemModules != null) {
193 finder = of(systemModules);
194 }
195 }
196
197 // fall back to parsing the module-info.class files in image
198 if (finder == null) {
199 finder = ofModuleInfos();
200 }
201
202 cachedSystemModuleFinder = finder;
203 return finder;
204
205 }
206
207 // exploded build (do not cache module finder)
208 Path dir = Paths.get(home, "modules");
209 if (!Files.isDirectory(dir))
210 throw new InternalError("Unable to detect the run-time image");
211 ModuleFinder f = ModulePath.of(ModuleBootstrap.patcher(), dir);
212 return new ModuleFinder() {
213 @Override
214 public Optional<ModuleReference> find(String name) {
215 PrivilegedAction<Optional<ModuleReference>> pa = () -> f.find(name);
216 return AccessController.doPrivileged(pa);
217 }
218 @Override
219 public Set<ModuleReference> findAll() {
220 PrivilegedAction<Set<ModuleReference>> pa = f::findAll;
221 return AccessController.doPrivileged(pa);
222 }
223 };
224 }
225
226 /**
227 * Parses the module-info.class of all module in the runtime image and
228 * returns a ModuleFinder to find the modules.
|
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.internal.module;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.UncheckedIOException;
31 import java.lang.module.ModuleDescriptor;
32 import java.lang.module.ModuleFinder;
33 import java.lang.module.ModuleReader;
34 import java.lang.module.ModuleReference;
35 import java.lang.reflect.Constructor;
36 import java.net.URI;
37 import java.net.URLConnection;
38 import java.nio.ByteBuffer;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.security.AccessController;
42 import java.security.PrivilegedAction;
43 import java.util.ArrayDeque;
44 import java.util.Collections;
45 import java.util.Deque;
46 import java.util.HashMap;
47 import java.util.HashSet;
48 import java.util.Iterator;
49 import java.util.Map;
50 import java.util.Objects;
51 import java.util.Optional;
52 import java.util.Set;
53 import java.util.Spliterator;
54 import java.util.function.Consumer;
55 import java.util.function.Supplier;
56 import java.util.stream.Stream;
57 import java.util.stream.StreamSupport;
58
59 import jdk.internal.jimage.ImageLocation;
60 import jdk.internal.jimage.ImageReader;
167 map[i] = Map.entry(name, mref);
168 }
169
170 return new SystemModuleFinder(mrefs, map);
171 }
172
173 /**
174 * Returns the ModuleFinder to find all system modules. Supports both
175 * images and exploded builds.
176 *
177 * @apiNote Used by ModuleFinder.ofSystem()
178 */
179 public static ModuleFinder ofSystem() {
180 ModuleFinder finder = cachedSystemModuleFinder;
181 if (finder != null) {
182 return finder;
183 }
184
185 // probe to see if this is an images build
186 String home = System.getProperty("java.home");
187 Path modules = Path.get(home, "lib", "modules");
188 if (Files.isRegularFile(modules)) {
189 if (USE_FAST_PATH) {
190 SystemModules systemModules = allSystemModules();
191 if (systemModules != null) {
192 finder = of(systemModules);
193 }
194 }
195
196 // fall back to parsing the module-info.class files in image
197 if (finder == null) {
198 finder = ofModuleInfos();
199 }
200
201 cachedSystemModuleFinder = finder;
202 return finder;
203
204 }
205
206 // exploded build (do not cache module finder)
207 Path dir = Path.get(home, "modules");
208 if (!Files.isDirectory(dir))
209 throw new InternalError("Unable to detect the run-time image");
210 ModuleFinder f = ModulePath.of(ModuleBootstrap.patcher(), dir);
211 return new ModuleFinder() {
212 @Override
213 public Optional<ModuleReference> find(String name) {
214 PrivilegedAction<Optional<ModuleReference>> pa = () -> f.find(name);
215 return AccessController.doPrivileged(pa);
216 }
217 @Override
218 public Set<ModuleReference> findAll() {
219 PrivilegedAction<Set<ModuleReference>> pa = f::findAll;
220 return AccessController.doPrivileged(pa);
221 }
222 };
223 }
224
225 /**
226 * Parses the module-info.class of all module in the runtime image and
227 * returns a ModuleFinder to find the modules.
|