KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > EjbTagsHandler


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

5 package xdoclet.modules.ejb;
6
7 import java.util.*;
8
9 import org.apache.commons.logging.Log;
10
11 import xjavadoc.*;
12
13 import xdoclet.DocletContext;
14 import xdoclet.DocletSupport;
15 import xdoclet.DocletTask;
16 import xdoclet.SubTask;
17 import xdoclet.XDocletException;
18 import xdoclet.XDocletTagSupport;
19 import xdoclet.modules.ejb.entity.*;
20 import xdoclet.modules.ejb.home.HomeInterfaceSubTask;
21 import xdoclet.modules.ejb.home.LocalHomeInterfaceSubTask;
22 import xdoclet.modules.ejb.intf.LocalInterfaceSubTask;
23 import xdoclet.modules.ejb.intf.RemoteInterfaceSubTask;
24 import xdoclet.modules.ejb.mdb.MdbSubTask;
25 import xdoclet.modules.ejb.mdb.MdbTagsHandler;
26 import xdoclet.modules.ejb.session.SessionSubTask;
27 import xdoclet.modules.ejb.session.SessionTagsHandler;
28 import xdoclet.tagshandler.PackageTagsHandler;
29 import xdoclet.util.LogUtil;
30 import xdoclet.util.Translator;
31 import xdoclet.util.TypeConversionUtil;
32
33 /**
34  * @author Ara Abrahamian (ara_e@email.com)
35  * @author Christoph G. Jung (christoph.jung@infor.de)
36  * @created Oct 15, 2001
37  * @xdoclet.taghandler namespace="Ejb"
38  * @version $Revision: 1.30 $
39  */

