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.IOException;
29 import java.io.UncheckedIOException;
30 import java.nio.ByteBuffer;
31 import java.nio.channels.FileChannel;
32 import java.nio.file.Path;
33 import java.security.MessageDigest;
34 import java.security.NoSuchAlgorithmException;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.Set;
39
40 /**
41 * The result of hashing the contents of a number of module artifacts.
42 */
43
44 public final class ModuleHashes {
45
46 /**
47 * A supplier of a message digest.
48 */
49 public static interface HashSupplier {
50 byte[] generate(String algorithm);
51 }
52
53
54 private final String algorithm;
55 private final Map<String, byte[]> nameToHash;
56
57 /**
58 * Creates a {@code ModuleHashes}.
59 *
60 * @param algorithm the algorithm used to create the hashes
61 * @param nameToHash the map of module name to hash value
62 */
63 public ModuleHashes(String algorithm, Map<String, byte[]> nameToHash) {
64 this.algorithm = algorithm;
65 this.nameToHash = Collections.unmodifiableMap(nameToHash);
66 }
67
68 /**
69 * Returns the algorithm used to hash the modules ("SHA-256" for example).
70 */
71 public String algorithm() {
72 return algorithm;
73 }
125 }
126 }
127
128 /**
129 * Computes the hash for every entry in the given map, returning a
130 * {@code ModuleHashes} to encapsulate the result. The map key is
131 * the entry name, typically the module name. The map value is the file
132 * path to the entry (module artifact).
133 *
134 * @return ModuleHashes that encapsulates the hashes
135 */
136 public static ModuleHashes generate(Map<String, Path> map, String algorithm) {
137 Map<String, byte[]> nameToHash = new HashMap<>();
138 for (Map.Entry<String, Path> entry: map.entrySet()) {
139 String name = entry.getKey();
140 Path path = entry.getValue();
141 nameToHash.put(name, computeHash(path, algorithm));
142 }
143 return new ModuleHashes(algorithm, nameToHash);
144 }
145 }
|
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.IOException;
29 import java.io.UncheckedIOException;
30 import java.nio.ByteBuffer;
31 import java.nio.channels.FileChannel;
32 import java.nio.file.Path;
33 import java.security.MessageDigest;
34 import java.security.NoSuchAlgorithmException;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.Objects;
39 import java.util.Set;
40
41 /**
42 * The result of hashing the contents of a number of module artifacts.
43 */
44
45 public final class ModuleHashes {
46
47 /**
48 * A supplier of a message digest.
49 */
50 public static interface HashSupplier {
51 byte[] generate(String algorithm);
52 }
53
54 private final String algorithm;
55 private final Map<String, byte[]> nameToHash;
56
57 /**
58 * Creates a {@code ModuleHashes}.
59 *
60 * @param algorithm the algorithm used to create the hashes
61 * @param nameToHash the map of module name to hash value
62 */
63 public ModuleHashes(String algorithm, Map<String, byte[]> nameToHash) {
64 this.algorithm = algorithm;
65 this.nameToHash = Collections.unmodifiableMap(nameToHash);
66 }
67
68 /**
69 * Returns the algorithm used to hash the modules ("SHA-256" for example).
70 */
71 public String algorithm() {
72 return algorithm;
73 }
125 }
126 }
127
128 /**
129 * Computes the hash for every entry in the given map, returning a
130 * {@code ModuleHashes} to encapsulate the result. The map key is
131 * the entry name, typically the module name. The map value is the file
132 * path to the entry (module artifact).
133 *
134 * @return ModuleHashes that encapsulates the hashes
135 */
136 public static ModuleHashes generate(Map<String, Path> map, String algorithm) {
137 Map<String, byte[]> nameToHash = new HashMap<>();
138 for (Map.Entry<String, Path> entry: map.entrySet()) {
139 String name = entry.getKey();
140 Path path = entry.getValue();
141 nameToHash.put(name, computeHash(path, algorithm));
142 }
143 return new ModuleHashes(algorithm, nameToHash);
144 }
145
146 /**
147 * This is used by jdk.internal.module.SystemModules class
148 * generated at link time.
149 */
150 public static class Builder {
151 final String algorithm;
152 Map<String, byte[]> nameToHash;
153
154 Builder(String algorithm) {
155 this.algorithm = Objects.requireNonNull(algorithm);
156 }
157
158 /**
159 * Sets the module hash for the given module name
160 */
161 public Builder hashForModule(String mn, byte[] hash) {
162 if (nameToHash == null)
163 nameToHash = new HashMap<>();
164
165 nameToHash.put(mn, hash);
166 return this;
167 }
168
169 /**
170 * Builds a {@code ModuleHashes}.
171 */
172 public ModuleHashes build() {
173 if (nameToHash != null) {
174 return new ModuleHashes(algorithm, nameToHash);
175 } else {
176 return null;
177 }
178 }
179 }
180 }
|