author | darcy |
Thu, 09 Jul 2009 12:31:30 -0700 | |
changeset 3224 | 3aa65803ae07 |
parent 2448 | 1e8128f3ff61 |
child 3111 | fefdeafb7ab9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2 |
* Copyright 1994-2009 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
package java.lang; |
|
26 |
||
27 |
import java.io.InputStream; |
|
28 |
import java.io.IOException; |
|
29 |
import java.io.File; |
|
30 |
import java.lang.reflect.Constructor; |
|
31 |
import java.lang.reflect.InvocationTargetException; |
|
32 |
import java.net.MalformedURLException; |
|
33 |
import java.net.URL; |
|
34 |
import java.security.AccessController; |
|
35 |
import java.security.AccessControlContext; |
|
36 |
import java.security.CodeSource; |
|
37 |
import java.security.Policy; |
|
38 |
import java.security.PrivilegedAction; |
|
39 |
import java.security.PrivilegedActionException; |
|
40 |
import java.security.PrivilegedExceptionAction; |
|
41 |
import java.security.ProtectionDomain; |
|
51 | 42 |
import java.security.cert.Certificate; |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
43 |
import java.util.Collections; |
2 | 44 |
import java.util.Enumeration; |
45 |
import java.util.HashMap; |
|
46 |
import java.util.HashSet; |
|
47 |
import java.util.Set; |
|
48 |
import java.util.Stack; |
|
49 |
import java.util.Map; |
|
50 |
import java.util.Vector; |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
51 |
import java.util.Hashtable; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
52 |
import java.util.WeakHashMap; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
53 |
import java.util.concurrent.ConcurrentHashMap; |
2 | 54 |
import sun.misc.ClassFileTransformer; |
55 |
import sun.misc.CompoundEnumeration; |
|
56 |
import sun.misc.Resource; |
|
57 |
import sun.misc.URLClassPath; |
|
58 |
import sun.misc.VM; |
|
59 |
import sun.reflect.Reflection; |
|
60 |
import sun.security.util.SecurityConstants; |
|
61 |
||
62 |
/** |
|
63 |
* A class loader is an object that is responsible for loading classes. The |
|
64 |
* class <tt>ClassLoader</tt> is an abstract class. Given the <a |
|
65 |
* href="#name">binary name</a> of a class, a class loader should attempt to |
|
66 |
* locate or generate data that constitutes a definition for the class. A |
|
67 |
* typical strategy is to transform the name into a file name and then read a |
|
68 |
* "class file" of that name from a file system. |
|
69 |
* |
|
70 |
* <p> Every {@link Class <tt>Class</tt>} object contains a {@link |
|
71 |
* Class#getClassLoader() reference} to the <tt>ClassLoader</tt> that defined |
|
72 |
* it. |
|
73 |
* |
|
74 |
* <p> <tt>Class</tt> objects for array classes are not created by class |
|
75 |
* loaders, but are created automatically as required by the Java runtime. |
|
76 |
* The class loader for an array class, as returned by {@link |
|
77 |
* Class#getClassLoader()} is the same as the class loader for its element |
|
78 |
* type; if the element type is a primitive type, then the array class has no |
|
79 |
* class loader. |
|
80 |
* |
|
81 |
* <p> Applications implement subclasses of <tt>ClassLoader</tt> in order to |
|
82 |
* extend the manner in which the Java virtual machine dynamically loads |
|
83 |
* classes. |
|
84 |
* |
|
85 |
* <p> Class loaders may typically be used by security managers to indicate |
|
86 |
* security domains. |
|
87 |
* |
|
88 |
* <p> The <tt>ClassLoader</tt> class uses a delegation model to search for |
|
89 |
* classes and resources. Each instance of <tt>ClassLoader</tt> has an |
|
90 |
* associated parent class loader. When requested to find a class or |
|
91 |
* resource, a <tt>ClassLoader</tt> instance will delegate the search for the |
|
92 |
* class or resource to its parent class loader before attempting to find the |
|
93 |
* class or resource itself. The virtual machine's built-in class loader, |
|
94 |
* called the "bootstrap class loader", does not itself have a parent but may |
|
95 |
* serve as the parent of a <tt>ClassLoader</tt> instance. |
|
96 |
* |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
97 |
* <p> Class loaders that support concurrent loading of classes are known as |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
98 |
* <em>parallel capable</em> class loaders and are required to register |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
99 |
* themselves at their class initialization time by invoking the |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
100 |
* {@link |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
101 |
* #registerAsParallelCapable <tt>ClassLoader.registerAsParallelCapable</tt>} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
102 |
* method. In environments in which the delegation model is not strictly |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
103 |
* hierarchical, class loaders need to be parallel capable, otherise class |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
104 |
* loading can lead to deadlocks because the loader lock is held for the |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
105 |
* duration of the class loading process (see {@link #loadClass |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
106 |
* <tt>loadClass</tt>} methods). |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
107 |
* |
2 | 108 |
* <p> Normally, the Java virtual machine loads classes from the local file |
109 |
* system in a platform-dependent manner. For example, on UNIX systems, the |
|
110 |
* virtual machine loads classes from the directory defined by the |
|
111 |
* <tt>CLASSPATH</tt> environment variable. |
|
112 |
* |
|
113 |
* <p> However, some classes may not originate from a file; they may originate |
|
114 |
* from other sources, such as the network, or they could be constructed by an |
|
115 |
* application. The method {@link #defineClass(String, byte[], int, int) |
|
116 |
* <tt>defineClass</tt>} converts an array of bytes into an instance of class |
|
117 |
* <tt>Class</tt>. Instances of this newly defined class can be created using |
|
118 |
* {@link Class#newInstance <tt>Class.newInstance</tt>}. |
|
119 |
* |
|
120 |
* <p> The methods and constructors of objects created by a class loader may |
|
121 |
* reference other classes. To determine the class(es) referred to, the Java |
|
122 |
* virtual machine invokes the {@link #loadClass <tt>loadClass</tt>} method of |
|
123 |
* the class loader that originally created the class. |
|
124 |
* |
|
125 |
* <p> For example, an application could create a network class loader to |
|
126 |
* download class files from a server. Sample code might look like: |
|
127 |
* |
|
128 |
* <blockquote><pre> |
|
129 |
* ClassLoader loader = new NetworkClassLoader(host, port); |
|
130 |
* Object main = loader.loadClass("Main", true).newInstance(); |
|
131 |
* . . . |
|
132 |
* </pre></blockquote> |
|
133 |
* |
|
134 |
* <p> The network class loader subclass must define the methods {@link |
|
135 |
* #findClass <tt>findClass</tt>} and <tt>loadClassData</tt> to load a class |
|
136 |
* from the network. Once it has downloaded the bytes that make up the class, |
|
137 |
* it should use the method {@link #defineClass <tt>defineClass</tt>} to |
|
138 |
* create a class instance. A sample implementation is: |
|
139 |
* |
|
140 |
* <blockquote><pre> |
|
141 |
* class NetworkClassLoader extends ClassLoader { |
|
142 |
* String host; |
|
143 |
* int port; |
|
144 |
* |
|
145 |
* public Class findClass(String name) { |
|
146 |
* byte[] b = loadClassData(name); |
|
147 |
* return defineClass(name, b, 0, b.length); |
|
148 |
* } |
|
149 |
* |
|
150 |
* private byte[] loadClassData(String name) { |
|
151 |
* // load the class data from the connection |
|
152 |
* . . . |
|
153 |
* } |
|
154 |
* } |
|
155 |
* </pre></blockquote> |
|
156 |
* |
|
157 |
* <h4> <a name="name">Binary names</a> </h4> |
|
158 |
* |
|
159 |
* <p> Any class name provided as a {@link String} parameter to methods in |
|
160 |
* <tt>ClassLoader</tt> must be a binary name as defined by the <a |
|
161 |
* href="http://java.sun.com/docs/books/jls/">Java Language Specification</a>. |
|
162 |
* |
|
163 |
* <p> Examples of valid class names include: |
|
164 |
* <blockquote><pre> |
|
165 |
* "java.lang.String" |
|
166 |
* "javax.swing.JSpinner$DefaultEditor" |
|
167 |
* "java.security.KeyStore$Builder$FileBuilder$1" |
|
168 |
* "java.net.URLClassLoader$3$1" |
|
169 |
* </pre></blockquote> |
|
170 |
* |
|
171 |
* @see #resolveClass(Class) |
|
172 |
* @since 1.0 |
|
173 |
*/ |
|
174 |
public abstract class ClassLoader { |
|
175 |
||
176 |
private static native void registerNatives(); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
177 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
178 |
// Set of classes which are registered as parallel capable class loaders |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
179 |
private static final Set<Class<? extends ClassLoader>> parallelLoaders |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
180 |
= Collections.newSetFromMap(Collections.synchronizedMap |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
181 |
(new WeakHashMap<Class<? extends ClassLoader>, Boolean>())); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
182 |
|
2 | 183 |
static { |
184 |
registerNatives(); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
185 |
parallelLoaders.add(ClassLoader.class); |
2 | 186 |
} |
187 |
||
188 |
// If initialization succeed this is set to true and security checks will |
|
189 |
// succeed. Otherwise the object is not initialized and the object is |
|
190 |
// useless. |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
191 |
private final boolean initialized; |
2 | 192 |
|
193 |
// The parent class loader for delegation |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
194 |
// Note: VM hardcoded the offset of this field, thus all new fields |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
195 |
// must be added *after* it. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
196 |
private final ClassLoader parent; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
197 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
198 |
// Maps class name to the corresponding lock object when the current |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
199 |
// class loader is parallel capable. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
200 |
// Note: VM also uses this field to decide if the current class loader |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
201 |
// is parallel capable and the appropriate lock object for class loading. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
202 |
private final ConcurrentHashMap<String, Object> parallelLockMap; |
2 | 203 |
|
204 |
// Hashtable that maps packages to certs |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
205 |
private final Map <String, Certificate[]> package2certs; |
2 | 206 |
|
207 |
// Shared among all packages with unsigned classes |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
208 |
private static final Certificate[] nocerts = new Certificate[0]; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
209 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
210 |
// The classes loaded by this class loader. The only purpose of this table |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
211 |
// is to keep the classes from being GC'ed until the loader is GC'ed. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
212 |
private final Vector<Class<?>> classes = new Vector<Class<?>>(); |
2 | 213 |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
214 |
// The "default" domain. Set as the default ProtectionDomain on newly |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
215 |
// created classes. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
216 |
private final ProtectionDomain defaultDomain = |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
217 |
new ProtectionDomain(new CodeSource(null, (Certificate[]) null), |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
218 |
null, this, null); |
2 | 219 |
|
220 |
// The initiating protection domains for all classes loaded by this loader |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
221 |
private final Set<ProtectionDomain> domains; |
2 | 222 |
|
223 |
// Invoked by the VM to record every loaded class with this loader. |
|
224 |
void addClass(Class c) { |
|
225 |
classes.addElement(c); |
|
226 |
} |
|
227 |
||
228 |
// The packages defined in this class loader. Each package name is mapped |
|
229 |
// to its corresponding Package object. |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
230 |
// @GuardedBy("itself") |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
231 |
private final HashMap<String, Package> packages = |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
232 |
new HashMap<String, Package>(); |
2 | 233 |
|
234 |
/** |
|
235 |
* Creates a new class loader using the specified parent class loader for |
|
236 |
* delegation. |
|
237 |
* |
|
238 |
* <p> If there is a security manager, its {@link |
|
239 |
* SecurityManager#checkCreateClassLoader() |
|
240 |
* <tt>checkCreateClassLoader</tt>} method is invoked. This may result in |
|
241 |
* a security exception. </p> |
|
242 |
* |
|
243 |
* @param parent |
|
244 |
* The parent class loader |
|
245 |
* |
|
246 |
* @throws SecurityException |
|
247 |
* If a security manager exists and its |
|
248 |
* <tt>checkCreateClassLoader</tt> method doesn't allow creation |
|
249 |
* of a new class loader. |
|
250 |
* |
|
251 |
* @since 1.2 |
|
252 |
*/ |
|
253 |
protected ClassLoader(ClassLoader parent) { |
|
254 |
SecurityManager security = System.getSecurityManager(); |
|
255 |
if (security != null) { |
|
256 |
security.checkCreateClassLoader(); |
|
257 |
} |
|
258 |
this.parent = parent; |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
259 |
if (parallelLoaders.contains(this.getClass())) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
260 |
parallelLockMap = new ConcurrentHashMap<String, Object>(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
261 |
package2certs = new ConcurrentHashMap<String, Certificate[]>(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
262 |
domains = |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
263 |
Collections.synchronizedSet(new HashSet<ProtectionDomain>()); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
264 |
assertionLock = new Object(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
265 |
} else { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
266 |
// no finer-grained lock; lock on the classloader instance |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
267 |
parallelLockMap = null; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
268 |
package2certs = new Hashtable<String, Certificate[]>(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
269 |
domains = new HashSet<ProtectionDomain>(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
270 |
assertionLock = this; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
271 |
} |
2 | 272 |
initialized = true; |
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Creates a new class loader using the <tt>ClassLoader</tt> returned by |
|
277 |
* the method {@link #getSystemClassLoader() |
|
278 |
* <tt>getSystemClassLoader()</tt>} as the parent class loader. |
|
279 |
* |
|
280 |
* <p> If there is a security manager, its {@link |
|
281 |
* SecurityManager#checkCreateClassLoader() |
|
282 |
* <tt>checkCreateClassLoader</tt>} method is invoked. This may result in |
|
283 |
* a security exception. </p> |
|
284 |
* |
|
285 |
* @throws SecurityException |
|
286 |
* If a security manager exists and its |
|
287 |
* <tt>checkCreateClassLoader</tt> method doesn't allow creation |
|
288 |
* of a new class loader. |
|
289 |
*/ |
|
290 |
protected ClassLoader() { |
|
291 |
SecurityManager security = System.getSecurityManager(); |
|
292 |
if (security != null) { |
|
293 |
security.checkCreateClassLoader(); |
|
294 |
} |
|
295 |
this.parent = getSystemClassLoader(); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
296 |
if (parallelLoaders.contains(this.getClass())) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
297 |
parallelLockMap = new ConcurrentHashMap<String, Object>(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
298 |
package2certs = new ConcurrentHashMap<String, Certificate[]>(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
299 |
domains = |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
300 |
Collections.synchronizedSet(new HashSet<ProtectionDomain>()); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
301 |
assertionLock = new Object(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
302 |
} else { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
303 |
// no finer-grained lock; lock on the classloader instance |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
304 |
parallelLockMap = null; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
305 |
package2certs = new Hashtable<String, Certificate[]>(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
306 |
domains = new HashSet<ProtectionDomain>(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
307 |
assertionLock = this; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
308 |
} |
2 | 309 |
initialized = true; |
310 |
} |
|
311 |
||
312 |
// -- Class -- |
|
313 |
||
314 |
/** |
|
315 |
* Loads the class with the specified <a href="#name">binary name</a>. |
|
316 |
* This method searches for classes in the same manner as the {@link |
|
317 |
* #loadClass(String, boolean)} method. It is invoked by the Java virtual |
|
318 |
* machine to resolve class references. Invoking this method is equivalent |
|
319 |
* to invoking {@link #loadClass(String, boolean) <tt>loadClass(name, |
|
320 |
* false)</tt>}. </p> |
|
321 |
* |
|
322 |
* @param name |
|
323 |
* The <a href="#name">binary name</a> of the class |
|
324 |
* |
|
325 |
* @return The resulting <tt>Class</tt> object |
|
326 |
* |
|
327 |
* @throws ClassNotFoundException |
|
328 |
* If the class was not found |
|
329 |
*/ |
|
330 |
public Class<?> loadClass(String name) throws ClassNotFoundException { |
|
331 |
return loadClass(name, false); |
|
332 |
} |
|
333 |
||
334 |
/** |
|
335 |
* Loads the class with the specified <a href="#name">binary name</a>. The |
|
336 |
* default implementation of this method searches for classes in the |
|
337 |
* following order: |
|
338 |
* |
|
339 |
* <p><ol> |
|
340 |
* |
|
341 |
* <li><p> Invoke {@link #findLoadedClass(String)} to check if the class |
|
342 |
* has already been loaded. </p></li> |
|
343 |
* |
|
344 |
* <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method |
|
345 |
* on the parent class loader. If the parent is <tt>null</tt> the class |
|
346 |
* loader built-in to the virtual machine is used, instead. </p></li> |
|
347 |
* |
|
348 |
* <li><p> Invoke the {@link #findClass(String)} method to find the |
|
349 |
* class. </p></li> |
|
350 |
* |
|
351 |
* </ol> |
|
352 |
* |
|
353 |
* <p> If the class was found using the above steps, and the |
|
354 |
* <tt>resolve</tt> flag is true, this method will then invoke the {@link |
|
355 |
* #resolveClass(Class)} method on the resulting <tt>Class</tt> object. |
|
356 |
* |
|
357 |
* <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link |
|
358 |
* #findClass(String)}, rather than this method. </p> |
|
359 |
* |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
360 |
* <p> Unless overridden, this method synchronizes on the result of |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
361 |
* {@link #getClassLoadingLock <tt>getClassLoadingLock</tt>} method |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
362 |
* during the entire class loading process. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
363 |
* |
2 | 364 |
* @param name |
365 |
* The <a href="#name">binary name</a> of the class |
|
366 |
* |
|
367 |
* @param resolve |
|
368 |
* If <tt>true</tt> then resolve the class |
|
369 |
* |
|
370 |
* @return The resulting <tt>Class</tt> object |
|
371 |
* |
|
372 |
* @throws ClassNotFoundException |
|
373 |
* If the class could not be found |
|
374 |
*/ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
375 |
protected Class<?> loadClass(String name, boolean resolve) |
2 | 376 |
throws ClassNotFoundException |
377 |
{ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
378 |
synchronized (getClassLoadingLock(name)) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
379 |
// First, check if the class has already been loaded |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
380 |
Class c = findLoadedClass(name); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
381 |
if (c == null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
382 |
try { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
383 |
if (parent != null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
384 |
c = parent.loadClass(name, false); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
385 |
} else { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
386 |
c = findBootstrapClass0(name); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
387 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
388 |
} catch (ClassNotFoundException e) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
389 |
// If still not found, then invoke findClass in order |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
390 |
// to find the class. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
391 |
c = findClass(name); |
2 | 392 |
} |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
393 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
394 |
if (resolve) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
395 |
resolveClass(c); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
396 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
397 |
return c; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
398 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
399 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
400 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
401 |
/** |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
402 |
* Returns the lock object for class loading operations. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
403 |
* For backward compatibility, the default implementation of this method |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
404 |
* behaves as follows. If this ClassLoader object is registered as |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
405 |
* parallel capable, the method returns a dedicated object associated |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
406 |
* with the specified class name. Otherwise, the method returns this |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
407 |
* ClassLoader object. </p> |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
408 |
* |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
409 |
* @param className |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
410 |
* The name of the to-be-loaded class |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
411 |
* |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
412 |
* @return the lock for class loading operations |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
413 |
* |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
414 |
* @throws NullPointerException |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
415 |
* If registered as parallel capable and <tt>className</tt> is null |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
416 |
* |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
417 |
* @see #loadClass(String, boolean) |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
418 |
* |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
419 |
* @since 1.7 |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
420 |
*/ |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
421 |
protected Object getClassLoadingLock(String className) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
422 |
Object lock = this; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
423 |
if (parallelLockMap != null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
424 |
Object newLock = new Object(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
425 |
lock = parallelLockMap.putIfAbsent(className, newLock); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
426 |
if (lock == null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
427 |
lock = newLock; |
2 | 428 |
} |
429 |
} |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
430 |
return lock; |
2 | 431 |
} |
432 |
||
433 |
// This method is invoked by the virtual machine to load a class. |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
434 |
private Class loadClassInternal(String name) |
2 | 435 |
throws ClassNotFoundException |
436 |
{ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
437 |
// For backward compatibility, explicitly lock on 'this' when |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
438 |
// the current class loader is not parallel capable. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
439 |
if (parallelLockMap == null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
440 |
synchronized (this) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
441 |
return loadClass(name); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
442 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
443 |
} else { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
444 |
return loadClass(name); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
445 |
} |
2 | 446 |
} |
447 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
448 |
// Invoked by the VM after loading class with this loader. |
2 | 449 |
private void checkPackageAccess(Class cls, ProtectionDomain pd) { |
450 |
final SecurityManager sm = System.getSecurityManager(); |
|
451 |
if (sm != null) { |
|
452 |
final String name = cls.getName(); |
|
453 |
final int i = name.lastIndexOf('.'); |
|
454 |
if (i != -1) { |
|
51 | 455 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
456 |
public Void run() { |
|
2 | 457 |
sm.checkPackageAccess(name.substring(0, i)); |
458 |
return null; |
|
459 |
} |
|
460 |
}, new AccessControlContext(new ProtectionDomain[] {pd})); |
|
461 |
} |
|
462 |
} |
|
463 |
domains.add(pd); |
|
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* Finds the class with the specified <a href="#name">binary name</a>. |
|
468 |
* This method should be overridden by class loader implementations that |
|
469 |
* follow the delegation model for loading classes, and will be invoked by |
|
470 |
* the {@link #loadClass <tt>loadClass</tt>} method after checking the |
|
471 |
* parent class loader for the requested class. The default implementation |
|
472 |
* throws a <tt>ClassNotFoundException</tt>. </p> |
|
473 |
* |
|
474 |
* @param name |
|
475 |
* The <a href="#name">binary name</a> of the class |
|
476 |
* |
|
477 |
* @return The resulting <tt>Class</tt> object |
|
478 |
* |
|
479 |
* @throws ClassNotFoundException |
|
480 |
* If the class could not be found |
|
481 |
* |
|
482 |
* @since 1.2 |
|
483 |
*/ |
|
484 |
protected Class<?> findClass(String name) throws ClassNotFoundException { |
|
485 |
throw new ClassNotFoundException(name); |
|
486 |
} |
|
487 |
||
488 |
/** |
|
489 |
* Converts an array of bytes into an instance of class <tt>Class</tt>. |
|
490 |
* Before the <tt>Class</tt> can be used it must be resolved. This method |
|
491 |
* is deprecated in favor of the version that takes a <a |
|
492 |
* href="#name">binary name</a> as its first argument, and is more secure. |
|
493 |
* |
|
494 |
* @param b |
|
495 |
* The bytes that make up the class data. The bytes in positions |
|
496 |
* <tt>off</tt> through <tt>off+len-1</tt> should have the format |
|
497 |
* of a valid class file as defined by the <a |
|
498 |
* href="http://java.sun.com/docs/books/vmspec/">Java Virtual |
|
499 |
* Machine Specification</a>. |
|
500 |
* |
|
501 |
* @param off |
|
502 |
* The start offset in <tt>b</tt> of the class data |
|
503 |
* |
|
504 |
* @param len |
|
505 |
* The length of the class data |
|
506 |
* |
|
507 |
* @return The <tt>Class</tt> object that was created from the specified |
|
508 |
* class data |
|
509 |
* |
|
510 |
* @throws ClassFormatError |
|
511 |
* If the data did not contain a valid class |
|
512 |
* |
|
513 |
* @throws IndexOutOfBoundsException |
|
514 |
* If either <tt>off</tt> or <tt>len</tt> is negative, or if |
|
515 |
* <tt>off+len</tt> is greater than <tt>b.length</tt>. |
|
516 |
* |
|
517 |
* @see #loadClass(String, boolean) |
|
518 |
* @see #resolveClass(Class) |
|
519 |
* |
|
520 |
* @deprecated Replaced by {@link #defineClass(String, byte[], int, int) |
|
521 |
* defineClass(String, byte[], int, int)} |
|
522 |
*/ |
|
523 |
@Deprecated |
|
524 |
protected final Class<?> defineClass(byte[] b, int off, int len) |
|
525 |
throws ClassFormatError |
|
526 |
{ |
|
527 |
return defineClass(null, b, off, len, null); |
|
528 |
} |
|
529 |
||
530 |
/** |
|
531 |
* Converts an array of bytes into an instance of class <tt>Class</tt>. |
|
532 |
* Before the <tt>Class</tt> can be used it must be resolved. |
|
533 |
* |
|
534 |
* <p> This method assigns a default {@link java.security.ProtectionDomain |
|
535 |
* <tt>ProtectionDomain</tt>} to the newly defined class. The |
|
536 |
* <tt>ProtectionDomain</tt> is effectively granted the same set of |
|
537 |
* permissions returned when {@link |
|
538 |
* java.security.Policy#getPermissions(java.security.CodeSource) |
|
539 |
* <tt>Policy.getPolicy().getPermissions(new CodeSource(null, null))</tt>} |
|
540 |
* is invoked. The default domain is created on the first invocation of |
|
541 |
* {@link #defineClass(String, byte[], int, int) <tt>defineClass</tt>}, |
|
542 |
* and re-used on subsequent invocations. |
|
543 |
* |
|
544 |
* <p> To assign a specific <tt>ProtectionDomain</tt> to the class, use |
|
545 |
* the {@link #defineClass(String, byte[], int, int, |
|
546 |
* java.security.ProtectionDomain) <tt>defineClass</tt>} method that takes a |
|
547 |
* <tt>ProtectionDomain</tt> as one of its arguments. </p> |
|
548 |
* |
|
549 |
* @param name |
|
550 |
* The expected <a href="#name">binary name</a> of the class, or |
|
551 |
* <tt>null</tt> if not known |
|
552 |
* |
|
553 |
* @param b |
|
554 |
* The bytes that make up the class data. The bytes in positions |
|
555 |
* <tt>off</tt> through <tt>off+len-1</tt> should have the format |
|
556 |
* of a valid class file as defined by the <a |
|
557 |
* href="http://java.sun.com/docs/books/vmspec/">Java Virtual |
|
558 |
* Machine Specification</a>. |
|
559 |
* |
|
560 |
* @param off |
|
561 |
* The start offset in <tt>b</tt> of the class data |
|
562 |
* |
|
563 |
* @param len |
|
564 |
* The length of the class data |
|
565 |
* |
|
566 |
* @return The <tt>Class</tt> object that was created from the specified |
|
567 |
* class data. |
|
568 |
* |
|
569 |
* @throws ClassFormatError |
|
570 |
* If the data did not contain a valid class |
|
571 |
* |
|
572 |
* @throws IndexOutOfBoundsException |
|
573 |
* If either <tt>off</tt> or <tt>len</tt> is negative, or if |
|
574 |
* <tt>off+len</tt> is greater than <tt>b.length</tt>. |
|
575 |
* |
|
576 |
* @throws SecurityException |
|
577 |
* If an attempt is made to add this class to a package that |
|
578 |
* contains classes that were signed by a different set of |
|
579 |
* certificates than this class (which is unsigned), or if |
|
580 |
* <tt>name</tt> begins with "<tt>java.</tt>". |
|
581 |
* |
|
582 |
* @see #loadClass(String, boolean) |
|
583 |
* @see #resolveClass(Class) |
|
584 |
* @see java.security.CodeSource |
|
585 |
* @see java.security.SecureClassLoader |
|
586 |
* |
|
587 |
* @since 1.1 |
|
588 |
*/ |
|
589 |
protected final Class<?> defineClass(String name, byte[] b, int off, int len) |
|
590 |
throws ClassFormatError |
|
591 |
{ |
|
592 |
return defineClass(name, b, off, len, null); |
|
593 |
} |
|
594 |
||
595 |
/* Determine protection domain, and check that: |
|
596 |
- not define java.* class, |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
597 |
- signer of this class matches signers for the rest of the classes in |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
598 |
package. |
2 | 599 |
*/ |
600 |
private ProtectionDomain preDefineClass(String name, |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
601 |
ProtectionDomain pd) |
2 | 602 |
{ |
603 |
if (!checkName(name)) |
|
604 |
throw new NoClassDefFoundError("IllegalName: " + name); |
|
605 |
||
606 |
if ((name != null) && name.startsWith("java.")) { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
607 |
throw new SecurityException |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
608 |
("Prohibited package name: " + |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
609 |
name.substring(0, name.lastIndexOf('.'))); |
2 | 610 |
} |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
611 |
if (pd == null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
612 |
pd = defaultDomain; |
2 | 613 |
} |
614 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
615 |
if (name != null) checkCerts(name, pd.getCodeSource()); |
2 | 616 |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
617 |
return pd; |
2 | 618 |
} |
619 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
620 |
private String defineClassSourceLocation(ProtectionDomain pd) |
2 | 621 |
{ |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
622 |
CodeSource cs = pd.getCodeSource(); |
2 | 623 |
String source = null; |
624 |
if (cs != null && cs.getLocation() != null) { |
|
625 |
source = cs.getLocation().toString(); |
|
626 |
} |
|
627 |
return source; |
|
628 |
} |
|
629 |
||
630 |
private Class defineTransformedClass(String name, byte[] b, int off, int len, |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
631 |
ProtectionDomain pd, |
2 | 632 |
ClassFormatError cfe, String source) |
633 |
throws ClassFormatError |
|
634 |
{ |
|
635 |
// Class format error - try to transform the bytecode and |
|
636 |
// define the class again |
|
637 |
// |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
638 |
ClassFileTransformer[] transformers = |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
639 |
ClassFileTransformer.getTransformers(); |
2 | 640 |
Class c = null; |
641 |
||
51 | 642 |
if (transformers != null) { |
643 |
for (ClassFileTransformer transformer : transformers) { |
|
644 |
try { |
|
645 |
// Transform byte code using transformer |
|
646 |
byte[] tb = transformer.transform(b, off, len); |
|
647 |
c = defineClass1(name, tb, 0, tb.length, |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
648 |
pd, source); |
51 | 649 |
break; |
650 |
} catch (ClassFormatError cfe2) { |
|
651 |
// If ClassFormatError occurs, try next transformer |
|
652 |
} |
|
2 | 653 |
} |
654 |
} |
|
655 |
||
656 |
// Rethrow original ClassFormatError if unable to transform |
|
657 |
// bytecode to well-formed |
|
658 |
// |
|
659 |
if (c == null) |
|
660 |
throw cfe; |
|
661 |
||
662 |
return c; |
|
663 |
} |
|
664 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
665 |
private void postDefineClass(Class c, ProtectionDomain pd) |
2 | 666 |
{ |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
667 |
if (pd.getCodeSource() != null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
668 |
Certificate certs[] = pd.getCodeSource().getCertificates(); |
2 | 669 |
if (certs != null) |
670 |
setSigners(c, certs); |
|
671 |
} |
|
672 |
} |
|
673 |
||
674 |
/** |
|
675 |
* Converts an array of bytes into an instance of class <tt>Class</tt>, |
|
676 |
* with an optional <tt>ProtectionDomain</tt>. If the domain is |
|
677 |
* <tt>null</tt>, then a default domain will be assigned to the class as |
|
678 |
* specified in the documentation for {@link #defineClass(String, byte[], |
|
679 |
* int, int)}. Before the class can be used it must be resolved. |
|
680 |
* |
|
681 |
* <p> The first class defined in a package determines the exact set of |
|
682 |
* certificates that all subsequent classes defined in that package must |
|
683 |
* contain. The set of certificates for a class is obtained from the |
|
684 |
* {@link java.security.CodeSource <tt>CodeSource</tt>} within the |
|
685 |
* <tt>ProtectionDomain</tt> of the class. Any classes added to that |
|
686 |
* package must contain the same set of certificates or a |
|
687 |
* <tt>SecurityException</tt> will be thrown. Note that if |
|
688 |
* <tt>name</tt> is <tt>null</tt>, this check is not performed. |
|
689 |
* You should always pass in the <a href="#name">binary name</a> of the |
|
690 |
* class you are defining as well as the bytes. This ensures that the |
|
691 |
* class you are defining is indeed the class you think it is. |
|
692 |
* |
|
693 |
* <p> The specified <tt>name</tt> cannot begin with "<tt>java.</tt>", since |
|
694 |
* all classes in the "<tt>java.*</tt> packages can only be defined by the |
|
695 |
* bootstrap class loader. If <tt>name</tt> is not <tt>null</tt>, it |
|
696 |
* must be equal to the <a href="#name">binary name</a> of the class |
|
697 |
* specified by the byte array "<tt>b</tt>", otherwise a {@link |
|
698 |
* <tt>NoClassDefFoundError</tt>} will be thrown. </p> |
|
699 |
* |
|
700 |
* @param name |
|
701 |
* The expected <a href="#name">binary name</a> of the class, or |
|
702 |
* <tt>null</tt> if not known |
|
703 |
* |
|
704 |
* @param b |
|
705 |
* The bytes that make up the class data. The bytes in positions |
|
706 |
* <tt>off</tt> through <tt>off+len-1</tt> should have the format |
|
707 |
* of a valid class file as defined by the <a |
|
708 |
* href="http://java.sun.com/docs/books/vmspec/">Java Virtual |
|
709 |
* Machine Specification</a>. |
|
710 |
* |
|
711 |
* @param off |
|
712 |
* The start offset in <tt>b</tt> of the class data |
|
713 |
* |
|
714 |
* @param len |
|
715 |
* The length of the class data |
|
716 |
* |
|
717 |
* @param protectionDomain |
|
718 |
* The ProtectionDomain of the class |
|
719 |
* |
|
720 |
* @return The <tt>Class</tt> object created from the data, |
|
721 |
* and optional <tt>ProtectionDomain</tt>. |
|
722 |
* |
|
723 |
* @throws ClassFormatError |
|
724 |
* If the data did not contain a valid class |
|
725 |
* |
|
726 |
* @throws NoClassDefFoundError |
|
727 |
* If <tt>name</tt> is not equal to the <a href="#name">binary |
|
728 |
* name</a> of the class specified by <tt>b</tt> |
|
729 |
* |
|
730 |
* @throws IndexOutOfBoundsException |
|
731 |
* If either <tt>off</tt> or <tt>len</tt> is negative, or if |
|
732 |
* <tt>off+len</tt> is greater than <tt>b.length</tt>. |
|
733 |
* |
|
734 |
* @throws SecurityException |
|
735 |
* If an attempt is made to add this class to a package that |
|
736 |
* contains classes that were signed by a different set of |
|
737 |
* certificates than this class, or if <tt>name</tt> begins with |
|
738 |
* "<tt>java.</tt>". |
|
739 |
*/ |
|
740 |
protected final Class<?> defineClass(String name, byte[] b, int off, int len, |
|
741 |
ProtectionDomain protectionDomain) |
|
742 |
throws ClassFormatError |
|
743 |
{ |
|
744 |
check(); |
|
745 |
protectionDomain = preDefineClass(name, protectionDomain); |
|
746 |
||
747 |
Class c = null; |
|
748 |
String source = defineClassSourceLocation(protectionDomain); |
|
749 |
||
750 |
try { |
|
751 |
c = defineClass1(name, b, off, len, protectionDomain, source); |
|
752 |
} catch (ClassFormatError cfe) { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
753 |
c = defineTransformedClass(name, b, off, len, protectionDomain, cfe, |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
754 |
source); |
2 | 755 |
} |
756 |
||
757 |
postDefineClass(c, protectionDomain); |
|
758 |
return c; |
|
759 |
} |
|
760 |
||
761 |
/** |
|
762 |
* Converts a {@link java.nio.ByteBuffer <tt>ByteBuffer</tt>} |
|
763 |
* into an instance of class <tt>Class</tt>, |
|
764 |
* with an optional <tt>ProtectionDomain</tt>. If the domain is |
|
765 |
* <tt>null</tt>, then a default domain will be assigned to the class as |
|
766 |
* specified in the documentation for {@link #defineClass(String, byte[], |
|
767 |
* int, int)}. Before the class can be used it must be resolved. |
|
768 |
* |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
769 |
* <p>The rules about the first class defined in a package determining the |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
770 |
* set of certificates for the package, and the restrictions on class names |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
771 |
* are identical to those specified in the documentation for {@link |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
772 |
* #defineClass(String, byte[], int, int, ProtectionDomain)}. |
2 | 773 |
* |
774 |
* <p> An invocation of this method of the form |
|
775 |
* <i>cl</i><tt>.defineClass(</tt><i>name</i><tt>,</tt> |
|
776 |
* <i>bBuffer</i><tt>,</tt> <i>pd</i><tt>)</tt> yields exactly the same |
|
777 |
* result as the statements |
|
778 |
* |
|
779 |
* <blockquote><tt> |
|
780 |
* ...<br> |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
781 |
* byte[] temp = new byte[</tt><i>bBuffer</i><tt>.{@link |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
782 |
* java.nio.ByteBuffer#remaining remaining}()];<br> |
2 | 783 |
* </tt><i>bBuffer</i><tt>.{@link java.nio.ByteBuffer#get(byte[]) |
784 |
* get}(temp);<br> |
|
785 |
* return {@link #defineClass(String, byte[], int, int, ProtectionDomain) |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
786 |
* </tt><i>cl</i><tt>.defineClass}(</tt><i>name</i><tt>, temp, 0, |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
787 |
* temp.length, </tt><i>pd</i><tt>);<br> |
2 | 788 |
* </tt></blockquote> |
789 |
* |
|
790 |
* @param name |
|
791 |
* The expected <a href="#name">binary name</a. of the class, or |
|
792 |
* <tt>null</tt> if not known |
|
793 |
* |
|
794 |
* @param b |
|
795 |
* The bytes that make up the class data. The bytes from positions |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
796 |
* <tt>b.position()</tt> through <tt>b.position() + b.limit() -1 |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
797 |
* </tt> should have the format of a valid class file as defined by |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
798 |
* the <a href="http://java.sun.com/docs/books/vmspec/">Java Virtual |
2 | 799 |
* Machine Specification</a>. |
800 |
* |
|
801 |
* @param protectionDomain |
|
802 |
* The ProtectionDomain of the class, or <tt>null</tt>. |
|
803 |
* |
|
804 |
* @return The <tt>Class</tt> object created from the data, |
|
805 |
* and optional <tt>ProtectionDomain</tt>. |
|
806 |
* |
|
807 |
* @throws ClassFormatError |
|
808 |
* If the data did not contain a valid class. |
|
809 |
* |
|
810 |
* @throws NoClassDefFoundError |
|
811 |
* If <tt>name</tt> is not equal to the <a href="#name">binary |
|
812 |
* name</a> of the class specified by <tt>b</tt> |
|
813 |
* |
|
814 |
* @throws SecurityException |
|
815 |
* If an attempt is made to add this class to a package that |
|
816 |
* contains classes that were signed by a different set of |
|
817 |
* certificates than this class, or if <tt>name</tt> begins with |
|
818 |
* "<tt>java.</tt>". |
|
819 |
* |
|
820 |
* @see #defineClass(String, byte[], int, int, ProtectionDomain) |
|
821 |
* |
|
822 |
* @since 1.5 |
|
823 |
*/ |
|
824 |
protected final Class<?> defineClass(String name, java.nio.ByteBuffer b, |
|
825 |
ProtectionDomain protectionDomain) |
|
826 |
throws ClassFormatError |
|
827 |
{ |
|
828 |
check(); |
|
829 |
||
830 |
int len = b.remaining(); |
|
831 |
||
832 |
// Use byte[] if not a direct ByteBufer: |
|
833 |
if (!b.isDirect()) { |
|
834 |
if (b.hasArray()) { |
|
835 |
return defineClass(name, b.array(), |
|
836 |
b.position() + b.arrayOffset(), len, |
|
837 |
protectionDomain); |
|
838 |
} else { |
|
839 |
// no array, or read-only array |
|
840 |
byte[] tb = new byte[len]; |
|
841 |
b.get(tb); // get bytes out of byte buffer. |
|
842 |
return defineClass(name, tb, 0, len, protectionDomain); |
|
843 |
} |
|
844 |
} |
|
845 |
||
846 |
protectionDomain = preDefineClass(name, protectionDomain); |
|
847 |
||
848 |
Class c = null; |
|
849 |
String source = defineClassSourceLocation(protectionDomain); |
|
850 |
||
851 |
try { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
852 |
c = defineClass2(name, b, b.position(), len, protectionDomain, |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
853 |
source); |
2 | 854 |
} catch (ClassFormatError cfe) { |
855 |
byte[] tb = new byte[len]; |
|
856 |
b.get(tb); // get bytes out of byte buffer. |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
857 |
c = defineTransformedClass(name, tb, 0, len, protectionDomain, cfe, |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
858 |
source); |
2 | 859 |
} |
860 |
||
861 |
postDefineClass(c, protectionDomain); |
|
862 |
return c; |
|
863 |
} |
|
864 |
||
865 |
private native Class defineClass0(String name, byte[] b, int off, int len, |
|
866 |
ProtectionDomain pd); |
|
867 |
||
868 |
private native Class defineClass1(String name, byte[] b, int off, int len, |
|
869 |
ProtectionDomain pd, String source); |
|
870 |
||
871 |
private native Class defineClass2(String name, java.nio.ByteBuffer b, |
|
872 |
int off, int len, ProtectionDomain pd, |
|
873 |
String source); |
|
874 |
||
875 |
// true if the name is null or has the potential to be a valid binary name |
|
876 |
private boolean checkName(String name) { |
|
877 |
if ((name == null) || (name.length() == 0)) |
|
878 |
return true; |
|
879 |
if ((name.indexOf('/') != -1) |
|
880 |
|| (!VM.allowArraySyntax() && (name.charAt(0) == '['))) |
|
881 |
return false; |
|
882 |
return true; |
|
883 |
} |
|
884 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
885 |
private void checkCerts(String name, CodeSource cs) { |
2 | 886 |
int i = name.lastIndexOf('.'); |
887 |
String pname = (i == -1) ? "" : name.substring(0, i); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
888 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
889 |
Certificate[] certs = null; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
890 |
if (cs != null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
891 |
certs = cs.getCertificates(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
892 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
893 |
Certificate[] pcerts = null; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
894 |
if (parallelLockMap == null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
895 |
synchronized (this) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
896 |
pcerts = package2certs.get(pname); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
897 |
if (pcerts == null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
898 |
package2certs.put(pname, (certs == null? nocerts:certs)); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
899 |
} |
2 | 900 |
} |
901 |
} else { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
902 |
pcerts = ((ConcurrentHashMap<String, Certificate[]>)package2certs). |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
903 |
putIfAbsent(pname, (certs == null? nocerts:certs)); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
904 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
905 |
if (pcerts != null && !compareCerts(pcerts, certs)) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
906 |
throw new SecurityException("class \""+ name + |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
907 |
"\"'s signer information does not match signer information of other classes in the same package"); |
2 | 908 |
} |
909 |
} |
|
910 |
||
911 |
/** |
|
912 |
* check to make sure the certs for the new class (certs) are the same as |
|
913 |
* the certs for the first class inserted in the package (pcerts) |
|
914 |
*/ |
|
51 | 915 |
private boolean compareCerts(Certificate[] pcerts, |
916 |
Certificate[] certs) |
|
2 | 917 |
{ |
918 |
// certs can be null, indicating no certs. |
|
919 |
if ((certs == null) || (certs.length == 0)) { |
|
920 |
return pcerts.length == 0; |
|
921 |
} |
|
922 |
||
923 |
// the length must be the same at this point |
|
924 |
if (certs.length != pcerts.length) |
|
925 |
return false; |
|
926 |
||
927 |
// go through and make sure all the certs in one array |
|
928 |
// are in the other and vice-versa. |
|
929 |
boolean match; |
|
930 |
for (int i = 0; i < certs.length; i++) { |
|
931 |
match = false; |
|
932 |
for (int j = 0; j < pcerts.length; j++) { |
|
933 |
if (certs[i].equals(pcerts[j])) { |
|
934 |
match = true; |
|
935 |
break; |
|
936 |
} |
|
937 |
} |
|
938 |
if (!match) return false; |
|
939 |
} |
|
940 |
||
941 |
// now do the same for pcerts |
|
942 |
for (int i = 0; i < pcerts.length; i++) { |
|
943 |
match = false; |
|
944 |
for (int j = 0; j < certs.length; j++) { |
|
945 |
if (pcerts[i].equals(certs[j])) { |
|
946 |
match = true; |
|
947 |
break; |
|
948 |
} |
|
949 |
} |
|
950 |
if (!match) return false; |
|
951 |
} |
|
952 |
||
953 |
return true; |
|
954 |
} |
|
955 |
||
956 |
/** |
|
957 |
* Links the specified class. This (misleadingly named) method may be |
|
958 |
* used by a class loader to link a class. If the class <tt>c</tt> has |
|
959 |
* already been linked, then this method simply returns. Otherwise, the |
|
960 |
* class is linked as described in the "Execution" chapter of the <a |
|
961 |
* href="http://java.sun.com/docs/books/jls/">Java Language |
|
962 |
* Specification</a>. |
|
963 |
* </p> |
|
964 |
* |
|
965 |
* @param c |
|
966 |
* The class to link |
|
967 |
* |
|
968 |
* @throws NullPointerException |
|
969 |
* If <tt>c</tt> is <tt>null</tt>. |
|
970 |
* |
|
971 |
* @see #defineClass(String, byte[], int, int) |
|
972 |
*/ |
|
973 |
protected final void resolveClass(Class<?> c) { |
|
974 |
check(); |
|
975 |
resolveClass0(c); |
|
976 |
} |
|
977 |
||
978 |
private native void resolveClass0(Class c); |
|
979 |
||
980 |
/** |
|
981 |
* Finds a class with the specified <a href="#name">binary name</a>, |
|
982 |
* loading it if necessary. |
|
983 |
* |
|
984 |
* <p> This method loads the class through the system class loader (see |
|
985 |
* {@link #getSystemClassLoader()}). The <tt>Class</tt> object returned |
|
986 |
* might have more than one <tt>ClassLoader</tt> associated with it. |
|
987 |
* Subclasses of <tt>ClassLoader</tt> need not usually invoke this method, |
|
988 |
* because most class loaders need to override just {@link |
|
989 |
* #findClass(String)}. </p> |
|
990 |
* |
|
991 |
* @param name |
|
992 |
* The <a href="#name">binary name</a> of the class |
|
993 |
* |
|
994 |
* @return The <tt>Class</tt> object for the specified <tt>name</tt> |
|
995 |
* |
|
996 |
* @throws ClassNotFoundException |
|
997 |
* If the class could not be found |
|
998 |
* |
|
999 |
* @see #ClassLoader(ClassLoader) |
|
1000 |
* @see #getParent() |
|
1001 |
*/ |
|
1002 |
protected final Class<?> findSystemClass(String name) |
|
1003 |
throws ClassNotFoundException |
|
1004 |
{ |
|
1005 |
check(); |
|
1006 |
ClassLoader system = getSystemClassLoader(); |
|
1007 |
if (system == null) { |
|
1008 |
if (!checkName(name)) |
|
1009 |
throw new ClassNotFoundException(name); |
|
1010 |
return findBootstrapClass(name); |
|
1011 |
} |
|
1012 |
return system.loadClass(name); |
|
1013 |
} |
|
1014 |
||
1015 |
private Class findBootstrapClass0(String name) |
|
1016 |
throws ClassNotFoundException |
|
1017 |
{ |
|
1018 |
check(); |
|
1019 |
if (!checkName(name)) |
|
1020 |
throw new ClassNotFoundException(name); |
|
1021 |
return findBootstrapClass(name); |
|
1022 |
} |
|
1023 |
||
1024 |
private native Class findBootstrapClass(String name) |
|
1025 |
throws ClassNotFoundException; |
|
1026 |
||
1027 |
// Check to make sure the class loader has been initialized. |
|
1028 |
private void check() { |
|
1029 |
if (!initialized) { |
|
1030 |
throw new SecurityException("ClassLoader object not initialized"); |
|
1031 |
} |
|
1032 |
} |
|
1033 |
||
1034 |
/** |
|
1035 |
* Returns the class with the given <a href="#name">binary name</a> if this |
|
1036 |
* loader has been recorded by the Java virtual machine as an initiating |
|
1037 |
* loader of a class with that <a href="#name">binary name</a>. Otherwise |
|
1038 |
* <tt>null</tt> is returned. </p> |
|
1039 |
* |
|
1040 |
* @param name |
|
1041 |
* The <a href="#name">binary name</a> of the class |
|
1042 |
* |
|
1043 |
* @return The <tt>Class</tt> object, or <tt>null</tt> if the class has |
|
1044 |
* not been loaded |
|
1045 |
* |
|
1046 |
* @since 1.1 |
|
1047 |
*/ |
|
1048 |
protected final Class<?> findLoadedClass(String name) { |
|
1049 |
check(); |
|
1050 |
if (!checkName(name)) |
|
1051 |
return null; |
|
1052 |
return findLoadedClass0(name); |
|
1053 |
} |
|
1054 |
||
1055 |
private native final Class findLoadedClass0(String name); |
|
1056 |
||
1057 |
/** |
|
1058 |
* Sets the signers of a class. This should be invoked after defining a |
|
1059 |
* class. </p> |
|
1060 |
* |
|
1061 |
* @param c |
|
1062 |
* The <tt>Class</tt> object |
|
1063 |
* |
|
1064 |
* @param signers |
|
1065 |
* The signers for the class |
|
1066 |
* |
|
1067 |
* @since 1.1 |
|
1068 |
*/ |
|
1069 |
protected final void setSigners(Class<?> c, Object[] signers) { |
|
1070 |
check(); |
|
1071 |
c.setSigners(signers); |
|
1072 |
} |
|
1073 |
||
1074 |
||
1075 |
// -- Resource -- |
|
1076 |
||
1077 |
/** |
|
1078 |
* Finds the resource with the given name. A resource is some data |
|
1079 |
* (images, audio, text, etc) that can be accessed by class code in a way |
|
1080 |
* that is independent of the location of the code. |
|
1081 |
* |
|
1082 |
* <p> The name of a resource is a '<tt>/</tt>'-separated path name that |
|
1083 |
* identifies the resource. |
|
1084 |
* |
|
1085 |
* <p> This method will first search the parent class loader for the |
|
1086 |
* resource; if the parent is <tt>null</tt> the path of the class loader |
|
1087 |
* built-in to the virtual machine is searched. That failing, this method |
|
1088 |
* will invoke {@link #findResource(String)} to find the resource. </p> |
|
1089 |
* |
|
1090 |
* @param name |
|
1091 |
* The resource name |
|
1092 |
* |
|
1093 |
* @return A <tt>URL</tt> object for reading the resource, or |
|
1094 |
* <tt>null</tt> if the resource could not be found or the invoker |
|
1095 |
* doesn't have adequate privileges to get the resource. |
|
1096 |
* |
|
1097 |
* @since 1.1 |
|
1098 |
*/ |
|
1099 |
public URL getResource(String name) { |
|
1100 |
URL url; |
|
1101 |
if (parent != null) { |
|
1102 |
url = parent.getResource(name); |
|
1103 |
} else { |
|
1104 |
url = getBootstrapResource(name); |
|
1105 |
} |
|
1106 |
if (url == null) { |
|
1107 |
url = findResource(name); |
|
1108 |
} |
|
1109 |
return url; |
|
1110 |
} |
|
1111 |
||
1112 |
/** |
|
1113 |
* Finds all the resources with the given name. A resource is some data |
|
1114 |
* (images, audio, text, etc) that can be accessed by class code in a way |
|
1115 |
* that is independent of the location of the code. |
|
1116 |
* |
|
1117 |
* <p>The name of a resource is a <tt>/</tt>-separated path name that |
|
1118 |
* identifies the resource. |
|
1119 |
* |
|
1120 |
* <p> The search order is described in the documentation for {@link |
|
1121 |
* #getResource(String)}. </p> |
|
1122 |
* |
|
1123 |
* @param name |
|
1124 |
* The resource name |
|
1125 |
* |
|
1126 |
* @return An enumeration of {@link java.net.URL <tt>URL</tt>} objects for |
|
1127 |
* the resource. If no resources could be found, the enumeration |
|
1128 |
* will be empty. Resources that the class loader doesn't have |
|
1129 |
* access to will not be in the enumeration. |
|
1130 |
* |
|
1131 |
* @throws IOException |
|
1132 |
* If I/O errors occur |
|
1133 |
* |
|
1134 |
* @see #findResources(String) |
|
1135 |
* |
|
1136 |
* @since 1.2 |
|
1137 |
*/ |
|
1138 |
public Enumeration<URL> getResources(String name) throws IOException { |
|
1139 |
Enumeration[] tmp = new Enumeration[2]; |
|
1140 |
if (parent != null) { |
|
1141 |
tmp[0] = parent.getResources(name); |
|
1142 |
} else { |
|
1143 |
tmp[0] = getBootstrapResources(name); |
|
1144 |
} |
|
1145 |
tmp[1] = findResources(name); |
|
1146 |
||
51 | 1147 |
return new CompoundEnumeration<URL>(tmp); |
2 | 1148 |
} |
1149 |
||
1150 |
/** |
|
1151 |
* Finds the resource with the given name. Class loader implementations |
|
1152 |
* should override this method to specify where to find resources. </p> |
|
1153 |
* |
|
1154 |
* @param name |
|
1155 |
* The resource name |
|
1156 |
* |
|
1157 |
* @return A <tt>URL</tt> object for reading the resource, or |
|
1158 |
* <tt>null</tt> if the resource could not be found |
|
1159 |
* |
|
1160 |
* @since 1.2 |
|
1161 |
*/ |
|
1162 |
protected URL findResource(String name) { |
|
1163 |
return null; |
|
1164 |
} |
|
1165 |
||
1166 |
/** |
|
1167 |
* Returns an enumeration of {@link java.net.URL <tt>URL</tt>} objects |
|
1168 |
* representing all the resources with the given name. Class loader |
|
1169 |
* implementations should override this method to specify where to load |
|
1170 |
* resources from. </p> |
|
1171 |
* |
|
1172 |
* @param name |
|
1173 |
* The resource name |
|
1174 |
* |
|
1175 |
* @return An enumeration of {@link java.net.URL <tt>URL</tt>} objects for |
|
1176 |
* the resources |
|
1177 |
* |
|
1178 |
* @throws IOException |
|
1179 |
* If I/O errors occur |
|
1180 |
* |
|
1181 |
* @since 1.2 |
|
1182 |
*/ |
|
1183 |
protected Enumeration<URL> findResources(String name) throws IOException { |
|
51 | 1184 |
return java.util.Collections.emptyEnumeration(); |
2 | 1185 |
} |
1186 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1187 |
// index 0: java.lang.ClassLoader.class |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1188 |
// index 1: the immediate caller of index 0. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1189 |
// index 2: the immediate caller of index 1. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1190 |
private static native Class<? extends ClassLoader> getCaller(int index); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1191 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1192 |
/** |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1193 |
* Registers the caller class loader as parallel capable. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1194 |
* In order for the registration to succeed, all super classes |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1195 |
* of the caller class loader must also be registered as |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1196 |
* parallel capable when this method is called. </p> |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1197 |
* Note that once a class loader is registered as |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1198 |
* parallel capable, there is no way to change it back. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1199 |
* In addition, registration should be done statically before |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1200 |
* any instance of the caller classloader being constructed. </p> |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1201 |
* |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1202 |
* @return true if the caller is successfully registered as |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1203 |
* parallel capable and false if otherwise. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1204 |
* |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1205 |
* @since 1.7 |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1206 |
*/ |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1207 |
protected static boolean registerAsParallelCapable() { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1208 |
Class<? extends ClassLoader> caller = getCaller(1); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1209 |
Class superCls = caller.getSuperclass(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1210 |
boolean result = false; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1211 |
// Explicit synchronization needed for composite action |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1212 |
synchronized (parallelLoaders) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1213 |
if (!parallelLoaders.contains(caller)) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1214 |
if (parallelLoaders.contains(superCls)) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1215 |
// register the immediate caller as parallel capable |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1216 |
// if and only if all of its super classes are. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1217 |
// Note: given current classloading sequence, if |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1218 |
// the immediate super class is parallel capable, |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1219 |
// all the super classes higher up must be too. |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1220 |
result = true; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1221 |
parallelLoaders.add(caller); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1222 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1223 |
} else result = true; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1224 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1225 |
return result; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1226 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1227 |
|
2 | 1228 |
/** |
1229 |
* Find a resource of the specified name from the search path used to load |
|
1230 |
* classes. This method locates the resource through the system class |
|
1231 |
* loader (see {@link #getSystemClassLoader()}). </p> |
|
1232 |
* |
|
1233 |
* @param name |
|
1234 |
* The resource name |
|
1235 |
* |
|
1236 |
* @return A {@link java.net.URL <tt>URL</tt>} object for reading the |
|
1237 |
* resource, or <tt>null</tt> if the resource could not be found |
|
1238 |
* |
|
1239 |
* @since 1.1 |
|
1240 |
*/ |
|
1241 |
public static URL getSystemResource(String name) { |
|
1242 |
ClassLoader system = getSystemClassLoader(); |
|
1243 |
if (system == null) { |
|
1244 |
return getBootstrapResource(name); |
|
1245 |
} |
|
1246 |
return system.getResource(name); |
|
1247 |
} |
|
1248 |
||
1249 |
/** |
|
1250 |
* Finds all resources of the specified name from the search path used to |
|
1251 |
* load classes. The resources thus found are returned as an |
|
1252 |
* {@link java.util.Enumeration <tt>Enumeration</tt>} of {@link |
|
1253 |
* java.net.URL <tt>URL</tt>} objects. |
|
1254 |
* |
|
1255 |
* <p> The search order is described in the documentation for {@link |
|
1256 |
* #getSystemResource(String)}. </p> |
|
1257 |
* |
|
1258 |
* @param name |
|
1259 |
* The resource name |
|
1260 |
* |
|
1261 |
* @return An enumeration of resource {@link java.net.URL <tt>URL</tt>} |
|
1262 |
* objects |
|
1263 |
* |
|
1264 |
* @throws IOException |
|
1265 |
* If I/O errors occur |
|
1266 |
||
1267 |
* @since 1.2 |
|
1268 |
*/ |
|
1269 |
public static Enumeration<URL> getSystemResources(String name) |
|
1270 |
throws IOException |
|
1271 |
{ |
|
1272 |
ClassLoader system = getSystemClassLoader(); |
|
1273 |
if (system == null) { |
|
1274 |
return getBootstrapResources(name); |
|
1275 |
} |
|
1276 |
return system.getResources(name); |
|
1277 |
} |
|
1278 |
||
1279 |
/** |
|
1280 |
* Find resources from the VM's built-in classloader. |
|
1281 |
*/ |
|
1282 |
private static URL getBootstrapResource(String name) { |
|
1283 |
URLClassPath ucp = getBootstrapClassPath(); |
|
1284 |
Resource res = ucp.getResource(name); |
|
1285 |
return res != null ? res.getURL() : null; |
|
1286 |
} |
|
1287 |
||
1288 |
/** |
|
1289 |
* Find resources from the VM's built-in classloader. |
|
1290 |
*/ |
|
51 | 1291 |
private static Enumeration<URL> getBootstrapResources(String name) |
2 | 1292 |
throws IOException |
1293 |
{ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1294 |
final Enumeration<Resource> e = |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1295 |
getBootstrapClassPath().getResources(name); |
51 | 1296 |
return new Enumeration<URL> () { |
1297 |
public URL nextElement() { |
|
1298 |
return e.nextElement().getURL(); |
|
2 | 1299 |
} |
1300 |
public boolean hasMoreElements() { |
|
1301 |
return e.hasMoreElements(); |
|
1302 |
} |
|
1303 |
}; |
|
1304 |
} |
|
1305 |
||
1306 |
// Returns the URLClassPath that is used for finding system resources. |
|
1307 |
static URLClassPath getBootstrapClassPath() { |
|
1308 |
if (bootstrapClassPath == null) { |
|
1309 |
bootstrapClassPath = sun.misc.Launcher.getBootstrapClassPath(); |
|
1310 |
} |
|
1311 |
return bootstrapClassPath; |
|
1312 |
} |
|
1313 |
||
1314 |
private static URLClassPath bootstrapClassPath; |
|
1315 |
||
1316 |
/** |
|
1317 |
* Returns an input stream for reading the specified resource. |
|
1318 |
* |
|
1319 |
* <p> The search order is described in the documentation for {@link |
|
1320 |
* #getResource(String)}. </p> |
|
1321 |
* |
|
1322 |
* @param name |
|
1323 |
* The resource name |
|
1324 |
* |
|
1325 |
* @return An input stream for reading the resource, or <tt>null</tt> |
|
1326 |
* if the resource could not be found |
|
1327 |
* |
|
1328 |
* @since 1.1 |
|
1329 |
*/ |
|
1330 |
public InputStream getResourceAsStream(String name) { |
|
1331 |
URL url = getResource(name); |
|
1332 |
try { |
|
1333 |
return url != null ? url.openStream() : null; |
|
1334 |
} catch (IOException e) { |
|
1335 |
return null; |
|
1336 |
} |
|
1337 |
} |
|
1338 |
||
1339 |
/** |
|
1340 |
* Open for reading, a resource of the specified name from the search path |
|
1341 |
* used to load classes. This method locates the resource through the |
|
1342 |
* system class loader (see {@link #getSystemClassLoader()}). </p> |
|
1343 |
* |
|
1344 |
* @param name |
|
1345 |
* The resource name |
|
1346 |
* |
|
1347 |
* @return An input stream for reading the resource, or <tt>null</tt> |
|
1348 |
* if the resource could not be found |
|
1349 |
* |
|
1350 |
* @since 1.1 |
|
1351 |
*/ |
|
1352 |
public static InputStream getSystemResourceAsStream(String name) { |
|
1353 |
URL url = getSystemResource(name); |
|
1354 |
try { |
|
1355 |
return url != null ? url.openStream() : null; |
|
1356 |
} catch (IOException e) { |
|
1357 |
return null; |
|
1358 |
} |
|
1359 |
} |
|
1360 |
||
1361 |
||
1362 |
// -- Hierarchy -- |
|
1363 |
||
1364 |
/** |
|
1365 |
* Returns the parent class loader for delegation. Some implementations may |
|
1366 |
* use <tt>null</tt> to represent the bootstrap class loader. This method |
|
1367 |
* will return <tt>null</tt> in such implementations if this class loader's |
|
1368 |
* parent is the bootstrap class loader. |
|
1369 |
* |
|
1370 |
* <p> If a security manager is present, and the invoker's class loader is |
|
1371 |
* not <tt>null</tt> and is not an ancestor of this class loader, then this |
|
1372 |
* method invokes the security manager's {@link |
|
1373 |
* SecurityManager#checkPermission(java.security.Permission) |
|
1374 |
* <tt>checkPermission</tt>} method with a {@link |
|
1375 |
* RuntimePermission#RuntimePermission(String) |
|
1376 |
* <tt>RuntimePermission("getClassLoader")</tt>} permission to verify |
|
1377 |
* access to the parent class loader is permitted. If not, a |
|
1378 |
* <tt>SecurityException</tt> will be thrown. </p> |
|
1379 |
* |
|
1380 |
* @return The parent <tt>ClassLoader</tt> |
|
1381 |
* |
|
1382 |
* @throws SecurityException |
|
1383 |
* If a security manager exists and its <tt>checkPermission</tt> |
|
1384 |
* method doesn't allow access to this class loader's parent class |
|
1385 |
* loader. |
|
1386 |
* |
|
1387 |
* @since 1.2 |
|
1388 |
*/ |
|
1389 |
public final ClassLoader getParent() { |
|
1390 |
if (parent == null) |
|
1391 |
return null; |
|
1392 |
SecurityManager sm = System.getSecurityManager(); |
|
1393 |
if (sm != null) { |
|
1394 |
ClassLoader ccl = getCallerClassLoader(); |
|
1395 |
if (ccl != null && !isAncestor(ccl)) { |
|
1396 |
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); |
|
1397 |
} |
|
1398 |
} |
|
1399 |
return parent; |
|
1400 |
} |
|
1401 |
||
1402 |
/** |
|
1403 |
* Returns the system class loader for delegation. This is the default |
|
1404 |
* delegation parent for new <tt>ClassLoader</tt> instances, and is |
|
1405 |
* typically the class loader used to start the application. |
|
1406 |
* |
|
1407 |
* <p> This method is first invoked early in the runtime's startup |
|
1408 |
* sequence, at which point it creates the system class loader and sets it |
|
1409 |
* as the context class loader of the invoking <tt>Thread</tt>. |
|
1410 |
* |
|
1411 |
* <p> The default system class loader is an implementation-dependent |
|
1412 |
* instance of this class. |
|
1413 |
* |
|
1414 |
* <p> If the system property "<tt>java.system.class.loader</tt>" is defined |
|
1415 |
* when this method is first invoked then the value of that property is |
|
1416 |
* taken to be the name of a class that will be returned as the system |
|
1417 |
* class loader. The class is loaded using the default system class loader |
|
1418 |
* and must define a public constructor that takes a single parameter of |
|
1419 |
* type <tt>ClassLoader</tt> which is used as the delegation parent. An |
|
1420 |
* instance is then created using this constructor with the default system |
|
1421 |
* class loader as the parameter. The resulting class loader is defined |
|
1422 |
* to be the system class loader. |
|
1423 |
* |
|
1424 |
* <p> If a security manager is present, and the invoker's class loader is |
|
1425 |
* not <tt>null</tt> and the invoker's class loader is not the same as or |
|
1426 |
* an ancestor of the system class loader, then this method invokes the |
|
1427 |
* security manager's {@link |
|
1428 |
* SecurityManager#checkPermission(java.security.Permission) |
|
1429 |
* <tt>checkPermission</tt>} method with a {@link |
|
1430 |
* RuntimePermission#RuntimePermission(String) |
|
1431 |
* <tt>RuntimePermission("getClassLoader")</tt>} permission to verify |
|
1432 |
* access to the system class loader. If not, a |
|
1433 |
* <tt>SecurityException</tt> will be thrown. </p> |
|
1434 |
* |
|
1435 |
* @return The system <tt>ClassLoader</tt> for delegation, or |
|
1436 |
* <tt>null</tt> if none |
|
1437 |
* |
|
1438 |
* @throws SecurityException |
|
1439 |
* If a security manager exists and its <tt>checkPermission</tt> |
|
1440 |
* method doesn't allow access to the system class loader. |
|
1441 |
* |
|
1442 |
* @throws IllegalStateException |
|
1443 |
* If invoked recursively during the construction of the class |
|
1444 |
* loader specified by the "<tt>java.system.class.loader</tt>" |
|
1445 |
* property. |
|
1446 |
* |
|
1447 |
* @throws Error |
|
1448 |
* If the system property "<tt>java.system.class.loader</tt>" |
|
1449 |
* is defined but the named class could not be loaded, the |
|
1450 |
* provider class does not define the required constructor, or an |
|
1451 |
* exception is thrown by that constructor when it is invoked. The |
|
1452 |
* underlying cause of the error can be retrieved via the |
|
1453 |
* {@link Throwable#getCause()} method. |
|
1454 |
* |
|
1455 |
* @revised 1.4 |
|
1456 |
*/ |
|
1457 |
public static ClassLoader getSystemClassLoader() { |
|
1458 |
initSystemClassLoader(); |
|
1459 |
if (scl == null) { |
|
1460 |
return null; |
|
1461 |
} |
|
1462 |
SecurityManager sm = System.getSecurityManager(); |
|
1463 |
if (sm != null) { |
|
1464 |
ClassLoader ccl = getCallerClassLoader(); |
|
1465 |
if (ccl != null && ccl != scl && !scl.isAncestor(ccl)) { |
|
1466 |
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); |
|
1467 |
} |
|
1468 |
} |
|
1469 |
return scl; |
|
1470 |
} |
|
1471 |
||
1472 |
private static synchronized void initSystemClassLoader() { |
|
1473 |
if (!sclSet) { |
|
1474 |
if (scl != null) |
|
1475 |
throw new IllegalStateException("recursive invocation"); |
|
1476 |
sun.misc.Launcher l = sun.misc.Launcher.getLauncher(); |
|
1477 |
if (l != null) { |
|
1478 |
Throwable oops = null; |
|
1479 |
scl = l.getClassLoader(); |
|
1480 |
try { |
|
51 | 1481 |
scl = AccessController.doPrivileged( |
1482 |
new SystemClassLoaderAction(scl)); |
|
2 | 1483 |
} catch (PrivilegedActionException pae) { |
1484 |
oops = pae.getCause(); |
|
1485 |
if (oops instanceof InvocationTargetException) { |
|
1486 |
oops = oops.getCause(); |
|
1487 |
} |
|
1488 |
} |
|
1489 |
if (oops != null) { |
|
1490 |
if (oops instanceof Error) { |
|
1491 |
throw (Error) oops; |
|
1492 |
} else { |
|
1493 |
// wrap the exception |
|
1494 |
throw new Error(oops); |
|
1495 |
} |
|
1496 |
} |
|
1497 |
} |
|
1498 |
sclSet = true; |
|
1499 |
} |
|
1500 |
} |
|
1501 |
||
1502 |
// Returns true if the specified class loader can be found in this class |
|
1503 |
// loader's delegation chain. |
|
1504 |
boolean isAncestor(ClassLoader cl) { |
|
1505 |
ClassLoader acl = this; |
|
1506 |
do { |
|
1507 |
acl = acl.parent; |
|
1508 |
if (cl == acl) { |
|
1509 |
return true; |
|
1510 |
} |
|
1511 |
} while (acl != null); |
|
1512 |
return false; |
|
1513 |
} |
|
1514 |
||
1515 |
// Returns the invoker's class loader, or null if none. |
|
1516 |
// NOTE: This must always be invoked when there is exactly one intervening |
|
1517 |
// frame from the core libraries on the stack between this method's |
|
1518 |
// invocation and the desired invoker. |
|
1519 |
static ClassLoader getCallerClassLoader() { |
|
1520 |
// NOTE use of more generic Reflection.getCallerClass() |
|
1521 |
Class caller = Reflection.getCallerClass(3); |
|
1522 |
// This can be null if the VM is requesting it |
|
1523 |
if (caller == null) { |
|
1524 |
return null; |
|
1525 |
} |
|
1526 |
// Circumvent security check since this is package-private |
|
1527 |
return caller.getClassLoader0(); |
|
1528 |
} |
|
1529 |
||
1530 |
// The class loader for the system |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1531 |
// @GuardedBy("ClassLoader.class") |
2 | 1532 |
private static ClassLoader scl; |
1533 |
||
1534 |
// Set to true once the system class loader has been set |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1535 |
// @GuardedBy("ClassLoader.class") |
2 | 1536 |
private static boolean sclSet; |
1537 |
||
1538 |
||
1539 |
// -- Package -- |
|
1540 |
||
1541 |
/** |
|
1542 |
* Defines a package by name in this <tt>ClassLoader</tt>. This allows |
|
1543 |
* class loaders to define the packages for their classes. Packages must |
|
1544 |
* be created before the class is defined, and package names must be |
|
1545 |
* unique within a class loader and cannot be redefined or changed once |
|
1546 |
* created. </p> |
|
1547 |
* |
|
1548 |
* @param name |
|
1549 |
* The package name |
|
1550 |
* |
|
1551 |
* @param specTitle |
|
1552 |
* The specification title |
|
1553 |
* |
|
1554 |
* @param specVersion |
|
1555 |
* The specification version |
|
1556 |
* |
|
1557 |
* @param specVendor |
|
1558 |
* The specification vendor |
|
1559 |
* |
|
1560 |
* @param implTitle |
|
1561 |
* The implementation title |
|
1562 |
* |
|
1563 |
* @param implVersion |
|
1564 |
* The implementation version |
|
1565 |
* |
|
1566 |
* @param implVendor |
|
1567 |
* The implementation vendor |
|
1568 |
* |
|
1569 |
* @param sealBase |
|
1570 |
* If not <tt>null</tt>, then this package is sealed with |
|
1571 |
* respect to the given code source {@link java.net.URL |
|
1572 |
* <tt>URL</tt>} object. Otherwise, the package is not sealed. |
|
1573 |
* |
|
1574 |
* @return The newly defined <tt>Package</tt> object |
|
1575 |
* |
|
1576 |
* @throws IllegalArgumentException |
|
1577 |
* If package name duplicates an existing package either in this |
|
1578 |
* class loader or one of its ancestors |
|
1579 |
* |
|
1580 |
* @since 1.2 |
|
1581 |
*/ |
|
1582 |
protected Package definePackage(String name, String specTitle, |
|
1583 |
String specVersion, String specVendor, |
|
1584 |
String implTitle, String implVersion, |
|
1585 |
String implVendor, URL sealBase) |
|
1586 |
throws IllegalArgumentException |
|
1587 |
{ |
|
1588 |
synchronized (packages) { |
|
1589 |
Package pkg = getPackage(name); |
|
1590 |
if (pkg != null) { |
|
1591 |
throw new IllegalArgumentException(name); |
|
1592 |
} |
|
1593 |
pkg = new Package(name, specTitle, specVersion, specVendor, |
|
1594 |
implTitle, implVersion, implVendor, |
|
1595 |
sealBase, this); |
|
1596 |
packages.put(name, pkg); |
|
1597 |
return pkg; |
|
1598 |
} |
|
1599 |
} |
|
1600 |
||
1601 |
/** |
|
1602 |
* Returns a <tt>Package</tt> that has been defined by this class loader |
|
1603 |
* or any of its ancestors. </p> |
|
1604 |
* |
|
1605 |
* @param name |
|
1606 |
* The package name |
|
1607 |
* |
|
1608 |
* @return The <tt>Package</tt> corresponding to the given name, or |
|
1609 |
* <tt>null</tt> if not found |
|
1610 |
* |
|
1611 |
* @since 1.2 |
|
1612 |
*/ |
|
1613 |
protected Package getPackage(String name) { |
|
1614 |
synchronized (packages) { |
|
51 | 1615 |
Package pkg = packages.get(name); |
2 | 1616 |
if (pkg == null) { |
1617 |
if (parent != null) { |
|
1618 |
pkg = parent.getPackage(name); |
|
1619 |
} else { |
|
1620 |
pkg = Package.getSystemPackage(name); |
|
1621 |
} |
|
1622 |
if (pkg != null) { |
|
1623 |
packages.put(name, pkg); |
|
1624 |
} |
|
1625 |
} |
|
1626 |
return pkg; |
|
1627 |
} |
|
1628 |
} |
|
1629 |
||
1630 |
/** |
|
1631 |
* Returns all of the <tt>Packages</tt> defined by this class loader and |
|
1632 |
* its ancestors. </p> |
|
1633 |
* |
|
1634 |
* @return The array of <tt>Package</tt> objects defined by this |
|
1635 |
* <tt>ClassLoader</tt> |
|
1636 |
* |
|
1637 |
* @since 1.2 |
|
1638 |
*/ |
|
1639 |
protected Package[] getPackages() { |
|
51 | 1640 |
Map<String, Package> map; |
2 | 1641 |
synchronized (packages) { |
51 | 1642 |
map = new HashMap<String, Package>(packages); |
2 | 1643 |
} |
1644 |
Package[] pkgs; |
|
1645 |
if (parent != null) { |
|
1646 |
pkgs = parent.getPackages(); |
|
1647 |
} else { |
|
1648 |
pkgs = Package.getSystemPackages(); |
|
1649 |
} |
|
1650 |
if (pkgs != null) { |
|
1651 |
for (int i = 0; i < pkgs.length; i++) { |
|
1652 |
String pkgName = pkgs[i].getName(); |
|
1653 |
if (map.get(pkgName) == null) { |
|
1654 |
map.put(pkgName, pkgs[i]); |
|
1655 |
} |
|
1656 |
} |
|
1657 |
} |
|
51 | 1658 |
return map.values().toArray(new Package[map.size()]); |
2 | 1659 |
} |
1660 |
||
1661 |
||
1662 |
// -- Native library access -- |
|
1663 |
||
1664 |
/** |
|
1665 |
* Returns the absolute path name of a native library. The VM invokes this |
|
1666 |
* method to locate the native libraries that belong to classes loaded with |
|
1667 |
* this class loader. If this method returns <tt>null</tt>, the VM |
|
1668 |
* searches the library along the path specified as the |
|
1669 |
* "<tt>java.library.path</tt>" property. </p> |
|
1670 |
* |
|
1671 |
* @param libname |
|
1672 |
* The library name |
|
1673 |
* |
|
1674 |
* @return The absolute path of the native library |
|
1675 |
* |
|
1676 |
* @see System#loadLibrary(String) |
|
1677 |
* @see System#mapLibraryName(String) |
|
1678 |
* |
|
1679 |
* @since 1.2 |
|
1680 |
*/ |
|
1681 |
protected String findLibrary(String libname) { |
|
1682 |
return null; |
|
1683 |
} |
|
1684 |
||
1685 |
/** |
|
1686 |
* The inner class NativeLibrary denotes a loaded native library instance. |
|
1687 |
* Every classloader contains a vector of loaded native libraries in the |
|
1688 |
* private field <tt>nativeLibraries</tt>. The native libraries loaded |
|
1689 |
* into the system are entered into the <tt>systemNativeLibraries</tt> |
|
1690 |
* vector. |
|
1691 |
* |
|
1692 |
* <p> Every native library requires a particular version of JNI. This is |
|
1693 |
* denoted by the private <tt>jniVersion</tt> field. This field is set by |
|
1694 |
* the VM when it loads the library, and used by the VM to pass the correct |
|
1695 |
* version of JNI to the native methods. </p> |
|
1696 |
* |
|
1697 |
* @see ClassLoader |
|
1698 |
* @since 1.2 |
|
1699 |
*/ |
|
1700 |
static class NativeLibrary { |
|
1701 |
// opaque handle to native library, used in native code. |
|
1702 |
long handle; |
|
1703 |
// the version of JNI environment the native library requires. |
|
1704 |
private int jniVersion; |
|
1705 |
// the class from which the library is loaded, also indicates |
|
1706 |
// the loader this native library belongs. |
|
1707 |
private Class fromClass; |
|
1708 |
// the canonicalized name of the native library. |
|
1709 |
String name; |
|
1710 |
||
1711 |
native void load(String name); |
|
1712 |
native long find(String name); |
|
1713 |
native void unload(); |
|
1714 |
||
1715 |
public NativeLibrary(Class fromClass, String name) { |
|
1716 |
this.name = name; |
|
1717 |
this.fromClass = fromClass; |
|
1718 |
} |
|
1719 |
||
1720 |
protected void finalize() { |
|
1721 |
synchronized (loadedLibraryNames) { |
|
1722 |
if (fromClass.getClassLoader() != null && handle != 0) { |
|
1723 |
/* remove the native library name */ |
|
1724 |
int size = loadedLibraryNames.size(); |
|
1725 |
for (int i = 0; i < size; i++) { |
|
1726 |
if (name.equals(loadedLibraryNames.elementAt(i))) { |
|
1727 |
loadedLibraryNames.removeElementAt(i); |
|
1728 |
break; |
|
1729 |
} |
|
1730 |
} |
|
1731 |
/* unload the library. */ |
|
1732 |
ClassLoader.nativeLibraryContext.push(this); |
|
1733 |
try { |
|
1734 |
unload(); |
|
1735 |
} finally { |
|
1736 |
ClassLoader.nativeLibraryContext.pop(); |
|
1737 |
} |
|
1738 |
} |
|
1739 |
} |
|
1740 |
} |
|
1741 |
// Invoked in the VM to determine the context class in |
|
1742 |
// JNI_Load/JNI_Unload |
|
1743 |
static Class getFromClass() { |
|
51 | 1744 |
return ClassLoader.nativeLibraryContext.peek().fromClass; |
2 | 1745 |
} |
1746 |
} |
|
1747 |
||
1748 |
// All native library names we've loaded. |
|
51 | 1749 |
private static Vector<String> loadedLibraryNames |
1750 |
= new Vector<String>(); |
|
1751 |
||
2 | 1752 |
// Native libraries belonging to system classes. |
51 | 1753 |
private static Vector<NativeLibrary> systemNativeLibraries |
1754 |
= new Vector<NativeLibrary>(); |
|
1755 |
||
2 | 1756 |
// Native libraries associated with the class loader. |
51 | 1757 |
private Vector<NativeLibrary> nativeLibraries |
1758 |
= new Vector<NativeLibrary>(); |
|
2 | 1759 |
|
1760 |
// native libraries being loaded/unloaded. |
|
51 | 1761 |
private static Stack<NativeLibrary> nativeLibraryContext |
1762 |
= new Stack<NativeLibrary>(); |
|
2 | 1763 |
|
1764 |
// The paths searched for libraries |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1765 |
private static String usr_paths[]; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1766 |
private static String sys_paths[]; |
2 | 1767 |
|
1768 |
private static String[] initializePath(String propname) { |
|
1769 |
String ldpath = System.getProperty(propname, ""); |
|
1770 |
String ps = File.pathSeparator; |
|
1771 |
int ldlen = ldpath.length(); |
|
1772 |
int i, j, n; |
|
1773 |
// Count the separators in the path |
|
1774 |
i = ldpath.indexOf(ps); |
|
1775 |
n = 0; |
|
1776 |
while (i >= 0) { |
|
1777 |
n++; |
|
1778 |
i = ldpath.indexOf(ps, i + 1); |
|
1779 |
} |
|
1780 |
||
1781 |
// allocate the array of paths - n :'s = n + 1 path elements |
|
1782 |
String[] paths = new String[n + 1]; |
|
1783 |
||
1784 |
// Fill the array with paths from the ldpath |
|
1785 |
n = i = 0; |
|
1786 |
j = ldpath.indexOf(ps); |
|
1787 |
while (j >= 0) { |
|
1788 |
if (j - i > 0) { |
|
1789 |
paths[n++] = ldpath.substring(i, j); |
|
1790 |
} else if (j - i == 0) { |
|
1791 |
paths[n++] = "."; |
|
1792 |
} |
|
1793 |
i = j + 1; |
|
1794 |
j = ldpath.indexOf(ps, i); |
|
1795 |
} |
|
1796 |
paths[n] = ldpath.substring(i, ldlen); |
|
1797 |
return paths; |
|
1798 |
} |
|
1799 |
||
1800 |
// Invoked in the java.lang.Runtime class to implement load and loadLibrary. |
|
1801 |
static void loadLibrary(Class fromClass, String name, |
|
1802 |
boolean isAbsolute) { |
|
1803 |
ClassLoader loader = |
|
1804 |
(fromClass == null) ? null : fromClass.getClassLoader(); |
|
1805 |
if (sys_paths == null) { |
|
1806 |
usr_paths = initializePath("java.library.path"); |
|
1807 |
sys_paths = initializePath("sun.boot.library.path"); |
|
1808 |
} |
|
1809 |
if (isAbsolute) { |
|
1810 |
if (loadLibrary0(fromClass, new File(name))) { |
|
1811 |
return; |
|
1812 |
} |
|
1813 |
throw new UnsatisfiedLinkError("Can't load library: " + name); |
|
1814 |
} |
|
1815 |
if (loader != null) { |
|
1816 |
String libfilename = loader.findLibrary(name); |
|
1817 |
if (libfilename != null) { |
|
1818 |
File libfile = new File(libfilename); |
|
1819 |
if (!libfile.isAbsolute()) { |
|
1820 |
throw new UnsatisfiedLinkError( |
|
1821 |
"ClassLoader.findLibrary failed to return an absolute path: " + libfilename); |
|
1822 |
} |
|
1823 |
if (loadLibrary0(fromClass, libfile)) { |
|
1824 |
return; |
|
1825 |
} |
|
1826 |
throw new UnsatisfiedLinkError("Can't load " + libfilename); |
|
1827 |
} |
|
1828 |
} |
|
1829 |
for (int i = 0 ; i < sys_paths.length ; i++) { |
|
1830 |
File libfile = new File(sys_paths[i], System.mapLibraryName(name)); |
|
1831 |
if (loadLibrary0(fromClass, libfile)) { |
|
1832 |
return; |
|
1833 |
} |
|
1834 |
} |
|
1835 |
if (loader != null) { |
|
1836 |
for (int i = 0 ; i < usr_paths.length ; i++) { |
|
1837 |
File libfile = new File(usr_paths[i], |
|
1838 |
System.mapLibraryName(name)); |
|
1839 |
if (loadLibrary0(fromClass, libfile)) { |
|
1840 |
return; |
|
1841 |
} |
|
1842 |
} |
|
1843 |
} |
|
1844 |
// Oops, it failed |
|
1845 |
throw new UnsatisfiedLinkError("no " + name + " in java.library.path"); |
|
1846 |
} |
|
1847 |
||
1848 |
private static boolean loadLibrary0(Class fromClass, final File file) { |
|
51 | 1849 |
boolean exists = AccessController.doPrivileged( |
1850 |
new PrivilegedAction<Object>() { |
|
2 | 1851 |
public Object run() { |
51 | 1852 |
return file.exists() ? Boolean.TRUE : null; |
1853 |
}}) |
|
1854 |
!= null; |
|
1855 |
if (!exists) { |
|
2 | 1856 |
return false; |
1857 |
} |
|
1858 |
String name; |
|
1859 |
try { |
|
1860 |
name = file.getCanonicalPath(); |
|
1861 |
} catch (IOException e) { |
|
1862 |
return false; |
|
1863 |
} |
|
1864 |
ClassLoader loader = |
|
1865 |
(fromClass == null) ? null : fromClass.getClassLoader(); |
|
51 | 1866 |
Vector<NativeLibrary> libs = |
2 | 1867 |
loader != null ? loader.nativeLibraries : systemNativeLibraries; |
1868 |
synchronized (libs) { |
|
1869 |
int size = libs.size(); |
|
1870 |
for (int i = 0; i < size; i++) { |
|
51 | 1871 |
NativeLibrary lib = libs.elementAt(i); |
2 | 1872 |
if (name.equals(lib.name)) { |
1873 |
return true; |
|
1874 |
} |
|
1875 |
} |
|
1876 |
||
1877 |
synchronized (loadedLibraryNames) { |
|
1878 |
if (loadedLibraryNames.contains(name)) { |
|
1879 |
throw new UnsatisfiedLinkError |
|
1880 |
("Native Library " + |
|
1881 |
name + |
|
1882 |
" already loaded in another classloader"); |
|
1883 |
} |
|
1884 |
/* If the library is being loaded (must be by the same thread, |
|
1885 |
* because Runtime.load and Runtime.loadLibrary are |
|
1886 |
* synchronous). The reason is can occur is that the JNI_OnLoad |
|
1887 |
* function can cause another loadLibrary invocation. |
|
1888 |
* |
|
1889 |
* Thus we can use a static stack to hold the list of libraries |
|
1890 |
* we are loading. |
|
1891 |
* |
|
1892 |
* If there is a pending load operation for the library, we |
|
1893 |
* immediately return success; otherwise, we raise |
|
1894 |
* UnsatisfiedLinkError. |
|
1895 |
*/ |
|
1896 |
int n = nativeLibraryContext.size(); |
|
1897 |
for (int i = 0; i < n; i++) { |
|
51 | 1898 |
NativeLibrary lib = nativeLibraryContext.elementAt(i); |
2 | 1899 |
if (name.equals(lib.name)) { |
1900 |
if (loader == lib.fromClass.getClassLoader()) { |
|
1901 |
return true; |
|
1902 |
} else { |
|
1903 |
throw new UnsatisfiedLinkError |
|
1904 |
("Native Library " + |
|
1905 |
name + |
|
1906 |
" is being loaded in another classloader"); |
|
1907 |
} |
|
1908 |
} |
|
1909 |
} |
|
1910 |
NativeLibrary lib = new NativeLibrary(fromClass, name); |
|
1911 |
nativeLibraryContext.push(lib); |
|
1912 |
try { |
|
1913 |
lib.load(name); |
|
1914 |
} finally { |
|
1915 |
nativeLibraryContext.pop(); |
|
1916 |
} |
|
1917 |
if (lib.handle != 0) { |
|
1918 |
loadedLibraryNames.addElement(name); |
|
1919 |
libs.addElement(lib); |
|
1920 |
return true; |
|
1921 |
} |
|
1922 |
return false; |
|
1923 |
} |
|
1924 |
} |
|
1925 |
} |
|
1926 |
||
1927 |
// Invoked in the VM class linking code. |
|
1928 |
static long findNative(ClassLoader loader, String name) { |
|
51 | 1929 |
Vector<NativeLibrary> libs = |
2 | 1930 |
loader != null ? loader.nativeLibraries : systemNativeLibraries; |
1931 |
synchronized (libs) { |
|
1932 |
int size = libs.size(); |
|
1933 |
for (int i = 0; i < size; i++) { |
|
51 | 1934 |
NativeLibrary lib = libs.elementAt(i); |
2 | 1935 |
long entry = lib.find(name); |
1936 |
if (entry != 0) |
|
1937 |
return entry; |
|
1938 |
} |
|
1939 |
} |
|
1940 |
return 0; |
|
1941 |
} |
|
1942 |
||
1943 |
||
1944 |
// -- Assertion management -- |
|
1945 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1946 |
final Object assertionLock; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1947 |
|
2 | 1948 |
// The default toggle for assertion checking. |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1949 |
// @GuardedBy("assertionLock") |
2 | 1950 |
private boolean defaultAssertionStatus = false; |
1951 |
||
1952 |
// Maps String packageName to Boolean package default assertion status Note |
|
1953 |
// that the default package is placed under a null map key. If this field |
|
1954 |
// is null then we are delegating assertion status queries to the VM, i.e., |
|
1955 |
// none of this ClassLoader's assertion status modification methods have |
|
1956 |
// been invoked. |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1957 |
// @GuardedBy("assertionLock") |
51 | 1958 |
private Map<String, Boolean> packageAssertionStatus = null; |
2 | 1959 |
|
1960 |
// Maps String fullyQualifiedClassName to Boolean assertionStatus If this |
|
1961 |
// field is null then we are delegating assertion status queries to the VM, |
|
1962 |
// i.e., none of this ClassLoader's assertion status modification methods |
|
1963 |
// have been invoked. |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1964 |
// @GuardedBy("assertionLock") |
51 | 1965 |
Map<String, Boolean> classAssertionStatus = null; |
2 | 1966 |
|
1967 |
/** |
|
1968 |
* Sets the default assertion status for this class loader. This setting |
|
1969 |
* determines whether classes loaded by this class loader and initialized |
|
1970 |
* in the future will have assertions enabled or disabled by default. |
|
1971 |
* This setting may be overridden on a per-package or per-class basis by |
|
1972 |
* invoking {@link #setPackageAssertionStatus(String, boolean)} or {@link |
|
1973 |
* #setClassAssertionStatus(String, boolean)}. </p> |
|
1974 |
* |
|
1975 |
* @param enabled |
|
1976 |
* <tt>true</tt> if classes loaded by this class loader will |
|
1977 |
* henceforth have assertions enabled by default, <tt>false</tt> |
|
1978 |
* if they will have assertions disabled by default. |
|
1979 |
* |
|
1980 |
* @since 1.4 |
|
1981 |
*/ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1982 |
public void setDefaultAssertionStatus(boolean enabled) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1983 |
synchronized (assertionLock) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1984 |
if (classAssertionStatus == null) |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1985 |
initializeJavaAssertionMaps(); |
2 | 1986 |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1987 |
defaultAssertionStatus = enabled; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
1988 |
} |
2 | 1989 |
} |
1990 |
||
1991 |
/** |
|
1992 |
* Sets the package default assertion status for the named package. The |
|
1993 |
* package default assertion status determines the assertion status for |
|
1994 |
* classes initialized in the future that belong to the named package or |
|
1995 |
* any of its "subpackages". |
|
1996 |
* |
|
1997 |
* <p> A subpackage of a package named p is any package whose name begins |
|
1998 |
* with "<tt>p.</tt>". For example, <tt>javax.swing.text</tt> is a |
|
1999 |
* subpackage of <tt>javax.swing</tt>, and both <tt>java.util</tt> and |
|
2000 |
* <tt>java.lang.reflect</tt> are subpackages of <tt>java</tt>. |
|
2001 |
* |
|
2002 |
* <p> In the event that multiple package defaults apply to a given class, |
|
2003 |
* the package default pertaining to the most specific package takes |
|
2004 |
* precedence over the others. For example, if <tt>javax.lang</tt> and |
|
2005 |
* <tt>javax.lang.reflect</tt> both have package defaults associated with |
|
2006 |
* them, the latter package default applies to classes in |
|
2007 |
* <tt>javax.lang.reflect</tt>. |
|
2008 |
* |
|
2009 |
* <p> Package defaults take precedence over the class loader's default |
|
2010 |
* assertion status, and may be overridden on a per-class basis by invoking |
|
2011 |
* {@link #setClassAssertionStatus(String, boolean)}. </p> |
|
2012 |
* |
|
2013 |
* @param packageName |
|
2014 |
* The name of the package whose package default assertion status |
|
2015 |
* is to be set. A <tt>null</tt> value indicates the unnamed |
|
2016 |
* package that is "current" |
|
2017 |
* (<a href="http://java.sun.com/docs/books/jls/">Java Language |
|
2018 |
* Specification</a>, section 7.4.2). |
|
2019 |
* |
|
2020 |
* @param enabled |
|
2021 |
* <tt>true</tt> if classes loaded by this classloader and |
|
2022 |
* belonging to the named package or any of its subpackages will |
|
2023 |
* have assertions enabled by default, <tt>false</tt> if they will |
|
2024 |
* have assertions disabled by default. |
|
2025 |
* |
|
2026 |
* @since 1.4 |
|
2027 |
*/ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2028 |
public void setPackageAssertionStatus(String packageName, |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2029 |
boolean enabled) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2030 |
synchronized (assertionLock) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2031 |
if (packageAssertionStatus == null) |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2032 |
initializeJavaAssertionMaps(); |
2 | 2033 |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2034 |
packageAssertionStatus.put(packageName, enabled); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2035 |
} |
2 | 2036 |
} |
2037 |
||
2038 |
/** |
|
2039 |
* Sets the desired assertion status for the named top-level class in this |
|
2040 |
* class loader and any nested classes contained therein. This setting |
|
2041 |
* takes precedence over the class loader's default assertion status, and |
|
2042 |
* over any applicable per-package default. This method has no effect if |
|
2043 |
* the named class has already been initialized. (Once a class is |
|
2044 |
* initialized, its assertion status cannot change.) |
|
2045 |
* |
|
2046 |
* <p> If the named class is not a top-level class, this invocation will |
|
2047 |
* have no effect on the actual assertion status of any class. </p> |
|
2048 |
* |
|
2049 |
* @param className |
|
2050 |
* The fully qualified class name of the top-level class whose |
|
2051 |
* assertion status is to be set. |
|
2052 |
* |
|
2053 |
* @param enabled |
|
2054 |
* <tt>true</tt> if the named class is to have assertions |
|
2055 |
* enabled when (and if) it is initialized, <tt>false</tt> if the |
|
2056 |
* class is to have assertions disabled. |
|
2057 |
* |
|
2058 |
* @since 1.4 |
|
2059 |
*/ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2060 |
public void setClassAssertionStatus(String className, boolean enabled) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2061 |
synchronized (assertionLock) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2062 |
if (classAssertionStatus == null) |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2063 |
initializeJavaAssertionMaps(); |
2 | 2064 |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2065 |
classAssertionStatus.put(className, enabled); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2066 |
} |
2 | 2067 |
} |
2068 |
||
2069 |
/** |
|
2070 |
* Sets the default assertion status for this class loader to |
|
2071 |
* <tt>false</tt> and discards any package defaults or class assertion |
|
2072 |
* status settings associated with the class loader. This method is |
|
2073 |
* provided so that class loaders can be made to ignore any command line or |
|
2074 |
* persistent assertion status settings and "start with a clean slate." |
|
2075 |
* </p> |
|
2076 |
* |
|
2077 |
* @since 1.4 |
|
2078 |
*/ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2079 |
public void clearAssertionStatus() { |
2 | 2080 |
/* |
2081 |
* Whether or not "Java assertion maps" are initialized, set |
|
2082 |
* them to empty maps, effectively ignoring any present settings. |
|
2083 |
*/ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2084 |
synchronized (assertionLock) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2085 |
classAssertionStatus = new HashMap<String, Boolean>(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2086 |
packageAssertionStatus = new HashMap<String, Boolean>(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2087 |
defaultAssertionStatus = false; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2088 |
} |
2 | 2089 |
} |
2090 |
||
2091 |
/** |
|
2092 |
* Returns the assertion status that would be assigned to the specified |
|
2093 |
* class if it were to be initialized at the time this method is invoked. |
|
2094 |
* If the named class has had its assertion status set, the most recent |
|
2095 |
* setting will be returned; otherwise, if any package default assertion |
|
2096 |
* status pertains to this class, the most recent setting for the most |
|
2097 |
* specific pertinent package default assertion status is returned; |
|
2098 |
* otherwise, this class loader's default assertion status is returned. |
|
2099 |
* </p> |
|
2100 |
* |
|
2101 |
* @param className |
|
2102 |
* The fully qualified class name of the class whose desired |
|
2103 |
* assertion status is being queried. |
|
2104 |
* |
|
2105 |
* @return The desired assertion status of the specified class. |
|
2106 |
* |
|
2107 |
* @see #setClassAssertionStatus(String, boolean) |
|
2108 |
* @see #setPackageAssertionStatus(String, boolean) |
|
2109 |
* @see #setDefaultAssertionStatus(boolean) |
|
2110 |
* |
|
2111 |
* @since 1.4 |
|
2112 |
*/ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2113 |
boolean desiredAssertionStatus(String className) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2114 |
synchronized (assertionLock) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2115 |
// assert classAssertionStatus != null; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2116 |
// assert packageAssertionStatus != null; |
2 | 2117 |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2118 |
// Check for a class entry |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2119 |
Boolean result = classAssertionStatus.get(className); |
2 | 2120 |
if (result != null) |
2121 |
return result.booleanValue(); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2122 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2123 |
// Check for most specific package entry |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2124 |
int dotIndex = className.lastIndexOf("."); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2125 |
if (dotIndex < 0) { // default package |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2126 |
result = packageAssertionStatus.get(null); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2127 |
if (result != null) |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2128 |
return result.booleanValue(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2129 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2130 |
while(dotIndex > 0) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2131 |
className = className.substring(0, dotIndex); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2132 |
result = packageAssertionStatus.get(className); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2133 |
if (result != null) |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2134 |
return result.booleanValue(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2135 |
dotIndex = className.lastIndexOf(".", dotIndex-1); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2136 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2137 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2138 |
// Return the classloader default |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2139 |
return defaultAssertionStatus; |
2 | 2140 |
} |
2141 |
} |
|
2142 |
||
2143 |
// Set up the assertions with information provided by the VM. |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2144 |
// Note: Should only be called inside a synchronized block |
2 | 2145 |
private void initializeJavaAssertionMaps() { |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2146 |
// assert Thread.holdsLock(assertionLock); |
2 | 2147 |
|
51 | 2148 |
classAssertionStatus = new HashMap<String, Boolean>(); |
2149 |
packageAssertionStatus = new HashMap<String, Boolean>(); |
|
2 | 2150 |
AssertionStatusDirectives directives = retrieveDirectives(); |
2151 |
||
2152 |
for(int i = 0; i < directives.classes.length; i++) |
|
2153 |
classAssertionStatus.put(directives.classes[i], |
|
51 | 2154 |
directives.classEnabled[i]); |
2 | 2155 |
|
2156 |
for(int i = 0; i < directives.packages.length; i++) |
|
2157 |
packageAssertionStatus.put(directives.packages[i], |
|
51 | 2158 |
directives.packageEnabled[i]); |
2 | 2159 |
|
2160 |
defaultAssertionStatus = directives.deflt; |
|
2161 |
} |
|
2162 |
||
2163 |
// Retrieves the assertion directives from the VM. |
|
2164 |
private static native AssertionStatusDirectives retrieveDirectives(); |
|
2165 |
} |
|
2166 |
||
2167 |
||
51 | 2168 |
class SystemClassLoaderAction |
2169 |
implements PrivilegedExceptionAction<ClassLoader> { |
|
2 | 2170 |
private ClassLoader parent; |
2171 |
||
2172 |
SystemClassLoaderAction(ClassLoader parent) { |
|
2173 |
this.parent = parent; |
|
2174 |
} |
|
2175 |
||
51 | 2176 |
public ClassLoader run() throws Exception { |
2 | 2177 |
String cls = System.getProperty("java.system.class.loader"); |
2178 |
if (cls == null) { |
|
2179 |
return parent; |
|
2180 |
} |
|
2181 |
||
51 | 2182 |
Constructor ctor = Class.forName(cls, true, parent) |
2183 |
.getDeclaredConstructor(new Class[] { ClassLoader.class }); |
|
2184 |
ClassLoader sys = (ClassLoader) ctor.newInstance( |
|
2185 |
new Object[] { parent }); |
|
2 | 2186 |
Thread.currentThread().setContextClassLoader(sys); |
2187 |
return sys; |
|
2188 |
} |
|
2189 |
} |