/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.loader;
import java.io.IOException;
import java.io.InputStream;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleReference;
import java.lang.module.ModuleReader;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.security.AccessController;
import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.SecureClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.stream.Stream;
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.VM;
import jdk.internal.module.ModulePatcher.PatchedModuleReader;
import jdk.internal.module.Resources;
import jdk.internal.vm.annotation.Stable;
import sun.security.util.LazyCodeSourcePermissionCollection;
/**
* The platform or application class loader. Resources loaded from modules
* defined to the boot class loader are also loaded via an instance of this
* ClassLoader type.
*
*
This ClassLoader supports loading of classes and resources from modules.
* Modules are defined to the ClassLoader by invoking the {@link #loadModule}
* method. Defining a module to this ClassLoader has the effect of making the
* types in the module visible.
*
* This ClassLoader also supports loading of classes and resources from a
* class path of URLs that are specified to the ClassLoader at construction
* time. The class path may expand at runtime (the Class-Path attribute in JAR
* files or via instrumentation agents).
*
* The delegation model used by this ClassLoader differs to the regular
* delegation model. When requested to load a class then this ClassLoader first
* maps the class name to its package name. If there is a module defined to a
* BuiltinClassLoader containing this package then the class loader delegates
* directly to that class loader. If there isn't a module containing the
* package then it delegates the search to the parent class loader and if not
* found in the parent then it searches the class path. The main difference
* between this and the usual delegation model is that it allows the platform
* class loader to delegate to the application class loader, important with
* upgraded modules defined to the platform class loader.
*/
public class BuiltinClassLoader
extends SecureClassLoader
{
static {
if (!ClassLoader.registerAsParallelCapable())
throw new InternalError("Unable to register as parallel capable");
}
// parent ClassLoader
private final BuiltinClassLoader parent;
// the URL class path, or null if there is no class path
private @Stable URLClassPath ucp;
/**
* A module defined/loaded by a built-in class loader.
*
* A LoadedModule encapsulates a ModuleReference along with its CodeSource
* URL to avoid needing to create this URL when defining classes.
*/
private static class LoadedModule {
private final BuiltinClassLoader loader;
private final ModuleReference mref;
private final URI uri; // may be null
private @Stable URL codeSourceURL; // may be null
LoadedModule(BuiltinClassLoader loader, ModuleReference mref) {
URL url = null;
this.uri = mref.location().orElse(null);
// for non-jrt schemes we need to resolve the codeSourceURL
// eagerly during bootstrap since the handler might be
// overridden
if (uri != null && !"jrt".equals(uri.getScheme())) {
url = createURL(uri);
}
this.loader = loader;
this.mref = mref;
this.codeSourceURL = url;
}
BuiltinClassLoader loader() { return loader; }
ModuleReference mref() { return mref; }
String name() { return mref.descriptor().name(); }
URL codeSourceURL() {
URL url = codeSourceURL;
if (url == null && uri != null) {
codeSourceURL = url = createURL(uri);
}
return url;
}
private URL createURL(URI uri) {
URL url = null;
try {
url = uri.toURL();
} catch (MalformedURLException | IllegalArgumentException e) {
}
return url;
}
}
// maps package name to loaded module for modules in the boot layer
private static final Map packageToModule;
static {
ArchivedClassLoaders archivedClassLoaders = ArchivedClassLoaders.get();
if (archivedClassLoaders != null) {
@SuppressWarnings("unchecked")
Map map
= (Map) archivedClassLoaders.packageToModule();
packageToModule = map;
} else {
packageToModule = new ConcurrentHashMap<>(1024);
}
}
/**
* Invoked by ArchivedClassLoaders to archive the package-to-module map.
*/
static Map packageToModule() {
return packageToModule;
}
// maps a module name to a module reference
private final Map nameToModule;
// maps a module reference to a module reader
private final Map moduleToReader;
// cache of resource name -> list of URLs.
// used only for resources that are not in module packages
private volatile SoftReference