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