36511
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 2015, 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 java.lang.module;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.UncheckedIOException;
|
|
30 |
import java.net.URI;
|
|
31 |
import java.util.Objects;
|
|
32 |
import java.util.Optional;
|
|
33 |
import java.util.function.Supplier;
|
|
34 |
|
|
35 |
import jdk.internal.module.Hasher.HashSupplier;
|
|
36 |
|
|
37 |
|
|
38 |
/**
|
|
39 |
* A reference to a module's content.
|
|
40 |
*
|
|
41 |
* <p> A module reference contains the module's descriptor and its location, if
|
|
42 |
* known. It also has the ability to create a {@link ModuleReader} in order to
|
|
43 |
* access the module's content, which may be inside the Java run-time system
|
|
44 |
* itself or in an artifact such as a modular JAR file.
|
|
45 |
*
|
|
46 |
* @see ModuleFinder
|
|
47 |
* @see ModuleReader
|
|
48 |
* @since 9
|
|
49 |
*/
|
|
50 |
|
|
51 |
public final class ModuleReference {
|
|
52 |
|
|
53 |
private final ModuleDescriptor descriptor;
|
|
54 |
private final URI location;
|
|
55 |
private final Supplier<ModuleReader> readerSupplier;
|
|
56 |
|
|
57 |
// the function that computes the hash of this module reference
|
|
58 |
private final HashSupplier hasher;
|
|
59 |
|
|
60 |
// cached hash string to avoid needing to compute it many times
|
|
61 |
private String cachedHash;
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Constructs a new instance of this class.
|
|
65 |
*/
|
|
66 |
ModuleReference(ModuleDescriptor descriptor,
|
|
67 |
URI location,
|
|
68 |
Supplier<ModuleReader> readerSupplier,
|
|
69 |
HashSupplier hasher)
|
|
70 |
{
|
|
71 |
this.descriptor = Objects.requireNonNull(descriptor);
|
|
72 |
this.location = location;
|
|
73 |
this.readerSupplier = Objects.requireNonNull(readerSupplier);
|
|
74 |
this.hasher = hasher;
|
|
75 |
}
|
|
76 |
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Constructs a new instance of this class.
|
|
80 |
*
|
|
81 |
* <p> The {@code readSupplier} parameter is the supplier of the {@link
|
|
82 |
* ModuleReader} that may be used to read the module content. Its {@link
|
|
83 |
* Supplier#get() get()} method throws {@link UncheckedIOException} if an
|
|
84 |
* I/O error occurs opening the module content. The {@code get()} method
|
|
85 |
* throws {@link SecurityException} if opening the module is denied by the
|
|
86 |
* security manager.
|
|
87 |
*
|
|
88 |
* @param descriptor
|
|
89 |
* The module descriptor
|
|
90 |
* @param location
|
|
91 |
* The module location or {@code null} if not known
|
|
92 |
* @param readerSupplier
|
|
93 |
* The {@code Supplier} of the {@code ModuleReader}
|
|
94 |
*/
|
|
95 |
public ModuleReference(ModuleDescriptor descriptor,
|
|
96 |
URI location,
|
|
97 |
Supplier<ModuleReader> readerSupplier)
|
|
98 |
{
|
|
99 |
this(descriptor, location, readerSupplier, null);
|
|
100 |
}
|
|
101 |
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Returns the module descriptor.
|
|
105 |
*
|
|
106 |
* @return The module descriptor
|
|
107 |
*/
|
|
108 |
public ModuleDescriptor descriptor() {
|
|
109 |
return descriptor;
|
|
110 |
}
|
|
111 |
|
|
112 |
|
|
113 |
/**
|
|
114 |
* Returns the location of this module's content, if known.
|
|
115 |
*
|
|
116 |
* <p> This URI, when present, is used as the {@linkplain
|
|
117 |
* java.security.CodeSource#getLocation location} value of a {@link
|
|
118 |
* java.security.CodeSource CodeSource} so that a module's classes can be
|
|
119 |
* granted specific permissions when loaded by a {@link
|
|
120 |
* java.security.SecureClassLoader SecureClassLoader}.
|
|
121 |
*
|
|
122 |
* @return The location or an empty {@code Optional} if not known
|
|
123 |
*/
|
|
124 |
public Optional<URI> location() {
|
|
125 |
return Optional.ofNullable(location);
|
|
126 |
}
|
|
127 |
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Opens the module content for reading.
|
|
131 |
*
|
|
132 |
* <p> This method opens the module content by invoking the {@link
|
|
133 |
* Supplier#get() get()} method of the {@code readSupplier} specified at
|
|
134 |
* construction time. </p>
|
|
135 |
*
|
|
136 |
* @return A {@code ModuleReader} to read the module
|
|
137 |
*
|
|
138 |
* @throws IOException
|
|
139 |
* If an I/O error occurs
|
|
140 |
* @throws SecurityException
|
|
141 |
* If denied by the security manager
|
|
142 |
*/
|
|
143 |
public ModuleReader open() throws IOException {
|
|
144 |
try {
|
|
145 |
return readerSupplier.get();
|
|
146 |
} catch (UncheckedIOException e) {
|
|
147 |
throw e.getCause();
|
|
148 |
}
|
|
149 |
|
|
150 |
}
|
|
151 |
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Computes the hash of this module, returning it as a hex string.
|
|
155 |
* Returns {@code null} if the hash cannot be computed.
|
|
156 |
*
|
|
157 |
* @throws java.io.UncheckedIOException if an I/O error occurs
|
|
158 |
*/
|
|
159 |
String computeHash(String algorithm) {
|
|
160 |
String result = cachedHash;
|
|
161 |
if (result != null)
|
|
162 |
return result;
|
|
163 |
if (hasher == null)
|
|
164 |
return null;
|
|
165 |
cachedHash = result = hasher.generate(algorithm);
|
|
166 |
return result;
|
|
167 |
}
|
|
168 |
|
|
169 |
private int hash;
|
|
170 |
|
|
171 |
/**
|
|
172 |
* Computes a hash code for this module reference.
|
|
173 |
*
|
|
174 |
* <p> The hash code is based upon the components of the reference, and
|
|
175 |
* satisfies the general contract of the {@link Object#hashCode
|
|
176 |
* Object.hashCode} method. </p>
|
|
177 |
*
|
|
178 |
* @return The hash-code value for this module reference
|
|
179 |
*/
|
|
180 |
@Override
|
|
181 |
public int hashCode() {
|
|
182 |
int hc = hash;
|
|
183 |
if (hc == 0) {
|
|
184 |
hc = Objects.hash(descriptor, location, readerSupplier, hasher);
|
|
185 |
if (hc != 0) hash = hc;
|
|
186 |
}
|
|
187 |
return hc;
|
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
|
191 |
* Tests this module reference for equality with the given object.
|
|
192 |
*
|
|
193 |
* <p> If the given object is not a {@code ModuleReference} then this
|
|
194 |
* method returns {@code false}. Two module references are equal if their
|
|
195 |
* module descriptors are equal, their locations are equal or both unknown,
|
|
196 |
* and were created with equal supplier objects to access the module
|
|
197 |
* content. </p>
|
|
198 |
*
|
|
199 |
* <p> This method satisfies the general contract of the {@link
|
|
200 |
* java.lang.Object#equals(Object) Object.equals} method. </p>
|
|
201 |
*
|
|
202 |
* @param ob
|
|
203 |
* the object to which this object is to be compared
|
|
204 |
*
|
|
205 |
* @return {@code true} if, and only if, the given object is a module
|
|
206 |
* reference that is equal to this module reference
|
|
207 |
*/
|
|
208 |
@Override
|
|
209 |
public boolean equals(Object ob) {
|
|
210 |
if (!(ob instanceof ModuleReference))
|
|
211 |
return false;
|
|
212 |
ModuleReference that = (ModuleReference)ob;
|
|
213 |
|
|
214 |
return Objects.equals(this.descriptor, that.descriptor)
|
|
215 |
&& Objects.equals(this.location, that.location)
|
|
216 |
&& Objects.equals(this.readerSupplier, that.readerSupplier)
|
|
217 |
&& Objects.equals(this.hasher, that.hasher);
|
|
218 |
}
|
|
219 |
|
|
220 |
/**
|
|
221 |
* Returns a string describing this module reference.
|
|
222 |
*
|
|
223 |
* @return A string describing this module reference
|
|
224 |
*/
|
|
225 |
@Override
|
|
226 |
public String toString() {
|
|
227 |
return ("[module " + descriptor().name()
|
|
228 |
+ ", location=" + location + "]");
|
|
229 |
}
|
|
230 |
|
|
231 |
}
|