1 /* 2 * Copyright (c) 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 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 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 = System.getProperty("java.home"); 62 63 static { 64 UNNAMED_MODULE 65 = SharedSecrets.getJavaLangReflectModuleAccess().defineUnnamedModule(null); 66 setBootLoaderUnnamedModule0(UNNAMED_MODULE); 67 } 68 69 // ServiceCatalog for the boot class loader 70 private static final ServicesCatalog SERVICES_CATALOG = new ServicesCatalog(); 71 72 // ClassLoaderValue map for boot class loader 73 private static final ConcurrentHashMap<?, ?> CLASS_LOADER_VALUE_MAP = 74 new ConcurrentHashMap<>(); 75 76 /** 77 * Returns the unnamed module for the boot loader. 78 */ 79 public static Module getUnnamedModule() { 80 return UNNAMED_MODULE; 81 } 82 83 /** 84 * Returns the ServiceCatalog for modules defined to the boot class loader. 85 */ 86 public static ServicesCatalog getServicesCatalog() { 87 return SERVICES_CATALOG; 88 } 89 90 /** 91 * Returns the ClassLoaderValue map for the boot class loader. 92 */ 93 public static ConcurrentHashMap<?, ?> getClassLoaderValueMap() { 94 return CLASS_LOADER_VALUE_MAP; 95 } 96 97 /** 98 * Register a module with this class loader so that its classes (and 99 * resources) become visible via this class loader. 100 */ 101 public static void loadModule(ModuleReference mref) { 102 ClassLoaders.bootLoader().loadModule(mref); 103 } 104 105 /** 106 * Loads the Class object with the given name defined to the boot loader. 107 */ 108 public static Class<?> loadClassOrNull(String name) { 109 return ClassLoaders.bootLoader().loadClassOrNull(name); 110 } 111 112 /** 113 * Returns a URL to a resource in a named module defined to the boot loader. 114 */ 115 public static URL findResource(String mn, String name) throws IOException { 116 return ClassLoaders.bootLoader().findResource(mn, name); 117 } 118 119 /** 120 * Returns an input stream to a resource in a named module defined to the 121 * boot loader. 122 */ 123 public static InputStream findResourceAsStream(String mn, String name) 124 throws IOException 125 { 126 return ClassLoaders.bootLoader().findResourceAsStream(mn, name); 127 } 128 129 /** 130 * Returns the URL to the given resource if the resource can be located 131 * on the boot class path. This method does not locate a resource in any 132 * of the named modules defined to the boot loader. 133 */ 134 public static URL findResource(String name) { 135 return ClassLoaders.bootLoader().findResource(name); 136 } 137 138 /** 139 * Returns an Iterator to iterate over the resources of the given name 140 * on the boot class path. This method does not locate resources in any 141 * of the named modules defined to the boot loader. 142 */ 143 public static Enumeration<URL> findResources(String name) throws IOException { 144 return ClassLoaders.bootLoader().findResources(name); 145 } 146 147 /** 148 * Define a package for the given class to the boot loader, if not already 149 * defined. 150 */ 151 public static Package definePackage(Class<?> c) { 152 return getDefinedPackage(c.getPackageName()); 153 } 154 155 /** 156 * Returns the Package of the given name defined to the boot loader or null 157 * if the package has not been defined. 158 */ 159 public static Package getDefinedPackage(String pn) { 160 Package pkg = ClassLoaders.bootLoader().getDefinedPackage(pn); 161 if (pkg == null) { 162 String location = getSystemPackageLocation(pn.replace('.', '/')); 163 if (location != null) { 164 pkg = PackageHelper.definePackage(pn.intern(), location); 165 } 166 } 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 */ 248 private static URL toFileURL(String location) { 249 return AccessController.doPrivileged(new PrivilegedAction<>() { 250 public URL run() { 251 Path path = Paths.get(location); 252 if (Files.isRegularFile(path)) { 253 try { 254 return path.toUri().toURL(); 255 } catch (MalformedURLException e) {} 256 } 257 return null; 258 } 259 }); 260 } 261 262 /** 263 * Returns the Manifest if the given location is a JAR file 264 * containing a manifest. 265 */ 266 private static Manifest getManifest(String location) { 267 return AccessController.doPrivileged(new PrivilegedAction<>() { 268 public Manifest run() { 269 Path jar = Paths.get(location); 270 try (InputStream in = Files.newInputStream(jar); 271 JarInputStream jis = new JarInputStream(in, false)) { 272 return jis.getManifest(); 273 } catch (IOException e) { 274 return null; 275 } 276 } 277 }); 278 } 279 } 280 281 /** 282 * Returns an array of the binary name of the packages defined by 283 * the boot loader, in VM internal form (forward slashes instead of dot). 284 */ 285 private static native String[] getSystemPackageNames(); 286 287 /** 288 * Returns the location of the package of the given name, if 289 * defined by the boot loader; otherwise {@code null} is returned. 290 * 291 * The location may be a module from the runtime image or exploded image, 292 * or from the boot class append path (i.e. -Xbootclasspath/a or 293 * BOOT-CLASS-PATH attribute specified in java agent). 294 */ 295 private static native String getSystemPackageLocation(String name); 296 private static native void setBootLoaderUnnamedModule0(Module module); 297 }