1 16 17 package org.springframework.jmx.export.assembler; 18 19 import java.beans.PropertyDescriptor ; 20 import java.lang.reflect.Method ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 import javax.management.Descriptor ; 25 import javax.management.JMException ; 26 import javax.management.MBeanOperationInfo ; 27 import javax.management.MBeanParameterInfo ; 28 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 29 import javax.management.modelmbean.ModelMBeanOperationInfo ; 30 31 import org.springframework.aop.framework.AopProxyUtils; 32 import org.springframework.aop.support.AopUtils; 33 import org.springframework.beans.BeanUtils; 34 import org.springframework.core.JdkVersion; 35 import org.springframework.jmx.support.JmxUtils; 36 37 61 public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBeanInfoAssembler { 62 63 66 protected static final String FIELD_GET_METHOD = "getMethod"; 67 68 71 protected static final String FIELD_SET_METHOD = "setMethod"; 72 73 76 protected static final String FIELD_ROLE = "role"; 77 78 81 protected static final String ROLE_GETTER = "getter"; 82 83 86 protected static final String ROLE_SETTER = "setter"; 87 88 91 protected static final String ROLE_OPERATION = "operation"; 92 93 96 protected static final String FIELD_VISIBILITY = "visibility"; 97 98 103 protected static final Integer ATTRIBUTE_OPERATION_VISIBILITY = new Integer (4); 104 105 108 protected static final String FIELD_CLASS = "class"; 109 112 protected static final String FIELD_LOG = "log"; 113 114 117 protected static final String FIELD_LOG_FILE = "logFile"; 118 119 122 protected static final String FIELD_CURRENCY_TIME_LIMIT = "currencyTimeLimit"; 123 124 127 protected static final String FIELD_DEFAULT = "default"; 128 129 132 protected static final String FIELD_PERSIST_POLICY = "persistPolicy"; 133 134 137 protected static final String FIELD_PERSIST_PERIOD = "persistPeriod"; 138 139 142 protected static final String FIELD_PERSIST_LOCATION = "persistLocation"; 143 144 147 protected static final String FIELD_PERSIST_NAME = "persistName"; 148 149 150 153 private Integer defaultCurrencyTimeLimit; 154 155 158 private boolean useStrictCasing = true; 159 160 private boolean exposeClassDescriptor = false; 161 162 163 183 public void setDefaultCurrencyTimeLimit(Integer defaultCurrencyTimeLimit) { 184 this.defaultCurrencyTimeLimit = defaultCurrencyTimeLimit; 185 } 186 187 190 protected Integer getDefaultCurrencyTimeLimit() { 191 return this.defaultCurrencyTimeLimit; 192 } 193 194 201 public void setUseStrictCasing(boolean useStrictCasing) { 202 this.useStrictCasing = useStrictCasing; 203 } 204 205 208 protected boolean isUseStrictCasing() { 209 return useStrictCasing; 210 } 211 212 228 public void setExposeClassDescriptor(boolean exposeClassDescriptor) { 229 this.exposeClassDescriptor = exposeClassDescriptor; 230 } 231 232 235 protected boolean isExposeClassDescriptor() { 236 return exposeClassDescriptor; 237 } 238 239 240 252 protected ModelMBeanAttributeInfo [] getAttributeInfo(Object managedBean, String beanKey) throws JMException { 253 PropertyDescriptor [] props = BeanUtils.getPropertyDescriptors(getClassToExpose(managedBean)); 254 List infos = new ArrayList (); 255 256 for (int i = 0; i < props.length; i++) { 257 Method getter = props[i].getReadMethod(); 258 if (getter != null && getter.getDeclaringClass() == Object .class) { 259 continue; 260 } 261 if (getter != null && !includeReadAttribute(getter, beanKey)) { 262 getter = null; 263 } 264 265 Method setter = props[i].getWriteMethod(); 266 if (setter != null && !includeWriteAttribute(setter, beanKey)) { 267 setter = null; 268 } 269 270 if (getter != null || setter != null) { 271 String attrName = JmxUtils.getAttributeName(props[i], isUseStrictCasing()); 273 String description = getAttributeDescription(props[i], beanKey); 274 ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo (attrName, description, getter, setter); 275 276 Descriptor desc = info.getDescriptor(); 277 if (getter != null) { 278 desc.setField(FIELD_GET_METHOD, getter.getName()); 279 } 280 if (setter != null) { 281 desc.setField(FIELD_SET_METHOD, setter.getName()); 282 } 283 284 populateAttributeDescriptor(desc, getter, setter, beanKey); 285 info.setDescriptor(desc); 286 infos.add(info); 287 } 288 } 289 290 return (ModelMBeanAttributeInfo []) infos.toArray(new ModelMBeanAttributeInfo [infos.size()]); 291 } 292 293 305 protected ModelMBeanOperationInfo [] getOperationInfo(Object managedBean, String beanKey) { 306 Method [] methods = getClassToExpose(managedBean).getMethods(); 307 List infos = new ArrayList (); 308 309 for (int i = 0; i < methods.length; i++) { 310 Method method = methods[i]; 311 if (JdkVersion.isAtLeastJava15() && method.isSynthetic()) { 312 continue; 313 } 314 if (method.getDeclaringClass().equals(Object .class)) { 315 continue; 316 } 317 318 ModelMBeanOperationInfo info = null; 319 PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method); 320 if (pd != null) { 321 if ((method.equals(pd.getReadMethod()) && includeReadAttribute(method, beanKey)) || 322 (method.equals(pd.getWriteMethod()) && includeWriteAttribute(method, beanKey))) { 323 info = createModelMBeanOperationInfo(method, pd.getName(), beanKey); 326 Descriptor desc = info.getDescriptor(); 327 if (method.equals(pd.getReadMethod())) { 328 desc.setField(FIELD_ROLE, ROLE_GETTER); 329 } 330 else { 331 desc.setField(FIELD_ROLE, ROLE_SETTER); 332 } 333 desc.setField(FIELD_VISIBILITY, ATTRIBUTE_OPERATION_VISIBILITY); 334 if (isExposeClassDescriptor()) { 335 desc.setField(FIELD_CLASS, getClassForDescriptor(managedBean).getName()); 336 } 337 info.setDescriptor(desc); 338 } 339 } 340 else if (includeOperation(method, beanKey)) { 341 info = createModelMBeanOperationInfo(method, method.getName(), beanKey); 342 Descriptor desc = info.getDescriptor(); 343 desc.setField(FIELD_ROLE, ROLE_OPERATION); 344 if (isExposeClassDescriptor()) { 345 desc.setField(FIELD_CLASS, getClassForDescriptor(managedBean).getName()); 346 } 347 populateOperationDescriptor(desc, method, beanKey); 348 info.setDescriptor(desc); 349 } 350 351 if (info != null) { 352 infos.add(info); 353 } 354 } 355 356 return (ModelMBeanOperationInfo []) infos.toArray(new ModelMBeanOperationInfo [infos.size()]); 357 } 358 359 368 protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, String name, String beanKey) { 369 MBeanParameterInfo [] params = getOperationParameters(method, beanKey); 370 if (params.length == 0) { 371 return new ModelMBeanOperationInfo (getOperationDescription(method, beanKey), method); 372 } 373 else { 374 return new ModelMBeanOperationInfo (name, 375 getOperationDescription(method, beanKey), 376 getOperationParameters(method, beanKey), 377 method.getReturnType().getName(), 378 MBeanOperationInfo.UNKNOWN); 379 } 380 } 381 382 393 protected Class getClassForDescriptor(Object managedBean) { 394 if (AopUtils.isJdkDynamicProxy(managedBean)) { 395 return AopProxyUtils.proxiedUserInterfaces(managedBean)[0]; 396 } 397 return getClassToExpose(managedBean); 398 } 399 400 401 409 protected abstract boolean includeReadAttribute(Method method, String beanKey); 410 411 419 protected abstract boolean includeWriteAttribute(Method method, String beanKey); 420 421 428 protected abstract boolean includeOperation(Method method, String beanKey); 429 430 431 440 protected String getAttributeDescription(PropertyDescriptor propertyDescriptor, String beanKey) { 441 return propertyDescriptor.getDisplayName(); 442 } 443 444 453 protected String getOperationDescription(Method method, String beanKey) { 454 return method.getName(); 455 } 456 457 465 protected MBeanParameterInfo [] getOperationParameters(Method method, String beanKey) { 466 return new MBeanParameterInfo [0]; 467 } 468 469 470 481 protected void populateMBeanDescriptor(Descriptor descriptor, Object managedBean, String beanKey) { 482 applyDefaultCurrencyTimeLimit(descriptor); 483 } 484 485 497 protected void populateAttributeDescriptor(Descriptor desc, Method getter, Method setter, String beanKey) { 498 applyDefaultCurrencyTimeLimit(desc); 499 } 500 501 512 protected void populateOperationDescriptor(Descriptor desc, Method method, String beanKey) { 513 applyDefaultCurrencyTimeLimit(desc); 514 } 515 516 522 protected final void applyDefaultCurrencyTimeLimit(Descriptor desc) { 523 if (getDefaultCurrencyTimeLimit() != null) { 524 desc.setField(FIELD_CURRENCY_TIME_LIMIT, getDefaultCurrencyTimeLimit().toString()); 525 } 526 } 527 528 539 protected void applyCurrencyTimeLimit(Descriptor desc, int currencyTimeLimit) { 540 if (currencyTimeLimit > 0) { 541 desc.setField(FIELD_CURRENCY_TIME_LIMIT, Integer.toString(currencyTimeLimit)); 543 } 544 else if (currencyTimeLimit == 0) { 545 desc.setField(FIELD_CURRENCY_TIME_LIMIT, Integer.toString(Integer.MAX_VALUE)); 547 } 548 else { 549 applyDefaultCurrencyTimeLimit(desc); 551 } 552 } 553 554 } 555 | Popular Tags |