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