KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > intf > InterfaceTagsHandler


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

5 package xdoclet.modules.ejb.intf;
6
7 import java.text.MessageFormat JavaDoc;
8 import java.util.Arrays JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 import org.apache.commons.logging.Log;
14
15 import xjavadoc.*;
16 import xdoclet.DocletContext;
17
18 import xdoclet.DocletTask;
19 import xdoclet.XDocletException;
20 import xdoclet.modules.ejb.*;
21 import xdoclet.modules.ejb.entity.EntityTagsHandler;
22 import xdoclet.modules.ejb.home.HomeTagsHandler;
23 import xdoclet.tagshandler.MethodTagsHandler;
24
25 import xdoclet.util.LogUtil;
26 import xdoclet.util.Translator;
27 import xdoclet.util.TypeConversionUtil;
28
29 /**
30  * @author Ara Abrahamian (ara_e@email.com)
31  * @author Christoph G. Jung (christoph.jung@infor.de)
32  * @created Oct 15, 2001
33  * @xdoclet.taghandler namespace="EjbIntf"
34  * @version $Revision: 1.21 $
35  */

36 public class InterfaceTagsHandler extends EjbTagsHandler
37 {
38     // constants related to J2EE1.4
39
public final static String JavaDoc SERVICE_ENDPOINT_INTERFACE = "ServiceEndpoint";
40     public final static String JavaDoc SERVICE_ENDPOINT_EXTENDS_PARAM = "service-endpoint-extends";
41
42     private String JavaDoc currentMethodViewType = null;
43     private String JavaDoc currentTagIntf = null;
44
45     /**
46      * Return the fully qualified name of the component interface of type specified. Works based on the <code>ejb:interface</code>
47      * class level tag. Relevant parameters for the <code>ejb:interface</code> tag are:
48      * <ul>
49      * <li> remote-class: The fully qualified name of the remote class - overrides all set patterns
50      * <li> local-class: The fully qualified name of the local class - overrides all set patterns
51      * <li> service-endpoint-class: The fully qualified name of the service endpoint class - overrides all set
52      * patterns
53      * <li> remote-pattern: The pattern to be used to determine the unqualified name of the remote class
54      * <li> local-pattern: The pattern to be used to determine the unqualified name of the local class
55      * <li> service-endpoint-pattern: The pattern to be used to determine the unqualified name of the service-endpoint
56      * class
57      * <li> pattern: The pattern to be used in determining the unqualified remote and/or local and/or service-endpoint
58      * interface name - used where remote- or local- or service-endpoint-pattern are not specified.
59      * <li> remote-package: The package the remote interface is to be placed in
60      * <li> local-package: The package the local interface is to be placed in
61      * <li> service-endpoint-package: The package the service-endpoint interface is to be placed in
62      * <li> package: The package the remote and/or local and/or service-endpoint interface is to be placed in - used
63      * where remote- or local- or service-endpoint-package are not specified.
64      * </ul>
65      *
66      *
67      * @param type Can be remote or local. Defaults to remote.
68      * @param clazz Description of Parameter
69      * @return The fully qualified name of the interface.
70      * @exception XDocletException
71      */

72     public static String JavaDoc getComponentInterface(String JavaDoc type, XClass clazz) throws XDocletException
73     {
74         // validate type
75
if (!"remote".equals(type) && !"local".equals(type) && !SERVICE_ENDPOINT.equals(type)) {
76             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.INTERFACE_INVALID_TYPE, new String JavaDoc[]{type}));
77         }
78
79         String JavaDoc fileName = clazz.getContainingPackage().getName();
80         String JavaDoc name_pattern = null;
81         String JavaDoc package_pattern = null;
82         String JavaDoc component_interface = null;
83
84         component_interface = clazz.getDoc().getTagAttributeValue("ejb:interface", type + "-class");
85         if (component_interface != null) {
86             return component_interface;
87         }
88
89         name_pattern = clazz.getDoc().getTagAttributeValue("ejb:interface", type + "-pattern");
90         if (name_pattern == null) {
91             name_pattern = clazz.getDoc().getTagAttributeValue("ejb:interface", "pattern");
92             if (name_pattern == null) {
93                 if ("remote".equals(type)) {
94                     name_pattern = getRemoteClassPattern();
95                 }
96                 else {
97                     if (SERVICE_ENDPOINT.equals(type)) {
98                         name_pattern = getServiceEndpointClassPattern();
99                     }
100                     else {
101                         name_pattern = getLocalClassPattern();
102                     }
103                 }
104             }
105         }
106
107         package_pattern = clazz.getDoc().getTagAttributeValue("ejb:interface", type + "-package");
108         if (package_pattern == null) {
109             package_pattern = clazz.getDoc().getTagAttributeValue("ejb:interface", "package");
110         }
111
112         String JavaDoc ejb_name = null;
113
114         if (name_pattern.indexOf("{0}") != -1) {
115             ejb_name = MessageFormat.format(name_pattern, new Object JavaDoc[]{getShortEjbNameFor(clazz)});
116         }
117         else {
118             ejb_name = name_pattern;
119         }
120
121         String JavaDoc subtask_name = null;
122
123         if (type.equals("remote")) {
124             subtask_name = DocletTask.getSubTaskName(RemoteInterfaceSubTask.class);
125         }
126         else if (SERVICE_ENDPOINT.equals(type)) {
127             subtask_name = DocletTask.getSubTaskName(ServiceEndpointSubTask.class);
128         }
129         else {
130             subtask_name = DocletTask.getSubTaskName(LocalInterfaceSubTask.class);
131         }
132
133         // Fix package name
134
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(choosePackage(fileName, package_pattern, subtask_name));
135
136         if (sb.length() > 0) {
137             sb.append('.');
138         }
139         sb.append(ejb_name);
140
141         return sb.toString();
142     }
143
144     /**
145      * Returns true if method is an interface method, false otherwise. Interface methods are
146      * remote/create/remove/finder/home methods.
147      *
148      * @param method Description of Parameter
149      * @return The InterfaceMethod value
150      */

