16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
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
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.Arrays;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Objects;
43 import java.util.Optional;
44 import java.util.Set;
45
46 import sun.security.action.GetPropertyAction;
47
48 /**
49 * A finder of modules. A {@code ModuleFinder} is used to find modules during
50 * <a href="Configuration.html#resolution">resolution</a> or
51 * <a href="Configuration.html#servicebinding">service binding</a>.
52 *
53 * <p> A {@code ModuleFinder} can only find one module with a given name. A
54 * {@code ModuleFinder} that finds modules in a sequence of directories, for
55 * example, will locate the first occurrence of a module of a given name and
56 * will ignore other modules of that name that appear in directories later in
325 * module finder will locate a module by invoking the {@code find} method
326 * of each module finder, in array index order, until either the module is
327 * found or all module finders have been searched. The {@link #findAll()
328 * findAll} method of the resulting module finder will return a set of
329 * modules that includes all modules located by the first module finder.
330 * The set of modules will include all modules located by the second or
331 * subsequent module finder that are not located by previous module finders
332 * in the sequence.
333 *
334 * <p> When locating modules then any exceptions or errors thrown by the
335 * {@code find} or {@code findAll} methods of the underlying module finders
336 * will be propogated to the caller of the resulting module finder's
337 * {@code find} or {@code findAll} methods. </p>
338 *
339 * @param finders
340 * The array of module finders
341 *
342 * @return A {@code ModuleFinder} that composes a sequence of module finders
343 */
344 static ModuleFinder compose(ModuleFinder... finders) {
345 final List<ModuleFinder> finderList = Arrays.asList(finders);
346 finderList.forEach(Objects::requireNonNull);
347
348 return new ModuleFinder() {
349 private final Map<String, ModuleReference> nameToModule = new HashMap<>();
350 private Set<ModuleReference> allModules;
351
352 @Override
353 public Optional<ModuleReference> find(String name) {
354 // cached?
355 ModuleReference mref = nameToModule.get(name);
356 if (mref != null)
357 return Optional.of(mref);
358 Optional<ModuleReference> omref = finderList.stream()
359 .map(f -> f.find(name))
360 .flatMap(Optional::stream)
361 .findFirst();
362 omref.ifPresent(m -> nameToModule.put(name, m));
363 return omref;
364 }
365
|
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
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
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
324 * module finder will locate a module by invoking the {@code find} method
325 * of each module finder, in array index order, until either the module is
326 * found or all module finders have been searched. The {@link #findAll()
327 * findAll} method of the resulting module finder will return a set of
328 * modules that includes all modules located by the first module finder.
329 * The set of modules will include all modules located by the second or
330 * subsequent module finder that are not located by previous module finders
331 * in the sequence.
332 *
333 * <p> When locating modules then any exceptions or errors thrown by the
334 * {@code find} or {@code findAll} methods of the underlying module finders
335 * will be propogated to the caller of the resulting module finder's
336 * {@code find} or {@code findAll} methods. </p>
337 *
338 * @param finders
339 * The array of module finders
340 *
341 * @return A {@code ModuleFinder} that composes a sequence of module finders
342 */
343 static ModuleFinder compose(ModuleFinder... finders) {
344 final List<ModuleFinder> finderList = List.of(finders);
345 finderList.forEach(Objects::requireNonNull);
346
347 return new ModuleFinder() {
348 private final Map<String, ModuleReference> nameToModule = new HashMap<>();
349 private Set<ModuleReference> allModules;
350
351 @Override
352 public Optional<ModuleReference> find(String name) {
353 // cached?
354 ModuleReference mref = nameToModule.get(name);
355 if (mref != null)
356 return Optional.of(mref);
357 Optional<ModuleReference> omref = finderList.stream()
358 .map(f -> f.find(name))
359 .flatMap(Optional::stream)
360 .findFirst();
361 omref.ifPresent(m -> nameToModule.put(name, m));
362 return omref;
363 }
364
|