46 import java.security.PrivilegedAction;
47 import java.security.PrivilegedActionException;
48 import java.security.PrivilegedExceptionAction;
49 import java.security.SecureClassLoader;
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.Enumeration;
54 import java.util.HashMap;
55 import java.util.Iterator;
56 import java.util.List;
57 import java.util.Map;
58 import java.util.Objects;
59 import java.util.Optional;
60 import java.util.concurrent.ConcurrentHashMap;
61 import java.util.stream.Stream;
62
63 import jdk.internal.misc.SharedSecrets;
64 import jdk.internal.module.Resources;
65
66
67 /**
68 * A class loader that loads classes and resources from a collection of
69 * modules, or from a single module where the class loader is a member
70 * of a pool of class loaders.
71 *
72 * <p> The delegation model used by this ClassLoader differs to the regular
73 * delegation model. When requested to load a class then this ClassLoader first
74 * maps the class name to its package name. If there a module defined to the
75 * Loader containing the package then the class loader attempts to load from
76 * that module. If the package is instead defined to a module in a "remote"
77 * ClassLoader then this class loader delegates directly to that class loader.
78 * The map of package name to remote class loader is created based on the
79 * modules read by modules defined to this class loader. If the package is not
80 * local or remote then this class loader will delegate to the parent class
81 * loader. This allows automatic modules (for example) to link to types in the
82 * unnamed module of the parent class loader.
83 *
84 * @see ModuleLayer#defineModulesWithOneLoader
85 * @see ModuleLayer#defineModulesWithManyLoaders
86 */
87
88 public final class Loader extends SecureClassLoader {
89
90 static {
91 ClassLoader.registerAsParallelCapable();
92 }
93
94 // the loader pool is in a pool, can be null
95 private final LoaderPool pool;
96
97 // parent ClassLoader, can be null
98 private final ClassLoader parent;
99
100 // maps a module name to a module reference
101 private final Map<String, ModuleReference> nameToModule;
102
103 // maps package name to a module loaded by this class loader
104 private final Map<String, LoadedModule> localPackageToModule;
105
106 // maps package name to a remote class loader, populated post initialization
107 private final Map<String, ClassLoader> remotePackageToLoader
108 = new ConcurrentHashMap<>();
109
110 // maps a module reference to a module reader, populated lazily
111 private final Map<ModuleReference, ModuleReader> moduleToReader
112 = new ConcurrentHashMap<>();
113
114 // ACC used when loading classes and resources */
115 private final AccessControlContext acc;
116
117 /**
118 * A module defined/loaded to a {@code Loader}.
119 */
120 private static class LoadedModule {
121 private final ModuleReference mref;
122 private final URL url; // may be null
123 private final CodeSource cs;
124
125 LoadedModule(ModuleReference mref) {
126 URL url = null;
127 if (mref.location().isPresent()) {
128 try {
129 url = mref.location().get().toURL();
130 } catch (MalformedURLException | IllegalArgumentException e) { }
131 }
132 this.mref = mref;
133 this.url = url;
134 this.cs = new CodeSource(url, (CodeSigner[]) null);
|
46 import java.security.PrivilegedAction;
47 import java.security.PrivilegedActionException;
48 import java.security.PrivilegedExceptionAction;
49 import java.security.SecureClassLoader;
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.Enumeration;
54 import java.util.HashMap;
55 import java.util.Iterator;
56 import java.util.List;
57 import java.util.Map;
58 import java.util.Objects;
59 import java.util.Optional;
60 import java.util.concurrent.ConcurrentHashMap;
61 import java.util.stream.Stream;
62
63 import jdk.internal.misc.SharedSecrets;
64 import jdk.internal.module.Resources;
65
66 /**
67 * A class loader that loads classes and resources from a collection of
68 * modules, or from a single module where the class loader is a member
69 * of a pool of class loaders.
70 *
71 * <p> The delegation model used by this ClassLoader differs to the regular
72 * delegation model. When requested to load a class then this ClassLoader first
73 * maps the class name to its package name. If there a module defined to the
74 * Loader containing the package then the class loader attempts to load from
75 * that module. If the package is instead defined to a module in a "remote"
76 * ClassLoader then this class loader delegates directly to that class loader.
77 * The map of package name to remote class loader is created based on the
78 * modules read by modules defined to this class loader. If the package is not
79 * local or remote then this class loader will delegate to the parent class
80 * loader. This allows automatic modules (for example) to link to types in the
81 * unnamed module of the parent class loader.
82 *
83 * @see ModuleLayer#defineModulesWithOneLoader
84 * @see ModuleLayer#defineModulesWithManyLoaders
85 */
86 public final class Loader extends SecureClassLoader {
87
88 static {
89 ClassLoader.registerAsParallelCapable();
90 }
91
92 /** the loader pool is in a pool, can be null */
93 private final LoaderPool pool;
94
95 /** parent ClassLoader, can be null */
96 private final ClassLoader parent;
97
98 /** maps a module name to a module reference */
99 private final Map<String, ModuleReference> nameToModule;
100
101 /** maps package name to a module loaded by this class loader */
102 private final Map<String, LoadedModule> localPackageToModule;
103
104 /**
105 * maps package name to a remote class loader, populated post
106 * initialization
107 */
108 private final Map<String, ClassLoader> remotePackageToLoader
109 = new ConcurrentHashMap<>();
110
111 /** maps a module reference to a module reader, populated lazily */
112 private final Map<ModuleReference, ModuleReader> moduleToReader
113 = new ConcurrentHashMap<>();
114
115 /** ACC used when loading classes and resources */
116 private final AccessControlContext acc;
117
118 /**
119 * A module defined/loaded to a {@code Loader}.
120 */
121 private static class LoadedModule {
122 private final ModuleReference mref;
123 private final URL url; // may be null
124 private final CodeSource cs;
125
126 LoadedModule(ModuleReference mref) {
127 URL url = null;
128 if (mref.location().isPresent()) {
129 try {
130 url = mref.location().get().toURL();
131 } catch (MalformedURLException | IllegalArgumentException e) { }
132 }
133 this.mref = mref;
134 this.url = url;
135 this.cs = new CodeSource(url, (CodeSigner[]) null);
|