KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployment > xml > helper > MetadataMerge


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: MetadataMerge.java 537 2006-05-29 17:05:14Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployment.xml.helper;
27
28 import static org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback.POST_CONSTRUCT;
29 import static org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback.PRE_DESTROY;
30
31 import java.util.List JavaDoc;
32
33 import org.objectweb.easybeans.deployment.annotations.ClassType;
34 import org.objectweb.easybeans.deployment.annotations.impl.JCommonBean;
35 import org.objectweb.easybeans.deployment.annotations.impl.JInterceptors;
36 import org.objectweb.easybeans.deployment.annotations.impl.JLocal;
37 import org.objectweb.easybeans.deployment.annotations.impl.JRemote;
38 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
39 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
40 import org.objectweb.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata;
41 import org.objectweb.easybeans.deployment.xml.struct.AbsBean;
42 import org.objectweb.easybeans.deployment.xml.struct.EJB3;
43 import org.objectweb.easybeans.deployment.xml.struct.EnterpriseBeans;
44 import org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback;
45 import org.objectweb.easybeans.deployment.xml.struct.Session;
46
47 /**
48  * This class merge the information of the struct representing EJB DD into the
49  * metadata.
50  * @author Florent Benoit
51  */

52 public final class MetadataMerge {
53
54     /**
55      * Metadata for an ejb-jar.
56      */

57     private EjbJarAnnotationMetadata ejbMetadata = null;
58
59     /**
60      * Helper class, no public constructor.
61      * @param ejbMetadata the metadata corresponding to an EJB-JAR file.
62      */

63     private MetadataMerge(final EjbJarAnnotationMetadata ejbMetadata) {
64         this.ejbMetadata = ejbMetadata;
65     }
66
67     /**
68      * Takes struct of metadata and adds/set information on class/methods
69      * metadata.
70      * @param ejbMetadata the metadata corresponding to an EJB-JAR file.
71      */

72     public static void merge(final EjbJarAnnotationMetadata ejbMetadata) {
73         new MetadataMerge(ejbMetadata).resolve();
74     }
75
76     /**
77      * Do all merging operations.
78      */

79     private void resolve() {
80
81         EJB3 ejb3 = ejbMetadata.getEjb3();
82         if (ejb3 != null) {
83             // Get enterprise beans
84
EnterpriseBeans enterpriseBeans = ejb3.getEnterpriseBeans();
85             if (enterpriseBeans != null) {
86
87                 // get session beans
88
List JavaDoc<Session> sessionList = enterpriseBeans.getSessionList();
89                 for (Session session : sessionList) {
90                     // get ejb-name
91
String JavaDoc ejbName = session.getEjbName();
92                     String JavaDoc ejbClass = session.getEjbClass();
93                     ClassAnnotationMetadata classAnnotationMetadata = findClassForEjb(ejbName, ejbClass);
94
95                     // class is found, then apply all customization.
96
applySessionBean(session, classAnnotationMetadata);
97                 }
98             }
99         }
100     }
101
102     /**
103      * Apply rules for a session bean.
104      * @param sessionbean the struct (XML DD)
105      * @param classAnnotationMetadata (Annotation metadata)
106      */

107     private void applySessionBean(final Session sessionbean, final ClassAnnotationMetadata classAnnotationMetadata) {
108         applyCommonBean(sessionbean, classAnnotationMetadata);
109
110         // set session-type
111
String JavaDoc sessionType = sessionbean.getSessionType();
112         if (sessionType == null && !classAnnotationMetadata.isSession()) {
113             throw new IllegalStateException JavaDoc("Missing session-type for bean '" + sessionbean
114                     + "' and no annotation in '" + classAnnotationMetadata + "'.");
115         }
116         // set the type of bean
117
if ("Stateless".equals(sessionType)) {
118             classAnnotationMetadata.setClassType(ClassType.STATELESS);
119         } else if ("Stateful".equals(sessionType)) {
120             classAnnotationMetadata.setClassType(ClassType.STATEFUL);
121         }
122
123         // local interface
124
if (sessionbean.getBusinessLocalList().size() > 0) {
125             JLocal jLocal = classAnnotationMetadata.getLocalInterfaces();
126             if (jLocal == null) {
127                 jLocal = new JLocal();
128                 classAnnotationMetadata.setLocalInterfaces(jLocal);
129             }
130             for (String JavaDoc itf : sessionbean.getBusinessLocalList()) {
131                 String JavaDoc encodedItf = encode(itf);
132                 if (!jLocal.contains(encodedItf)) {
133                     jLocal.addInterface(encodedItf);
134                 }
135             }
136         }
137
138         // remote interface
139
if (sessionbean.getBusinessRemoteList().size() > 0) {
140             JRemote jRemote = classAnnotationMetadata.getRemoteInterfaces();
141             if (jRemote == null) {
142                 jRemote = new JRemote();
143                 classAnnotationMetadata.setRemoteInterfaces(jRemote);
144             }
145             for (String JavaDoc itf : sessionbean.getBusinessRemoteList()) {
146                 String JavaDoc encodedItf = encode(itf);
147                 if (!jRemote.contains(encodedItf)) {
148                     jRemote.addInterface(encodedItf);
149                 }
150             }
151         }
152     }
153
154     /**
155      * Apply rules for a common bean.
156      * @param bean the struct (XML DD)
157      * @param classAnnotationMetadata (Annotation metadata)
158      */

159     private void applyCommonBean(final AbsBean bean, final ClassAnnotationMetadata classAnnotationMetadata) {
160         // no Common bean, create one.
161
if (classAnnotationMetadata.getJCommonBean() == null) {
162             classAnnotationMetadata.setJCommonBean(new JCommonBean());
163         }
164         // Sets the ejb-name
165
JCommonBean jcommonBean = classAnnotationMetadata.getJCommonBean();
166         jcommonBean.setName(bean.getEjbName());
167
168         // TODO: ordering ?
169
// post-construct / pre-destroy
170
applyLifeCycle(bean, bean.getPostConstructList(), classAnnotationMetadata, POST_CONSTRUCT);
171         applyLifeCycle(bean, bean.getPreDestroyList(), classAnnotationMetadata, PRE_DESTROY);
172
173     }
174
175     /**
176      * Apply rules for a common bean (lifecycle callbacks).
177      * @param bean the struct (XML DD)
178      * @param lifecycleList the list of lifecycle callbacks.
179      * @param classAnnotationMetadata (Annotation metadata).
180      * @param type the type of lifecycle.
181      */

182     private void applyLifeCycle(final AbsBean bean, final List JavaDoc<LifeCycleCallback> lifecycleList,
183             final ClassAnnotationMetadata classAnnotationMetadata, final String JavaDoc type) {
184         for (LifeCycleCallback lifecycleCallback : lifecycleList) {
185
186             String JavaDoc encodedClassName = null;
187
188             // if no class, takes the bean's class
189
if (lifecycleCallback.getClassName() == null) {
190                 encodedClassName = classAnnotationMetadata.getClassName();
191             } else {
192                 encodedClassName = encode(lifecycleCallback.getClassName());
193             }
194             JInterceptors interceptors = classAnnotationMetadata.getAnnotationInterceptors();
195             if (interceptors == null) {
196                 interceptors = new JInterceptors();
197                 classAnnotationMetadata.setAnnotationsInterceptors(interceptors);
198             }
199             if (!interceptors.contains(encodedClassName)) {
200                 interceptors.addClass(encodedClassName);
201             }
202             // add LifeCycle on the metadata class
203
ClassAnnotationMetadata lifeCycleMetadata = ejbMetadata.getClassAnnotationMetadata(encodedClassName);
204             if (lifeCycleMetadata == null) {
205                 throw new IllegalStateException JavaDoc("Cannot find metadata for class '" + encodedClassName
206                         + "' which is in a lifecycle.");
207             }
208
209             // get method and call setter to true
210
for (MethodAnnotationMetadata methodMetadata : lifeCycleMetadata.getMethodAnnotationMetadataCollection()) {
211                 if (methodMetadata.getMethodName().equals(lifecycleCallback.getMethod())) {
212                     if (POST_CONSTRUCT.equals(type)) {
213                         methodMetadata.setPostConstruct(true);
214                         lifeCycleMetadata.addPostConstructMethodMetadata(methodMetadata);
215                     } else if (PRE_DESTROY.equals(type)) {
216                         methodMetadata.setPreDestroy(true);
217                         lifeCycleMetadata.addPreDestroyMethodMetadata(methodMetadata);
218                     } else {
219                         throw new IllegalStateException JavaDoc("No case for type '" + type + "'..");
220                     }
221                 }
222             }
223         }
224     }
225
226     /**
227      * Find/Gets a classAnnotationMetadata for a given ejb name and/or a given
228      * ejbclass name.
229      * @param ejbName the name of the EJB.
230      * @param ejbClass the class of the EJB.
231      * @return a classAnnotationMetadata if found, else exception
232      */

233     private ClassAnnotationMetadata findClassForEjb(final String JavaDoc ejbName, final String JavaDoc ejbClass) {
234         ClassAnnotationMetadata classAnnotationMetadata = null;
235
236         // First, try with ejbClass if not null
237
if (ejbClass != null) {
238             classAnnotationMetadata = ejbMetadata.getClassAnnotationMetadata(encode(ejbClass));
239             // found ? return it
240
if (classAnnotationMetadata != null) {
241                 return classAnnotationMetadata;
242             }
243         }
244
245         // else, needs to find a class with the specified name
246
for (ClassAnnotationMetadata tmpMetaData : ejbMetadata.getClassAnnotationMetadataCollection()) {
247             if (tmpMetaData.isBean()) {
248                 // check name
249
JCommonBean bean = tmpMetaData.getJCommonBean();
250                 if (bean != null) {
251                     // found !
252
if (ejbName != null && ejbName.equals(bean.getName())) {
253                         return tmpMetaData;
254                     }
255                 }
256             }
257         }
258         throw new IllegalStateException JavaDoc("No class with ejb-name '" + ejbName + "' or ejb-class '" + ejbClass
259                 + "' was found in the EJB-JAR file '" + ejbMetadata);
260     }
261
262     /**
263      * Encode a classname (replace . by /) as it's the internal form of the
264      * metadata
265      * @param className the name of the class to encode
266      * @return encoded name
267      */

268     private static String JavaDoc encode(final String JavaDoc className) {
269         return className.replace(".", "/");
270     }
271 }
272
Popular Tags