001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime.reflect.opt;
038:
039: import java.lang.reflect.InvocationTargetException;
040: import java.lang.reflect.Method;
041: import java.lang.ref.WeakReference;
042: import java.security.AccessController;
043: import java.security.PrivilegedAction;
044: import java.util.Collections;
045: import java.util.HashMap;
046: import java.util.Map;
047: import java.util.WeakHashMap;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050:
051: import com.sun.xml.bind.Util;
052: import com.sun.xml.bind.v2.runtime.reflect.Accessor;
053:
054: /**
055: * A {@link ClassLoader} used to "inject" optimized accessor classes
056: * into the VM.
057: *
058: * <p>
059: * Its parent class loader needs to be set to the one that can see the user
060: * class.
061: *
062: * @author Kohsuke Kawaguchi
063: */
064: final class Injector {
065:
066: /**
067: * {@link Injector}s keyed by their parent {@link ClassLoader}.
068: *
069: * We only need one injector per one user class loader.
070: */
071: private static final Map<ClassLoader, WeakReference<Injector>> injectors = Collections
072: .synchronizedMap(new WeakHashMap<ClassLoader, WeakReference<Injector>>());
073:
074: private static final Logger logger = Util.getClassLogger();
075:
076: /**
077: * Injects a new class into the given class loader.
078: *
079: * @return null
080: * if it fails to inject.
081: */
082: static Class inject(ClassLoader cl, String className, byte[] image) {
083: Injector injector = get(cl);
084: if (injector != null)
085: return injector.inject(className, image);
086: else
087: return null;
088: }
089:
090: /**
091: * Returns the already injected class, or null.
092: */
093: static Class find(ClassLoader cl, String className) {
094: Injector injector = get(cl);
095: if (injector != null)
096: return injector.find(className);
097: else
098: return null;
099: }
100:
101: /**
102: * Gets or creates an {@link Injector} for the given class loader.
103: *
104: * @return null
105: * if it fails.
106: */
107: private static Injector get(ClassLoader cl) {
108: Injector injector = null;
109: WeakReference<Injector> wr = injectors.get(cl);
110: if (wr != null)
111: injector = wr.get();
112: if (injector == null)
113: try {
114: injectors.put(cl, new WeakReference<Injector>(
115: injector = new Injector(cl)));
116: } catch (SecurityException e) {
117: logger
118: .log(
119: Level.FINE,
120: "Unable to set up a back-door for the injector",
121: e);
122: return null;
123: }
124: return injector;
125: }
126:
127: /**
128: * Injected classes keyed by their names.
129: */
130: private final Map<String, Class> classes = new HashMap<String, Class>();
131:
132: private final ClassLoader parent;
133:
134: /**
135: * True if this injector is capable of injecting accessors.
136: * False otherwise, which happens if this classloader can't see {@link Accessor}.
137: */
138: private final boolean loadable;
139:
140: private static final Method defineClass;
141: private static final Method resolveClass;
142:
143: static {
144: try {
145: defineClass = ClassLoader.class.getDeclaredMethod(
146: "defineClass", String.class, byte[].class,
147: Integer.TYPE, Integer.TYPE);
148: resolveClass = ClassLoader.class.getDeclaredMethod(
149: "resolveClass", Class.class);
150: } catch (NoSuchMethodException e) {
151: // impossible
152: throw new NoSuchMethodError(e.getMessage());
153: }
154: AccessController.doPrivileged(new PrivilegedAction<Void>() {
155: public Void run() {
156: // TODO: check security implication
157: // do these setAccessible allow anyone to call these methods freely?s
158: defineClass.setAccessible(true);
159: resolveClass.setAccessible(true);
160: return null;
161: }
162: });
163: }
164:
165: private Injector(ClassLoader parent) {
166: this .parent = parent;
167: assert parent != null;
168:
169: boolean loadable = false;
170:
171: try {
172: loadable = parent.loadClass(Accessor.class.getName()) == Accessor.class;
173: } catch (ClassNotFoundException e) {
174: ; // not loadable
175: }
176:
177: this .loadable = loadable;
178: }
179:
180: private synchronized Class inject(String className, byte[] image) {
181: if (!loadable) // this injector cannot inject anything
182: return null;
183:
184: Class c = classes.get(className);
185: if (c == null) {
186: // we need to inject a class into the
187: try {
188: c = (Class) defineClass.invoke(parent, className
189: .replace('/', '.'), image, 0, image.length);
190: resolveClass.invoke(parent, c);
191: } catch (IllegalAccessException e) {
192: logger.log(Level.FINE, "Unable to inject " + className,
193: e);
194: return null;
195: } catch (InvocationTargetException e) {
196: logger.log(Level.FINE, "Unable to inject " + className,
197: e);
198: return null;
199: } catch (SecurityException e) {
200: logger.log(Level.FINE, "Unable to inject " + className,
201: e);
202: return null;
203: } catch (LinkageError e) {
204: logger.log(Level.FINE, "Unable to inject " + className,
205: e);
206: return null;
207: }
208: classes.put(className, c);
209: }
210: return c;
211: }
212:
213: private synchronized Class find(String className) {
214: return classes.get(className);
215: }
216: }
|