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