ohair@286: /*
mkos@408: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286: *
ohair@286: * This code is free software; you can redistribute it and/or modify it
ohair@286: * under the terms of the GNU General Public License version 2 only, as
ohair@286: * published by the Free Software Foundation. Oracle designates this
ohair@286: * particular file as subject to the "Classpath" exception as provided
ohair@286: * by Oracle in the LICENSE file that accompanied this code.
ohair@286: *
ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286: * version 2 for more details (a copy is included in the LICENSE file that
ohair@286: * accompanied this code).
ohair@286: *
ohair@286: * You should have received a copy of the GNU General Public License version
ohair@286: * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286: *
ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286: * or visit www.oracle.com if you need additional information or have any
ohair@286: * questions.
ohair@286: */
ohair@286:
ohair@286: package com.sun.xml.internal.bind.v2.runtime.reflect.opt;
ohair@286:
ohair@286: import java.lang.reflect.InvocationTargetException;
ohair@286: import java.lang.reflect.Method;
ohair@286: import java.lang.ref.WeakReference;
ohair@286: import java.security.AccessController;
ohair@286: import java.security.PrivilegedAction;
ohair@286: import java.util.concurrent.locks.Lock;
ohair@286: import java.util.concurrent.locks.ReentrantReadWriteLock;
ohair@286: import java.util.HashMap;
ohair@286: import java.util.Map;
ohair@286: import java.util.WeakHashMap;
ohair@286: import java.util.logging.Level;
ohair@286: import java.util.logging.Logger;
ohair@286:
ohair@286: import com.sun.xml.internal.bind.Util;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
ohair@286:
ohair@286: /**
ohair@286: * A {@link ClassLoader} used to "inject" optimized accessor classes
ohair@286: * into the VM.
ohair@286: *
ohair@286: *
ohair@286: * Its parent class loader needs to be set to the one that can see the user
ohair@286: * class.
ohair@286: *
ohair@286: * @author Kohsuke Kawaguchi
ohair@286: */
ohair@286: final class Injector {
ohair@286:
ohair@286: /**
ohair@286: * {@link Injector}s keyed by their parent {@link ClassLoader}.
ohair@286: *
ohair@286: * We only need one injector per one user class loader.
ohair@286: */
ohair@286: private static final ReentrantReadWriteLock irwl = new ReentrantReadWriteLock();
ohair@286: private static final Lock ir = irwl.readLock();
ohair@286: private static final Lock iw = irwl.writeLock();
ohair@286: private static final Map> injectors =
ohair@286: new WeakHashMap>();
ohair@286: private static final Logger logger = Util.getClassLogger();
ohair@286:
ohair@286: /**
ohair@286: * Injects a new class into the given class loader.
ohair@286: *
ohair@286: * @return null
ohair@286: * if it fails to inject.
ohair@286: */
ohair@286: static Class inject(ClassLoader cl, String className, byte[] image) {
ohair@286: Injector injector = get(cl);
ohair@286: if (injector != null) {
ohair@286: return injector.inject(className, image);
ohair@286: } else {
ohair@286: return null;
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Returns the already injected class, or null.
ohair@286: */
ohair@286: static Class find(ClassLoader cl, String className) {
ohair@286: Injector injector = get(cl);
ohair@286: if (injector != null) {
ohair@286: return injector.find(className);
ohair@286: } else {
ohair@286: return null;
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Gets or creates an {@link Injector} for the given class loader.
ohair@286: *
ohair@286: * @return null
ohair@286: * if it fails.
ohair@286: */
ohair@286: private static Injector get(ClassLoader cl) {
ohair@286: Injector injector = null;
ohair@286: WeakReference wr;
ohair@286: ir.lock();
ohair@286: try {
ohair@286: wr = injectors.get(cl);
ohair@286: } finally {
ohair@286: ir.unlock();
ohair@286: }
ohair@286: if (wr != null) {
ohair@286: injector = wr.get();
ohair@286: }
ohair@286: if (injector == null) {
ohair@286: try {
ohair@286: wr = new WeakReference(injector = new Injector(cl));
ohair@286: iw.lock();
ohair@286: try {
ohair@286: if (!injectors.containsKey(cl)) {
ohair@286: injectors.put(cl, wr);
ohair@286: }
ohair@286: } finally {
ohair@286: iw.unlock();
ohair@286: }
ohair@286: } catch (SecurityException e) {
ohair@286: logger.log(Level.FINE, "Unable to set up a back-door for the injector", e);
ohair@286: return null;
ohair@286: }
ohair@286: }
ohair@286: return injector;
ohair@286: }
ohair@286: /**
ohair@286: * Injected classes keyed by their names.
ohair@286: */
ohair@286: private final Map classes = new HashMap();
ohair@286: private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
ohair@286: private final Lock r = rwl.readLock();
ohair@286: private final Lock w = rwl.writeLock();
ohair@286: private final ClassLoader parent;
ohair@286: /**
ohair@286: * True if this injector is capable of injecting accessors.
ohair@286: * False otherwise, which happens if this classloader can't see {@link Accessor}.
ohair@286: */
ohair@286: private final boolean loadable;
ohair@286: private static final Method defineClass;
ohair@286: private static final Method resolveClass;
ohair@286: private static final Method findLoadedClass;
ohair@286:
ohair@286: static {
ohair@286: try {
ohair@286: defineClass = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, Integer.TYPE, Integer.TYPE);
ohair@286: resolveClass = ClassLoader.class.getDeclaredMethod("resolveClass", Class.class);
ohair@286: findLoadedClass = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
ohair@286: } catch (NoSuchMethodException e) {
ohair@286: // impossible
ohair@286: throw new NoSuchMethodError(e.getMessage());
ohair@286: }
ohair@286: AccessController.doPrivileged(new PrivilegedAction() {
ohair@286:
mkos@408: @Override
ohair@286: public Void run() {
ohair@286: // TODO: check security implication
ohair@286: // do these setAccessible allow anyone to call these methods freely?s
ohair@286: defineClass.setAccessible(true);
ohair@286: resolveClass.setAccessible(true);
ohair@286: findLoadedClass.setAccessible(true);
ohair@286: return null;
ohair@286: }
ohair@286: });
ohair@286: }
ohair@286:
ohair@286: private Injector(ClassLoader parent) {
ohair@286: this.parent = parent;
ohair@286: assert parent != null;
ohair@286:
ohair@286: boolean loadableCheck = false;
ohair@286:
ohair@286: try {
ohair@286: loadableCheck = parent.loadClass(Accessor.class.getName()) == Accessor.class;
ohair@286: } catch (ClassNotFoundException e) {
ohair@286: // not loadable
ohair@286: }
ohair@286:
ohair@286: this.loadable = loadableCheck;
ohair@286: }
ohair@286:
ohair@286: @SuppressWarnings("LockAcquiredButNotSafelyReleased")
ohair@286: private Class inject(String className, byte[] image) {
ohair@286: if (!loadable) // this injector cannot inject anything
ohair@286: {
ohair@286: return null;
ohair@286: }
ohair@286:
ohair@286: boolean wlocked = false;
ohair@286: boolean rlocked = false;
ohair@286: try {
ohair@286:
ohair@286: r.lock();
ohair@286: rlocked = true;
ohair@286:
ohair@286: Class c = classes.get(className);
ohair@286:
ohair@286: // Unlock now during the findLoadedClass process to avoid
ohair@286: // deadlocks
ohair@286: r.unlock();
ohair@286: rlocked = false;
ohair@286:
ohair@286: //find loaded class from classloader
ohair@286: if (c == null) {
ohair@286:
ohair@286: try {
ohair@286: c = (Class) findLoadedClass.invoke(parent, className.replace('/', '.'));
ohair@286: } catch (IllegalArgumentException e) {
ohair@286: logger.log(Level.FINE, "Unable to find " + className, e);
ohair@286: } catch (IllegalAccessException e) {
ohair@286: logger.log(Level.FINE, "Unable to find " + className, e);
ohair@286: } catch (InvocationTargetException e) {
ohair@286: Throwable t = e.getTargetException();
ohair@286: logger.log(Level.FINE, "Unable to find " + className, t);
ohair@286: }
ohair@286:
ohair@286: if (c != null) {
ohair@286:
ohair@286: w.lock();
ohair@286: wlocked = true;
ohair@286:
ohair@286: classes.put(className, c);
ohair@286:
ohair@286: w.unlock();
ohair@286: wlocked = false;
ohair@286:
ohair@286: return c;
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: if (c == null) {
ohair@286:
ohair@286: r.lock();
ohair@286: rlocked = true;
ohair@286:
ohair@286: c = classes.get(className);
ohair@286:
ohair@286: // Unlock now during the define/resolve process to avoid
ohair@286: // deadlocks
ohair@286: r.unlock();
ohair@286: rlocked = false;
ohair@286:
ohair@286: if (c == null) {
ohair@286:
ohair@286: // we need to inject a class into the
ohair@286: try {
ohair@286: c = (Class) defineClass.invoke(parent, className.replace('/', '.'), image, 0, image.length);
ohair@286: resolveClass.invoke(parent, c);
ohair@286: } catch (IllegalAccessException e) {
ohair@286: logger.log(Level.FINE, "Unable to inject " + className, e);
ohair@286: return null;
ohair@286: } catch (InvocationTargetException e) {
ohair@286: Throwable t = e.getTargetException();
ohair@286: if (t instanceof LinkageError) {
ohair@286: logger.log(Level.FINE, "duplicate class definition bug occured? Please report this : " + className, t);
ohair@286: } else {
ohair@286: logger.log(Level.FINE, "Unable to inject " + className, t);
ohair@286: }
ohair@286: return null;
ohair@286: } catch (SecurityException e) {
ohair@286: logger.log(Level.FINE, "Unable to inject " + className, e);
ohair@286: return null;
ohair@286: } catch (LinkageError e) {
ohair@286: logger.log(Level.FINE, "Unable to inject " + className, e);
ohair@286: return null;
ohair@286: }
ohair@286:
ohair@286: w.lock();
ohair@286: wlocked = true;
ohair@286:
ohair@286: // During the time we were unlocked, we could have tried to
ohair@286: // load the class from more than one thread. Check now to see
ohair@286: // if someone else beat us to registering this class
ohair@286: if (!classes.containsKey(className)) {
ohair@286: classes.put(className, c);
ohair@286: }
ohair@286:
ohair@286: w.unlock();
ohair@286: wlocked = false;
ohair@286: }
ohair@286: }
ohair@286: return c;
ohair@286: } finally {
ohair@286: if (rlocked) {
ohair@286: r.unlock();
ohair@286: }
ohair@286: if (wlocked) {
ohair@286: w.unlock();
ohair@286: }
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: private Class find(String className) {
ohair@286: r.lock();
ohair@286: try {
ohair@286: return classes.get(className);
ohair@286: } finally {
ohair@286: r.unlock();
ohair@286: }
ohair@286: }
ohair@286: }