KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > jmx > JMXTagsHandler


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

5 package xdoclet.modules.jmx;
6
7 import java.util.*;
8
9 import xjavadoc.*;
10
11 import xdoclet.XDocletException;
12 import xdoclet.XDocletMessages;
13 import xdoclet.tagshandler.AbstractProgramElementTagsHandler;
14 import xdoclet.tagshandler.MethodTagsHandler;
15
16 import xdoclet.util.Translator;
17
18 /**
19  * @author Jerome Bernard (jerome.bernard@xtremejava.com)
20  * @author Ara Abrahamian (ara_e@email.com)
21  * @created 31 January 2002
22  * @xdoclet.taghandler namespace="Jmx"
23  * @version $Revision: 1.12 $
24  * @todo attributes - XXX: Does this need to be synchronized?
25  * @todo ifIsGetterMethod, ifIsSetterMethod - TODO: There is a big overlap here with stuff in ejb - have
26  * a look.
27  */

28 public class JMXTagsHandler extends AbstractProgramElementTagsHandler
29 {
30     /**
31      * For use in extracting method names.
32      */

33     protected MethodTagsHandler handler = new MethodTagsHandler();
34
35     /**
36      * Collection of attributes. XXX: Does this need to be synchronized?
37      */

38     protected Map attributes = Collections.synchronizedMap(new HashMap());
39
40     /**
41      * For looping through indexed tags.
42      */

43     protected int index = 0;
44
45     /**
46      * Returns the MBean name for the current class. Looks for the name parameter in the jmx:mbean tag on the current
47      * class.
48      *
49      * @return Description of the Returned Value
50      * @exception XDocletException Description of Exception
51      * @doc.tag type="content" description="Returns the MBean name for the current class. Looks for
52      * the name parameter in the jmx:mbean tag on the current class."
53      * @see #getMBeanName
54      */

55     public String JavaDoc mbeanName() throws XDocletException
56     {
57         return getMBeanName(getCurrentClass());
58     }
59
60     /**
61      * TODO: There is a big overlap here with stuff in ejb - have a look.
62      *
63      * @param template
64      * @param attributes
65      * @exception XDocletException
66      * @doc.tag type="block" description="Executes the block if the current method is a getter"
67      */

68     public void ifIsGetterMethod(String JavaDoc template, Properties attributes) throws XDocletException
69     {
70         if (isGetterMethod()) {
71             generate(template);
72         }
73     }
74
75     /**
76      * TODO: There is a big overlap here with stuff in ejb - have a look.
77      *
78      * @param template
79      * @param attributes
80      * @exception XDocletException
81      * @doc.tag type="block" description="Executes the block if the current method is a setter"
82      */

83     public void ifIsSetterMethod(String JavaDoc template, Properties attributes) throws XDocletException
84     {
85         if (isSetterMethod()) {
86             generate(template);
87         }
88     }
89
90     /**
91      * @param template
92      * @param attributes
93      * @exception XDocletException
94      * @doc.tag type="block"
95      */

96     public void ifHasAttributeDescription(String JavaDoc template, Properties attributes) throws XDocletException
97     {
98         boolean hasGetterMethod = false;
99         String JavaDoc name = handler.methodNameWithoutPrefix();
100         String JavaDoc description = getTagValue(
101             FOR_METHOD,
102             "jmx:managed-attribute",
103             "description",
104             null,
105             null,
106             true,
107             false
108             );
109
110         Collection methods = getCurrentClass().getMethods();
111
112         for (Iterator i = methods.iterator(); i.hasNext(); ) {
113             XMethod method = (XMethod)i.next();
114
115             if (method.getName().equals("get" + name) || method.getName().equals("is" + name)) {
116                 hasGetterMethod = true;
117             }
118         }
119
120         if ((isSetterMethod() && !hasGetterMethod) || isGetterMethod()) {
121             attributes.put(name, description);
122             generate(template);
123         }
124     }
125
126     /**
127      * Describe what the method does
128      *
129      * @param template Describe what the parameter does
130      * @param attributes Describe what the parameter does
131      * @exception XDocletException Describe the exception
132      */

133     public void forAllIndexedMethodParams(String JavaDoc template, Properties attributes) throws XDocletException
134     {
135         Collection tags = getCurrentMethod().getDoc().getTags("jmx:managed-operation-parameter");
136
137         index = 0;
138         for (int i = 0; i < tags.size(); i++) {
139             generate(template);
140             index++;
141         }
142     }
143
144     /**
145      * Describe what the method does
146      *
147      * @param template Describe what the parameter does
148      * @param attributes Describe what the parameter does
149      * @exception XDocletException Describe the exception
150      */

151     public void forAllIndexedConstructorParams(String JavaDoc template, Properties attributes) throws XDocletException
152     {
153         Collection tags = getCurrentConstructor().getDoc().getTags("jmx:managed-constructor-parameter");
154
155         index = 0;
156         for (int i = 0; i < tags.size(); i++) {
157             generate(template);
158             index++;
159         }
160     }
161
162     /**
163      * @param attributes Describe what the parameter does
164      * @return Describe the return value
165      * @exception XDocletException Describe the exception
166      * @todo refactor common code with indexedConstructorParamValue into a private method
167      */

168     public String JavaDoc indexedMethodParamValue(Properties attributes) throws XDocletException
169     {
170         String JavaDoc tagName = attributes.getProperty("tagName");
171         String JavaDoc paramName = attributes.getProperty("paramName");
172
173         if (tagName == null || paramName == null) {
174             throw new XDocletException(Translator.getString(XDocletModulesJmxMessages.class, XDocletModulesJmxMessages.MISSING_ATTRIBUTE));
175         }
176
177         List tags = Arrays.asList(getCurrentMethod().getDoc().getTags(tagName).toArray());
178         XTag tag = (XTag) tags.get(index);
179         String JavaDoc tagContent = tag.getValue();
180         int begin = tagContent.indexOf(paramName + "=\"") + paramName.length() + 2;
181         int end = tagContent.indexOf("\"", begin);
182
183         return tagContent.substring(begin, end);
184     }
185
186     /**
187      * Describe what the method does
188      *
189      * @param attributes Describe what the parameter does
190      * @return Describe the return value
191      * @exception XDocletException Describe the exception
192      */

193     public String JavaDoc indexedConstructorParamValue(Properties attributes) throws XDocletException
194     {
195         if (attributes == null)
196             throw new XDocletException(Translator.getString(XDocletModulesJmxMessages.class, XDocletModulesJmxMessages.MISSING_ATTRIBUTE));
197         String JavaDoc tagName = attributes.getProperty("tagName");
198         String JavaDoc paramName = attributes.getProperty("paramName");
199
200         if (tagName == null || paramName == null) {
201             throw new XDocletException(Translator.getString(XDocletModulesJmxMessages.class, XDocletModulesJmxMessages.MISSING_ATTRIBUTE));
202         }
203
204         List tags = Arrays.asList(getCurrentConstructor().getDoc().getTags(tagName).toArray());
205         XTag tag = (XTag) tags.get(index);
206
207         // This is weird. XTag has methods for this! (Aslak)
208
String JavaDoc tagContent = tag.getValue();
209         int begin = tagContent.indexOf(paramName + "=\"") + paramName.length() + 2;
210         int end = tagContent.indexOf("\"", begin);
211
212         return tagContent.substring(begin, end);
213     }
214
215     /**
216      * Describe what the method does
217      *
218      * @return Describe the return value
219      * @exception XDocletException Describe the exception
220      */

221     public String JavaDoc constructorSignature() throws XDocletException
222     {
223         XConstructor currentConstructor = getCurrentConstructor();
224         String JavaDoc signature = currentConstructor.getSignature(false);
225
226         // Remove spaces from the signature
227
while (signature.indexOf(" ") != -1) {
228             int index = signature.indexOf(" ");
229             String JavaDoc before = signature.substring(0, index);
230             String JavaDoc after = signature.substring(index + 1, signature.length());
231
232             signature = before + after;
233         }
234         // Build the complete signature
235
return "public " + currentConstructor.getName() + signature;
236     }
237
238     /**
239      * Implementation of {@link #mbeanName}.
240      *
241      * @param clazz
242      * @return Description of the Returned Value
243      * @exception XDocletException Description of Exception
244      */

245     protected String JavaDoc getMBeanName(XClass clazz) throws XDocletException
246     {
247         XTag bean_tag = clazz.getDoc().getTag("jmx:mbean");
248
249         if (bean_tag == null) {
250             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.CLASS_TAG_EXPECTED,
251                 new String JavaDoc[]{"@jmx:mbean", clazz.getQualifiedName()}));
252         }
253
254         String JavaDoc param_val = bean_tag.getAttributeValue("name");
255
256         if (param_val == null) {
257             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.CLASS_TAG_PARAMETER_EXPECTED,
258                 new String JavaDoc[]{"name", "@jmx:mbean", clazz.getQualifiedName()}));
259         }
260
261         return param_val;
262     }
263
264     /**
265      * @return The GetterMethod value
266      * @todo (Aslak) this is very general stuff. It should be implemented higher up in the hierarchy if it isn't
267      * already done somewhere
268      */

269     protected boolean isGetterMethod()
270     {
271         String JavaDoc methodName = getCurrentMethod().getName();
272         XClass retType = getCurrentMethod().getReturnType().getType();
273         Collection params = getCurrentMethod().getParameters();
274
275         if (!retType.getQualifiedName().equals("void") && params.size() == 0) {
276             if (methodName.startsWith("get") || (methodName.startsWith("is") && retType.getQualifiedName().equals("boolean"))) {
277                 return true;
278             }
279         }
280         return false;
281     }
282
283     /**
284      * @return The SetterMethod value
285      * @todo (Aslak) this is very general stuff. It should be implemented higher up in the hierarchy if it isn't
286      * already done somewhere
287      */

288     protected boolean isSetterMethod()
289     {
290         String JavaDoc methodName = getCurrentMethod().getName();
291         XClass retType = getCurrentMethod().getReturnType().getType();
292         Collection params = getCurrentMethod().getParameters();
293
294         return (retType.getQualifiedName().equals("void") && params.size() == 1 && methodName.startsWith("set"));
295     }
296
297 }
298
Popular Tags