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