< prev index next >

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

Print this page




  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.HashMap;
  38 import java.util.HashSet;
  39 import java.util.List;
  40 import java.util.Map;
  41 import java.util.Objects;
  42 import java.util.Optional;
  43 import java.util.Set;
  44 


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


 120      * ModuleReference} element in the returned set then it is guaranteed that
 121      * {@link #find find} will locate the {@code ModuleReference} if invoked
 122      * to find that module. </p>
 123      *
 124      * @apiNote This is important to have for methods such as {@link
 125      * Configuration#resolveRequiresAndUses resolveRequiresAndUses} that need
 126      * to scan the module path to find modules that provide a specific service.
 127      *
 128      * @return The set of all module references that this finder locates
 129      *
 130      * @throws FindException
 131      *         If an error occurs finding all modules
 132      *
 133      * @throws SecurityException
 134      *         If denied by the security manager
 135      */
 136     Set<ModuleReference> findAll();
 137 
 138     /**
 139      * Returns a module finder that locates the <em>system modules</em>. The
 140      * system modules are typically linked into the Java run-time image.
 141      * The module finder will always find {@code java.base}.
 142      *
 143      * <p> If there is a security manager set then its {@link
 144      * SecurityManager#checkPermission(Permission) checkPermission} method is
 145      * invoked to check that the caller has been granted {@link FilePermission}
 146      * to recursively read the directory that is the value of the system
 147      * property {@code java.home}. </p>
 148      *
 149      * @return A {@code ModuleFinder} that locates the system modules
 150      *
 151      * @throws SecurityException
 152      *         If denied by the security manager
 153      */
 154     static ModuleFinder ofSystem() {
 155         String home;
 156 
 157         SecurityManager sm = System.getSecurityManager();
 158         if (sm != null) {
 159             PrivilegedAction<String> pa = new GetPropertyAction("java.home");
 160             home = AccessController.doPrivileged(pa);
 161             Permission p = new FilePermission(home + File.separator + "-", "read");
 162             sm.checkPermission(p);
 163         } else {
 164             home = System.getProperty("java.home");
 165         }
 166 
 167         Path modules = Paths.get(home, "lib", "modules");
 168         if (Files.isRegularFile(modules)) {
 169             return new SystemModuleFinder();
 170         } else {
 171             Path mlib = Paths.get(home, "modules");
 172             if (Files.isDirectory(mlib)) {
 173                 return of(mlib);
 174             } else {
 175                 throw new InternalError("Unable to detect the run-time image");
 176             }
 177         }
 178     }
 179 
 180     /**
 181      * Returns a module finder that locates modules on the file system by
 182      * searching a sequence of directories and/or packaged modules.
 183      *
 184      * Each element in the given array is one of:
 185      * <ol>
 186      *     <li><p> A path to a directory of modules.</p></li>
 187      *     <li><p> A path to the <em>top-level</em> directory of an
 188      *         <em>exploded module</em>. </p></li>
 189      *     <li><p> A path to a <em>packaged module</em>. </p></li>




  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.HashMap;
  38 import java.util.HashSet;
  39 import java.util.List;
  40 import java.util.Map;
  41 import java.util.Objects;
  42 import java.util.Optional;
  43 import java.util.Set;
  44 
  45 import jdk.internal.module.ModulePath;
  46 import jdk.internal.module.SystemModuleFinder;
  47 import sun.security.action.GetPropertyAction;
  48 
  49 /**
  50  * A finder of modules. A {@code ModuleFinder} is used to find modules during
  51  * <a href="Configuration.html#resolution">resolution</a> or
  52  * <a href="Configuration.html#servicebinding">service binding</a>.
  53  *
  54  * <p> A {@code ModuleFinder} can only find one module with a given name. A
  55  * {@code ModuleFinder} that finds modules in a sequence of directories, for
  56  * example, will locate the first occurrence of a module of a given name and
  57  * will ignore other modules of that name that appear in directories later in
  58  * the sequence. </p>
  59  *
  60  * <p> Example usage: </p>
  61  *
  62  * <pre>{@code
  63  *     Path dir1, dir2, dir3;
  64  *
  65  *     ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
  66  *


 122      * ModuleReference} element in the returned set then it is guaranteed that
 123      * {@link #find find} will locate the {@code ModuleReference} if invoked
 124      * to find that module. </p>
 125      *
 126      * @apiNote This is important to have for methods such as {@link
 127      * Configuration#resolveRequiresAndUses resolveRequiresAndUses} that need
 128      * to scan the module path to find modules that provide a specific service.
 129      *
 130      * @return The set of all module references that this finder locates
 131      *
 132      * @throws FindException
 133      *         If an error occurs finding all modules
 134      *
 135      * @throws SecurityException
 136      *         If denied by the security manager
 137      */
 138     Set<ModuleReference> findAll();
 139 
 140     /**
 141      * Returns a module finder that locates the <em>system modules</em>. The
 142      * system modules are the modules in the Java run-time image.
 143      * The module finder will always find {@code java.base}.
 144      *
 145      * <p> If there is a security manager set then its {@link
 146      * SecurityManager#checkPermission(Permission) checkPermission} method is
 147      * invoked to check that the caller has been granted {@link FilePermission}
 148      * to recursively read the directory that is the value of the system
 149      * property {@code java.home}. </p>
 150      *
 151      * @return A {@code ModuleFinder} that locates the system modules
 152      *
 153      * @throws SecurityException
 154      *         If denied by the security manager
 155      */
 156     static ModuleFinder ofSystem() {
 157         String home;
 158 
 159         SecurityManager sm = System.getSecurityManager();
 160         if (sm != null) {
 161             PrivilegedAction<String> pa = new GetPropertyAction("java.home");
 162             home = AccessController.doPrivileged(pa);
 163             Permission p = new FilePermission(home + File.separator + "-", "read");
 164             sm.checkPermission(p);
 165         } else {
 166             home = System.getProperty("java.home");
 167         }
 168 
 169         Path modules = Paths.get(home, "lib", "modules");
 170         if (Files.isRegularFile(modules)) {
 171             return SystemModuleFinder.getInstance();
 172         } else {
 173             Path mlib = Paths.get(home, "modules");
 174             if (Files.isDirectory(mlib)) {
 175                 return of(mlib);
 176             } else {
 177                 throw new InternalError("Unable to detect the run-time image");
 178             }
 179         }
 180     }
 181 
 182     /**
 183      * Returns a module finder that locates modules on the file system by
 184      * searching a sequence of directories and/or packaged modules.
 185      *
 186      * Each element in the given array is one of:
 187      * <ol>
 188      *     <li><p> A path to a directory of modules.</p></li>
 189      *     <li><p> A path to the <em>top-level</em> directory of an
 190      *         <em>exploded module</em>. </p></li>
 191      *     <li><p> A path to a <em>packaged module</em>. </p></li>


< prev index next >