151     public static boolean isInterfaceMethod(XMethod method)
152     {
153         boolean result = isComponentInterfaceMethod(method) ||
154             HomeTagsHandler.isCreateMethod(method) ||
155             HomeTagsHandler.isRemoveMethod(method) ||
156             HomeTagsHandler.isFinderMethod(method) ||
157             HomeTagsHandler.isHomeMethod(method);
158
159         return result;
160     }
161
162     /**
163      * Returns true if method is a component interface method, false otherwise. Component interface methods are marked
164      * with a ejb:interface-method tag.
165      *
166      * @param method Description of Parameter
167      * @return The RemoteMethod value
168      */

169     public static boolean isComponentInterfaceMethod(XMethod method)
170     {
171         return method.getDoc().hasTag("ejb:interface-method");
172     }
173
174     /**
175      * Gets the BeanClassNameFromInterfaceNameFor attribute of the InterfaceTagsHandler class
176      *
177      * @param return_type Describe what the parameter does
178      * @return The BeanClassNameFromInterfaceNameFor value
179      * @exception XDocletException
180      */

181     public static String JavaDoc getBeanClassNameFromInterfaceNameFor(String JavaDoc return_type) throws XDocletException
182     {
183         Log log = LogUtil.getLog(InterfaceTagsHandler.class, "getBeanClassNameFromInterfaceName");
184
185         Collection JavaDoc classes = getXJavaDoc().getSourceClasses();
186
187         if (log.isDebugEnabled()) {
188             log.debug("return_type=" + return_type);
189         }
190         if (return_type == null) {
191             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.ASK_FOR_BEAN_FROM_NULL_INTERFACE, new String JavaDoc[]{return_type}));
192         }
193
194         for (Iterator JavaDoc i = classes.iterator(); i.hasNext(); ) {
195             XClass clazz = (XClass) i.next();
196
197             if (log.isDebugEnabled()) {
198                 log.debug("clazz=" + clazz);
199             }
200
201             if (EntityTagsHandler.isEntity(clazz)) {
202                 String JavaDoc remote_intf_name = getComponentInterface("remote", clazz);
203                 String JavaDoc local_intf_name = getComponentInterface("local", clazz);
204
205                 if (log.isDebugEnabled()) {
206                     log.debug("remote_intf_name=" + remote_intf_name);
207                     log.debug("local_intf_name=" + local_intf_name);
208                 }
209
210                 if (return_type.equals(remote_intf_name) || return_type.equals(local_intf_name)) {
211                     if (log.isDebugEnabled()) {
212                         log.debug("Found! beanClassNameFromInterfaceName returns with: " + clazz.getQualifiedName());
213                     }
214
215                     return clazz.getQualifiedName();
216                 }
217             }
218         }
219         if (log.isDebugEnabled()) {
220             log.warn("NOT FOUND! bean class coreesponding to IF " + return_type);
221         }
222         throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.BEAN_CLASS_NOT_FOUND_FOR_INTERFACE, new String JavaDoc[]{return_type}));
223     }
224
225     /**
226      * Returns true if method is a remote interface method by looking at view-type parameter.
227      *
228      * @param method Description of Parameter
229      * @return The isRemoteMethod value
230      * @exception XDocletException
231      */

