KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployment > annotations > helper > bean > session > SessionBeanInterface


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: SessionBeanInterface.java 847 2006-07-12 09:51:27Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployment.annotations.helper.bean.session;
27
28 import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
29 import static org.objectweb.easybeans.deployment.annotations.helper.bean.InheritanceInterfacesHelper.JAVA_LANG_OBJECT;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.objectweb.easybeans.deployment.annotations.JMethod;
35 import org.objectweb.easybeans.deployment.annotations.impl.JAnnotationResource;
36 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
37 import org.objectweb.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata;
38 /**
39  * This class analyze interfaces of the session bean. If the session bean
40  * implements javax.ejb.SessionBean interface, add lifecycle callbacks and add
41  * resource injection for setSessionContext method.
42  * @author Florent Benoit
43  */

44 public final class SessionBeanInterface {
45
46     /**
47      * SessionBean interface.
48      */

49     private static final String JavaDoc SESSION_BEAN_INTERFACE = "javax/ejb/SessionBean";
50
51
52     /**
53      * setSessionContext() method.
54      */

55     private static final JMethod SETSESSIONCONTEXT_METHOD = new JMethod(ACC_PUBLIC, "setSessionContext",
56             "(Ljavax/ejb/SessionContext;)V", null, new String JavaDoc[] {"javax/ejb/EJBException", "java/rmi/RemoteException"});
57
58
59     /**
60      * ejbRemove() method.
61      */

62     private static final JMethod EJBREMOVE_METHOD = new JMethod(ACC_PUBLIC, "ejbRemove",
63             "()V", null, new String JavaDoc[] {"javax/ejb/EJBException", "java/rmi/RemoteException"});
64
65     /**
66      * ejbActivate() method.
67      */

68     private static final JMethod EJBACTIVATE_METHOD = new JMethod(ACC_PUBLIC, "ejbActivate",
69             "()V", null, new String JavaDoc[] {"javax/ejb/EJBException", "java/rmi/RemoteException"});
70
71
72     /**
73      * ejbPassivate() method.
74      */

75     private static final JMethod EJBPASSIVATE_METHOD = new JMethod(ACC_PUBLIC, "ejbPassivate",
76             "()V", null, new String JavaDoc[] {"javax/ejb/EJBException", "java/rmi/RemoteException"});
77
78
79     /**
80      * Helper class, no public constructor.
81      */

82     private SessionBeanInterface() {
83     }
84
85     /**
86      * Try to see if bean implements javax.ejb.SessionBean interface.
87      * @param sessionBean Session bean to analyze
88      */

89     public static void resolve(final ClassAnnotationMetadata sessionBean) {
90         // Make a list of interfaces
91
List JavaDoc<String JavaDoc> allInterfaces = getAllInterfacesFromClass(sessionBean);
92
93         // if SESSION_BEAN_INTERFACE is contained in the list, add some metadata
94
if (allInterfaces.contains(SESSION_BEAN_INTERFACE)) {
95             // first add dependency injection for setSessionContext method.
96
JAnnotationResource jAnnotationResource = new JAnnotationResource();
97
98             // add resource on setSessionContext method
99
MethodAnnotationMetadata setCtxMethod = getMethod(sessionBean, SETSESSIONCONTEXT_METHOD, false);
100             setCtxMethod.setJAnnotationResource(jAnnotationResource);
101
102
103             // ejbRemove() method
104
MethodAnnotationMetadata ejbRemoveMethod = getMethod(sessionBean, EJBREMOVE_METHOD, true);
105             ejbRemoveMethod.setPreDestroy(true);
106             if (!sessionBean.getPreDestroyMethodsMetadata().contains(ejbRemoveMethod)) {
107                 sessionBean.addPreDestroyMethodMetadata(ejbRemoveMethod);
108             }
109
110             // ejbActivate() method
111
MethodAnnotationMetadata ejbActivateMethod = getMethod(sessionBean, EJBACTIVATE_METHOD, true);
112             ejbRemoveMethod.setPostActivate(true);
113             if (!sessionBean.getPostActivateMethodsMetadata().contains(ejbActivateMethod)) {
114                 sessionBean.addPostActivateMethodMetadata(ejbActivateMethod);
115             }
116
117             // ejbPassivate() method
118
MethodAnnotationMetadata ejbPassivateMethod = getMethod(sessionBean, EJBPASSIVATE_METHOD, true);
119             ejbRemoveMethod.setPrePassivate(true);
120             if (!sessionBean.getPrePassivateMethodsMetadata().contains(ejbPassivateMethod)) {
121                 sessionBean.addPrePassivateMethodMetadata(ejbPassivateMethod);
122             }
123
124         }
125
126
127     }
128
129     /**
130      * Gets method metadata on the given class metadata for the given method.
131      * @param sessionBean the class metadata on which retrieve the method
132      * @param jMethod the method to get
133      * @param inherited get the correct method in super class, not inherited
134      * @return the method metadata, else exception
135      */

136     private static MethodAnnotationMetadata getMethod(final ClassAnnotationMetadata sessionBean, final JMethod jMethod, final boolean inherited) {
137         MethodAnnotationMetadata method = sessionBean.getMethodAnnotationMetadata(jMethod);
138         if (method == null) {
139             throw new IllegalStateException JavaDoc("Bean '" + sessionBean + "' implements " + SESSION_BEAN_INTERFACE
140                     + " but no " + jMethod + " method found in metadata");
141         }
142         // gets the correct method on the correct level. (not the inherited method) if we don't want the inherited method.
143
if (method.isInherited() && !inherited) {
144             String JavaDoc superClassName = sessionBean.getSuperName();
145             // loop while class is not java.lang.Object
146
while (!JAVA_LANG_OBJECT.equals(superClassName)) {
147                 ClassAnnotationMetadata superMetaData = sessionBean.getEjbJarAnnotationMetadata()
148                 .getClassAnnotationMetadata(superClassName);
149                 // If the method is found in the super class and is not inherited, use this one
150
if (superMetaData != null) {
151                     MethodAnnotationMetadata superMethod = superMetaData.getMethodAnnotationMetadata(jMethod);
152                     if (superMethod != null && !superMethod.isInherited()) {
153                         return superMethod;
154                     }
155                     superClassName = superMetaData.getSuperName();
156                 } else {
157                     // break the loop
158
superClassName = JAVA_LANG_OBJECT;
159                 }
160             }
161
162         }
163
164         return method;
165     }
166
167
168     /**
169      * Gets all interfaces used by a class.
170      * @param sessionBean the metadata to analyze.
171      * @return the list of interfaces from a given class.
172      */

173     public static List JavaDoc<String JavaDoc> getAllInterfacesFromClass(final ClassAnnotationMetadata sessionBean) {
174         // build list
175
List JavaDoc<String JavaDoc> allInterfaces = new ArrayList JavaDoc<String JavaDoc>();
176
177         // Class to analyze
178
String JavaDoc className = sessionBean.getClassName();
179
180         // loop while class is not java.lang.Object
181
while (!JAVA_LANG_OBJECT.equals(className)) {
182             ClassAnnotationMetadata metaData = sessionBean.getEjbJarAnnotationMetadata()
183             .getClassAnnotationMetadata(className);
184             // find metadata, all interfaces found
185
if (metaData != null) {
186                 String JavaDoc[] interfaces = metaData.getInterfaces();
187                 if (interfaces != null) {
188                     for (String JavaDoc itf : interfaces) {
189                         allInterfaces.add(itf);
190                     }
191                 }
192                 className = metaData.getSuperName();
193             } else {
194                 // break the loop
195
className = JAVA_LANG_OBJECT;
196             }
197         }
198         return allInterfaces;
199     }
200 }
201
Popular Tags