jdk/src/java.base/share/classes/java/lang/module/ModuleFinder.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 36511 9d0388c6b336
child 37779 7c84df693837
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.FilePermission;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.security.AccessController;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.security.Permission;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.security.PrivilegedAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.stream.Stream;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36511
diff changeset
    42
import sun.security.action.GetPropertyAction;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 * A finder of modules. A {@code ModuleFinder} is used to find modules during
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * <a href="Configuration.html#resolution">resolution</a> or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 * <a href="Configuration.html#servicebinding">service binding</a>.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 * <p> A {@code ModuleFinder} can only find one module with a given name. A
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 * {@code ModuleFinder} that finds modules in a sequence of directories, for
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 * example, will locate the first occurrence of a module of a given name and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 * will ignore other modules of that name that appear in directories later in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * the sequence. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 * <p> Example usage: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 * <pre>{@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
 *     Path dir1, dir2, dir3;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 *     ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
 *     Optional<ModuleReference> omref = finder.find("jdk.foo");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 *     if (omref.isPresent()) { ... }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 * }</pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 * <p> The {@link #find(String) find} and {@link #findAll() findAll} methods
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 * defined here can fail for several reasons. These include include I/O errors,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 * errors detected parsing a module descriptor ({@code module-info.class}), or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 * in the case of {@code ModuleFinder} returned by {@link #of ModuleFinder.of},
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
 * that two or more modules with the same name are found in a directory.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
 * When an error is detected then these methods throw {@link FindException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
 * FindException} with an appropriate {@link Throwable#getCause cause}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
 * The behavior of a {@code ModuleFinder} after a {@code FindException} is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
 * thrown is undefined. For example, invoking {@code find} after an exception
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
 * is thrown may or may not scan the same modules that lead to the exception.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
 * It is recommended that a module finder be discarded after an exception is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
 * thrown. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
 * <p> A {@code ModuleFinder} is not required to be thread safe. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
 * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
public interface ModuleFinder {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
     * Finds a reference to a module of a given name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
     * <p> A {@code ModuleFinder} provides a consistent view of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
     * modules that it locates. If {@code find} is invoked several times to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
     * locate the same module (by name) then it will return the same result
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
     * each time. If a module is located then it is guaranteed to be a member
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
     * of the set of modules returned by the {@link #findAll() findAll}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
     * method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
     *         The name of the module to find
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     * @return A reference to a module with the given name or an empty
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     *         {@code Optional} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
     * @throws FindException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
     *         If an error occurs finding the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
    Optional<ModuleReference> find(String name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
     * Returns the set of all module references that this finder can locate.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
     * <p> A {@code ModuleFinder} provides a consistent view of the modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
     * that it locates. If {@link #findAll() findAll} is invoked several times
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
     * then it will return the same (equals) result each time. For each {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
     * ModuleReference} element in the returned set then it is guaranteed that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
     * {@link #find find} will locate the {@code ModuleReference} if invoked
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
     * to find that module. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
     * @apiNote This is important to have for methods such as {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
     * Configuration#resolveRequiresAndUses resolveRequiresAndUses} that need
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
     * to scan the module path to find modules that provide a specific service.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
     * @return The set of all module references that this finder locates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
     * @throws FindException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
     *         If an error occurs finding all modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
    Set<ModuleReference> findAll();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
     * Returns a module finder that locates the <em>system modules</em>. The
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     * system modules are typically linked into the Java run-time image.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
     * The module finder will always find {@code java.base}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
     * <p> If there is a security manager set then its {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
     * SecurityManager#checkPermission(Permission) checkPermission} method is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
     * invoked to check that the caller has been granted {@link FilePermission}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
     * to recursively read the directory that is the value of the system
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
     * property {@code java.home}. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
     * @return A {@code ModuleFinder} that locates the system modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
    static ModuleFinder ofSystem() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        String home;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        if (sm != null) {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36511
diff changeset
   156
            PrivilegedAction<String> pa = new GetPropertyAction("java.home");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
            home = AccessController.doPrivileged(pa);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            Permission p = new FilePermission(home + File.separator + "-", "read");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            sm.checkPermission(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
            home = System.getProperty("java.home");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        Path modules = Paths.get(home, "lib", "modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        if (Files.isRegularFile(modules)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
            return new SystemModuleFinder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
            Path mlib = Paths.get(home, "modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
            if (Files.isDirectory(mlib)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
                return of(mlib);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
                throw new InternalError("Unable to detect the run-time image");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
     * Returns a module finder that locates modules on the file system by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
     * searching a sequence of directories and/or packaged modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
     * Each element in the given array is one of:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
     * <ol>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
     *     <li><p> A path to a directory of modules.</p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
     *     <li><p> A path to the <em>top-level</em> directory of an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
     *         <em>exploded module</em>. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
     *     <li><p> A path to a <em>packaged module</em>. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
     * </ol>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
     * The module finder locates modules by searching each directory, exploded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
     * module, or packaged module in array index order. It finds the first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
     * occurrence of a module with a given name and ignores other modules of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
     * that name that appear later in the sequence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
     * <p> If an element is a path to a directory of modules then each entry in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
     * the directory is a packaged module or the top-level directory of an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
     * exploded module. The module finder's {@link #find(String) find} or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
     * {@link #findAll() findAll} methods throw {@link FindException} if a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
     * directory containing more than one module with the same name is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
     * encountered. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
     * <p> If an element in the array is a path to a directory, and that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
     * directory contains a file named {@code module-info.class}, then the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
     * directory is treated as an exploded module rather than a directory of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
     * modules. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
     * <p> The module finder returned by this method supports modules that are
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
     * packaged as JAR files. A JAR file with a {@code module-info.class} in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
     * the top-level directory of the JAR file is a modular JAR and is an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
     * <em>explicit module</em>. A JAR file that does not have a {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
     * module-info.class} in the top-level directory is an {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
     * ModuleDescriptor#isAutomatic automatic} module. The {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
     * ModuleDescriptor} for an automatic module is created as follows:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
     * <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
     *     <li><p> The module {@link ModuleDescriptor#name() name}, and {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
     *     ModuleDescriptor#version() version} if applicable, is derived from
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
     *     the file name of the JAR file as follows: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
     *     <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
     *         <li><p> The {@code .jar} suffix is removed. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
     *         <li><p> If the name matches the regular expression {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
     *         "-(\\d+(\\.|$))"} then the module name will be derived from the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
     *         subsequence proceeding the hyphen of the first occurrence. The
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
     *         subsequence after the hyphen is parsed as a {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
     *         ModuleDescriptor.Version} and ignored if it cannot be parsed as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
     *         a {@code Version}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
     *         <li><p> For the module name, then all non-alphanumeric
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
     *         characters ({@code [^A-Za-z0-9])} are replaced with a dot
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
     *         ({@code "."}), all repeating dots are replaced with one dot,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
     *         and all leading and trailing dots are removed. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
     *         <li><p> As an example, a JAR file named {@code foo-bar.jar} will
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
     *         derive a module name {@code foo.bar} and no version. A JAR file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
     *         named {@code foo-1.2.3-SNAPSHOT.jar} will derive a module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
     *         {@code foo} and {@code 1.2.3-SNAPSHOT} as the version. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
     *     </ul></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
     *     <li><p> It {@link ModuleDescriptor#requires() requires} {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
     *     java.base}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
     *     <li><p> All entries in the JAR file with names ending with {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
     *     .class} are assumed to be class files where the name corresponds
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
     *     to the fully qualified name of the class. The packages of all
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
     *     classes are {@link ModuleDescriptor#exports() exported}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
     *     <li><p> The contents of all entries starting with {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
     *     META-INF/services/} are assumed to be service configuration files
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
     *     (see {@link java.util.ServiceLoader}). The name of the file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
     *     (that follows {@code META-INF/services/}) is assumed to be the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
     *     fully-qualified binary name of a service type. The entries in the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
     *     file are assumed to be the fully-qualified binary names of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
     *     provider classes. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
     *     <li><p> If the JAR file has a {@code Main-Class} attribute in its
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
     *     main manifest then its value is the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
     *     ModuleDescriptor#mainClass() main class}. </p></li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
     * </ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
     * <p> In addition to JAR files, an implementation may also support modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
     * that are packaged in other implementation specific module formats. As
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
     * with automatic modules, the contents of a packaged or exploded module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
     * may need to be <em>scanned</em> in order to determine the packages in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
     * the module. If a {@code .class} file that corresponds to a class in an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
     * unnamed package is encountered then {@code FindException} is thrown. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
     * <p> Finders created by this method are lazy and do not eagerly check
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
     * that the given file paths are directories or packaged modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
     * Consequently, the {@code find} or {@code findAll} methods will only
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
     * fail if invoking these methods results in searching a directory or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
     * packaged module and an error is encountered. Paths to files that do not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
     * exist are ignored. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
     * @param entries
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
     *        A possibly-empty array of paths to directories of modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
     *        or paths to packaged or exploded modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
     * @return A {@code ModuleFinder} that locates modules on the file system
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
    static ModuleFinder of(Path... entries) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
        return new ModulePath(entries);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
     * Returns a module finder that is the equivalent to composing two
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
     * module finders. The resulting finder will locate modules references
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
     * using {@code first}; if not found then it will attempt to locate module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
     * references using {@code second}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
     * <p> The {@link #findAll() findAll} method of the resulting module finder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
     * will locate all modules located by the first module finder. It will
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
     * also locate all modules located by the second module finder that are not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
     * located by the first module finder. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
     * @apiNote This method will eventually be changed to take a sequence of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
     *          module finders.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
     * @param first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
     *        The first module finder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
     * @param second
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
     *        The second module finder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
     * @return A {@code ModuleFinder} that composes two module finders
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
    static ModuleFinder compose(ModuleFinder first, ModuleFinder second) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
        Objects.requireNonNull(first);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
        Objects.requireNonNull(second);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        return new ModuleFinder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
            Set<ModuleReference> allModules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
            public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
                Optional<ModuleReference> om = first.find(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
                if (!om.isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
                    om = second.find(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
                return om;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
            public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
                if (allModules == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
                    allModules = Stream.concat(first.findAll().stream(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
                                               second.findAll().stream())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
                                       .map(a -> a.descriptor().name())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                                       .distinct()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
                                       .map(this::find)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
                                       .map(Optional::get)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
                                       .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
                return allModules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
     * Returns an empty module finder.  The empty finder does not find any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
     * modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
     * @apiNote This is useful when using methods such as {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
     * Configuration#resolveRequires resolveRequires} where two finders are
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
     * specified. An alternative is {@code ModuleFinder.of()}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
     * @return A {@code ModuleFinder} that does not find any modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
    static ModuleFinder empty() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
        // an alternative implementation of ModuleFinder.of()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
        return new ModuleFinder() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
            @Override public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
                Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
                return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
            @Override public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
                return Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
}