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.io.InputStream;
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042:
043: import com.sun.xml.bind.Util;
044: import com.sun.xml.bind.v2.bytecode.ClassTailor;
045:
046: /**
047: * @author Kohsuke Kawaguchi
048: */
049: class AccessorInjector {
050:
051: private static final Logger logger = Util.getClassLogger();
052:
053: protected static final boolean noOptimize = Util
054: .getSystemProperty(ClassTailor.class.getName()
055: + ".noOptimize") != null;
056:
057: static {
058: if (noOptimize)
059: logger.info("The optimized code generation is disabled");
060: }
061:
062: /**
063: * Loads the optimized class and returns it.
064: *
065: * @return null
066: * if it fails for some reason.
067: */
068: public static Class<?> prepare(Class beanClass,
069: String templateClassName, String newClassName,
070: String... replacements) {
071:
072: if (noOptimize)
073: return null;
074:
075: try {
076: ClassLoader cl = beanClass.getClassLoader();
077: if (cl == null)
078: return null; // how do I inject classes to this "null" class loader? for now, back off.
079:
080: Class c = Injector.find(cl, newClassName);
081: if (c == null) {
082: byte[] image = tailor(templateClassName, newClassName,
083: replacements);
084: // try {
085: // new FileOutputStream("debug.class").write(image);
086: // } catch (IOException e) {
087: // e.printStackTrace();
088: // }
089: if (image == null)
090: return null;
091: c = Injector.inject(cl, newClassName, image);
092: }
093: return c;
094: } catch (SecurityException e) {
095: // we don't have enough permission to do this
096: logger
097: .log(
098: Level.INFO,
099: "Unable to create an optimized TransducedAccessor ",
100: e);
101: return null;
102: }
103: }
104:
105: /**
106: * Customizes a class file by replacing constant pools.
107: *
108: * @param templateClassName
109: * The resouce that contains the template class file.
110: * @param replacements
111: * A list of pair of strings that specify the substitution
112: * {@code String[]{search_0, replace_0, search_1, replace_1, ..., search_n, replace_n }
113: *
114: * The search strings found in the constant pool will be replaced by the corresponding
115: * replacement string.
116: */
117: private static byte[] tailor(String templateClassName,
118: String newClassName, String... replacements) {
119: InputStream resource;
120: if (CLASS_LOADER != null)
121: resource = CLASS_LOADER
122: .getResourceAsStream(templateClassName + ".class");
123: else
124: resource = ClassLoader
125: .getSystemResourceAsStream(templateClassName
126: + ".class");
127: if (resource == null)
128: return null;
129:
130: return ClassTailor.tailor(resource, templateClassName,
131: newClassName, replacements);
132: }
133:
134: private static final ClassLoader CLASS_LOADER = AccessorInjector.class
135: .getClassLoader();
136: }
|