40 public class EjbTagsHandler extends XDocletTagSupport
41 {
42     // further constants related to J2EE1.4
43
public final static String JavaDoc SERVICE_ENDPOINT = "service-endpoint";
44     public final static String JavaDoc ALL = "all";
45     public final static String JavaDoc SERVICE_ENDPOINT_SUFFIX = "Service";
46
47     protected final static String JavaDoc LOCAL_SUFFIX = "Local";
48
49     private XClass currentEjbClass;
50
51     /**
52      * Gets the AConcreteEJBean attribute of the EjbTagsHandler class
53      *
54      * @param clazz Describe what the parameter does
55      * @return The AConcreteEJBean value
56      * @exception XDocletException
57      */

58     public static boolean isAConcreteEJBean(XClass clazz) throws XDocletException
59     {
60         XTag beanTag = clazz.getDoc().getTag("ejb:bean");
61
62         if (beanTag != null) {
63             String JavaDoc generateStr = beanTag.getAttributeValue("generate");
64
65             if (generateStr != null) {
66                 boolean generate = TypeConversionUtil.stringToBoolean(generateStr, true);
67
68                 // generate="true" specifically
69
if (generate == true) {
70                     return true;
71                 }
72                 else {
73                     // generate="false" specifically
74
return false;
75                 }
76             }
77
78             // ejb:beam name specified, so it's a concrete ejb
79
if (beanTag.getAttributeValue("name") != null) {
80                 return true;
81             }
82         }
83
84         // now try to guess because it wasn't specifically specified whether
85
// it should be generated or not
86
SubTask subtask = getSubTaskClassForClass(clazz);
87
88         if (clazz.isAbstract() == true) {
89             if (hasANonDocletGeneratedSubClass(clazz) == true) {
90                 return false;
91             }
92
93             // an abstract mdb/etc?
94
if (subtask == null) {
95                 return false;
96             }
97
98             // if <entitycmp/bmp/session/> is on, then do the best to guess correctly
99
if (DocletContext.getInstance().isSubTaskDefined(subtask.getSubTaskName()) == true) {
100                 //none of the above guesses worked, assume it's concrete!
101
return true;
102             }
103             else {
104                 // if <entitycmp/bmp/session/> is off, so if class is abstract then the bean is abstract except for entity cmp beans in ejb2 cmp2
105
if (CmpTagsHandler.isEntityCmp(clazz) && CmpTagsHandler.isUsingCmp2Impl(clazz)) {
106                     return true;
107                 }
108
109                 return false;
110             }
111         }
112         else {
113             // if <entitycmp/bmp/> is on, then it's an error or not specify the class abstract, except for <session/> that non-abstract is also legal
114
if (subtask != null && DocletContext.getInstance().isSubTaskDefined(subtask.getSubTaskName())) {
115                 if (subtask.getSubTaskName().equals(DocletTask.getSubTaskName(SessionSubTask.class))) {
116                     return true;
117                 }
118
119                 String JavaDoc currentClassName = clazz.getQualifiedName();
120
121                 throw new XDocletException(Translator.getString("xdoclet.modules.ejb.Messages", "class_not_abstract",
122                     new String JavaDoc[]{currentClassName, DocletTask.getSubTaskName(SessionSubTask.class)}));
123             }
124             else {
125                 return true;
126             }
127         }
128     }
129
130     /**
131      * Returns the EJB name of the clazz by seaching for ejb:bean's name parameter. If that is not found, it uses the
132      * class' name minus any suffix from the list in the 'ejbClassNameSuffix' config parameter ("Bean,EJB,Ejb" by
133      * default).
134      *
135      * @param clazz The EJB bean class for which we want the EJB name
136      * @return The EjbName value
137      * @see #ejbName(java.util.Properties)
138      */

139     public static String JavaDoc getEjbNameFor(XClass clazz)
140     {
141         XTag beanTag = clazz.getDoc().getTag("ejb:bean");
142         String JavaDoc paramValue = null;
143
144         if (beanTag != null) {
145             paramValue = beanTag.getAttributeValue("name");
146         }
147
148         if (paramValue == null) {
149             String JavaDoc className = clazz.getName();
150
151             // remove any suffix from ejbClassNameSuffix list
152
String JavaDoc suffixlist = (String JavaDoc) getDocletContext().getConfigParam("ejbClassNameSuffix");
153             StringTokenizer st = new StringTokenizer(suffixlist, ",");
154
155             while (st.hasMoreTokens()) {
156                 String JavaDoc suffix = st.nextToken();
157
158                 if (className.endsWith(suffix)) {
159                     int index = className.lastIndexOf(suffix);
160
161                     className = className.substring(0, index);
162                     break;
163                 }
164             }
165
166             return className;
167         }
168
169         return paramValue;
170     }
171
172     /**
173      * Returns short version of the EJB name of the clazz.
174      *
175      * @param clazz the class we want its short EJB name
176      * @return The shortEjbName value
177      * @see #shortEjbName()
178      */

179     public static String JavaDoc getShortEjbNameFor(XClass clazz)
180     {
181         Log log = LogUtil.getLog(EjbTagsHandler.class, "shortEjbName");
182
183         // Find the last part of the name
184
StringTokenizer ejbNameTokens = new StringTokenizer(getEjbNameFor(clazz), ":./\\-");
185         String JavaDoc name;
186
187         do {
188             name = ejbNameTokens.nextToken();
189         } while (ejbNameTokens.hasMoreTokens());
190
191         if (log.isDebugEnabled()) {
192             log.debug("Name=" + name);
193         }
194
195         return name;
196     }
197
198     /**
199      * @param clazz Description of Parameter
200      * @return a unique id for clazz
201      */

202     public static String JavaDoc getEjbIdFor(XClass clazz)
203     {
204         return getEjbNameFor(clazz).replace('/', '_');
205     }
206
207     /**
208      * Returns the EJB specification version used. The generated files will be compatible with the version specified.
209      *
210      * @return The Ejbspec value
211      */

212     public static String JavaDoc getEjbSpec()
213     {
214         return (String JavaDoc) getDocletContext().getConfigParam("EjbSpec");
215     }
216
217     public static boolean isLocalEjb(XClass clazz) throws XDocletException
218     {
219         return isViewtypeEjb(clazz, "local");
220     }
221
222     public static boolean isRemoteEjb(XClass clazz) throws XDocletException
223     {
224         return isViewtypeEjb(clazz, "remote");
225     }
226
227
228     public static boolean isServiceEndpointEjb(XClass clazz) throws XDocletException
229     {
230         return isViewtypeEjb(clazz, SERVICE_ENDPOINT);
231     }
232
233     /**
234      * Returns true if clazz is only a local EJB by looking at ejb:bean's view-type parameter.
235      *
236      * @param clazz Description of Parameter
237      * @return The OnlyLocalEjb value
238      * @exception XDocletException
239      */

240     public static boolean isOnlyLocalEjb(XClass clazz) throws XDocletException
241     {
242         return isLocalEjb(clazz) && !isRemoteEjb(clazz) && !isServiceEndpointEjb(clazz);
243     }
244
245     /**
246      * Returns true if clazz is only a remote EJB by looking at ejb:bean's view-type parameter.
247      *
248      * @param clazz Description of Parameter
249      * @return The OnlyRemoteEjb value
250      * @exception XDocletException
251      */

252     public static boolean isOnlyRemoteEjb(XClass clazz) throws XDocletException
253     {
254         return isRemoteEjb(clazz) && !isLocalEjb(clazz) && !isServiceEndpointEjb(clazz);
255     }
256
257     /**
258      * Returns true if clazz is only a service endpoint EJB by looking at ejb:bean's view-type parameter.
259      *
260      * @param clazz Description of Parameter
261      * @return The OnlyRemoteEjb value
262      * @exception XDocletException
263      */

264     public static boolean isOnlyServiceEndpointEjb(XClass clazz) throws XDocletException
265     {
266         return isServiceEndpointEjb(clazz) && !isRemoteEjb(clazz) && !isLocalEjb(clazz);
267     }
268
269     /**
270      * Returns the class with the specified ejb name
271      *
272      * @param name
273      * @return
274      * @exception XDocletException
275      */

276     public static XClass getEjb(String JavaDoc name) throws XDocletException
277     {
278         Collection classes = getXJavaDoc().getSourceClasses();
279
280         for (Iterator i = classes.iterator(); i.hasNext(); ) {
281             XClass clazz = (XClass) i.next();
282
283             if (name.equals(getEjbNameFor(clazz))) {
284                 return clazz;
285             }
286         }
287         return null;
288     }
289
290     /**
291      * Returns true of clazz is an EJB (derived from an EJB type), false otherwise.
292      *
293      * @param clazz Description of Parameter
294      * @return The Ejb value
295      * @exception XDocletException
296      */

297     public static boolean isEjb(XClass clazz) throws XDocletException
298     {
299         return clazz.isA("javax.ejb.SessionBean")
300             || clazz.isA("javax.ejb.EntityBean")
301             || clazz.isA("javax.ejb.MessageDrivenBean");
302     }
303
304     /**
305      * Returns modified package name for a package name. If package name ends with one of the toReplace Strings, then
306      * it's substituted by the replaceWith String. If packagePattern not null then it's roughly used.
307      *
308      * @param packageName The name of the package name the new package name will be derived from
309      * @param packagePattern The package pattern to use. Can be null
310      * @param subtask
311      * @return Description of the Returned Value
312      * @todo this method is really an utility method that should be deprecated here and moved to
313      * PackageTagsHandler or even somewhere else
314      */

315     public static String JavaDoc choosePackage(String JavaDoc packageName, String JavaDoc packagePattern, String JavaDoc subtask)
316     {
317         Log log = LogUtil.getLog(EjbTagsHandler.class, "choosePackage");
318
319         ArrayList packageSubstitutions = PackageTagsHandler.getPackageSubstitutions(subtask);
320
321         if (log.isDebugEnabled()) {
322             log.debug("Package name=" + packageName + " - Pattern=" + packagePattern);
323         }
324
325         if (packagePattern != null) {
326             // later we may do some parametric {0} fancy stuff here
327
return packagePattern;
328         }
329         else {
330             for (int i = 0; i < packageSubstitutions.size(); i++) {
331                 PackageTagsHandler.PackageSubstitution ps = (PackageTagsHandler.PackageSubstitution) packageSubstitutions.get(i);
332                 StringTokenizer st = new StringTokenizer(ps.getPackages(), ",", false);
333
334                 if (ps.getUseFirst() == false) {
335                     while (st.hasMoreTokens()) {
336                         String JavaDoc packages = st.nextToken();
337                         String JavaDoc suffix = "." + packages;
338
339                         if (packageName.endsWith(suffix)) {
340                             packageName = packageName.substring(0, packageName.length() - suffix.length()) + '.' + ps.getSubstituteWith();
341                             break;
342                         }
343                     }
344                 }
345                 else {
346                     packageName = PackageTagsHandler.replaceInline(packageName, ps.getPackages(), ps.getSubstituteWith());
347                 }
348             }
349         }
350
351         if (log.isDebugEnabled()) {
352             log.debug("packageName=" + packageName);
353         }
354
355         return packageName;
356     }
357
358     /**
359      * Returns the name of EJB ref.
360      *
361      * @return The name of current EJB bean.
362      * @exception XDocletException
363      * @doc.tag type="content"
364      */

365     public static String JavaDoc ejbRefName() throws XDocletException
366     {
367         return ejbRefName(getCurrentClassTag(), getCurrentClass());
368     }
369
370     /**
371      * Returns the name of EJB ref.
372      *
373      * @param tag
374      * @param clazz
375      * @return The name of current EJB bean.
376      * @exception XDocletException
377      * @doc.tag type="content"
378      */

379     public static String JavaDoc ejbRefName(XTag tag, XClass clazz) throws XDocletException
380     {
381         String JavaDoc ejbRefName = null;
382
383         String JavaDoc refName = tag.getAttributeValue("ref-name");
384
385         if (refName != null) {
386             ejbRefName = refName;
387         }
388         else {
389             ejbRefName = prefixWithEjbSlash(getEjbNameFor(clazz));
390
391             String JavaDoc type = tag.getAttributeValue("view-type");
392
393             if (type != null) {
394                 if (type.equals("local") && isLocalEjb(clazz)) {
395                     ejbRefName = ejbRefName + LOCAL_SUFFIX;
396                 }
397             }
398             else {
399                 if (isLocalEjb(getCurrentClass()) && !isRemoteEjb(clazz)) {
400                     ejbRefName = ejbRefName + LOCAL_SUFFIX;
401                 }
402             }
403         }
404
405         return ejbRefName;
406     }
407
408     /**
409      * Replace &quot;.&quot; by &quot;/&quot; and add &quot;ejb/&quot; to the parameter.
410      *
411      * @param ejbName The string to parse
412      * @return The parsed String
413      */

414     protected static String JavaDoc prefixWithEjbSlash(String JavaDoc ejbName)
415     {
416         ejbName = ejbName.replace('.', '/');
417         if (ejbName.startsWith("ejb/")) {
418             return ejbName;
419         }
420         else {
421             return "ejb/" + ejbName;
422         }
423     }
424
425     private static boolean isViewtypeEjb(XClass clazz, String JavaDoc viewtype) throws XDocletException
426     {
427         // EJB 1.1 beans have only the remote view type
428
if (getEjbSpec().equals("1.1")) {
429             return "remote".equals(viewtype);
430         }
431
432         String JavaDoc value = getTagValue(
433             FOR_CLASS,
434             clazz.getDoc(),
435             "ejb:bean",
436             "view-type",
437             "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL,
438             null,
439             true,
440             false
441             );
442
443         if (value == null) {
444             //default is both if ejb2, remote if ejb1.1
445
if (!viewtype.equals(SERVICE_ENDPOINT)) {
446                 return true;
447             }
448             else {
449                 return false;
450             }
451         }
452         else {
453             if (value.indexOf(viewtype) != -1) {
454                 return true;
455             }
456             else {
457                 if (value.indexOf(ALL) != -1) {
458                     return true;
459                 }
460                 else if (!viewtype.equals(SERVICE_ENDPOINT) && value.indexOf("both") != -1) {
461                     return true;
462                 }
463                 else {
464                     return false;
465                 }
466             }
467         }
468     }
469
470
471     /**
472      * Gets the SubTaskClassForClass attribute of the EjbTagsHandler class
473      *
474      * @param clazz Describe what the parameter does
475      * @return The SubTaskClassForClass value
476      * @exception XDocletException
477      */

478     private static SubTask getSubTaskClassForClass(XClass clazz) throws XDocletException
479     {
480         if (CmpTagsHandler.isEntityCmp(clazz)) {
481             return DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(EntityCmpSubTask.class));
482         }
483
484         else if (MdbTagsHandler.isMessageDriven(clazz)) {
485             return DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(MdbSubTask.class));
486         }
487         else if (BmpTagsHandler.isEntityBmp(clazz)) {
488             return DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(EntityBmpSubTask.class));
489         }
490         else if (SessionTagsHandler.isSession(clazz)) {
491             return DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(SessionSubTask.class));
492         }
493         else {
494             return null;
495         }
496     }
497
498     /**
499      * Describe what the method does
500      *
501      * @param currentClass Describe what the parameter does
502      * @return Describe the return value
503      */

