KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > session > SessionTagsHandler


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.ejb.session;
6
7 import java.text.MessageFormat JavaDoc;
8 import java.util.*;
9
10 import xjavadoc.*;
11 import xdoclet.DocletContext;
12 import xdoclet.DocletSupport;
13
14 import xdoclet.DocletTask;
15 import xdoclet.XDocletException;
16 import xdoclet.modules.ejb.EjbTagsHandler;
17 import xdoclet.modules.ejb.session.SessionSubTask;
18
19 /**
20  * @author Ara Abrahamian (ara_e@email.com)
21  * @created Oct 16, 2001
22  * @xdoclet.taghandler namespace="EjbSession"
23  * @version $Revision: 1.13 $
24  */

25 public class SessionTagsHandler extends EjbTagsHandler
26 {
27     /**
28      * Gets the SessionClassFor attribute of the SessionTagsHandler class
29      *
30      * @param clazz Describe what the parameter does
31      * @return The SessionClassFor value
32      */

33     public static String JavaDoc getSessionClassFor(XClass clazz)
34     {
35         String JavaDoc fileName = clazz.getContainingPackage().getName();
36         String JavaDoc sessionName = MessageFormat.format(getSessionClassPattern(), new Object JavaDoc[]{getShortEjbNameFor(clazz)});
37
38         // Fix package name
39
fileName = choosePackage(fileName, null, DocletTask.getSubTaskName(SessionSubTask.class));
40         if (fileName.length() > 0) {
41             fileName += ".";
42         }
43
44         fileName += sessionName;
45
46         return fileName;
47     }
48
49     /**
50      * Returns true if clazz is a session bean, false otherwise.
51      *
52      * @param clazz Description of Parameter
53      * @return The Session value
54      */

55     public static boolean isSession(XClass clazz)
56     {
57         return clazz.isA("javax.ejb.SessionBean");
58     }
59
60     /**
61      * Gets the SessionClassPattern attribute of the SessionTagsHandler class
62      *
63      * @return The SessionClassPattern value
64      */

65     protected static String JavaDoc getSessionClassPattern()
66     {
67         SessionSubTask sessionSubtask = ((SessionSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(SessionSubTask.class)));
68
69         if (sessionSubtask != null) {
70             return sessionSubtask.getSessionClassPattern();
71         }
72         else {
73             return SessionSubTask.DEFAULT_SESSION_CLASS_PATTERN;
74         }
75     }
76
77     /**
78      * Returns true if clazz is a stateful session bean, false otherwise. Entity type is determined by looking at the
79      * ejb:bean's type parameter.
80      *
81      * @param clazz Description of Parameter
82      * @return The StatefulSession value
83      * @exception XDocletException
84      */

85     public boolean isStatefulSession(XClass clazz) throws XDocletException
86     {
87         if (isSession(clazz) == false) {
88             return false;
89         }
90
91         String JavaDoc value = getCurrentClass().getDoc().getTagAttributeValue("ejb:bean", "type", false);
92
93         if (value != null && value.equals("Stateful")) {
94             return true;
95         }
96         else {
97             return false;
98         }
99     }
100
101     /**
102      * Returns true if clazz is a stateless session bean, false otherwise. Entity type is determined by looking at the
103      * ejb:bean's type parameter.
104      *
105      * @param clazz Description of Parameter
106      * @return The StatelessSession value
107      * @exception XDocletException
108      */

109     public boolean isStatelessSession(XClass clazz) throws XDocletException
110     {
111         if (isSession(clazz) == false) {
112             return false;
113         }
114
115         String JavaDoc value = getCurrentClass().getDoc().getTagAttributeValue("ejb:bean", "type", false);
116
117         if (value != null) {
118             return value.equals("Stateless");
119         }
120         else {
121             // it's stateful if it implements SessionSynchronization
122
if (clazz.isA("javax.ejb.SessionSynchronization")) {
123                 return false;
124             }
125
126             // it's stateful if it has create methods with parameters,
127
// stateless if has a single ejbCreate() method with no args and returning void
128
Collection methods = clazz.getMethods();
129             boolean hasEmptyCreateMethod = false;
130             boolean hasOtherCreateMethods = false;
131
132             for (Iterator i = methods.iterator(); i.hasNext(); ) {
133                 XMethod method = (XMethod) i.next();
134
135                 // if an empty create method
136
if (method.getName().equals("ejbCreate") && method.getParameters().size() == 0) {
137                     hasEmptyCreateMethod = true;
138                 }
139                 else if (method.getName().startsWith("ejbCreate") &&
140                     method.getParameters().size() > 0 &&
141                     method.getReturnType().getType().getQualifiedName().equals("void")) {
142                     hasOtherCreateMethods = true;
143                 }
144             }
145
146             if (hasEmptyCreateMethod == true && hasOtherCreateMethods == false) {
147                 return true;
148             }
149             else {
150                 return false;
151             }
152         }
153     }
154
155     /**
156      * Returns the name of generated session class.
157      *
158      * @return The name of generated session class.
159      * @exception XDocletException
160      * @doc.tag type="content"
161      */

162     public String JavaDoc sessionClass() throws XDocletException
163     {
164         return getSessionClassFor(getCurrentClass());
165     }
166
167     /**
168      * Evaluate the body block if current class is of an stateless session bean type.
169      *
170      * @param template The body of the block tag
171      * @exception XDocletException
172      * @see #isStatelessSession(xjavadoc.XClass)
173      * @doc.tag type="block"
174      */

175     public void ifStatelessSession(String JavaDoc template) throws XDocletException
176     {
177         if (isStatelessSession(getCurrentClass())) {
178             generate(template);
179         }
180     }
181
182     /**
183      * Evaluate the body block if current class is not of an stateless session bean type.
184      *
185      * @param template The body of the block tag
186      * @exception XDocletException
187      * @see #isStatelessSession(xjavadoc.XClass)
188      * @doc.tag type="block"
189      */

190     public void ifNotStatelessSession(String JavaDoc template) throws XDocletException
191     {
192         if (!isStatelessSession(getCurrentClass())) {
193             generate(template);
194         }
195     }
196
197     /**
198      * Evaluate the body block if current class is of an stateful session bean type.
199      *
200      * @param template The body of the block tag
201      * @exception XDocletException
202      * @see #isStatefulSession(xjavadoc.XClass)
203      * @doc.tag type="block"
204      */

205     public void ifStatefulSession(String JavaDoc template) throws XDocletException
206     {
207         if (isStatefulSession(getCurrentClass())) {
208             generate(template);
209         }
210     }
211
212     /**
213      * Evaluate the body block if current class is not of a stateful session bean type.
214      *
215      * @param template The body of the block tag
216      * @exception XDocletException
217      * @see #isStatefulSession(xjavadoc.XClass)
218      * @doc.tag type="block"
219      */

220     public void ifNotStatefulSession(String JavaDoc template) throws XDocletException
221     {
222         if (!isStatefulSession(getCurrentClass())) {
223             generate(template);
224         }
225     }
226
227     /**
228      * Evaluates the body block for each EJBean derived from SessionBean.
229      *
230      * @param template The body of the block tag
231      * @exception XDocletException
232      * @see #isSession(xjavadoc.XClass)
233      * @doc.tag type="block"
234      */

235     public void forAllSessionBeans(String JavaDoc template) throws XDocletException
236     {
237         Collection classes = getXJavaDoc().getSourceClasses();
238
239         for (Iterator i = classes.iterator(); i.hasNext(); ) {
240             XClass clazz = (XClass) i.next();
241
242             setCurrentClass(clazz);
243
244             if (DocletSupport.isDocletGenerated(getCurrentClass())) {
245                 continue;
246             }
247
248             if (!hasHavingClassTag(getCurrentClass())) {
249                 continue;
250             }
251
252             if (isSession(getCurrentClass())) {
253                 generate(template);
254             }
255         }
256     }
257
258     /**
259      * Evaluates the body block for each EJBean derived from SessionBean which is stateful.
260      *
261      * @param template The body of the block tag
262      * @exception XDocletException
263      * @see #isStatefulSession(xjavadoc.XClass)
264      * @doc.tag type="block"
265      */

266     public void forAllStatefulSessionBeans(String JavaDoc template) throws XDocletException
267     {
268         Collection classes = getXJavaDoc().getSourceClasses();
269
270         for (Iterator i = classes.iterator(); i.hasNext(); ) {
271             XClass clazz = (XClass) i.next();
272
273             setCurrentClass(clazz);
274
275             if (DocletSupport.isDocletGenerated(getCurrentClass())) {
276                 continue;
277             }
278
279             if (!hasHavingClassTag(getCurrentClass())) {
280                 continue;
281             }
282
283             if (isStatefulSession(getCurrentClass())) {
284                 generate(template);
285             }
286         }
287     }
288
289     /**
290      * Evaluates the body block for each EJBean derived from SessionBean which is stateless.
291      *
292      * @param template The body of the block tag
293      * @exception XDocletException
294      * @see #isStatelessSession(xjavadoc.XClass)
295      * @doc.tag type="block"
296      */

297     public void forAllStatelessSessionBeans(String JavaDoc template) throws XDocletException
298     {
299         Collection classes = getXJavaDoc().getSourceClasses();
300
301         for (Iterator i = classes.iterator(); i.hasNext(); ) {
302             XClass clazz = (XClass) i.next();
303
304             setCurrentClass(clazz);
305
306             if (DocletSupport.isDocletGenerated(getCurrentClass())) {
307                 continue;
308             }
309
310             if (!hasHavingClassTag(getCurrentClass())) {
311                 continue;
312             }
313
314             if (isStatelessSession(getCurrentClass())) {
315                 generate(template);
316             }
317         }
318     }
319 }
320
Popular Tags