|
1 /* |
|
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.bind.v2.runtime.reflect.opt; |
|
27 |
|
28 import java.lang.reflect.InvocationTargetException; |
|
29 import java.lang.reflect.Method; |
|
30 import java.lang.ref.WeakReference; |
|
31 import java.security.AccessController; |
|
32 import java.security.PrivilegedAction; |
|
33 import java.util.concurrent.locks.Lock; |
|
34 import java.util.concurrent.locks.ReentrantReadWriteLock; |
|
35 import java.util.HashMap; |
|
36 import java.util.Map; |
|
37 import java.util.WeakHashMap; |
|
38 import java.util.logging.Level; |
|
39 import java.util.logging.Logger; |
|
40 |
|
41 import com.sun.xml.internal.bind.Util; |
|
42 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; |
|
43 |
|
44 /** |
|
45 * A {@link ClassLoader} used to "inject" optimized accessor classes |
|
46 * into the VM. |
|
47 * |
|
48 * <p> |
|
49 * Its parent class loader needs to be set to the one that can see the user |
|
50 * class. |
|
51 * |
|
52 * @author Kohsuke Kawaguchi |
|
53 */ |
|
54 final class Injector { |
|
55 |
|
56 /** |
|
57 * {@link Injector}s keyed by their parent {@link ClassLoader}. |
|
58 * |
|
59 * We only need one injector per one user class loader. |
|
60 */ |
|
61 private static final ReentrantReadWriteLock irwl = new ReentrantReadWriteLock(); |
|
62 private static final Lock ir = irwl.readLock(); |
|
63 private static final Lock iw = irwl.writeLock(); |
|
64 private static final Map<ClassLoader, WeakReference<Injector>> injectors = |
|
65 new WeakHashMap<ClassLoader, WeakReference<Injector>>(); |
|
66 private static final Logger logger = Util.getClassLogger(); |
|
67 |
|
68 /** |
|
69 * Injects a new class into the given class loader. |
|
70 * |
|
71 * @return null |
|
72 * if it fails to inject. |
|
73 */ |
|
74 static Class inject(ClassLoader cl, String className, byte[] image) { |
|
75 Injector injector = get(cl); |
|
76 if (injector != null) { |
|
77 return injector.inject(className, image); |
|
78 } else { |
|
79 return null; |
|
80 } |
|
81 } |
|
82 |
|
83 /** |
|
84 * Returns the already injected class, or null. |
|
85 */ |
|
86 static Class find(ClassLoader cl, String className) { |
|
87 Injector injector = get(cl); |
|
88 if (injector != null) { |
|
89 return injector.find(className); |
|
90 } else { |
|
91 return null; |
|
92 } |
|
93 } |
|
94 |
|
95 /** |
|
96 * Gets or creates an {@link Injector} for the given class loader. |
|
97 * |
|
98 * @return null |
|
99 * if it fails. |
|
100 */ |
|
101 private static Injector get(ClassLoader cl) { |
|
102 Injector injector = null; |
|
103 WeakReference<Injector> wr; |
|
104 ir.lock(); |
|
105 try { |
|
106 wr = injectors.get(cl); |
|
107 } finally { |
|
108 ir.unlock(); |
|
109 } |
|
110 if (wr != null) { |
|
111 injector = wr.get(); |
|
112 } |
|
113 if (injector == null) { |
|
114 try { |
|
115 wr = new WeakReference<Injector>(injector = new Injector(cl)); |
|
116 iw.lock(); |
|
117 try { |
|
118 if (!injectors.containsKey(cl)) { |
|
119 injectors.put(cl, wr); |
|
120 } |
|
121 } finally { |
|
122 iw.unlock(); |
|
123 } |
|
124 } catch (SecurityException e) { |
|
125 logger.log(Level.FINE, "Unable to set up a back-door for the injector", e); |
|
126 return null; |
|
127 } |
|
128 } |
|
129 return injector; |
|
130 } |
|
131 /** |
|
132 * Injected classes keyed by their names. |
|
133 */ |
|
134 private final Map<String, Class> classes = new HashMap<String, Class>(); |
|
135 private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); |
|
136 private final Lock r = rwl.readLock(); |
|
137 private final Lock w = rwl.writeLock(); |
|
138 private final ClassLoader parent; |
|
139 /** |
|
140 * True if this injector is capable of injecting accessors. |
|
141 * False otherwise, which happens if this classloader can't see {@link Accessor}. |
|
142 */ |
|
143 private final boolean loadable; |
|
144 private static final Method defineClass; |
|
145 private static final Method resolveClass; |
|
146 private static final Method findLoadedClass; |
|
147 |
|
148 static { |
|
149 try { |
|
150 defineClass = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, Integer.TYPE, Integer.TYPE); |
|
151 resolveClass = ClassLoader.class.getDeclaredMethod("resolveClass", Class.class); |
|
152 findLoadedClass = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class); |
|
153 } catch (NoSuchMethodException e) { |
|
154 // impossible |
|
155 throw new NoSuchMethodError(e.getMessage()); |
|
156 } |
|
157 AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
158 |
|
159 public Void run() { |
|
160 // TODO: check security implication |
|
161 // do these setAccessible allow anyone to call these methods freely?s |
|
162 defineClass.setAccessible(true); |
|
163 resolveClass.setAccessible(true); |
|
164 findLoadedClass.setAccessible(true); |
|
165 return null; |
|
166 } |
|
167 }); |
|
168 } |
|
169 |
|
170 private Injector(ClassLoader parent) { |
|
171 this.parent = parent; |
|
172 assert parent != null; |
|
173 |
|
174 boolean loadableCheck = false; |
|
175 |
|
176 try { |
|
177 loadableCheck = parent.loadClass(Accessor.class.getName()) == Accessor.class; |
|
178 } catch (ClassNotFoundException e) { |
|
179 // not loadable |
|
180 } |
|
181 |
|
182 this.loadable = loadableCheck; |
|
183 } |
|
184 |
|
185 @SuppressWarnings("LockAcquiredButNotSafelyReleased") |
|
186 private Class inject(String className, byte[] image) { |
|
187 if (!loadable) // this injector cannot inject anything |
|
188 { |
|
189 return null; |
|
190 } |
|
191 |
|
192 boolean wlocked = false; |
|
193 boolean rlocked = false; |
|
194 try { |
|
195 |
|
196 r.lock(); |
|
197 rlocked = true; |
|
198 |
|
199 Class c = classes.get(className); |
|
200 |
|
201 // Unlock now during the findLoadedClass process to avoid |
|
202 // deadlocks |
|
203 r.unlock(); |
|
204 rlocked = false; |
|
205 |
|
206 //find loaded class from classloader |
|
207 if (c == null) { |
|
208 |
|
209 try { |
|
210 c = (Class) findLoadedClass.invoke(parent, className.replace('/', '.')); |
|
211 } catch (IllegalArgumentException e) { |
|
212 logger.log(Level.FINE, "Unable to find " + className, e); |
|
213 } catch (IllegalAccessException e) { |
|
214 logger.log(Level.FINE, "Unable to find " + className, e); |
|
215 } catch (InvocationTargetException e) { |
|
216 Throwable t = e.getTargetException(); |
|
217 logger.log(Level.FINE, "Unable to find " + className, t); |
|
218 } |
|
219 |
|
220 if (c != null) { |
|
221 |
|
222 w.lock(); |
|
223 wlocked = true; |
|
224 |
|
225 classes.put(className, c); |
|
226 |
|
227 w.unlock(); |
|
228 wlocked = false; |
|
229 |
|
230 return c; |
|
231 } |
|
232 } |
|
233 |
|
234 if (c == null) { |
|
235 |
|
236 r.lock(); |
|
237 rlocked = true; |
|
238 |
|
239 c = classes.get(className); |
|
240 |
|
241 // Unlock now during the define/resolve process to avoid |
|
242 // deadlocks |
|
243 r.unlock(); |
|
244 rlocked = false; |
|
245 |
|
246 if (c == null) { |
|
247 |
|
248 // we need to inject a class into the |
|
249 try { |
|
250 c = (Class) defineClass.invoke(parent, className.replace('/', '.'), image, 0, image.length); |
|
251 resolveClass.invoke(parent, c); |
|
252 } catch (IllegalAccessException e) { |
|
253 logger.log(Level.FINE, "Unable to inject " + className, e); |
|
254 return null; |
|
255 } catch (InvocationTargetException e) { |
|
256 Throwable t = e.getTargetException(); |
|
257 if (t instanceof LinkageError) { |
|
258 logger.log(Level.FINE, "duplicate class definition bug occured? Please report this : " + className, t); |
|
259 } else { |
|
260 logger.log(Level.FINE, "Unable to inject " + className, t); |
|
261 } |
|
262 return null; |
|
263 } catch (SecurityException e) { |
|
264 logger.log(Level.FINE, "Unable to inject " + className, e); |
|
265 return null; |
|
266 } catch (LinkageError e) { |
|
267 logger.log(Level.FINE, "Unable to inject " + className, e); |
|
268 return null; |
|
269 } |
|
270 |
|
271 w.lock(); |
|
272 wlocked = true; |
|
273 |
|
274 // During the time we were unlocked, we could have tried to |
|
275 // load the class from more than one thread. Check now to see |
|
276 // if someone else beat us to registering this class |
|
277 if (!classes.containsKey(className)) { |
|
278 classes.put(className, c); |
|
279 } |
|
280 |
|
281 w.unlock(); |
|
282 wlocked = false; |
|
283 } |
|
284 } |
|
285 return c; |
|
286 } finally { |
|
287 if (rlocked) { |
|
288 r.unlock(); |
|
289 } |
|
290 if (wlocked) { |
|
291 w.unlock(); |
|
292 } |
|
293 } |
|
294 } |
|
295 |
|
296 private Class find(String className) { |
|
297 r.lock(); |
|
298 try { |
|
299 return classes.get(className); |
|
300 } finally { |
|
301 r.unlock(); |
|
302 } |
|
303 } |
|
304 } |