504     private static boolean hasANonDocletGeneratedSubClass(XClass currentClass)
505     {
506         // check if it's abstract and has a non-xdoclet-generated derived class
507
String JavaDoc fullClassName = currentClass.getQualifiedName();
508         Collection classes = getXJavaDoc().getSourceClasses();
509
510         for (Iterator i = classes.iterator(); i.hasNext(); ) {
511             XClass clazz = (XClass) i.next();
512
513             if (fullClassName.equals(clazz.getQualifiedName()) == false &&
514                 !clazz.getDoc().hasTag("xdoclet-generated") &&
515                 clazz.isA(fullClassName)) {
516                 return true;
517             }
518         }
519
520         return false;
521     }
522
523     /**
524      * Returns the name of current EJB bean.
525      *
526      * @param attributes The attributes of the template tag
527      * @return The name of current EJB bean.
528      * @exception XDocletException
529      * @see #getEjbNameFor(xjavadoc.XClass)
530      * @doc.tag type="content"
531      * @doc.param name="prefixWithEjbSlash" optional="true" values="true,false" description="Specifies
532      * whether to prefix it with ejb/ or not. False by default."
533      */

534     public String JavaDoc ejbName(Properties attributes) throws XDocletException
535     {
536         String JavaDoc prefixWithEjbSlashStr = attributes.getProperty("prefixWithEjbSlash");
537         boolean prefixWithEjbSlash = TypeConversionUtil.stringToBoolean(prefixWithEjbSlashStr, false);
538         String JavaDoc ejbName = getEjbNameFor(currentEjbClass == null ? getCurrentClass() : currentEjbClass);
539
540         if (prefixWithEjbSlash == true) {
541             return prefixWithEjbSlash(ejbName);
542         }
543         else {
544             return ejbName;
545         }
546     }
547
548     /**
549      * Returns the name of EJB ref.
550      *
551      * @return The name of current EJB bean.
552      * @exception XDocletException
553      * @doc.tag type="content"
554      */