232     public static boolean isRemoteMethod(XMethod method) throws XDocletException
233     {
234         return isComponentInterfaceMethodOfViewType(method, "remote");
235     }
236
237     /**
238      * Returns true if method is a local interface method by looking at view-type parameter.
239      *
240      * @param method Description of Parameter
241      * @return The isRemoteMethod value
242      * @exception XDocletException
243      */

244     public static boolean isLocalMethod(XMethod method) throws XDocletException
245     {
246         return isComponentInterfaceMethodOfViewType(method, "local");
247     }
248
249     /**
250      * Returns true if method is a local interface method by looking at view-type parameter.
251      *
252      * @param method Description of Parameter
253      * @return The isRemoteMethod value
254      * @exception XDocletException
255      */

256     public static boolean isServiceEndpointMethod(XMethod method) throws XDocletException
257     {
258         return isComponentInterfaceMethodOfViewType(method, SERVICE_ENDPOINT);
259     }
260
261     /**
262      * Gets the RemoteClassPattern attribute of the InterfaceTagsHandler class
263      *
264      * @return The RemoteClassPattern value
265      */

266     protected static String JavaDoc getRemoteClassPattern()
267     {
268         RemoteInterfaceSubTask remoteintf_subtask = ((RemoteInterfaceSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(RemoteInterfaceSubTask.class)));
269
270         if (remoteintf_subtask != null) {
271             return remoteintf_subtask.getRemoteClassPattern();
272         }
273         else {
274             return RemoteInterfaceSubTask.DEFAULT_REMOTE_CLASS_PATTERN;
275         }
276     }
277
278     /**
279      * Gets the LocalClassPattern attribute of the InterfaceTagsHandler class
280      *
281      * @return The LocalClassPattern value
282      */

283     protected static String JavaDoc getLocalClassPattern()
284     {
285         LocalInterfaceSubTask localintf_subtask = ((LocalInterfaceSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(LocalInterfaceSubTask.class)));
286
287         if (localintf_subtask != null) {
288             return localintf_subtask.getLocalClassPattern();
289         }
290         else {
291             return LocalInterfaceSubTask.DEFAULT_LOCAL_CLASS_PATTERN;
292         }
293     }
294
295     /**
296      * Gets the LocalClassPattern attribute of the InterfaceTagsHandler class
297      *
298      * @return The LocalClassPattern value
299      */

300     protected static String JavaDoc getServiceEndpointClassPattern()
301     {
302         ServiceEndpointSubTask seintf_subtask = ((ServiceEndpointSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(ServiceEndpointSubTask.class)));
303
304         if (seintf_subtask != null) {
305             return seintf_subtask.getServiceEndpointClassPattern();
306         }
307         else {
308             return ServiceEndpointSubTask.DEFAULT_SERVICE_ENDPOINT_CLASS_PATTERN;
309         }
310     }
311
312     /**
313      * Gets the ComponentInterfaceMethodOfViewType attribute of the InterfaceTagsHandler class
314      *
315      * @param method Describe what the parameter does
316      * @param type Describe what the parameter does
317      * @return The ComponentInterfaceMethodOfViewType value
318      * @exception XDocletException
319      */

