1 16 17 package org.springframework.jmx.export.assembler; 18 19 import java.beans.PropertyDescriptor ; 20 import java.lang.reflect.Method ; 21 22 import javax.management.Descriptor ; 23 import javax.management.MBeanParameterInfo ; 24 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 25 26 import org.springframework.aop.support.AopUtils; 27 import org.springframework.beans.BeanUtils; 28 import org.springframework.beans.factory.InitializingBean; 29 import org.springframework.jmx.export.metadata.InvalidMetadataException; 30 import org.springframework.jmx.export.metadata.JmxAttributeSource; 31 import org.springframework.jmx.export.metadata.JmxMetadataUtils; 32 import org.springframework.jmx.export.metadata.ManagedAttribute; 33 import org.springframework.jmx.export.metadata.ManagedNotification; 34 import org.springframework.jmx.export.metadata.ManagedOperation; 35 import org.springframework.jmx.export.metadata.ManagedOperationParameter; 36 import org.springframework.jmx.export.metadata.ManagedResource; 37 import org.springframework.util.StringUtils; 38 39 58 public class MetadataMBeanInfoAssembler extends AbstractReflectiveMBeanInfoAssembler 59 implements AutodetectCapableMBeanInfoAssembler, InitializingBean { 60 61 private JmxAttributeSource attributeSource; 62 63 64 70 public void setAttributeSource(JmxAttributeSource attributeSource) { 71 this.attributeSource = attributeSource; 72 } 73 74 public void afterPropertiesSet() { 75 if (this.attributeSource == null) { 76 throw new IllegalArgumentException ("'attributeSource' is required"); 77 } 78 } 79 80 81 85 protected void checkManagedBean(Object managedBean) throws IllegalArgumentException { 86 if (AopUtils.isJdkDynamicProxy(managedBean)) { 87 throw new IllegalArgumentException ( 88 "MetadataMBeanInfoAssembler does not support JDK dynamic proxies - " + 89 "export the target beans directly or use CGLIB proxies instead"); 90 } 91 } 92 93 99 public boolean includeBean(Class beanClass, String beanName) { 100 return (this.attributeSource.getManagedResource(getClassToExpose(beanClass)) != null); 101 } 102 103 109 protected boolean includeReadAttribute(Method method, String beanKey) { 110 return hasManagedAttribute(method); 111 } 112 113 119 protected boolean includeWriteAttribute(Method method, String beanKey) { 120 return hasManagedAttribute(method); 121 } 122 123 129 protected boolean includeOperation(Method method, String beanKey) { 130 PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method); 131 if (pd != null) { 132 return hasManagedAttribute(method); 133 } 134 else { 135 return hasManagedOperation(method); 136 } 137 } 138 139 142 private boolean hasManagedAttribute(Method method) { 143 return (this.attributeSource.getManagedAttribute(method) != null); 144 } 145 146 150 private boolean hasManagedOperation(Method method) { 151 return (this.attributeSource.getManagedOperation(method) != null); 152 } 153 154 155 159 protected String getDescription(Object managedBean, String beanKey) { 160 ManagedResource mr = this.attributeSource.getManagedResource(getClassToExpose(managedBean)); 161 return (mr != null ? mr.getDescription() : ""); 162 } 163 164 169 protected String getAttributeDescription(PropertyDescriptor propertyDescriptor, String beanKey) { 170 Method readMethod = propertyDescriptor.getReadMethod(); 171 Method writeMethod = propertyDescriptor.getWriteMethod(); 172 173 ManagedAttribute getter = 174 (readMethod != null) ? this.attributeSource.getManagedAttribute(readMethod) : null; 175 ManagedAttribute setter = 176 (writeMethod != null) ? this.attributeSource.getManagedAttribute(writeMethod) : null; 177 178 if (getter != null && StringUtils.hasText(getter.getDescription())) { 179 return getter.getDescription(); 180 } 181 else if (setter != null && StringUtils.hasText(setter.getDescription())) { 182 return setter.getDescription(); 183 } 184 return propertyDescriptor.getDisplayName(); 185 } 186 187 191 protected String getOperationDescription(Method method, String beanKey) { 192 PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method); 193 if (pd != null) { 194 ManagedAttribute ma = this.attributeSource.getManagedAttribute(method); 195 if (ma != null && StringUtils.hasText(ma.getDescription())) { 196 return ma.getDescription(); 197 } 198 return method.getName(); 199 } 200 else { 201 ManagedOperation mo = this.attributeSource.getManagedOperation(method); 202 if (mo != null && StringUtils.hasText(mo.getDescription())) { 203 return mo.getDescription(); 204 } 205 return method.getName(); 206 } 207 } 208 209 214 protected MBeanParameterInfo [] getOperationParameters(Method method, String beanKey) { 215 ManagedOperationParameter[] params = this.attributeSource.getManagedOperationParameters(method); 216 if (params == null || params.length == 0) { 217 return new MBeanParameterInfo [0]; 218 } 219 220 MBeanParameterInfo [] parameterInfo = new MBeanParameterInfo [params.length]; 221 Class [] methodParameters = method.getParameterTypes(); 222 223 for (int i = 0; i < params.length; i++) { 224 ManagedOperationParameter param = params[i]; 225 parameterInfo[i] = 226 new MBeanParameterInfo (param.getName(), methodParameters[i].getName(), param.getDescription()); 227 } 228 229 return parameterInfo; 230 } 231 232 236 protected ModelMBeanNotificationInfo [] getNotificationInfo(Object managedBean, String beanKey) { 237 ManagedNotification[] notificationAttributes = 238 this.attributeSource.getManagedNotifications(getClassToExpose(managedBean)); 239 ModelMBeanNotificationInfo [] notificationInfos = 240 new ModelMBeanNotificationInfo [notificationAttributes.length]; 241 242 for (int i = 0; i < notificationAttributes.length; i++) { 243 ManagedNotification attribute = notificationAttributes[i]; 244 notificationInfos[i] = JmxMetadataUtils.convertToModelMBeanNotificationInfo(attribute); 245 } 246 247 return notificationInfos; 248 } 249 250 256 protected void populateMBeanDescriptor(Descriptor desc, Object managedBean, String beanKey) { 257 ManagedResource mr = this.attributeSource.getManagedResource(getClassToExpose(managedBean)); 258 if (mr == null) { 259 throw new InvalidMetadataException( 260 "No ManagedResource attribute found for class: " + getClassToExpose(managedBean)); 261 } 262 263 applyCurrencyTimeLimit(desc, mr.getCurrencyTimeLimit()); 264 265 if (mr.isLog()) { 267 desc.setField(FIELD_LOG, "true"); 268 } 269 if (StringUtils.hasLength(mr.getLogFile())) { 270 desc.setField(FIELD_LOG_FILE, mr.getLogFile()); 271 } 272 273 if (StringUtils.hasLength(mr.getPersistPolicy())) { 274 desc.setField(FIELD_PERSIST_POLICY, mr.getPersistPolicy()); 275 } 276 if (mr.getPersistPeriod() >= 0) { 277 desc.setField(FIELD_PERSIST_PERIOD, Integer.toString(mr.getPersistPeriod())); 278 } 279 if (StringUtils.hasLength(mr.getPersistName())) { 280 desc.setField(FIELD_PERSIST_NAME, mr.getPersistName()); 281 } 282 if (StringUtils.hasLength(mr.getPersistLocation())) { 283 desc.setField(FIELD_PERSIST_LOCATION, mr.getPersistLocation()); 284 } 285 } 286 287 293 protected void populateAttributeDescriptor(Descriptor desc, Method getter, Method setter, String beanKey) { 294 ManagedAttribute gma = 295 (getter == null) ? ManagedAttribute.EMPTY : this.attributeSource.getManagedAttribute(getter); 296 ManagedAttribute sma = 297 (setter == null) ? ManagedAttribute.EMPTY : this.attributeSource.getManagedAttribute(setter); 298 299 applyCurrencyTimeLimit(desc, resolveIntDescriptor(gma.getCurrencyTimeLimit(), sma.getCurrencyTimeLimit())); 300 301 Object defaultValue = resolveObjectDescriptor(gma.getDefaultValue(), sma.getDefaultValue()); 302 desc.setField(FIELD_DEFAULT, defaultValue); 303 304 String persistPolicy = resolveStringDescriptor(gma.getPersistPolicy(), sma.getPersistPolicy()); 305 if (StringUtils.hasLength(persistPolicy)) { 306 desc.setField(FIELD_PERSIST_POLICY, persistPolicy); 307 } 308 int persistPeriod = resolveIntDescriptor(gma.getPersistPeriod(), sma.getPersistPeriod()); 309 if (persistPeriod >= 0) { 310 desc.setField(FIELD_PERSIST_PERIOD, Integer.toString(persistPeriod)); 311 } 312 } 313 314 319 protected void populateOperationDescriptor(Descriptor desc, Method method, String beanKey) { 320 ManagedOperation mo = this.attributeSource.getManagedOperation(method); 321 if (mo != null) { 322 applyCurrencyTimeLimit(desc, mo.getCurrencyTimeLimit()); 323 } 324 } 325 326 335 private int resolveIntDescriptor(int getter, int setter) { 336 return (getter >= setter ? getter : setter); 337 } 338 339 347 private Object resolveObjectDescriptor(Object getter, Object setter) { 348 return (getter != null ? getter : setter); 349 } 350 351 361 private String resolveStringDescriptor(String getter, String setter) { 362 return (StringUtils.hasLength(getter) ? getter : setter); 363 } 364 365 } 366 | Popular Tags |