< prev index next >

src/java.base/share/classes/java/lang/module/ModuleFinder.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.module;
  27 
  28 import java.io.File;
  29 import java.io.FilePermission;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.security.AccessController;
  34 import java.security.Permission;
  35 import java.security.PrivilegedAction;
  36 import java.util.Collections;
  37 import java.util.Objects;
  38 import java.util.Optional;
  39 import java.util.Set;
  40 import java.util.stream.Collectors;
  41 import java.util.stream.Stream;

  42 
  43 /**
  44  * A finder of modules. A {@code ModuleFinder} is used to find modules during
  45  * <a href="Configuration.html#resolution">resolution</a> or
  46  * <a href="Configuration.html#servicebinding">service binding</a>.
  47  *
  48  * <p> A {@code ModuleFinder} can only find one module with a given name. A
  49  * {@code ModuleFinder} that finds modules in a sequence of directories, for
  50  * example, will locate the first occurrence of a module of a given name and
  51  * will ignore other modules of that name that appear in directories later in
  52  * the sequence. </p>
  53  *
  54  * <p> Example usage: </p>
  55  *
  56  * <pre>{@code
  57  *     Path dir1, dir2, dir3;
  58  *
  59  *     ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
  60  *
  61  *     Optional<ModuleReference> omref = finder.find("jdk.foo");


 135      * Returns a module finder that locates the <em>system modules</em>. The
 136      * system modules are typically linked into the Java run-time image.
 137      * The module finder will always find {@code java.base}.
 138      *
 139      * <p> If there is a security manager set then its {@link
 140      * SecurityManager#checkPermission(Permission) checkPermission} method is
 141      * invoked to check that the caller has been granted {@link FilePermission}
 142      * to recursively read the directory that is the value of the system
 143      * property {@code java.home}. </p>
 144      *
 145      * @return A {@code ModuleFinder} that locates the system modules
 146      *
 147      * @throws SecurityException
 148      *         If denied by the security manager
 149      */
 150     static ModuleFinder ofSystem() {
 151         String home;
 152 
 153         SecurityManager sm = System.getSecurityManager();
 154         if (sm != null) {
 155             PrivilegedAction<String> pa = () -> System.getProperty("java.home");
 156             home = AccessController.doPrivileged(pa);
 157             Permission p = new FilePermission(home + File.separator + "-", "read");
 158             sm.checkPermission(p);
 159         } else {
 160             home = System.getProperty("java.home");
 161         }
 162 
 163         Path modules = Paths.get(home, "lib", "modules");
 164         if (Files.isRegularFile(modules)) {
 165             return new SystemModuleFinder();
 166         } else {
 167             Path mlib = Paths.get(home, "modules");
 168             if (Files.isDirectory(mlib)) {
 169                 return of(mlib);
 170             } else {
 171                 throw new InternalError("Unable to detect the run-time image");
 172             }
 173         }
 174     }
 175 




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.module;
  27 
  28 import java.io.File;
  29 import java.io.FilePermission;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.security.AccessController;
  34 import java.security.Permission;
  35 import java.security.PrivilegedAction;
  36 import java.util.Collections;
  37 import java.util.Objects;
  38 import java.util.Optional;
  39 import java.util.Set;
  40 import java.util.stream.Collectors;
  41 import java.util.stream.Stream;
  42 import sun.security.action.GetPropertyAction;
  43 
  44 /**
  45  * A finder of modules. A {@code ModuleFinder} is used to find modules during
  46  * <a href="Configuration.html#resolution">resolution</a> or
  47  * <a href="Configuration.html#servicebinding">service binding</a>.
  48  *
  49  * <p> A {@code ModuleFinder} can only find one module with a given name. A
  50  * {@code ModuleFinder} that finds modules in a sequence of directories, for
  51  * example, will locate the first occurrence of a module of a given name and
  52  * will ignore other modules of that name that appear in directories later in
  53  * the sequence. </p>
  54  *
  55  * <p> Example usage: </p>
  56  *
  57  * <pre>{@code
  58  *     Path dir1, dir2, dir3;
  59  *
  60  *     ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
  61  *
  62  *     Optional<ModuleReference> omref = finder.find("jdk.foo");


 136      * Returns a module finder that locates the <em>system modules</em>. The
 137      * system modules are typically linked into the Java run-time image.
 138      * The module finder will always find {@code java.base}.
 139      *
 140      * <p> If there is a security manager set then its {@link
 141      * SecurityManager#checkPermission(Permission) checkPermission} method is
 142      * invoked to check that the caller has been granted {@link FilePermission}
 143      * to recursively read the directory that is the value of the system
 144      * property {@code java.home}. </p>
 145      *
 146      * @return A {@code ModuleFinder} that locates the system modules
 147      *
 148      * @throws SecurityException
 149      *         If denied by the security manager
 150      */
 151     static ModuleFinder ofSystem() {
 152         String home;
 153 
 154         SecurityManager sm = System.getSecurityManager();
 155         if (sm != null) {
 156             PrivilegedAction<String> pa = new GetPropertyAction("java.home");
 157             home = AccessController.doPrivileged(pa);
 158             Permission p = new FilePermission(home + File.separator + "-", "read");
 159             sm.checkPermission(p);
 160         } else {
 161             home = System.getProperty("java.home");
 162         }
 163 
 164         Path modules = Paths.get(home, "lib", "modules");
 165         if (Files.isRegularFile(modules)) {
 166             return new SystemModuleFinder();
 167         } else {
 168             Path mlib = Paths.get(home, "modules");
 169             if (Files.isDirectory(mlib)) {
 170                 return of(mlib);
 171             } else {
 172                 throw new InternalError("Unable to detect the run-time image");
 173             }
 174         }
 175     }
 176 


< prev index next >