1 /* 2 * Copyright (c) 2015, 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 package jdk.internal.loader; 26 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.lang.module.ModuleReference; 30 import java.net.MalformedURLException; 31 import java.net.URI; 32 import java.net.URL; 33 import java.nio.file.Files; 34 import java.nio.file.Path; 35 import java.security.AccessController; 36 import java.security.PrivilegedAction; 37 import java.util.Arrays; 38 import java.util.Enumeration; 39 import java.util.Optional; 40 import java.util.concurrent.ConcurrentHashMap; 41 import java.util.jar.JarInputStream; 42 import java.util.jar.Manifest; 43 import java.util.stream.Stream; 44 45 import jdk.internal.misc.JavaLangAccess; 46 import jdk.internal.misc.SharedSecrets; 47 import jdk.internal.module.ServicesCatalog; 48 49 /** 50 * Find resources and packages in modules defined to the boot class loader or 51 * resources and packages on the "boot class path" specified via -Xbootclasspath/a. 52 */ 53 54 public class BootLoader { 55 private BootLoader() { } 56 57 // The unnamed module for the boot loader 58 private static final Module UNNAMED_MODULE; 59 private static final String JAVA_HOME = System.getProperty("java.home"); 60 61 static { 62 UNNAMED_MODULE = SharedSecrets.getJavaLangAccess().defineUnnamedModule(null); 63 setBootLoaderUnnamedModule0(UNNAMED_MODULE); 64 } 65 66 // ServiceCatalog for the boot class loader 67 private static final ServicesCatalog SERVICES_CATALOG = ServicesCatalog.create(); 68 69 // ClassLoaderValue map for the boot class loader 70 private static final ConcurrentHashMap<?, ?> CLASS_LOADER_VALUE_MAP 71 = new ConcurrentHashMap<>(); 72 73 /** 74 * Returns the unnamed module for the boot loader. 75 */ 76 public static Module getUnnamedModule() { 77 return UNNAMED_MODULE; 78 } 79 80 /** 81 * Returns the ServiceCatalog for modules defined to the boot class loader. 82 */ 83 public static ServicesCatalog getServicesCatalog() { 84 return SERVICES_CATALOG; 85 } 86 87 /** 88 * Returns the ClassLoaderValue map for the boot class loader. 89 */ 90 public static ConcurrentHashMap<?, ?> getClassLoaderValueMap() { 91 return CLASS_LOADER_VALUE_MAP; 92 } 93 94 /** 95 * Returns {@code true} if there is a class path associated with the 96 * BootLoader. 97 */ 98 public static boolean hasClassPath() { 99 return ClassLoaders.bootLoader().hasClassPath(); 100 } 101 102 /** 103 * Register a module with this class loader so that its classes (and 104 * resources) become visible via this class loader. 105 */ 106 public static void loadModule(ModuleReference mref) { 107 ClassLoaders.bootLoader().loadModule(mref); 108 } 109 110 /** 111 * Loads the Class object with the given name defined to the boot loader. 112 */ 113 public static Class<?> loadClassOrNull(String name) { 114 return ClassLoaders.bootLoader().loadClassOrNull(name); 115 } 116 117 /** 118 * Loads the Class object with the given name in the given module 119 * defined to the boot loader. Returns {@code null} if not found. 120 */ 121 public static Class<?> loadClass(Module module, String name) { 122 Class<?> c = loadClassOrNull(name); 123 if (c != null && c.getModule() == module) { 124 return c; 125 } else { 126 return null; 127 } 128 } 129 130 /** 131 * Returns a URL to a resource in a module defined to the boot loader. 132 */ 133 public static URL findResource(String mn, String name) throws IOException { 134 return ClassLoaders.bootLoader().findResource(mn, name); 135 } 136 137 /** 138 * Returns an input stream to a resource in a module defined to the 139 * boot loader. 140 */ 141 public static InputStream findResourceAsStream(String mn, String name) 142 throws IOException 143 { 144 return ClassLoaders.bootLoader().findResourceAsStream(mn, name); 145 } 146 147 /** 148 * Returns the URL to the given resource in any of the modules 149 * defined to the boot loader and the boot class path. 150 */ 151 public static URL findResource(String name) { 152 return ClassLoaders.bootLoader().findResource(name); 153 } 154 155 /** 156 * Returns an Iterator to iterate over the resources of the given name 157 * in any of the modules defined to the boot loader. 158 */ 159 public static Enumeration<URL> findResources(String name) throws IOException { 160 return ClassLoaders.bootLoader().findResources(name); 161 } 162 163 /** 164 * Define a package for the given class to the boot loader, if not already 165 * defined. 166 */ 167 public static Package definePackage(Class<?> c) { 168 return getDefinedPackage(c.getPackageName()); 169 } 170 171 /** 172 * Returns the Package of the given name defined to the boot loader or null 173 * if the package has not been defined. 174 */ 175 public static Package getDefinedPackage(String pn) { 176 Package pkg = ClassLoaders.bootLoader().getDefinedPackage(pn); 177 if (pkg == null) { 178 String location = getSystemPackageLocation(pn.replace('.', '/')); 179 if (location != null) { 180 pkg = PackageHelper.definePackage(pn.intern(), location); 181 } 182 } 183 return pkg; 184 } 185 186 /** 187 * Returns a stream of the packages defined to the boot loader. 188 */ 189 public static Stream<Package> packages() { 190 return Arrays.stream(getSystemPackageNames()) 191 .map(name -> getDefinedPackage(name.replace('/', '.'))); 192 } 193 194 /** 195 * Helper class to define {@code Package} objects for packages in modules 196 * defined to the boot loader. 197 */ 198 static class PackageHelper { 199 private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess(); 200 201 /** 202 * Define the {@code Package} with the given name. The specified 203 * location is a jrt URL to a named module in the run-time image, 204 * a file URL to a module in an exploded run-time image, or a file 205 * path to an entry on the boot class path (java agent Boot-Class-Path 206 * or -Xbootclasspath/a. 207 * 208 * <p> If the given location is a JAR file containing a manifest, 209 * the defined Package contains the versioning information from 210 * the manifest, if present. 211 * 212 * @param name package name 213 * @param location location where the package is (jrt URL or file URL 214 * for a named module in the run-time or exploded image; 215 * a file path for a package from -Xbootclasspath/a) 216 */ 217 static Package definePackage(String name, String location) { 218 Module module = findModule(location); 219 if (module != null) { 220 // named module from runtime image or exploded module 221 if (name.isEmpty()) 222 throw new InternalError("empty package in " + location); 223 return JLA.definePackage(ClassLoaders.bootLoader(), name, module); 224 } 225 226 // package in unnamed module (-Xbootclasspath/a) 227 URL url = toFileURL(location); 228 Manifest man = url != null ? getManifest(location) : null; 229 230 return ClassLoaders.bootLoader().defineOrCheckPackage(name, man, url); 231 } 232 233 /** 234 * Finds the module at the given location defined to the boot loader. 235 * The module is either in runtime image or exploded image. 236 * Otherwise this method returns null. 237 */ 238 private static Module findModule(String location) { 239 String mn = null; 240 if (location.startsWith("jrt:/")) { 241 // named module in runtime image ("jrt:/".length() == 5) 242 mn = location.substring(5, location.length()); 243 } else if (location.startsWith("file:/")) { 244 // named module in exploded image 245 Path path = Path.of(URI.create(location)); 246 Path modulesDir = Path.of(JAVA_HOME, "modules"); 247 if (path.startsWith(modulesDir)) { 248 mn = path.getFileName().toString(); 249 } 250 } 251 252 if (mn != null) { 253 // named module from runtime image or exploded module 254 Optional<Module> om = ModuleLayer.boot().findModule(mn); 255 if (!om.isPresent()) 256 throw new InternalError(mn + " not in boot layer"); 257 return om.get(); 258 } 259 260 return null; 261 } 262 263 /** 264 * Returns URL if the given location is a regular file path. 265 */ 266 private static URL toFileURL(String location) { 267 return AccessController.doPrivileged(new PrivilegedAction<>() { 268 public URL run() { 269 Path path = Path.of(location); 270 if (Files.isRegularFile(path)) { 271 try { 272 return path.toUri().toURL(); 273 } catch (MalformedURLException e) {} 274 } 275 return null; 276 } 277 }); 278 } 279 280 /** 281 * Returns the Manifest if the given location is a JAR file 282 * containing a manifest. 283 */ 284 private static Manifest getManifest(String location) { 285 return AccessController.doPrivileged(new PrivilegedAction<>() { 286 public Manifest run() { 287 Path jar = Path.of(location); 288 try (InputStream in = Files.newInputStream(jar); 289 JarInputStream jis = new JarInputStream(in, false)) { 290 return jis.getManifest(); 291 } catch (IOException e) { 292 return null; 293 } 294 } 295 }); 296 } 297 } 298 299 /** 300 * Returns an array of the binary name of the packages defined by 301 * the boot loader, in VM internal form (forward slashes instead of dot). 302 */ 303 private static native String[] getSystemPackageNames(); 304 305 /** 306 * Returns the location of the package of the given name, if 307 * defined by the boot loader; otherwise {@code null} is returned. 308 * 309 * The location may be a module from the runtime image or exploded image, 310 * or from the boot class append path (i.e. -Xbootclasspath/a or 311 * BOOT-CLASS-PATH attribute specified in java agent). 312 */ 313 private static native String getSystemPackageLocation(String name); 314 private static native void setBootLoaderUnnamedModule0(Module module); 315 }