KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > client > ClientEnhancer


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: ClientEnhancer.java 489 2006-05-22 15:48:37Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.client;
26
27 import static org.objectweb.easybeans.enhancer.injection.InjectionClassAdapter.JAVA_LANG_OBJECT;
28
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.objectweb.asm.ClassReader;
35 import org.objectweb.asm.ClassWriter;
36 import org.objectweb.easybeans.deployment.annotations.analyzer.ScanClassVisitor;
37 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
38 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
39 import org.objectweb.easybeans.enhancer.Enhancer;
40 import org.objectweb.easybeans.enhancer.EnhancerException;
41 import org.objectweb.easybeans.enhancer.client.ClientLifeCycleAdapter;
42 import org.objectweb.easybeans.enhancer.injection.InjectionClassAdapter;
43 import org.objectweb.easybeans.loader.EasyBeansClassLoader;
44 import org.objectweb.easybeans.log.JLog;
45 import org.objectweb.easybeans.log.JLogFactory;
46
47 /**
48  * /**
49  * This enhancer is used by the client container to enhance client classes.
50  * For example, it will allow to use @EJB annotations, etc.
51  * @author Florent Benoit
52  */

53 public class ClientEnhancer extends Enhancer {
54
55     /**
56      * Logger.
57      */

58     private static JLog logger = JLogFactory.getLog(ClientEnhancer.class);
59
60     /**
61      * Creates an new enhancer.
62      * @param loader classloader where to define enhanced classes.
63      * @param ejbJarAnnotationMetadata object with references to the metadata.
64      * @param map a map allowing to give some objects to the enhancer.
65      */

66     public ClientEnhancer(final ClassLoader JavaDoc loader, final EjbJarAnnotationMetadata ejbJarAnnotationMetadata,
67             final Map JavaDoc<String JavaDoc, Object JavaDoc> map) {
68         super(loader, ejbJarAnnotationMetadata, map);
69     }
70
71     /**
72      * Allow to enhance a given set of classes.
73      * @param loader the classloader that will be used to load the classes.
74      * @param classesToEnhance the set of classes to enhance
75      * @param map a map allowing to give some objects to the enhancer.
76      * @throws EnhancerException if enhancer fails
77      */

78     public static void enhance(final ClassLoader JavaDoc loader, final List JavaDoc<String JavaDoc> classesToEnhance,
79             final Map JavaDoc<String JavaDoc, Object JavaDoc> map) throws EnhancerException {
80         EjbJarAnnotationMetadata ejbJarAnnotationMetadata = new EjbJarAnnotationMetadata();
81         ScanClassVisitor scanVisitor = new ScanClassVisitor(ejbJarAnnotationMetadata);
82         logger.info("ClassLoader used = {0}", loader);
83         for (String JavaDoc clazz : classesToEnhance) {
84             read(clazz, loader, scanVisitor, ejbJarAnnotationMetadata);
85         }
86
87         ClientEnhancer clientEnhancer = new ClientEnhancer(loader, ejbJarAnnotationMetadata, map);
88         clientEnhancer.enhance();
89     }
90
91
92     /**
93      * Visits the given class and all available parent classes.
94      * @param className the class to visit.
95      * @param loader the classloader to use.
96      * @param scanVisitor the ASM visitor.
97      * @param ejbJarAnnotationMetadata the structure containing class metadata
98      * @throws EnhancerException if class can't be analyzed.
99      */

100     private static void read(final String JavaDoc className, final ClassLoader JavaDoc loader, final ScanClassVisitor scanVisitor,
101             final EjbJarAnnotationMetadata ejbJarAnnotationMetadata) throws EnhancerException {
102         String JavaDoc readingClass = className;
103         if (!className.toLowerCase().endsWith(".class")) {
104             readingClass = className + ".class";
105         }
106
107         InputStream JavaDoc is = loader.getResourceAsStream(readingClass);
108
109         logger.info("Visiting class {0}", className);
110         try {
111             new ClassReader(is).accept(scanVisitor, false);
112         } catch (IOException JavaDoc e) {
113             throw new EnhancerException("Cannot read the given class '" + className + "'.", e);
114         }
115
116         // get the class parsed
117
ClassAnnotationMetadata classMetadata = ejbJarAnnotationMetadata.getClassAnnotationMetadata(className);
118         String JavaDoc superClassName = classMetadata.getSuperName();
119         if (!superClassName.equals(JAVA_LANG_OBJECT)) {
120             read(superClassName, loader, scanVisitor, ejbJarAnnotationMetadata);
121         }
122         try {
123             is.close();
124         } catch (IOException JavaDoc e) {
125             throw new EnhancerException("Cannot close the input stream for class '" + className + "'.", e);
126         }
127
128     }
129
130     /**
131      * Override the super class method by using only the injection adapter.
132      * @throws EnhancerException if the class can't be enhanced.
133      */

134     @Override JavaDoc
135     public void enhance() throws EnhancerException {
136         for (ClassAnnotationMetadata classAnnotationMetadata : getEjbJarAnnotationMetadata()
137                 .getClassAnnotationMetadataCollection()) {
138             ClassReader cr = getClassReader(classAnnotationMetadata);
139             ClassWriter cw = new ClassWriter(true);
140             InjectionClassAdapter injectionClassAdapter = new InjectionClassAdapter(classAnnotationMetadata, cw,
141                     getMap(), true);
142             ClientLifeCycleAdapter clientLifeCycleAdapter = new ClientLifeCycleAdapter(classAnnotationMetadata,
143                     injectionClassAdapter);
144             cr.accept(clientLifeCycleAdapter, false);
145
146             ((EasyBeansClassLoader) getClassLoader()).addClassDefinition(classAnnotationMetadata.getClassName()
147                     .replace("/", "."), cw.toByteArray());
148         }
149     }
150
151 }
152
Popular Tags