1 /* |
|
2 * Copyright (c) 2012, 2016, 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 |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.jdeps; |
|
27 |
|
28 import java.io.File; |
|
29 import java.io.FileNotFoundException; |
|
30 import java.io.IOException; |
|
31 import java.io.UncheckedIOException; |
|
32 import java.lang.module.Configuration; |
|
33 import java.lang.module.ModuleDescriptor; |
|
34 import java.lang.module.ModuleFinder; |
|
35 import java.lang.module.ModuleReference; |
|
36 import java.lang.module.ResolvedModule; |
|
37 import java.net.URI; |
|
38 import java.nio.file.FileSystem; |
|
39 import java.nio.file.FileSystemNotFoundException; |
|
40 import java.nio.file.FileSystems; |
|
41 import java.nio.file.Files; |
|
42 import java.nio.file.Path; |
|
43 import java.nio.file.Paths; |
|
44 import java.nio.file.ProviderNotFoundException; |
|
45 import java.util.*; |
|
46 import java.util.stream.Collectors; |
|
47 |
|
48 import static java.lang.module.ModuleDescriptor.Requires.Modifier.*; |
|
49 |
|
50 public class ModulePaths { |
|
51 final ModuleFinder finder; |
|
52 final Map<String, Module> modules = new LinkedHashMap<>(); |
|
53 |
|
54 public ModulePaths(String upgradeModulePath, String modulePath) { |
|
55 this(upgradeModulePath, modulePath, Collections.emptyList()); |
|
56 } |
|
57 |
|
58 public ModulePaths(String upgradeModulePath, String modulePath, List<Path> jars) { |
|
59 ModuleFinder finder = ModuleFinder.ofSystem(); |
|
60 if (upgradeModulePath != null) { |
|
61 finder = ModuleFinder.compose(createModulePathFinder(upgradeModulePath), finder); |
|
62 } |
|
63 if (jars.size() > 0) { |
|
64 finder = ModuleFinder.compose(finder, ModuleFinder.of(jars.toArray(new Path[0]))); |
|
65 } |
|
66 if (modulePath != null) { |
|
67 finder = ModuleFinder.compose(finder, createModulePathFinder(modulePath)); |
|
68 } |
|
69 this.finder = finder; |
|
70 |
|
71 // add modules from modulepaths |
|
72 finder.findAll().stream().forEach(mref -> |
|
73 modules.computeIfAbsent(mref.descriptor().name(), mn -> toModule(mn, mref)) |
|
74 ); |
|
75 } |
|
76 |
|
77 /** |
|
78 * Returns the list of Modules that can be found in the specified |
|
79 * module paths. |
|
80 */ |
|
81 Map<String, Module> getModules() { |
|
82 return modules; |
|
83 } |
|
84 |
|
85 Set<Module> dependences(String... roots) { |
|
86 Configuration cf = configuration(roots); |
|
87 return cf.modules().stream() |
|
88 .map(ResolvedModule::name) |
|
89 .map(modules::get) |
|
90 .collect(Collectors.toSet()); |
|
91 } |
|
92 |
|
93 Configuration configuration(String... roots) { |
|
94 return Configuration.empty().resolveRequires(finder, ModuleFinder.empty(), Set.of(roots)); |
|
95 } |
|
96 |
|
97 private static ModuleFinder createModulePathFinder(String mpaths) { |
|
98 if (mpaths == null) { |
|
99 return null; |
|
100 } else { |
|
101 String[] dirs = mpaths.split(File.pathSeparator); |
|
102 Path[] paths = new Path[dirs.length]; |
|
103 int i = 0; |
|
104 for (String dir : dirs) { |
|
105 paths[i++] = Paths.get(dir); |
|
106 } |
|
107 return ModuleFinder.of(paths); |
|
108 } |
|
109 } |
|
110 |
|
111 private static Module toModule(String mn, ModuleReference mref) { |
|
112 return SystemModulePath.find(mn) |
|
113 .orElse(toModule(new Module.Builder(mn), mref)); |
|
114 } |
|
115 |
|
116 private static Module toModule(Module.Builder builder, ModuleReference mref) { |
|
117 ModuleDescriptor md = mref.descriptor(); |
|
118 builder.descriptor(md); |
|
119 for (ModuleDescriptor.Requires req : md.requires()) { |
|
120 builder.require(req.name(), req.modifiers().contains(PUBLIC)); |
|
121 } |
|
122 for (ModuleDescriptor.Exports exp : md.exports()) { |
|
123 builder.export(exp.source(), exp.targets()); |
|
124 } |
|
125 builder.packages(md.packages()); |
|
126 |
|
127 try { |
|
128 URI location = mref.location() |
|
129 .orElseThrow(FileNotFoundException::new); |
|
130 builder.location(location); |
|
131 builder.classes(getClassReader(location, md.name())); |
|
132 } catch (IOException e) { |
|
133 throw new UncheckedIOException(e); |
|
134 } |
|
135 return builder.build(); |
|
136 } |
|
137 |
|
138 static class SystemModulePath { |
|
139 final static Module JAVA_BASE; |
|
140 |
|
141 private final static FileSystem fs; |
|
142 private final static Path root; |
|
143 private final static Map<String, Module> installed = new HashMap<>(); |
|
144 static { |
|
145 if (isJrtAvailable()) { |
|
146 // jrt file system |
|
147 fs = FileSystems.getFileSystem(URI.create("jrt:/")); |
|
148 root = fs.getPath("/modules"); |
|
149 } else { |
|
150 // exploded image |
|
151 String javahome = System.getProperty("java.home"); |
|
152 fs = FileSystems.getDefault(); |
|
153 root = Paths.get(javahome, "modules"); |
|
154 } |
|
155 |
|
156 ModuleFinder.ofSystem().findAll().stream() |
|
157 .forEach(mref -> |
|
158 installed.computeIfAbsent(mref.descriptor().name(), |
|
159 mn -> toModule(new Module.Builder(mn, true), mref)) |
|
160 ); |
|
161 JAVA_BASE = installed.get("java.base"); |
|
162 |
|
163 Profile.init(installed); |
|
164 } |
|
165 |
|
166 private static boolean isJrtAvailable() { |
|
167 try { |
|
168 FileSystems.getFileSystem(URI.create("jrt:/")); |
|
169 return true; |
|
170 } catch (ProviderNotFoundException | FileSystemNotFoundException e) { |
|
171 return false; |
|
172 } |
|
173 } |
|
174 |
|
175 public static Optional<Module> find(String mn) { |
|
176 return installed.containsKey(mn) ? Optional.of(installed.get(mn)) |
|
177 : Optional.empty(); |
|
178 } |
|
179 |
|
180 public static boolean contains(Module m) { |
|
181 return installed.containsValue(m); |
|
182 } |
|
183 |
|
184 public static ClassFileReader getClassReader(String modulename) throws IOException { |
|
185 Path mp = root.resolve(modulename); |
|
186 if (Files.exists(mp) && Files.isDirectory(mp)) { |
|
187 return ClassFileReader.newInstance(fs, mp); |
|
188 } else { |
|
189 throw new FileNotFoundException(mp.toString()); |
|
190 } |
|
191 } |
|
192 } |
|
193 |
|
194 /** |
|
195 * Returns a ModuleClassReader that only reads classes for the given modulename. |
|
196 */ |
|
197 public static ClassFileReader getClassReader(URI location, String modulename) |
|
198 throws IOException { |
|
199 if (location.getScheme().equals("jrt")) { |
|
200 return SystemModulePath.getClassReader(modulename); |
|
201 } else { |
|
202 Path path = Paths.get(location); |
|
203 return ClassFileReader.newInstance(path); |
|
204 } |
|
205 } |
|
206 } |
|