001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.instrument.classloading.glassfish;
018:
019: import java.lang.instrument.ClassFileTransformer;
020:
021: import com.sun.enterprise.loader.InstrumentableClassLoader;
022:
023: import org.springframework.instrument.classloading.LoadTimeWeaver;
024: import org.springframework.util.Assert;
025: import org.springframework.util.ClassUtils;
026:
027: /**
028: * {@link LoadTimeWeaver} implementation for GlassFish's
029: * {@link InstrumentableClassLoader}.
030: *
031: * @author Costin Leau
032: * @author Juergen Hoeller
033: * @since 2.0.1
034: * @see com.sun.enterprise.loader.InstrumentableClassLoader
035: */
036: public class GlassFishLoadTimeWeaver implements LoadTimeWeaver {
037:
038: private final InstrumentableClassLoader classLoader;
039:
040: /**
041: * Create a new instance of the <code>GlassFishLoadTimeWeaver</code> class
042: * using the default {@link ClassLoader}.
043: * @see #GlassFishLoadTimeWeaver(ClassLoader)
044: */
045: public GlassFishLoadTimeWeaver() {
046: this (ClassUtils.getDefaultClassLoader());
047: }
048:
049: /**
050: * Create a new instance of the <code>GlassFishLoadTimeWeaver</code> class.
051: * @param classLoader the specific {@link ClassLoader} to use; must not be <code>null</code>
052: * @throws IllegalArgumentException if the supplied <code>classLoader</code> is <code>null</code>;
053: * or if the supplied <code>classLoader</code> is not an {@link InstrumentableClassLoader}
054: */
055: public GlassFishLoadTimeWeaver(ClassLoader classLoader) {
056: Assert.notNull(classLoader, "ClassLoader must not be null");
057: InstrumentableClassLoader icl = determineClassLoader(classLoader);
058: if (icl == null) {
059: throw new IllegalArgumentException(
060: classLoader
061: + " and its parents are not suitable ClassLoaders: "
062: + "An ["
063: + InstrumentableClassLoader.class.getName()
064: + "] implementation is required.");
065: }
066: this .classLoader = icl;
067: }
068:
069: /**
070: * Determine the GlassFish {@link InstrumentableClassLoader} for the given
071: * {@link ClassLoader}.
072: * @param classLoader the <code>ClassLoader</code> to check
073: * @return the <code>InstrumentableClassLoader</code>, or <code>null</code> if none found
074: */
075: protected InstrumentableClassLoader determineClassLoader(
076: ClassLoader classLoader) {
077: // Detect transformation-aware ClassLoader by traversing the hierarchy
078: // (as in GlassFish, Spring can be loaded by the WebappClassLoader).
079: for (ClassLoader cl = classLoader; cl != null; cl = cl
080: .getParent()) {
081: if (cl instanceof InstrumentableClassLoader) {
082: return (InstrumentableClassLoader) cl;
083: }
084: }
085: return null;
086: }
087:
088: public void addTransformer(ClassFileTransformer transformer) {
089: this .classLoader.addTransformer(new ClassTransformerAdapter(
090: transformer));
091: }
092:
093: public ClassLoader getInstrumentableClassLoader() {
094: return (ClassLoader) this .classLoader;
095: }
096:
097: public ClassLoader getThrowawayClassLoader() {
098: return this.classLoader.copy();
099: }
100:
101: }
|