555     public String JavaDoc ejbExternalRefName() throws XDocletException
556     {
557         String JavaDoc ejbRefName = null;
558         String JavaDoc refName = getCurrentClassTag().getAttributeValue("ref-name");
559
560         if (refName != null) {
561             ejbRefName = refName;
562         }
563         else {
564             ejbRefName = prefixWithEjbSlash(getCurrentClassTag().getAttributeValue("ejb-name"));
565         }
566
567         return ejbRefName;
568     }
569
570     /**
571      * Returns the symbolic name of the current class. For an EJBean it's the value of ejb:bean's name parameter.
572      *
573      * @return The symbolic name of the current class
574      * @exception XDocletException
575      * @see #shortEjbName()
576      * @doc.tag type="content"
577      */

578     public String JavaDoc symbolicClassName() throws XDocletException
579     {
580         return shortEjbName();
581     }
582
583     /**
584      * Returns short version of ejbName(). Example: "foo.bar.MyBean" ->"MyBean", "foo/bar/MyBean" ->"MyBean"
585      *
586      * @return Description of the Returned Value
587      * @exception XDocletException
588      * @see #getShortEjbNameFor(xjavadoc.XClass)
589      * @doc.tag type="content"
590      */

591     public String JavaDoc shortEjbName() throws XDocletException
592     {
593         return getShortEjbNameFor(getCurrentClass());
594     }
595
596     /**
597      * Evaluates the body block for each EJBean derived from one of the three EJB types: EntityBean, SessionBean or
598      * MessageDrivenBean.
599      *
600      * @param template The body of the block tag
601      * @exception XDocletException
602      * @see xdoclet.modules.ejb.entity.EntityTagsHandler#isEntity(xjavadoc.XClass)
603      * @see xdoclet.modules.ejb.session.SessionTagsHandler#isSession(xjavadoc.XClass)
604      * @see xdoclet.modules.ejb.mdb.MdbTagsHandler#isMessageDriven(xjavadoc.XClass)
605      * @doc.tag type="block"
606      */

