001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ClassesEnhancer.java 2057 2007-11-21 15:35:32Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.enhancer;
025:
026: import java.io.InputStream;
027: import java.net.URL;
028: import java.net.URLClassLoader;
029: import java.util.List;
030:
031: import org.ow2.easybeans.asm.ClassReader;
032: import org.ow2.easybeans.deployment.annotations.analyzer.ScanClassVisitor;
033: import org.ow2.easybeans.deployment.annotations.helper.ResolverHelper;
034: import org.ow2.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
035: import org.ow2.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
036: import org.ow2.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata;
037: import org.ow2.easybeans.enhancer.Enhancer;
038:
039: /**
040: * Enhance a set of given classes.
041: * Rely on core enhancer.
042: * @author Florent Benoit
043: */
044: public final class ClassesEnhancer extends Enhancer {
045:
046: /**
047: * Type available (for adding adapter).
048: */
049: public static enum TYPE {
050: INTERCEPTOR, CALLBACK, ALL
051: }
052:
053: /**
054: * Class extension.
055: */
056: public static final String EXT_CLASS = ".class";
057:
058: /**
059: * Creates an new enhancer.
060: * @param loader classloader where to define enhanced classes.
061: * @param ejbJarAnnotationMetadata object with references to the metadata.
062: */
063: public ClassesEnhancer(final ClassLoader loader,
064: final EjbJarAnnotationMetadata ejbJarAnnotationMetadata) {
065: super (loader, ejbJarAnnotationMetadata, null);
066: }
067:
068: /**
069: * Enhance the classes simulating a Bean. It uses a child classloder of the current thread.<br>
070: * @param classesToEnhance the list of class on which run class adapters
071: * @param type the type of adapter to run.
072: * @throws Exception if it fails
073: */
074: public static void enhanceNewClassLoader(
075: final List<String> classesToEnhance, final TYPE type)
076: throws Exception {
077: ClassLoader loader = Thread.currentThread()
078: .getContextClassLoader();
079: ClassLoader childLoader = new URLClassLoader(new URL[] {},
080: loader);
081: Thread.currentThread().setContextClassLoader(childLoader);
082: try {
083: enhance(classesToEnhance, TYPE.INTERCEPTOR);
084: } finally {
085: Thread.currentThread().setContextClassLoader(loader);
086: }
087: }
088:
089: /**
090: * Enhance the classes simulating a Bean.<br>
091: * @param classesToEnhance the list of class on which run class adapters
092: * @param type the type of adapter to run.
093: * @throws Exception if it fails
094: */
095: public static void enhance(final List<String> classesToEnhance,
096: final TYPE type) throws Exception {
097: EjbJarAnnotationMetadata ejbJarAnnotationMetadata = new EjbJarAnnotationMetadata();
098: ScanClassVisitor scanVisitor = new ScanClassVisitor(
099: ejbJarAnnotationMetadata);
100: ClassLoader loader = Thread.currentThread()
101: .getContextClassLoader();
102:
103: InputStream is = null;
104: for (String clazz : classesToEnhance) {
105: is = loader.getResourceAsStream(clazz);
106: new ClassReader(is).accept(scanVisitor, 0);
107: }
108:
109: ResolverHelper.resolve(ejbJarAnnotationMetadata);
110:
111: // Remove some TX interceptors (so it works offline)
112: for (ClassAnnotationMetadata classAnnotationMetadata : ejbJarAnnotationMetadata
113: .getClassAnnotationMetadataCollection()) {
114: if (classAnnotationMetadata.isBean()) {
115: // Remove global EasyBeans interceptors
116: classAnnotationMetadata
117: .setGlobalEasyBeansInterceptors(null);
118: for (MethodAnnotationMetadata m : classAnnotationMetadata
119: .getMethodAnnotationMetadataCollection()) {
120: m.setInterceptors(null);
121: }
122: }
123: }
124:
125: ClassesEnhancer classesEnhancer = new ClassesEnhancer(loader,
126: ejbJarAnnotationMetadata);
127: classesEnhancer.enhance();
128: }
129:
130: }
|