aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime.reflect.opt; aoqi@0: aoqi@0: import java.lang.reflect.InvocationTargetException; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.lang.ref.WeakReference; aoqi@0: import java.security.AccessController; aoqi@0: import java.security.PrivilegedAction; aoqi@0: import java.util.concurrent.locks.Lock; aoqi@0: import java.util.concurrent.locks.ReentrantReadWriteLock; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.Map; aoqi@0: import java.util.WeakHashMap; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.Util; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; aoqi@0: aoqi@0: /** aoqi@0: * A {@link ClassLoader} used to "inject" optimized accessor classes aoqi@0: * into the VM. aoqi@0: * aoqi@0: *

aoqi@0: * Its parent class loader needs to be set to the one that can see the user aoqi@0: * class. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: final class Injector { aoqi@0: aoqi@0: /** aoqi@0: * {@link Injector}s keyed by their parent {@link ClassLoader}. aoqi@0: * aoqi@0: * We only need one injector per one user class loader. aoqi@0: */ aoqi@0: private static final ReentrantReadWriteLock irwl = new ReentrantReadWriteLock(); aoqi@0: private static final Lock ir = irwl.readLock(); aoqi@0: private static final Lock iw = irwl.writeLock(); aoqi@0: private static final Map> injectors = aoqi@0: new WeakHashMap>(); aoqi@0: private static final Logger logger = Util.getClassLogger(); aoqi@0: aoqi@0: /** aoqi@0: * Injects a new class into the given class loader. aoqi@0: * aoqi@0: * @return null aoqi@0: * if it fails to inject. aoqi@0: */ aoqi@0: static Class inject(ClassLoader cl, String className, byte[] image) { aoqi@0: Injector injector = get(cl); aoqi@0: if (injector != null) { aoqi@0: return injector.inject(className, image); aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the already injected class, or null. aoqi@0: */ aoqi@0: static Class find(ClassLoader cl, String className) { aoqi@0: Injector injector = get(cl); aoqi@0: if (injector != null) { aoqi@0: return injector.find(className); aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets or creates an {@link Injector} for the given class loader. aoqi@0: * aoqi@0: * @return null aoqi@0: * if it fails. aoqi@0: */ aoqi@0: private static Injector get(ClassLoader cl) { aoqi@0: Injector injector = null; aoqi@0: WeakReference wr; aoqi@0: ir.lock(); aoqi@0: try { aoqi@0: wr = injectors.get(cl); aoqi@0: } finally { aoqi@0: ir.unlock(); aoqi@0: } aoqi@0: if (wr != null) { aoqi@0: injector = wr.get(); aoqi@0: } aoqi@0: if (injector == null) { aoqi@0: try { aoqi@0: wr = new WeakReference(injector = new Injector(cl)); aoqi@0: iw.lock(); aoqi@0: try { aoqi@0: if (!injectors.containsKey(cl)) { aoqi@0: injectors.put(cl, wr); aoqi@0: } aoqi@0: } finally { aoqi@0: iw.unlock(); aoqi@0: } aoqi@0: } catch (SecurityException e) { aoqi@0: logger.log(Level.FINE, "Unable to set up a back-door for the injector", e); aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: return injector; aoqi@0: } aoqi@0: /** aoqi@0: * Injected classes keyed by their names. aoqi@0: */ aoqi@0: private final Map classes = new HashMap(); aoqi@0: private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); aoqi@0: private final Lock r = rwl.readLock(); aoqi@0: private final Lock w = rwl.writeLock(); aoqi@0: private final ClassLoader parent; aoqi@0: /** aoqi@0: * True if this injector is capable of injecting accessors. aoqi@0: * False otherwise, which happens if this classloader can't see {@link Accessor}. aoqi@0: */ aoqi@0: private final boolean loadable; aoqi@0: private static final Method defineClass; aoqi@0: private static final Method resolveClass; aoqi@0: private static final Method findLoadedClass; aoqi@0: aoqi@0: static { aoqi@0: try { aoqi@0: defineClass = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, Integer.TYPE, Integer.TYPE); aoqi@0: resolveClass = ClassLoader.class.getDeclaredMethod("resolveClass", Class.class); aoqi@0: findLoadedClass = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class); aoqi@0: } catch (NoSuchMethodException e) { aoqi@0: // impossible aoqi@0: throw new NoSuchMethodError(e.getMessage()); aoqi@0: } aoqi@0: AccessController.doPrivileged(new PrivilegedAction() { aoqi@0: aoqi@0: @Override aoqi@0: public Void run() { aoqi@0: // TODO: check security implication aoqi@0: // do these setAccessible allow anyone to call these methods freely?s aoqi@0: defineClass.setAccessible(true); aoqi@0: resolveClass.setAccessible(true); aoqi@0: findLoadedClass.setAccessible(true); aoqi@0: return null; aoqi@0: } aoqi@0: }); aoqi@0: } aoqi@0: aoqi@0: private Injector(ClassLoader parent) { aoqi@0: this.parent = parent; aoqi@0: assert parent != null; aoqi@0: aoqi@0: boolean loadableCheck = false; aoqi@0: aoqi@0: try { aoqi@0: loadableCheck = parent.loadClass(Accessor.class.getName()) == Accessor.class; aoqi@0: } catch (ClassNotFoundException e) { aoqi@0: // not loadable aoqi@0: } aoqi@0: aoqi@0: this.loadable = loadableCheck; aoqi@0: } aoqi@0: aoqi@0: @SuppressWarnings("LockAcquiredButNotSafelyReleased") aoqi@0: private Class inject(String className, byte[] image) { aoqi@0: if (!loadable) // this injector cannot inject anything aoqi@0: { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: boolean wlocked = false; aoqi@0: boolean rlocked = false; aoqi@0: try { aoqi@0: aoqi@0: r.lock(); aoqi@0: rlocked = true; aoqi@0: aoqi@0: Class c = classes.get(className); aoqi@0: aoqi@0: // Unlock now during the findLoadedClass process to avoid aoqi@0: // deadlocks aoqi@0: r.unlock(); aoqi@0: rlocked = false; aoqi@0: aoqi@0: //find loaded class from classloader aoqi@0: if (c == null) { aoqi@0: aoqi@0: try { aoqi@0: c = (Class) findLoadedClass.invoke(parent, className.replace('/', '.')); aoqi@0: } catch (IllegalArgumentException e) { aoqi@0: logger.log(Level.FINE, "Unable to find " + className, e); aoqi@0: } catch (IllegalAccessException e) { aoqi@0: logger.log(Level.FINE, "Unable to find " + className, e); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: Throwable t = e.getTargetException(); aoqi@0: logger.log(Level.FINE, "Unable to find " + className, t); aoqi@0: } aoqi@0: aoqi@0: if (c != null) { aoqi@0: aoqi@0: w.lock(); aoqi@0: wlocked = true; aoqi@0: aoqi@0: classes.put(className, c); aoqi@0: aoqi@0: w.unlock(); aoqi@0: wlocked = false; aoqi@0: aoqi@0: return c; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (c == null) { aoqi@0: aoqi@0: r.lock(); aoqi@0: rlocked = true; aoqi@0: aoqi@0: c = classes.get(className); aoqi@0: aoqi@0: // Unlock now during the define/resolve process to avoid aoqi@0: // deadlocks aoqi@0: r.unlock(); aoqi@0: rlocked = false; aoqi@0: aoqi@0: if (c == null) { aoqi@0: aoqi@0: // we need to inject a class into the aoqi@0: try { aoqi@0: c = (Class) defineClass.invoke(parent, className.replace('/', '.'), image, 0, image.length); aoqi@0: resolveClass.invoke(parent, c); aoqi@0: } catch (IllegalAccessException e) { aoqi@0: logger.log(Level.FINE, "Unable to inject " + className, e); aoqi@0: return null; aoqi@0: } catch (InvocationTargetException e) { aoqi@0: Throwable t = e.getTargetException(); aoqi@0: if (t instanceof LinkageError) { aoqi@0: logger.log(Level.FINE, "duplicate class definition bug occured? Please report this : " + className, t); aoqi@0: } else { aoqi@0: logger.log(Level.FINE, "Unable to inject " + className, t); aoqi@0: } aoqi@0: return null; aoqi@0: } catch (SecurityException e) { aoqi@0: logger.log(Level.FINE, "Unable to inject " + className, e); aoqi@0: return null; aoqi@0: } catch (LinkageError e) { aoqi@0: logger.log(Level.FINE, "Unable to inject " + className, e); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: w.lock(); aoqi@0: wlocked = true; aoqi@0: aoqi@0: // During the time we were unlocked, we could have tried to aoqi@0: // load the class from more than one thread. Check now to see aoqi@0: // if someone else beat us to registering this class aoqi@0: if (!classes.containsKey(className)) { aoqi@0: classes.put(className, c); aoqi@0: } aoqi@0: aoqi@0: w.unlock(); aoqi@0: wlocked = false; aoqi@0: } aoqi@0: } aoqi@0: return c; aoqi@0: } finally { aoqi@0: if (rlocked) { aoqi@0: r.unlock(); aoqi@0: } aoqi@0: if (wlocked) { aoqi@0: w.unlock(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private Class find(String className) { aoqi@0: r.lock(); aoqi@0: try { aoqi@0: return classes.get(className); aoqi@0: } finally { aoqi@0: r.unlock(); aoqi@0: } aoqi@0: } aoqi@0: }