607     public void forAllBeans(String JavaDoc template) throws XDocletException
608     {
609         Collection classes = getXJavaDoc().getSourceClasses();
610
611         for (Iterator i = classes.iterator(); i.hasNext(); ) {
612             XClass clazz = (XClass) i.next();
613
614             setCurrentClass(clazz);
615
616             if (DocletSupport.isDocletGenerated(getCurrentClass())) {
617                 continue;
618             }
619
620             if (EntityTagsHandler.isEntity(getCurrentClass()) || SessionTagsHandler.isSession(getCurrentClass()) ||
621                 MdbTagsHandler.isMessageDriven(getCurrentClass())) {
622                 currentEjbClass = getCurrentClass();
623                 generate(template);
624                 currentEjbClass = null;
625             }
626         }
627     }
628
629     /**
630      * Evaluates the body block if current bean is a concrete bean meaning the generate parameter of ejb:bean is either
631      * not specified or equals to "true", otherwise the bean is just an abstract base class bean not meant to be used as
632      * a EJBean but serve as the base for other EJBeans.
633      *
634      * @param template The body of the block tag
635      * @param attributes The attributes of the template tag
636      * @exception XDocletException
637      * @doc.tag type="block"
638      */

639     public void ifIsAConcreteEJBean(String JavaDoc template, Properties attributes) throws XDocletException
640     {
641         if (isAConcreteEJBean(getCurrentClass()) == true) {
642             generate(template);
643         }
644     }
645
646     /**
647      * Returns Bean type : "Entity", "Session" or "Message Driven".
648      *
649      * @return "Entity", "Session" or "Message Driven".
650      * @exception XDocletException
651      * @see xdoclet.modules.ejb.entity.EntityTagsHandler#isEntity(xjavadoc.XClass)
652      * @see xdoclet.modules.ejb.session.SessionTagsHandler#isSession(xjavadoc.XClass)
653      * @see xdoclet.modules.ejb.mdb.MdbTagsHandler#isMessageDriven(xjavadoc.XClass)
654      * @doc.tag type="content"
655      */