320     private static boolean isComponentInterfaceMethodOfViewType(XMethod method, String JavaDoc type) throws XDocletException
321     {
322         //if a home method like create/remote/etc
323
if (isComponentInterfaceMethod(method) == false) {
324             //you can't specify method-intf for ejbCreate/ejbRemote/etc for now
325
//so return false and methodIntf will use Home/LocalHome
326
return false;
327         }
328         else {
329             String JavaDoc view_type = getTagValue(
330                 FOR_CLASS,
331                 method.getDoc(),
332                 "ejb:interface-method",
333                 "view-type",
334                 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL,
335                 "both",
336                 false,
337                 false
338                 );
339
340             if (view_type.indexOf(type) != -1) {
341                 return true;
342             }
343             else {
344                 if (view_type.indexOf(ALL) != -1) {
345                     return true;
346                 }
347                 else if (!view_type.equals(SERVICE_ENDPOINT) && view_type.indexOf("both") != -1) {
348                     return true;
349                 }
350                 else {
351                     return false;
352                 }
353             }
354         }
355     }
356
357     /**
358      * Gets the ViewTypesFromString attribute of the InterfaceTagsHandler class
359      *
360      * @param viewType Describe what the parameter does
361      * @return The ViewTypesFromString value
362      */

363     private static String JavaDoc[] getViewTypesFromString(String JavaDoc viewType)
364     {
365         if (viewType != null) {
366             if (viewType.equalsIgnoreCase(ALL)) {
367                 return new String JavaDoc[]{"local", "remote", SERVICE_ENDPOINT};
368             }
369
370             if (viewType.equalsIgnoreCase("both")) {
371                 return new String JavaDoc[]{"local", "remote"};
372             }
373
374             if (viewType.equalsIgnoreCase("remote")) {
375                 return new String JavaDoc[]{"remote"};
376             }
377
378             if (viewType.equalsIgnoreCase("remote-" + SERVICE_ENDPOINT)) {
379                 return new String JavaDoc[]{"remote", SERVICE_ENDPOINT};
380             }
381
382             if (viewType.equalsIgnoreCase("local")) {
383                 return new String JavaDoc[]{"local"};
384             }
385
386             if (viewType.equalsIgnoreCase("local-" + SERVICE_ENDPOINT)) {
387                 return new String JavaDoc[]{"local", SERVICE_ENDPOINT};
388             }
389
390             if (viewType.equalsIgnoreCase(SERVICE_ENDPOINT)) {
391                 return new String JavaDoc[]{SERVICE_ENDPOINT};
392             }
393         }
394
395         // If we're using EJB 1.1, the default is "remote", otherwise it's "both"
396
if (EjbTagsHandler.getEjbSpec().equals("1.1"))
397             return new String JavaDoc[]{"remote"};
398         else
399             return new String JavaDoc[]{"local", "remote"};
400     }
401
402     /**
403      * Returns the full qualified local or remote interface name for the bean, depending on the value of type parameter.
404      *
405      * @param attributes The attributes of the template tag
406      * @return Description of the Returned Value
407      * @exception XDocletException
408      * @doc.tag type="content"
409      * @doc.param name="type" optional="false" values="remote,local" description="Specifies the type
410      * of component interface."
411      */

412     public String JavaDoc componentInterface(Properties JavaDoc attributes) throws XDocletException
413     {
414         String JavaDoc type = attributes.getProperty("type");
415
416         type = type != null ? type : "remote";
417
418         return getComponentInterface(type, getCurrentClass());
419     }
420
421     /**
422      * Evaluate the body block if the current method is not an EJB local or remote interface method.
423      *
424      * @param template The body of the block tag
425      * @param attributes The attributes of the template tag
426      * @exception XDocletException
427      * @doc.tag type="block"
428      * @doc.param name="interface" optional="false" description="The type of interface to check for
429      * the methods validity in. Can be either \"local\" or \"remote\"."
430      */

