1 16 17 package org.springframework.jmx.export.annotation; 18 19 import java.beans.PropertyDescriptor ; 20 import java.lang.annotation.Annotation ; 21 import java.lang.reflect.Method ; 22 23 import org.springframework.beans.BeanUtils; 24 import org.springframework.beans.annotation.AnnotationBeanUtils; 25 import org.springframework.jmx.export.metadata.InvalidMetadataException; 26 import org.springframework.jmx.export.metadata.JmxAttributeSource; 27 import org.springframework.jmx.export.metadata.ManagedAttribute; 28 import org.springframework.jmx.export.metadata.ManagedNotification; 29 import org.springframework.jmx.export.metadata.ManagedOperation; 30 import org.springframework.jmx.export.metadata.ManagedOperationParameter; 31 import org.springframework.jmx.export.metadata.ManagedResource; 32 import org.springframework.core.annotation.AnnotationUtils; 33 34 50 public class AnnotationJmxAttributeSource implements JmxAttributeSource { 51 52 public ManagedResource getManagedResource(Class beanClass) throws InvalidMetadataException { 53 Annotation ann = beanClass.getAnnotation(org.springframework.jmx.export.annotation.ManagedResource.class); 54 if (ann == null) { 55 return null; 56 } 57 ManagedResource managedResource = new ManagedResource(); 58 AnnotationBeanUtils.copyPropertiesToBean(ann, managedResource); 59 return managedResource; 60 } 61 62 public ManagedAttribute getManagedAttribute(Method method) throws InvalidMetadataException { 63 org.springframework.jmx.export.annotation.ManagedAttribute ann = 64 AnnotationUtils.getAnnotation(method, org.springframework.jmx.export.annotation.ManagedAttribute.class); 65 if (ann == null) { 66 return null; 67 } 68 ManagedAttribute managedAttribute = new ManagedAttribute(); 69 AnnotationBeanUtils.copyPropertiesToBean(ann, managedAttribute, "defaultValue"); 70 if (ann.defaultValue().length() > 0) { 71 managedAttribute.setDefaultValue(ann.defaultValue()); 72 } 73 return managedAttribute; 74 } 75 76 public ManagedOperation getManagedOperation(Method method) throws InvalidMetadataException { 77 PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method); 78 if (pd != null) { 79 throw new InvalidMetadataException( 80 "The ManagedOperation attribute is not valid for JavaBean properties. Use ManagedAttribute instead."); 81 } 82 83 Annotation ann = AnnotationUtils.getAnnotation(method, org.springframework.jmx.export.annotation.ManagedOperation.class); 84 if (ann == null) { 85 return null; 86 } 87 88 ManagedOperation op = new ManagedOperation(); 89 AnnotationBeanUtils.copyPropertiesToBean(ann, op); 90 return op; 91 } 92 93 public ManagedOperationParameter[] getManagedOperationParameters(Method method) 94 throws InvalidMetadataException { 95 96 ManagedOperationParameters params = AnnotationUtils.getAnnotation(method, ManagedOperationParameters.class); 97 ManagedOperationParameter[] result = null; 98 if (params == null) { 99 result = new ManagedOperationParameter[0]; 100 } 101 else { 102 Annotation [] paramData = params.value(); 103 result = new ManagedOperationParameter[paramData.length]; 104 for (int i = 0; i < paramData.length; i++) { 105 Annotation annotation = paramData[i]; 106 ManagedOperationParameter managedOperationParameter = new ManagedOperationParameter(); 107 AnnotationBeanUtils.copyPropertiesToBean(annotation, managedOperationParameter); 108 result[i] = managedOperationParameter; 109 } 110 } 111 return result; 112 } 113 114 public ManagedNotification[] getManagedNotifications(Class clazz) throws InvalidMetadataException { 115 ManagedNotifications notificationsAnn = (ManagedNotifications) clazz.getAnnotation(ManagedNotifications.class); 116 if(notificationsAnn == null) { 117 return new ManagedNotification[0]; 118 } 119 Annotation [] notifications = notificationsAnn.value(); 120 ManagedNotification[] result = new ManagedNotification[notifications.length]; 121 for (int i = 0; i < notifications.length; i++) { 122 Annotation notification = notifications[i]; 123 124 ManagedNotification managedNotification = new ManagedNotification(); 125 AnnotationBeanUtils.copyPropertiesToBean(notification, managedNotification); 126 result[i] = managedNotification; 127 } 128 return result; 129 } 130 131 } 132 | Popular Tags |