656     public String JavaDoc beanType() throws XDocletException
657     {
658         if (EntityTagsHandler.isEntity(getCurrentClass())) {
659             return "Entity";
660         }
661         else if (SessionTagsHandler.isSession(getCurrentClass())) {
662             return "Session";
663         }
664         else if (MdbTagsHandler.isMessageDriven(getCurrentClass())) {
665             return "Message Driven";
666         }
667         else {
668             return "Unknown";
669         }
670     }
671
672     /**
673      * Returns the full-qualified name of the current class's concrete class. This is the class that is generated and is
674      * derived from current class.
675      *
676      * @return The full-qualified name of the current class's concrete class
677      * @exception XDocletException
678      * @see xdoclet.modules.ejb.session.SessionTagsHandler#sessionClass()
679      * @see xdoclet.modules.ejb.entity.BmpTagsHandler#entityBmpClass()
680      * @see xdoclet.modules.ejb.entity.CmpTagsHandler#entityCmpClass()
681      * @see xdoclet.modules.ejb.mdb.MdbTagsHandler#messageDrivenClass()
682      * @doc.tag type="content"
683      */

684     public String JavaDoc concreteFullClassName() throws XDocletException
685     {
686         XTag beanTag = getCurrentClass().getDoc().getTag("ejb.bean");
687         String JavaDoc paramValue = null;
688
689         if (beanTag != null) {
690             paramValue = beanTag.getAttributeValue("impl-class-name");
691         }
692
693         if (SessionTagsHandler.isSession(getCurrentClass())) {
694             if (DocletContext.getInstance().isSubTaskDefined(DocletTask.getSubTaskName(SessionSubTask.class))) {
695                 return SessionTagsHandler.getSessionClassFor(getCurrentClass());
696             }
697             else {
698                 return (paramValue != null) ? paramValue : getCurrentClass().getQualifiedName();
699             }
700         }
701         else if (BmpTagsHandler.isEntityBmp(getCurrentClass())) {
702             if (DocletContext.getInstance().isSubTaskDefined(DocletTask.getSubTaskName(EntityBmpSubTask.class))) {
703                 return BmpTagsHandler.getEntityBmpClassFor(getCurrentClass());
704             }
705             else {
706                 return (paramValue != null) ? paramValue : getCurrentClass().getQualifiedName();
707             }
708         }
709
710         else if (CmpTagsHandler.isEntityCmp(getCurrentClass())) {
711             if (DocletContext.getInstance().isSubTaskDefined(DocletTask.getSubTaskName(EntityCmpSubTask.class))) {
712                 return CmpTagsHandler.getEntityCmpClassFor(getCurrentClass());
713             }
714             else {
715                 return (paramValue != null) ? paramValue : getCurrentClass().getQualifiedName();
716             }
717         }
718         else if (MdbTagsHandler.isMessageDriven(getCurrentClass())) {
719
720             if (DocletContext.getInstance().isSubTaskDefined(DocletTask.getSubTaskName(MdbSubTask.class))) {
721                 return MdbTagsHandler.getMessageDrivenClassFor(getCurrentClass());
722             }
723             else {
724                 return (paramValue != null) ? paramValue : getCurrentClass().getQualifiedName();
725             }
726         }
727         else {
728             return null;
729         }
730     }
731
732     /**
733      * Returns unique id for current ejb.
734      *
735      * @return Description of the Returned Value
736      * @exception XDocletException
737      * @doc.tag type="content"
738      */

739     public String JavaDoc id() throws XDocletException
740     {
741         return getEjbIdFor(getCurrentClass());
742     }
743
744     /**
745      * @param template Description of Parameter
746      * @exception XDocletException
747      * @doc.tag type="block"
748      */

749     public void ifLocalEjb(String JavaDoc template) throws XDocletException
750     {
751         if (isLocalEjb(getCurrentClass())) {
752             generate(template);
753         }
754     }
755
756     /**
757      * @param template Description of Parameter
758      * @exception XDocletException
759      * @doc.tag type="block"
760      */

761     public void ifRemoteEjb(String JavaDoc template) throws XDocletException
762     {
763         if (isRemoteEjb(getCurrentClass())) {
764             generate(template);
765         }
766     }
767
768     /**
769      * @param template Description of Parameter
770      * @exception XDocletException
771      * @doc.tag type="block"
772      */

773     public void ifServiceEndpointEjb(String JavaDoc template) throws XDocletException
774     {
775         if (isServiceEndpointEjb(getCurrentClass())) {
776             generate(template);
777         }
778     }
779
780
781     /**
782      * @param template
783      * @exception XDocletException
784      * @doc.tag type="block"
785      */

786     public void ifNotLocalEjb(String JavaDoc template) throws XDocletException
787     {
788         if (!isLocalEjb(getCurrentClass())) {
789             generate(template);
790         }
791     }
792
793     /**
794      * @param template
795      * @exception XDocletException
796      * @doc.tag type="block"
797      */

798     public void ifNotRemoteEjb(String JavaDoc template) throws XDocletException
799     {
800         if (!isRemoteEjb(getCurrentClass())) {
801             generate(template);
802         }
803     }
804
805     /**
806      * @param template
807      * @exception XDocletException
808      * @doc.tag type="block"
809      */