431     public void ifIsNotInterfaceMethod(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException
432     {
433         String JavaDoc intFace = attributes.getProperty("interface");
434
435         if (intFace == null) {
436             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.TAG_MISSING_INTERFACE_PARAMETER, new String JavaDoc[]{"<XDtEjbIntf:ifIsNotInterfaceMethod>"}));
437         }
438
439         if (!isInterfaceMethod(intFace)) {
440             generate(template);
441         }
442     }
443
444     /**
445      * Evaluate the body block if the current method is an EJB local or remote interface method.
446      *
447      * @param template The body of the block tag
448      * @param attributes The attributes of the template tag
449      * @exception XDocletException
450      * @doc.tag type="block"
451      * @doc.param name="interface" optional="false" description="The type of interface to check for
452      * the methods validity in. Can be either \"local\" or \"remote\"."
453      * @doc.param name="superclasses" optional="true" description="Traverse superclasses too. With
454      * false value used in remote/local. Default is True."
455      */

456     public void ifIsInterfaceMethod(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException
457     {
458         String JavaDoc intf_view_type = attributes.getProperty("interface");
459         String JavaDoc superclasses_str = attributes.getProperty("superclasses");
460         boolean superclasses = TypeConversionUtil.stringToBoolean(superclasses_str, true);
461
462         if (intf_view_type == null) {
463             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.TAG_MISSING_INTERFACE_PARAMETER, new String JavaDoc[]{"<XDtEjbIntf:ifIsInterfaceMethod>"}));
464         }
465
466         if (isInterfaceMethod(intf_view_type)) {
467             boolean currentMethodDoesntBelongsToCurrentClass = !getCurrentMethod().getContainingClass().equals(getCurrentClass());
468
469             if (superclasses == false && currentMethodDoesntBelongsToCurrentClass == true) {
470                 return;
471             }
472
473             generate(template);
474         }
475     }
476
477     /**
478      * Evaluates the body block for each view-type of current method. Sets currentMethodViewType internal variable of
479      * tag handler class, used by nested methodIntf.
480      *
481      * @param template The body of the block tag
482      * @param attributes The attributes of the template tag
483      * @exception XDocletException
484      * @doc.tag type="block"
485      */

