KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > enhancer > ClassesEnhancer


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ClassesEnhancer.java 408 2006-04-25 14:56:11Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.enhancer;
26
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLClassLoader JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.objectweb.asm.ClassReader;
33 import org.objectweb.easybeans.deployment.annotations.analyzer.ScanClassVisitor;
34 import org.objectweb.easybeans.deployment.annotations.helper.ResolverHelper;
35 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
36 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
37 import org.objectweb.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata;
38 import org.objectweb.easybeans.enhancer.Enhancer;
39
40
41 /**
42  * Enhance a set of given classes.
43  * Rely on core enhancer.
44  * @author Florent Benoit
45  */

46 public final class ClassesEnhancer extends Enhancer {
47
48     /**
49      * Type available (for adding adapter).
50      */

51     public static enum TYPE {INTERCEPTOR, CALLBACK, ALL}
52
53     /**
54      * Class extension.
55      */

56     public static final String JavaDoc EXT_CLASS = ".class";
57
58     /**
59      * Creates an new enhancer.
60      * @param loader classloader where to define enhanced classes.
61      * @param ejbJarAnnotationMetadata object with references to the metadata.
62      */

63     public ClassesEnhancer(final ClassLoader JavaDoc loader, final EjbJarAnnotationMetadata ejbJarAnnotationMetadata) {
64         super(loader, ejbJarAnnotationMetadata, null);
65     }
66
67     /**
68      * Enhance the classes simulating a Bean. It uses a child classloder of the current thread.<br>
69      * @param classesToEnhance the list of class on which run class adapters
70      * @param type the type of adapter to run.
71      * @throws Exception if it fails
72      */

73     public static void enhanceNewClassLoader(final List JavaDoc<String JavaDoc> classesToEnhance, final TYPE type) throws Exception JavaDoc {
74         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
75         ClassLoader JavaDoc childLoader = new URLClassLoader JavaDoc(new URL JavaDoc[]{}, loader);
76         Thread.currentThread().setContextClassLoader(childLoader);
77         try{
78             enhance(classesToEnhance, TYPE.INTERCEPTOR);
79         }finally{
80             Thread.currentThread().setContextClassLoader(loader);
81         }
82     }
83
84     /**
85      * Enhance the classes simulating a Bean.<br>
86      * @param classesToEnhance the list of class on which run class adapters
87      * @param type the type of adapter to run.
88      * @throws Exception if it fails
89      */

90     public static void enhance(final List JavaDoc<String JavaDoc> classesToEnhance, final TYPE type) throws Exception JavaDoc {
91         EjbJarAnnotationMetadata ejbJarAnnotationMetadata = new EjbJarAnnotationMetadata();
92         ScanClassVisitor scanVisitor = new ScanClassVisitor(ejbJarAnnotationMetadata);
93         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
94
95         InputStream JavaDoc is = null;
96         for (String JavaDoc clazz : classesToEnhance) {
97             is = loader.getResourceAsStream(clazz);
98             new ClassReader(is).accept(scanVisitor, false);
99         }
100
101         ResolverHelper.resolve(ejbJarAnnotationMetadata);
102
103
104         // Remove some TX interceptors (so it works offline)
105
for (ClassAnnotationMetadata classAnnotationMetadata : ejbJarAnnotationMetadata
106                 .getClassAnnotationMetadataCollection()) {
107             if (classAnnotationMetadata.isBean()) {
108                 // Remove global EasyBeans interceptors
109
classAnnotationMetadata.setGlobalEasyBeansInterceptors(null);
110                 for (MethodAnnotationMetadata m : classAnnotationMetadata.getMethodAnnotationMetadataCollection()) {
111                     m.setInterceptors(null);
112                 }
113             }
114         }
115
116         ClassesEnhancer classesEnhancer = new ClassesEnhancer(loader, ejbJarAnnotationMetadata);
117         classesEnhancer.enhance();
118     }
119
120 }
121
Popular Tags