810     public void ifNotServiceEndpointEjb(String JavaDoc template) throws XDocletException
811     {
812         if (!isServiceEndpointEjb(getCurrentClass())) {
813             generate(template);
814         }
815     }
816
817     /**
818      * sub-classes which deal with patternized class names return a reasonable value
819      *
820      * @param clazz the class
821      * @param type type value used for view-type of remote/local
822      * @return dependent class name for the class and type
823      * @exception XDocletException
824      */

825     protected String JavaDoc getDependentClassFor(XClass clazz, String JavaDoc type) throws XDocletException
826     {
827         return null;
828     }
829
830     /**
831      * Gets the DependentClassTagName attribute of the EjbTagsHandler object
832      *
833      * @return The DependentClassTagName value
834      */

835     protected String JavaDoc getDependentClassTagName()
836     {
837         //it's too much dependency, we should find a better way
838
if (getDocletContext().getActiveSubTask().getSubTaskName().equals(DocletTask.getSubTaskName(DataObjectSubTask.class))) {
839             return "ejb:data-object";
840         }
841         else if (getDocletContext().getActiveSubTask().getSubTaskName().equals(DocletTask.getSubTaskName(EntityBmpSubTask.class)) ||
842             getDocletContext().getActiveSubTask().getSubTaskName().equals(DocletTask.getSubTaskName(EntityCmpSubTask.class))) {
843             return "ejb:bean";
844         }
845         else if (getDocletContext().getActiveSubTask().getSubTaskName().equals(DocletTask.getSubTaskName(RemoteInterfaceSubTask.class)) ||
846             getDocletContext().getActiveSubTask().getSubTaskName().equals(DocletTask.getSubTaskName(LocalInterfaceSubTask.class))) {
847             return "ejb:interface";
848         }
849         else if (getDocletContext().getActiveSubTask().getSubTaskName().equals(DocletTask.getSubTaskName(HomeInterfaceSubTask.class)) ||
850             getDocletContext().getActiveSubTask().getSubTaskName().equals(DocletTask.getSubTaskName(LocalHomeInterfaceSubTask.class))) {
851             return "ejb:interface";
852         }
853         else if (getDocletContext().getActiveSubTask().getSubTaskName().equals(DocletTask.getSubTaskName(EntityPkSubTask.class))) {
854             return "ejb:pk";
855         }
856         else {
857             return null;
858         }
859     }
860
861     /**
862      * Returns true if class/method denoted by doc has ejb:transaction tag, false otherwise.
863      *
864      * @param doc Description of Parameter
865      * @return Description of the Returned Value
866      * @exception XDocletException
867      */

868     protected boolean hasTransaction(XDoc doc) throws XDocletException
869     {
870         return doc.hasTag("ejb:transaction");
871     }
872
873     /**
874      * Returns the name of the class pk/etc class extends.
875      *
876      * @param clazz the class
877      * @param tagName name of the tag (ejb:bean for example, used for getting generate parameter)
878      * @param type type value used for view type of remote/local
879      * @param extendsParamName extends parameter name (is "extends" for ejb:bean but is "local-extends" for local)
880      * @param defaultBaseClassName default base class name, returned when not deriving from another base class
881      * @return correct value for the extends statement of a generated class
882      * @exception XDocletException
883      */