486     public void forAllInterfaceViewTypes(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException
487     {
488         String JavaDoc view_type;
489
490         if (isComponentInterfaceMethod(getCurrentMethod())) {
491             //is a component intf method
492
view_type = getTagValue(
493                 FOR_METHOD,
494                 getCurrentMethod().getDoc(),
495                 "ejb.interface-method",
496                 "view-type",
497                 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL,
498                 null,
499                 true,
500                 false
501                 );
502         }
503         else {
504             //is a home intf method
505
view_type = getTagValue(
506                 FOR_METHOD,
507                 getCurrentMethod().getDoc(),
508                 "ejb.home-method",
509                 "view-type",
510                 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL,
511                 null,
512                 true,
513                 false
514                 );
515
516             if (view_type == null) {
517                 view_type = getTagValue(
518                     FOR_METHOD,
519                     getCurrentMethod().getDoc(),
520                     "ejb.create-method",
521                     "view-type",
522                     "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL,
523                     null,
524                     true,
525                     false
526                     );
527             }
528         }
529
530         // get the class-level view-type parameter if not specified at method level
531
if (view_type == null) {
532             view_type = getTagValue(
533                 FOR_CLASS,
534                 getCurrentClass().getDoc(),
535                 "ejb.bean",
536                 "view-type",
537                 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL,
538                 null,
539                 true,
540                 false
541                 );
542         }
543
544         String JavaDoc[] view_types = getViewTypesFromString(view_type);
545
546         for (int i = 0; i < view_types.length; i++) {
547             currentMethodViewType = view_types[i];
548
549             generate(template);
550         }
551
552         currentMethodViewType = null;
553     }
554
555     /**
556      * Evaluates the body block if ejb:interface-method defined for current method.
557      *
558      * @param template The body of the block tag
559      * @exception XDocletException
560      * @see #isInterfaceMethod(xjavadoc.XMethod)
561      * @doc.tag type="block"
562      */

563     public void ifIsInterfaceMethod(String JavaDoc template) throws XDocletException
564     {
565         if (isInterfaceMethod(getCurrentMethod())) {
566             generate(template);
567         }
568     }
569
570     /**
571      * Returns "Remote" is current method has ejb:remote-method defined, "Home" otherwise.
572      *
573      * @param attributes The attributes of the template tag
574      * @return "Remote" or "Home".
575      * @exception XDocletException
576      * @see #isRemoteMethod(xjavadoc.XMethod)
577      * @doc.tag type="content"
578      */

579     public String JavaDoc methodIntf(Properties JavaDoc attributes) throws XDocletException
580     {
581         String JavaDoc view_type = attributes.getProperty("interface");
582
583         if (EjbTagsHandler.getEjbSpec().equals("1.1"))
584             view_type = "remote";
585
586         if (view_type == null) {
587             //if not explicitly specified for this call, then use currentMethodViewType
588
if (currentMethodViewType != null) {
589                 view_type = currentMethodViewType;
590             }
591             else {
592                 if (EjbTagsHandler.isOnlyLocalEjb(getCurrentClass())) {
593                     view_type = "local";
594                 }
595                 else if (EjbTagsHandler.isOnlyRemoteEjb(getCurrentClass())) {
596                     view_type = "remote";
597                 }
598                 else if (EjbTagsHandler.isOnlyServiceEndpointEjb(getCurrentClass())) {
599                     view_type = SERVICE_ENDPOINT;
600                 }
601                 else if (EntityTagsHandler.isEntity(getCurrentClass())) {
602                     view_type = "local";
603                 }
604                 else {
605                     view_type = "remote";
606                 }
607             }
608         }
609
610         if (view_type.equals("remote")) {
611             return (isRemoteMethod(getCurrentMethod()) ? "Remote" : "Home");
612         }
613         else if (view_type.equals(SERVICE_ENDPOINT)) {
614             return SERVICE_ENDPOINT_INTERFACE;
615         }
616         else {
617             return (isLocalMethod(getCurrentMethod()) ? "Local" : "LocalHome");
618         }
619     }
620
621     /**
622      * Returns interface method name for the current interface method.
623      *
624      * @return "Remote" or "Home".
625      * @exception XDocletException
626      * @see #getInterfaceMethodName(java.lang.String)
627      * @doc.tag type="content"
628      */

629     public String JavaDoc interfaceMethodName() throws XDocletException
630     {
631         return getInterfaceMethodName(getCurrentMethod().getName());
632     }
633
634     /**
635      * Returns the bean implementation class name for the interface name specified as the return type of current method
636      * or the method specified by parameter interface if any.
637      *
638      * @param attributes
639      * @return Bean class name
640      * @exception XDocletException
641      * @doc.tag type="content"
642      */

643     public String JavaDoc beanClassNameFromInterfaceName(Properties JavaDoc attributes) throws XDocletException
644     {
645         String JavaDoc return_type = attributes.getProperty("interface");
646
647         if (return_type == null) {
648             return_type = MethodTagsHandler.getMethodTypeFor(getCurrentMethod());
649         }
650
651         String JavaDoc bean_class_name = getBeanClassNameFromInterfaceNameFor(return_type);
652
653         if (bean_class_name == null) {
654             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.INTERFACE_IMPL_NOT_FOUND, new String JavaDoc[]{return_type}));
655         }
656
657         return bean_class_name;
658     }
659
660     /**
661      * Returns the name of the class home interface extends.
662      *
663      * @param attributes
664      * @return The name of generated PK class.
665      * @exception XDocletException
666      * @doc.tag type="content"
667      */

668     public String JavaDoc extendsFrom(Properties JavaDoc attributes) throws XDocletException
669     {
670         String JavaDoc type = attributes.getProperty("type");
671
672         type = type != null ? type : "remote";
673
674         String JavaDoc extends_param_name = "";
675
676         if (type.equals("remote")) {
677             extends_param_name = "extends";
678         }
679         else if (type.equals(SERVICE_ENDPOINT)) {
680             extends_param_name = SERVICE_ENDPOINT_EXTENDS_PARAM;
681         }
682         else {
683             extends_param_name = "local-extends";
684         }
685
686         String JavaDoc def_base_class_name = "";
687
688         if (type.equals("remote")) {
689             def_base_class_name = "javax.ejb.EJBObject";
690         }
691         else if (type.equals(SERVICE_ENDPOINT)) {
692             def_base_class_name = "java.rmi.Remote";
693         }
694         else {
695             def_base_class_name = "javax.ejb.EJBLocalObject";
696         }
697
698         return extendsFromFor(getCurrentClass(), "ejb:interface", type, extends_param_name, def_base_class_name);
699     }
700
701     /**
702      * Evaluates the body if the view-type of the current method is compatible with the value of the current method
703      * tag's parameter with the passed name.
704      *
705      * @param template The body of the block tag
706      * @param attributes The attributes of the template tag
707      * @exception XDocletException Description of Exception
708      * @doc.tag type="block"
709      * @doc.param name="paramName" optional="false" description=+The param name for the view-type on
710      * the current method tag"
711      */

