1 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 28 public class JMXTagsHandler extends AbstractProgramElementTagsHandler 29 { 30 33 protected MethodTagsHandler handler = new MethodTagsHandler(); 34 35 38 protected Map attributes = Collections.synchronizedMap(new HashMap()); 39 40 43 protected int index = 0; 44 45 55 public String mbeanName() throws XDocletException 56 { 57 return getMBeanName(getCurrentClass()); 58 } 59 60 68 public void ifIsGetterMethod(String template, Properties attributes) throws XDocletException 69 { 70 if (isGetterMethod()) { 71 generate(template); 72 } 73 } 74 75 83 public void ifIsSetterMethod(String template, Properties attributes) throws XDocletException 84 { 85 if (isSetterMethod()) { 86 generate(template); 87 } 88 } 89 90 96 public void ifHasAttributeDescription(String template, Properties attributes) throws XDocletException 97 { 98 boolean hasGetterMethod = false; 99 String name = handler.methodNameWithoutPrefix(); 100 String 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 133 public void forAllIndexedMethodParams(String 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 151 public void forAllIndexedConstructorParams(String 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 168 public String indexedMethodParamValue(Properties attributes) throws XDocletException 169 { 170 String tagName = attributes.getProperty("tagName"); 171 String 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 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 193 public String indexedConstructorParamValue(Properties attributes) throws XDocletException 194 { 195 if (attributes == null) 196 throw new XDocletException(Translator.getString(XDocletModulesJmxMessages.class, XDocletModulesJmxMessages.MISSING_ATTRIBUTE)); 197 String tagName = attributes.getProperty("tagName"); 198 String 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 String 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 221 public String constructorSignature() throws XDocletException 222 { 223 XConstructor currentConstructor = getCurrentConstructor(); 224 String signature = currentConstructor.getSignature(false); 225 226 while (signature.indexOf(" ") != -1) { 228 int index = signature.indexOf(" "); 229 String before = signature.substring(0, index); 230 String after = signature.substring(index + 1, signature.length()); 231 232 signature = before + after; 233 } 234 return "public " + currentConstructor.getName() + signature; 236 } 237 238 245 protected String 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 []{"@jmx:mbean", clazz.getQualifiedName()})); 252 } 253 254 String 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 []{"name", "@jmx:mbean", clazz.getQualifiedName()})); 259 } 260 261 return param_val; 262 } 263 264 269 protected boolean isGetterMethod() 270 { 271 String 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 288 protected boolean isSetterMethod() 289 { 290 String 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 |