src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/reflect/opt/Injector.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

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

mercurial