712     public void ifCurrentMethodViewTypeEquals(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException
713     {
714         String JavaDoc param = attributes.getProperty("paramName");
715         String JavaDoc value = getCurrentMethodTag().getAttributeValue(param);
716
717         if (value == null) {
718             generate(template);
719         }
720         else {
721             String JavaDoc[] viewTypes = getViewTypesFromString(value);
722
723             Arrays.sort(viewTypes);
724             if (Arrays.binarySearch(viewTypes, currentMethodViewType) >= 0) {
725                 generate(template);
726             }
727
728         }
729     }
730
731     /**
732      * Evaluates the body for all interfaces which are compatible to the view-type which is set on the current class
733      * tag. The body of this tag is also evaluated once, if no view-type attribute is set on the current class tag.
734      *
735      * @param template The body of the block tag
736      * @param attributes The attributes of the template tag
737      * @exception XDocletException Description of Exception
738      * @doc.tag type="block"
739      * @doc.param name="paramName" optional="false" description="The param name for the view-type on
740      * the current class tag."
741      */

742     public void forAllClassTagIntf(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException
743     {
744         String JavaDoc param = attributes.getProperty("paramName");
745         String JavaDoc value = getCurrentClassTag().getAttributeValue(param);
746
747         if (value == null) {
748             currentTagIntf = null;
749             generate(template);
750         }
751         else {
752             String JavaDoc[] view_types = getViewTypesFromString(value);
753
754             for (int i = 0; i < view_types.length; i++) {
755                 if (view_types[i].equals("remote")) {
756                     currentTagIntf = "Remote";
757                     generate(template);
758                     currentTagIntf = "Home";
759                     generate(template);
760                 }
761                 else if (view_types[i].equals("local")) {
762                     currentTagIntf = "Local";
763                     generate(template);
764                     currentTagIntf = "LocalHome";
765                     generate(template);
766                 }
767                 else if (view_types[i].equals(SERVICE_ENDPOINT)) {
768                     currentTagIntf = SERVICE_ENDPOINT_INTERFACE;
769                     generate(template);
770                 }
771             }
772             currentTagIntf = null;
773         }
774     }
775
776     /**
777      * Returns the current interface inside a forAllClassTagIntf block
778      *
779      * @return
780      * @exception XDocletException Description of Exception
781      * @see #forAllClassTagIntf(String, Properties)
782      * @doc.tag type="content"
783      */

784     public String JavaDoc classTagIntf() throws XDocletException
785     {
786         return currentTagIntf;
787     }
788
789     /**
790      * Evaluates the body if a current interface is available inside a forAllClassTagIntf block
791      *
792      * @param template The body of the block tag
793      * @exception XDocletException Description of Exception
794      * @see #forAllClassTagIntf(String, Properties)
795      * @doc.tag type="block"
796      */

797     public void ifHasClassTagIntf(String JavaDoc template) throws XDocletException
798     {
799         if (currentTagIntf != null) {
800             generate(template);
801         }
802     }
803
804     /**
805      * Implements functionality required by {@link #ifIsInterfaceMethod} and {@link #ifIsNotInterfaceMethod}. To
806      * determine what interfaces the method should appear in, check the first for a <code>view-type</code> parameter to
807      * the method level <code>ejb:interface-method</code> tag. If that is absent use the <code>view-type</code> tag from
808      * <code>ejb:bean</code> .
809      *
810      * @param intFace The type of interface to test the method for.
811      * @return true if the method should occur in the specified interface.
812      * @exception XDocletException
813      */

814     protected boolean isInterfaceMethod(String JavaDoc intFace) throws XDocletException
815     {
816         if (!getCurrentMethod().getDoc().hasTag("ejb:interface-method")) {
817             return false;
818         }
819
820         String JavaDoc viewType = getTagValue(
821             FOR_CLASS,
822             getCurrentMethod().getDoc(),
823             "ejb:interface-method",
824             "view-type",
825             "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL,
826             null,
827             false,
828             false
829             );
830
831
832         if (viewType == null) {
833             viewType = getTagValue(
834                 FOR_CLASS,
835                 getCurrentClass().getDoc(),
836                 "ejb:bean",
837                 "view-type",
838                 "remote,local,both," + SERVICE_ENDPOINT + ",local-" + SERVICE_ENDPOINT + ",remote-" + SERVICE_ENDPOINT + "," + ALL,
839                 "both",
840                 true,
841                 false
842                 );
843         }
844
845         if ("both".equals(viewType)) {
846             viewType = "local,remote";
847         }
848         else if (ALL.equals(viewType)) {
849             viewType = "local,remote," + SERVICE_ENDPOINT;
850         }
851
852         return viewType.indexOf(intFace) >= 0;
853     }
854
855     /**
856      * Returns the interface method name depending on its type.
857      *
858      * @param name Description of Parameter
859      * @return "create" if ejbCreate, "remote" if ejbRemove, find <blabl>if ejbFind, home <blabla>
860      * if ejbHome.
861      * @exception XDocletException
862      */

863     protected String JavaDoc getInterfaceMethodName(String JavaDoc name) throws XDocletException
864     {
865         if (name.startsWith("ejbCreate")) {
866             return HomeTagsHandler.toCreateMethod(name);
867         }
868         else if (name.equals("ejbRemove")) {
869             return "remove";
870         }
871         else if (name.startsWith("ejbFind")) {
872             return HomeTagsHandler.toFinderMethod(name);
873         }
874         else if (name.startsWith("ejbHome")) {
875             return HomeTagsHandler.toHomeMethod(name);
876         }
877         else {
878             return name;
879         }
880     }
881
882     /**
883      * Gets the DependentClassFor attribute of the InterfaceTagsHandler object
884      *
885      * @param clazz Describe what the parameter does
886      * @param type Describe what the parameter does
887      * @return The DependentClassFor value
888      * @exception XDocletException
889      */

890     protected String JavaDoc getDependentClassFor(XClass clazz, String JavaDoc type) throws XDocletException
891     {
892         if ((type.equals("local") && isLocalEjb(clazz)) || (type.equals("remote") && isRemoteEjb(clazz)) || type.equals(SERVICE_ENDPOINT) && isServiceEndpointEjb(clazz)) {
893             return getComponentInterface(type, clazz);
894         }
895         else {
896             return null;
897         }
898     }
899
900     /**
901      * Loops over all classes and if value equals to local or remote or service-endpoint interface name of an EJBean
902      * full qualified name of that EJB is returned.
903      *
904      * @param value Description of Parameter
905      * @return Description of the Returned Value
906      * @exception XDocletException
907      */

908     protected String JavaDoc fromInterfaceToBean(String JavaDoc value) throws XDocletException
909     {
910         Collection JavaDoc classes = getXJavaDoc().getSourceClasses();
911
912         for (Iterator JavaDoc i = classes.iterator(); i.hasNext(); ) {
913             XClass clazz = (XClass) i.next();
914
915             if (isEjb(clazz)) {
916                 String JavaDoc remote_interface_name = getComponentInterface("remote", clazz);
917                 String JavaDoc local_interface_name = getComponentInterface("local", clazz);
918                 String JavaDoc service_endpoint_interface_name = getComponentInterface(SERVICE_ENDPOINT, clazz);
919
920                 if (value.equals(remote_interface_name) || value.equals(local_interface_name) || value.equals(service_endpoint_interface_name)) {
921                     return clazz.getQualifiedName();
922                 }
923             }
924         }
925
926         return value;
927     }
928 }
929
Popular Tags