KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > dd > EjbRefTagsHandler


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

5 package xdoclet.modules.ejb.dd;
6
7 import java.util.Collection JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import xjavadoc.XClass;
14 import xjavadoc.XTag;
15 import xdoclet.XDocletException;
16 import xdoclet.modules.ejb.EjbTagsHandler;
17 import xdoclet.modules.ejb.XDocletModulesEjbMessages;
18 import xdoclet.modules.ejb.home.HomeTagsHandler;
19 import xdoclet.util.LogUtil;
20 import xdoclet.util.Translator;
21 import xdoclet.util.TypeConversionUtil;
22
23 /**
24  * @author Ara Abrahamian
25  * @created Oct 16, 2001
26  * @xdoclet.taghandler namespace="EjbRef"
27  * @version $Revision 1.1 $
28  * @todo refactor ejbRefId properly to account for ejb:bean - it may not be needed anymore.
29  * @todo refactor storeReferringClassId properly to take ejb:bean into account - may not be needed
30  * anymore.
31  * @deprecated please use {@link xdoclet.modules.ejb.env.EnvEjbRefTagsHandler}
32  */

33 public class EjbRefTagsHandler extends EjbTagsHandler
34 {
35     /**
36      * The id of the EJB referencing another EJB, used for setting up a correct unique id for the ejb-ref.
37      *
38      * @see #ejbRefId()
39      * @see #forAllEjbRefs(java.lang.String,java.util.Properties)
40      * @see #storeReferringClassId()
41      */

42     protected transient String JavaDoc referringClassId;
43
44     /**
45      * Returns unique id for the specified ejb-ref. It prefixes it with the referring class's id, then a _ and the id of
46      * the ejb object.
47      *
48      * @return Description of the Returned Value
49      * @exception XDocletException
50      * @todo refactor this properly to account for ejb:bean - it may not be needed anymore.
51      * @doc.tag type="content"
52      */

53     public String JavaDoc ejbRefId() throws XDocletException
54     {
55         return referringClassId + '_' + EjbTagsHandler.getEjbIdFor(getCurrentClass());
56     }
57
58     /**
59      * Evaluates the body block for each ejb:ejb-ref defined for the EJB. One of the useful things is does is to lookup
60      * the EJB using the ejb-name parameter of ejb:ejb-ref and fill in other required info.
61      *
62      * @param template The body of the block tag
63      * @param attributes The attributes of the template tag
64      * @exception XDocletException
65      * @doc.tag type="block"
66      */

67     public void forAllEjbRefs(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException
68     {
69         Log log = LogUtil.getLog(EjbRefTagsHandler.class, "forAllEjbRefs");
70
71         boolean superclasses = TypeConversionUtil.stringToBoolean(attributes.getProperty("superclasses"), true);
72
73         XClass oldCurClass = getCurrentClass();
74
75         XClass currentClass = getCurrentClass();
76         HashMap JavaDoc already = new HashMap JavaDoc();
77
78         do {
79             Collection JavaDoc tags = currentClass.getDoc().getTags("ejb.ejb-ref");
80
81             for (Iterator JavaDoc i = tags.iterator(); i.hasNext(); ) {
82                 XTag tag = (XTag) i.next();
83
84                 setCurrentClassTag(tag);
85
86                 storeReferringClassId();
87
88                 String JavaDoc ejbNameAttribute = tag.getAttributeValue("ejb-name");
89
90                 if (ejbNameAttribute == null || ejbNameAttribute.length() < 1) {
91                     mandatoryParamNotFound(tag.getDoc(), "ejb-name", "ejb.ejb-ref");
92                 }
93
94                 XClass refedEJBClass = findEjb(ejbNameAttribute);
95
96                 setCurrentClass(refedEJBClass);
97
98                 String JavaDoc refName = EjbTagsHandler.ejbRefName();
99
100                 if (!already.containsKey(refName)) {
101                     already.put(refName, tag);
102                     pushCurrentClass(refedEJBClass);
103                     generate(template);
104                     popCurrentClass();
105                 }
106                 else {
107                     XTag previousTag = (XTag) already.get(refName);
108
109                     if (!previousTag.getAttributeValue("ejb-name").equals(tag.getAttributeValue("ejb-name")) ||
110                         !previousTag.getAttributeValue("jndi-name").equals(tag.getAttributeValue("jndi-name"))) {
111                         log.error("Duplicate @ejb.ejb-ref found with different parameters!");
112                         log.error("Previous tag: @ejb.ejb-ref ref-name=\"" +
113                             previousTag.getAttributeValue("ref-name") + "\" ejb-name=\"" +
114                             previousTag.getAttributeValue("ejb-name") + "\" view-type=\"" +
115                             previousTag.getAttributeValue("view-type") + "\"");
116                         log.error("Current tag: @ejb.ejb-ref ref-name=\"" +
117                             tag.getAttributeValue("ref-name") + "\" ejb-name=\"" +
118                             tag.getAttributeValue("ejb-name") + "\" view-type=\"" +
119                             tag.getAttributeValue("view-type") + "\"");
120                         throw new XDocletException("Duplicate @ejb.ejb-ref with different parameters");
121                     }
122                     else {
123                         log.warn("Duplicated @ejb.ejb-ref found, ref-name=\"" + refName + "\"");
124                     }
125                 }
126
127                 setCurrentClassTag(null);
128                 referringClassId = null;
129             }
130             if (superclasses == true) {
131                 currentClass = currentClass.getSuperclass();
132             }
133             else {
134                 break;
135             }
136         } while (currentClass != null);
137
138         setCurrentClass(oldCurClass);
139     }
140
141     /**
142      * Returns the global JNDI name for the current EJB ref.
143      *
144      * @return The JNDI name of current EJB ref.
145      * @exception XDocletException
146      * @doc.tag type="content"
147      */

148     public String JavaDoc ejbRefJndiName() throws XDocletException
149     {
150         String JavaDoc ejbRefJndiName = null;
151
152         String JavaDoc jndiNameParameter = getCurrentClassTag().getAttributeValue("jndi-name");
153
154         if (jndiNameParameter != null) {
155             ejbRefJndiName = jndiNameParameter;
156         }
157         else {
158             String JavaDoc refed_ejb_name = getCurrentClassTag().getAttributeValue("ejb-name");
159
160             if (refed_ejb_name == null) {
161                 throw new XDocletException("No ejb-name attribute found in ejb-ref specified in bean " + getCurrentClass());
162             }
163
164             XClass refed_clazz = findEjb(refed_ejb_name);
165             String JavaDoc ejb_type = isLocalEjb(refed_clazz) ? "local" : "remote";
166
167             ejbRefJndiName = HomeTagsHandler.getJndiNameOfTypeFor(ejb_type, refed_clazz);
168
169 //Ara: we'll uncomment it later:
170
// String type = getCurrentClassTag().getAttributeValue("view-type");
171
//
172
// if (type != null && type.equals("local") && isLocalEjb(getCurrentClass()) && isRemoteEjb(getCurrentClass())) {
173
// ejbRefJndiName = ejbRefJndiName + LOCAL_SUFFIX;
174
// }
175
}
176
177         return ejbRefJndiName;
178     }
179
180     /**
181      * Generates code if the ejb-ref is local
182      *
183      * @param template
184      * @exception XDocletException
185      * @doc.tag type="block"
186      */

187     public void ifLocalEjbRef(String JavaDoc template) throws XDocletException
188     {
189         if (isLocalEjbRef(getCurrentClassTag())) {
190             generate(template);
191         }
192     }
193
194     /**
195      * Generates code if the ejb-ref is local
196      *
197      * @param template
198      * @exception XDocletException
199      * @doc.tag type="block"
200      */

201     public void ifRemoteEjbRef(String JavaDoc template) throws XDocletException
202     {
203         if (isRemoteEjbRef(getCurrentClassTag())) {
204             generate(template);
205         }
206     }
207
208     /**
209      * Return true if the ejb-ref is local
210      *
211      * @param ejbRefTag
212      * @return true if the ejb-ref is local otherwise false
213      * @exception XDocletException
214      */

215     protected boolean isLocalEjbRef(XTag ejbRefTag) throws XDocletException
216     {
217         String JavaDoc viewTypeParameter = ejbRefTag.getAttributeValue("view-type");
218
219         if (viewTypeParameter == null) {
220             return isLocalEjb(getCurrentClass()) && !isRemoteEjb(getCurrentClass());
221         }
222         else {
223             return "local".equals(viewTypeParameter);
224         }
225     }
226
227     /**
228      * Return true if the ejb-ref is remote
229      *
230      * @param ejbRefTag
231      * @return true if the ejb-ref is remote otherwise false
232      * @exception XDocletException
233      */

234     protected boolean isRemoteEjbRef(XTag ejbRefTag) throws XDocletException
235     {
236         return !isLocalEjbRef(ejbRefTag);
237     }
238
239     /**
240      * Stores the id of current EJB for further use by other tags in referringClassId attribute.
241      *
242      * @exception XDocletException
243      * @todo refactor this properly to take ejb:bean into account - may not be needed anymore.
244      */

245     protected void storeReferringClassId() throws XDocletException
246     {
247         referringClassId = EjbTagsHandler.getEjbIdFor(getCurrentClass());
248     }
249
250     /**
251      * Finds and returns the class with the specified ejbName. An XDocletException is thrown if not found.
252      *
253      * @param ejbName Description of Parameter
254      * @return Description of the Returned Value
255      * @exception XDocletException
256      */

257     protected XClass findEjb(String JavaDoc ejbName) throws XDocletException
258     {
259         Collection JavaDoc classes = getXJavaDoc().getSourceClasses();
260
261         for (Iterator JavaDoc i = classes.iterator(); i.hasNext(); ) {
262             XClass clazz = (XClass) i.next();
263
264             if (isEjb(clazz) && ejbName.equals(getEjbNameFor(clazz))) {
265                 return clazz;
266             }
267         }
268
269         throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.NOT_DEFINED, new String JavaDoc[]{ejbName}));
270     }
271 }
272
Popular Tags