1 /*
2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
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
45 import java.net.URI;
46 import java.nio.file.DirectoryStream;
47 import java.nio.file.FileSystem;
48 import java.nio.file.FileSystems;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.ArrayList;
53 import java.util.Collections;
54 import java.util.HashMap;
55 import java.util.HashSet;
56 import java.util.LinkedHashMap;
57 import java.util.LinkedHashSet;
58 import java.util.List;
59 import java.util.Map;
60 import java.util.Objects;
61 import java.util.Optional;
62 import java.util.Set;
63 import java.util.function.Function;
64 import java.util.function.Supplier;
65 import java.util.stream.Stream;
66
67 public class JdepsConfiguration implements AutoCloseable {
68 // the token for "all modules on the module path"
69 public static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
70 public static final String ALL_DEFAULT = "ALL-DEFAULT";
71 public static final String ALL_SYSTEM = "ALL-SYSTEM";
72
73 public static final String MODULE_INFO = "module-info.class";
74
75 private final SystemModuleFinder system;
76 private final ModuleFinder finder;
77
78 private final Map<String, Module> nameToModule = new LinkedHashMap<>();
79 private final Map<String, Module> packageToModule = new HashMap<>();
80 private final Map<String, List<Archive>> packageToUnnamedModule = new HashMap<>();
81
82 private final List<Archive> classpathArchives = new ArrayList<>();
83 private final List<Archive> initialArchives = new ArrayList<>();
84 private final Set<Module> rootModules = new HashSet<>();
302
303 public Runtime.Version getVersion() {
304 return version;
305 }
306
307 /*
308 * Close all archives e.g. JarFile
309 */
310 @Override
311 public void close() throws IOException {
312 for (Archive archive : initialArchives)
313 archive.close();
314 for (Archive archive : classpathArchives)
315 archive.close();
316 for (Module module : nameToModule.values())
317 module.close();
318 }
319
320 static class SystemModuleFinder implements ModuleFinder {
321 private static final String JAVA_HOME = System.getProperty("java.home");
322 private static final String JAVA_SE = "java.se";
323
324 private final FileSystem fileSystem;
325 private final Path root;
326 private final Map<String, ModuleReference> systemModules;
327
328 SystemModuleFinder() {
329 if (Files.isRegularFile(Paths.get(JAVA_HOME, "lib", "modules"))) {
330 // jrt file system
331 this.fileSystem = FileSystems.getFileSystem(URI.create("jrt:/"));
332 this.root = fileSystem.getPath("/modules");
333 this.systemModules = walk(root);
334 } else {
335 // exploded image
336 this.fileSystem = FileSystems.getDefault();
337 root = Paths.get(JAVA_HOME, "modules");
338 this.systemModules = ModuleFinder.ofSystem().findAll().stream()
339 .collect(toMap(mref -> mref.descriptor().name(), Function.identity()));
340 }
341 }
342
427 public Optional<ModuleReference> find(String mn) {
428 return systemModules.containsKey(mn)
429 ? Optional.of(systemModules.get(mn)) : Optional.empty();
430 }
431
432 public Stream<String> moduleNames() {
433 return systemModules.values().stream()
434 .map(mref -> mref.descriptor().name());
435 }
436
437 public ClassFileReader getClassReader(String modulename) throws IOException {
438 Path mp = root.resolve(modulename);
439 if (Files.exists(mp) && Files.isDirectory(mp)) {
440 return ClassFileReader.newInstance(fileSystem, mp);
441 } else {
442 throw new FileNotFoundException(mp.toString());
443 }
444 }
445
446 public Set<String> defaultSystemRoots() {
447 Set<String> roots = new HashSet<>();
448 boolean hasJava = false;
449 if (systemModules.containsKey(JAVA_SE)) {
450 // java.se is a system module
451 hasJava = true;
452 roots.add(JAVA_SE);
453 }
454
455 for (ModuleReference mref : systemModules.values()) {
456 String mn = mref.descriptor().name();
457 if (hasJava && mn.startsWith("java."))
458 continue;
459
460 // add as root if observable and exports at least one package
461 ModuleDescriptor descriptor = mref.descriptor();
462 for (ModuleDescriptor.Exports e : descriptor.exports()) {
463 if (!e.isQualified()) {
464 roots.add(mn);
465 break;
466 }
467 }
468 }
469 return roots;
470 }
471 }
472
473 public static class Builder {
474
475 final SystemModuleFinder systemModulePath;
476 final Set<String> rootModules = new HashSet<>();
477 final List<Archive> initialArchives = new ArrayList<>();
478 final List<Path> paths = new ArrayList<>();
479 final List<Path> classPaths = new ArrayList<>();
480 final Set<String> tokens = new HashSet<>();
481
482 ModuleFinder upgradeModulePath;
483 ModuleFinder appModulePath;
484 Runtime.Version version;
485
486 public Builder() {
487 this.systemModulePath = new SystemModuleFinder();
488 }
489
|
1 /*
2 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
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
45 import java.net.URI;
46 import java.nio.file.DirectoryStream;
47 import java.nio.file.FileSystem;
48 import java.nio.file.FileSystems;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.ArrayList;
53 import java.util.Collections;
54 import java.util.HashMap;
55 import java.util.HashSet;
56 import java.util.LinkedHashMap;
57 import java.util.LinkedHashSet;
58 import java.util.List;
59 import java.util.Map;
60 import java.util.Objects;
61 import java.util.Optional;
62 import java.util.Set;
63 import java.util.function.Function;
64 import java.util.function.Supplier;
65 import java.util.stream.Collectors;
66 import java.util.stream.Stream;
67
68 public class JdepsConfiguration implements AutoCloseable {
69 // the token for "all modules on the module path"
70 public static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
71 public static final String ALL_DEFAULT = "ALL-DEFAULT";
72 public static final String ALL_SYSTEM = "ALL-SYSTEM";
73
74 public static final String MODULE_INFO = "module-info.class";
75
76 private final SystemModuleFinder system;
77 private final ModuleFinder finder;
78
79 private final Map<String, Module> nameToModule = new LinkedHashMap<>();
80 private final Map<String, Module> packageToModule = new HashMap<>();
81 private final Map<String, List<Archive>> packageToUnnamedModule = new HashMap<>();
82
83 private final List<Archive> classpathArchives = new ArrayList<>();
84 private final List<Archive> initialArchives = new ArrayList<>();
85 private final Set<Module> rootModules = new HashSet<>();
303
304 public Runtime.Version getVersion() {
305 return version;
306 }
307
308 /*
309 * Close all archives e.g. JarFile
310 */
311 @Override
312 public void close() throws IOException {
313 for (Archive archive : initialArchives)
314 archive.close();
315 for (Archive archive : classpathArchives)
316 archive.close();
317 for (Module module : nameToModule.values())
318 module.close();
319 }
320
321 static class SystemModuleFinder implements ModuleFinder {
322 private static final String JAVA_HOME = System.getProperty("java.home");
323
324 private final FileSystem fileSystem;
325 private final Path root;
326 private final Map<String, ModuleReference> systemModules;
327
328 SystemModuleFinder() {
329 if (Files.isRegularFile(Paths.get(JAVA_HOME, "lib", "modules"))) {
330 // jrt file system
331 this.fileSystem = FileSystems.getFileSystem(URI.create("jrt:/"));
332 this.root = fileSystem.getPath("/modules");
333 this.systemModules = walk(root);
334 } else {
335 // exploded image
336 this.fileSystem = FileSystems.getDefault();
337 root = Paths.get(JAVA_HOME, "modules");
338 this.systemModules = ModuleFinder.ofSystem().findAll().stream()
339 .collect(toMap(mref -> mref.descriptor().name(), Function.identity()));
340 }
341 }
342
427 public Optional<ModuleReference> find(String mn) {
428 return systemModules.containsKey(mn)
429 ? Optional.of(systemModules.get(mn)) : Optional.empty();
430 }
431
432 public Stream<String> moduleNames() {
433 return systemModules.values().stream()
434 .map(mref -> mref.descriptor().name());
435 }
436
437 public ClassFileReader getClassReader(String modulename) throws IOException {
438 Path mp = root.resolve(modulename);
439 if (Files.exists(mp) && Files.isDirectory(mp)) {
440 return ClassFileReader.newInstance(fileSystem, mp);
441 } else {
442 throw new FileNotFoundException(mp.toString());
443 }
444 }
445
446 public Set<String> defaultSystemRoots() {
447 return systemModules.values().stream()
448 .map(ModuleReference::descriptor)
449 .filter(descriptor -> descriptor.exports()
450 .stream()
451 .filter(e -> !e.isQualified())
452 .findAny()
453 .isPresent())
454 .map(ModuleDescriptor::name)
455 .collect(Collectors.toSet());
456 }
457 }
458
459 public static class Builder {
460
461 final SystemModuleFinder systemModulePath;
462 final Set<String> rootModules = new HashSet<>();
463 final List<Archive> initialArchives = new ArrayList<>();
464 final List<Path> paths = new ArrayList<>();
465 final List<Path> classPaths = new ArrayList<>();
466 final Set<String> tokens = new HashSet<>();
467
468 ModuleFinder upgradeModulePath;
469 ModuleFinder appModulePath;
470 Runtime.Version version;
471
472 public Builder() {
473 this.systemModulePath = new SystemModuleFinder();
474 }
475
|