1 /*
2 * Copyright (c) 2017, 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
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.Map;
39 import java.util.Set;
40 import java.util.function.Consumer;
41 import java.util.function.Function;
42 import java.util.stream.Stream;
43 import static java.util.stream.Collectors.*;
44
45 /**
46 * A Builder to compute ModuleHashes from a given configuration
47 */
48 public class ModuleHashesBuilder {
49 private final Configuration configuration;
50 private final Set<String> hashModuleCandidates;
51
52 /**
53 * Constructs a ModuleHashesBuilder that finds the packaged modules
54 * from the location of ModuleReference found from the given Configuration.
55 *
56 * @param config Configuration for building module hashes
81 while ((rm = todo.poll()) != null) {
82 if (visited.add(rm)) {
83 builder.addNode(rm.name());
84 for (ResolvedModule dm : rm.reads()) {
85 if (!visited.contains(dm)) {
86 todo.push(dm);
87 }
88 builder.addEdge(rm.name(), dm.name());
89 }
90 }
91 }
92
93 // each node in a transposed graph is a matching packaged module
94 // in which the hash of the modules that depend upon it is recorded
95 Graph<String> transposedGraph = builder.build().transpose();
96
97 // traverse the modules in topological order that will identify
98 // the modules to record the hashes - it is the first matching
99 // module and has not been hashed during the traversal.
100 Set<String> mods = new HashSet<>();
101 Map<String, ModuleHashes> hashes = new HashMap<>();
102 builder.build()
103 .orderedNodes()
104 .filter(mn -> roots.contains(mn) && !mods.contains(mn))
105 .forEach(mn -> {
106 // Compute hashes of the modules that depend on mn directly and
107 // indirectly excluding itself.
108 Set<String> ns = transposedGraph.dfs(mn)
109 .stream()
110 .filter(n -> !n.equals(mn) && hashModuleCandidates.contains(n))
111 .collect(toSet());
112 mods.add(mn);
113 mods.addAll(ns);
114
115 if (!ns.isEmpty()) {
116 Map<String, Path> moduleToPath = ns.stream()
117 .collect(toMap(Function.identity(), this::moduleToPath));
118 hashes.put(mn, ModuleHashes.generate(moduleToPath, "SHA-256"));
119 }
120 });
121 return hashes;
|
1 /*
2 * Copyright (c) 2017, 2020, 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 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.TreeMap;
38 import java.util.HashSet;
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
53 /**
54 * Constructs a ModuleHashesBuilder that finds the packaged modules
55 * from the location of ModuleReference found from the given Configuration.
56 *
57 * @param config Configuration for building module hashes
82 while ((rm = todo.poll()) != null) {
83 if (visited.add(rm)) {
84 builder.addNode(rm.name());
85 for (ResolvedModule dm : rm.reads()) {
86 if (!visited.contains(dm)) {
87 todo.push(dm);
88 }
89 builder.addEdge(rm.name(), dm.name());
90 }
91 }
92 }
93
94 // each node in a transposed graph is a matching packaged module
95 // in which the hash of the modules that depend upon it is recorded
96 Graph<String> transposedGraph = builder.build().transpose();
97
98 // traverse the modules in topological order that will identify
99 // the modules to record the hashes - it is the first matching
100 // module and has not been hashed during the traversal.
101 Set<String> mods = new HashSet<>();
102 Map<String, ModuleHashes> hashes = new TreeMap<>();
103 builder.build()
104 .orderedNodes()
105 .filter(mn -> roots.contains(mn) && !mods.contains(mn))
106 .forEach(mn -> {
107 // Compute hashes of the modules that depend on mn directly and
108 // indirectly excluding itself.
109 Set<String> ns = transposedGraph.dfs(mn)
110 .stream()
111 .filter(n -> !n.equals(mn) && hashModuleCandidates.contains(n))
112 .collect(toSet());
113 mods.add(mn);
114 mods.addAll(ns);
115
116 if (!ns.isEmpty()) {
117 Map<String, Path> moduleToPath = ns.stream()
118 .collect(toMap(Function.identity(), this::moduleToPath));
119 hashes.put(mn, ModuleHashes.generate(moduleToPath, "SHA-256"));
120 }
121 });
122 return hashes;
|