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 package jdk.internal.loader;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.lang.module.ModuleReference;
30 import java.lang.reflect.Layer;
31 import java.lang.reflect.Module;
32 import java.net.MalformedURLException;
33 import java.net.URL;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.security.AccessController;
38 import java.security.PrivilegedAction;
39 import java.util.Arrays;
40 import java.util.Enumeration;
41 import java.util.Optional;
42 import java.util.concurrent.ConcurrentHashMap;
43 import java.util.jar.JarInputStream;
44 import java.util.jar.Manifest;
45 import java.util.stream.Stream;
46
47 import jdk.internal.misc.JavaLangAccess;
48 import jdk.internal.misc.SharedSecrets;
49 import jdk.internal.module.ServicesCatalog;
50
51 /**
52 * Find resources and packages in modules defined to the boot class loader or
167 return pkg;
168 }
169
170 /**
171 * Returns a stream of the packages defined to the boot loader.
172 */
173 public static Stream<Package> packages() {
174 return Arrays.stream(getSystemPackageNames())
175 .map(name -> getDefinedPackage(name.replace('/', '.')));
176 }
177
178 /**
179 * Helper class to define {@code Package} objects for packages in modules
180 * defined to the boot loader.
181 */
182 static class PackageHelper {
183 private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
184
185 /**
186 * Define the {@code Package} with the given name. The specified
187 * location is a jrt URL to a named module in the run-time image, a
188 * file path to a module in an exploded run-time image, or the file
189 * path to an enty on the boot class path (java agent Boot-Class-Path
190 * or -Xbootclasspath/a.
191 *
192 * <p> If the given location is a JAR file containing a manifest,
193 * the defined Package contains the versioning information from
194 * the manifest, if present.
195 *
196 * @param name package name
197 * @param location location where the package is (jrt URL or file path)
198 */
199 static Package definePackage(String name, String location) {
200 Module module = findModule(location);
201 if (module != null) {
202 // named module from runtime image or exploded module
203 if (name.isEmpty())
204 throw new InternalError("empty package in " + location);
205 return JLA.definePackage(ClassLoaders.bootLoader(), name, module);
206 }
207
208 // package in unnamed module (-Xbootclasspath/a)
209 URL url = toFileURL(location);
210 Manifest man = url != null ? getManifest(location) : null;
211
212 return ClassLoaders.bootLoader().defineOrCheckPackage(name, man, url);
213 }
214
215 /**
216 * Finds the module at the given location defined to the boot loader.
217 * The module is either in runtime image or exploded image.
218 * Otherwise this method returns null.
219 */
220 private static Module findModule(String location) {
221 String mn = null;
222 if (location.startsWith("jrt:/")) {
223 // named module in runtime image ("jrt:/".length() == 5)
224 mn = location.substring(5, location.length());
225 } else {
226 // named module in exploded image
227 Path path = Paths.get(location);
228 Path modulesDir = Paths.get(JAVA_HOME, "modules");
229 if (path.startsWith(modulesDir)) {
230 mn = path.getFileName().toString();
231 }
232 }
233
234 if (mn != null) {
235 // named module from runtime image or exploded module
236 Optional<Module> om = Layer.boot().findModule(mn);
237 if (!om.isPresent())
238 throw new InternalError(mn + " not in boot layer");
239 return om.get();
240 }
241
242 return null;
243 }
244
245 /**
246 * Returns URL if the given location is a regular file path.
247 */
|
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 package jdk.internal.loader;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.lang.module.ModuleReference;
30 import java.lang.reflect.Layer;
31 import java.lang.reflect.Module;
32 import java.net.MalformedURLException;
33 import java.net.URI;
34 import java.net.URL;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.security.AccessController;
39 import java.security.PrivilegedAction;
40 import java.util.Arrays;
41 import java.util.Enumeration;
42 import java.util.Optional;
43 import java.util.concurrent.ConcurrentHashMap;
44 import java.util.jar.JarInputStream;
45 import java.util.jar.Manifest;
46 import java.util.stream.Stream;
47
48 import jdk.internal.misc.JavaLangAccess;
49 import jdk.internal.misc.SharedSecrets;
50 import jdk.internal.module.ServicesCatalog;
51
52 /**
53 * Find resources and packages in modules defined to the boot class loader or
168 return pkg;
169 }
170
171 /**
172 * Returns a stream of the packages defined to the boot loader.
173 */
174 public static Stream<Package> packages() {
175 return Arrays.stream(getSystemPackageNames())
176 .map(name -> getDefinedPackage(name.replace('/', '.')));
177 }
178
179 /**
180 * Helper class to define {@code Package} objects for packages in modules
181 * defined to the boot loader.
182 */
183 static class PackageHelper {
184 private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
185
186 /**
187 * Define the {@code Package} with the given name. The specified
188 * location is a jrt URL to a named module in the run-time image,
189 * a file URL to a module in an exploded run-time image, or a file
190 * path to an entry on the boot class path (java agent Boot-Class-Path
191 * or -Xbootclasspath/a.
192 *
193 * <p> If the given location is a JAR file containing a manifest,
194 * the defined Package contains the versioning information from
195 * the manifest, if present.
196 *
197 * @param name package name
198 * @param location location where the package is (jrt URL or file URL
199 * for a named module in the run-time or exploded image;
200 * a file path for a package from -Xbootclasspath/a)
201 */
202 static Package definePackage(String name, String location) {
203 Module module = findModule(location);
204 if (module != null) {
205 // named module from runtime image or exploded module
206 if (name.isEmpty())
207 throw new InternalError("empty package in " + location);
208 return JLA.definePackage(ClassLoaders.bootLoader(), name, module);
209 }
210
211 // package in unnamed module (-Xbootclasspath/a)
212 URL url = toFileURL(location);
213 Manifest man = url != null ? getManifest(location) : null;
214
215 return ClassLoaders.bootLoader().defineOrCheckPackage(name, man, url);
216 }
217
218 /**
219 * Finds the module at the given location defined to the boot loader.
220 * The module is either in runtime image or exploded image.
221 * Otherwise this method returns null.
222 */
223 private static Module findModule(String location) {
224 String mn = null;
225 if (location.startsWith("jrt:/")) {
226 // named module in runtime image ("jrt:/".length() == 5)
227 mn = location.substring(5, location.length());
228 } else if (location.startsWith("file:")) {
229 // named module in exploded image
230 Path path = Paths.get(URI.create(location));
231 Path modulesDir = Paths.get(JAVA_HOME, "modules");
232 if (path.startsWith(modulesDir)) {
233 mn = path.getFileName().toString();
234 }
235 }
236
237 if (mn != null) {
238 // named module from runtime image or exploded module
239 Optional<Module> om = Layer.boot().findModule(mn);
240 if (!om.isPresent())
241 throw new InternalError(mn + " not in boot layer");
242 return om.get();
243 }
244
245 return null;
246 }
247
248 /**
249 * Returns URL if the given location is a regular file path.
250 */
|