884     protected String JavaDoc extendsFromFor(XClass clazz, String JavaDoc tagName, String JavaDoc type, String JavaDoc extendsParamName, String JavaDoc defaultBaseClassName) throws XDocletException
885     {
886         Log log = LogUtil.getLog(EjbTagsHandler.class, "extendsFromFor");
887
888         log.debug("Looking " + type + " extendsFrom for class " + clazz.getName());
889
890         // see ejb:pk/etc generate="?" in superclass
891
XClass superclass = clazz.getSuperclass();
892
893         boolean generateSuper;
894
895         if (superclass.getDoc().hasTag(tagName)) {
896             String JavaDoc generateSuperStr = getTagValue(
897                 FOR_CLASS,
898                 superclass.getDoc(),
899                 tagName,
900                 "generate",
901                 null,
902                 null,
903                 false,
904                 false
905                 );
906
907             generateSuper = TypeConversionUtil.stringToBoolean(generateSuperStr, true);
908         }
909         else {
910             // Two Cases : PersonBean and BaseEntityBean
911
generateSuper = false;
912
913             Collection interfaces = clazz.getSuperclass().getInterfaces();
914
915             for (Iterator i = interfaces.iterator(); i.hasNext(); ) {
916                 XClass intf = (XClass) i.next();
917
918                 // if superclass is not javax.ejb.EntityBean then we have a superclass which
919
// is itself deriving from javax.ejb.EntityBean
920
if (intf.getQualifiedName().equals("javax.ejb.EntityBean") ||
921                     intf.getQualifiedName().equals("javax.ejb.SessionBean") ||
922                     intf.getQualifiedName().equals("javax.ejb.MessageDrivenBean")) {
923                     //it derives from javax.ejb.*Bean and no superclass for pk/etc class is explicitly defined
924
generateSuper = true;
925                 }
926             }
927         }
928
929         log.debug(clazz.getName() + " superclass is generatable? " + generateSuper);
930
931
932         // note: look for ejb:pk/etc extends in superclasses also only if generate="false" in superclass
933
// so extends attribute is inherited only if superclass's pk/etc is not to be generated
934
String JavaDoc extendsValue = getTagValue(
935             FOR_CLASS,
936             clazz.getDoc(),
937             tagName,
938             extendsParamName,
939             null,
940             null,
941             !generateSuper,
942             false
943             );
944
945         // if explicitly specified
946
if (extendsValue != null) {
947             log.debug(clazz.getName() + " contains an explicit extends. Returning " + extendsValue);
948             return extendsValue;
949         }
950         else {
951             // now try to guess if we are deriving from another ejb bean, and if so, then derive from
952
// that bean's pk class too (if generate="true" for superclass's pk/etc class)
953
log.debug(clazz.getName() + " does not contains an explicit extends. Trying to guess it");
954
955             // java.lang.Object (the only that have no superclass)
956
if (superclass.getSuperclass() == null) {
957                 log.debug("Top of class hierachy reached. Returning default extends: " + defaultBaseClassName);
958                 return defaultBaseClassName;
959             }
960             // if a superclass with generate="true"
961
else if (generateSuper == true) {
962                 String JavaDoc className = getDependentClassFor(superclass, type);
963
964                 if (className != null) {
965                     log.debug("Superclass " + superclass.getName() + " has a dependent class: " + className);
966                     return className;
967                 }
968                 else {
969                     log.debug("No dependent class for " + superclass.getName() + ". Returning default extends: " + defaultBaseClassName);
970                     return defaultBaseClassName;
971                 }
972             }
973             else {
974                 // so we have a superclass with pk-generate="false", look at superclass of that superclass!
975
log.debug("Can't guess now. Going deeper into class hierarchy");
976                 return extendsFromFor(superclass, tagName, type, extendsParamName, defaultBaseClassName);
977             }
978         }
979     }
980
981     /**
982      * Describe what the method does
983      *
984      * @param clazz Describe what the parameter does
985      * @param tagName Describe what the parameter does
986      * @return Describe the return value
987      * @exception XDocletException
988      */

989     protected boolean shouldTraverseSuperclassForDependentClass(XClass clazz, String JavaDoc tagName) throws XDocletException
990     {
991         Log log = LogUtil.getLog(EjbTagsHandler.class, "shouldTraverseSuperclassForDependentClass");
992
993         if (clazz.getQualifiedName().equals("java.lang.Object")) {
994             log.debug("clazz = java.lang.Object");
995
996             return true;
997         }
998
999         if (!clazz.isA("javax.ejb.EntityBean")
1000            && !clazz.isA("javax.ejb.SessionBean")) {
1001            log.debug(clazz.getQualifiedName() + " is _not_ of type javax.ejb.EntityBean,javax.ejb.SessionBean");
1002
1003            return true;
1004        }
1005        else {
1006            log.debug(clazz.getQualifiedName() + " _is_ of type javax.ejb.EntityBean,javax.ejb.SessionBean");
1007        }
1008
1009        // see ejb:bean generate="?" in superclass
1010
String JavaDoc generateBeanStr = getTagValue(
1011            FOR_CLASS,
1012            clazz.getDoc(),
1013            "ejb:bean",
1014            "generate",
1015            null,
1016            "true",
1017            false,
1018            false
1019            );
1020
1021        boolean generateBean = TypeConversionUtil.stringToBoolean(generateBeanStr, true);
1022
1023        if (generateBean == false) {
1024            log.debug("generateBean == false");
1025
1026            return true;
1027        }
1028
1029        boolean generate = false;
1030
1031        if (tagName != null) {
1032            // see ejb:pk/etc generate="?" in superclass
1033
String JavaDoc generateStr = getTagValue(
1034                FOR_CLASS,
1035                clazz.getDoc(),
1036                tagName,
1037                "generate",
1038                null,
1039                "true",
1040                false,
1041                false
1042                );
1043
1044            generate = TypeConversionUtil.stringToBoolean(generateStr, true);
1045        }
1046
1047        if (generate == false) {
1048            log.debug("generate == false");
1049
1050            return true;
1051        }
1052        else {
1053            log.debug("generate == true");
1054
1055            return false;
1056        }
1057    }
1058}
1059
Popular Tags