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 jdk.internal.module;
27
28 import java.io.PrintStream;
29 import java.lang.module.Configuration;
30 import java.lang.module.ResolvedModule;
31 import java.net.URI;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.ArrayDeque;
35 import java.util.Collections;
36 import java.util.Deque;
37 import java.util.HashMap;
38 import java.util.HashSet;
39 import java.util.LinkedList;
40 import java.util.Map;
41 import java.util.Set;
42 import java.util.function.Consumer;
43 import java.util.function.Function;
44 import java.util.stream.Stream;
45 import static java.util.stream.Collectors.*;
46
47 /**
48 * A Builder to compute ModuleHashes from a given configuration
49 */
50 public class ModuleHashesBuilder {
51 private final Configuration configuration;
52 private final Set<String> hashModuleCandidates;
53
112 .stream()
113 .filter(n -> !n.equals(mn) && hashModuleCandidates.contains(n))
114 .collect(toSet());
115 mods.add(mn);
116 mods.addAll(ns);
117
118 if (!ns.isEmpty()) {
119 Map<String, Path> moduleToPath = ns.stream()
120 .collect(toMap(Function.identity(), this::moduleToPath));
121 hashes.put(mn, ModuleHashes.generate(moduleToPath, "SHA-256"));
122 }
123 });
124 return hashes;
125 }
126
127 private Path moduleToPath(String name) {
128 ResolvedModule rm = configuration.findModule(name).orElseThrow(
129 () -> new InternalError("Selected module " + name + " not on module path"));
130
131 URI uri = rm.reference().location().get();
132 Path path = Paths.get(uri);
133 String fn = path.getFileName().toString();
134 if (!fn.endsWith(".jar") && !fn.endsWith(".jmod")) {
135 throw new UnsupportedOperationException(path + " is not a modular JAR or jmod file");
136 }
137 return path;
138 }
139
140 /*
141 * Utility class
142 */
143 static class Graph<T> {
144 private final Set<T> nodes;
145 private final Map<T, Set<T>> edges;
146
147 public Graph(Set<T> nodes, Map<T, Set<T>> edges) {
148 this.nodes = Collections.unmodifiableSet(nodes);
149 this.edges = Collections.unmodifiableMap(edges);
150 }
151
152 public Set<T> nodes() {
|
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 jdk.internal.module;
27
28 import java.io.PrintStream;
29 import java.lang.module.Configuration;
30 import java.lang.module.ResolvedModule;
31 import java.net.URI;
32 import java.nio.file.Path;
33 import java.util.ArrayDeque;
34 import java.util.Collections;
35 import java.util.Deque;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.LinkedList;
39 import java.util.Map;
40 import java.util.Set;
41 import java.util.function.Consumer;
42 import java.util.function.Function;
43 import java.util.stream.Stream;
44 import static java.util.stream.Collectors.*;
45
46 /**
47 * A Builder to compute ModuleHashes from a given configuration
48 */
49 public class ModuleHashesBuilder {
50 private final Configuration configuration;
51 private final Set<String> hashModuleCandidates;
52
111 .stream()
112 .filter(n -> !n.equals(mn) && hashModuleCandidates.contains(n))
113 .collect(toSet());
114 mods.add(mn);
115 mods.addAll(ns);
116
117 if (!ns.isEmpty()) {
118 Map<String, Path> moduleToPath = ns.stream()
119 .collect(toMap(Function.identity(), this::moduleToPath));
120 hashes.put(mn, ModuleHashes.generate(moduleToPath, "SHA-256"));
121 }
122 });
123 return hashes;
124 }
125
126 private Path moduleToPath(String name) {
127 ResolvedModule rm = configuration.findModule(name).orElseThrow(
128 () -> new InternalError("Selected module " + name + " not on module path"));
129
130 URI uri = rm.reference().location().get();
131 Path path = Path.get(uri);
132 String fn = path.getFileName().toString();
133 if (!fn.endsWith(".jar") && !fn.endsWith(".jmod")) {
134 throw new UnsupportedOperationException(path + " is not a modular JAR or jmod file");
135 }
136 return path;
137 }
138
139 /*
140 * Utility class
141 */
142 static class Graph<T> {
143 private final Set<T> nodes;
144 private final Map<T, Set<T>> edges;
145
146 public Graph(Set<T> nodes, Map<T, Set<T>> edges) {
147 this.nodes = Collections.unmodifiableSet(nodes);
148 this.edges = Collections.unmodifiableMap(edges);
149 }
150
151 public Set<T> nodes() {
|