1717package org .springframework .jmx .export .annotation ;
1818
1919import java .lang .annotation .Annotation ;
20+ import java .lang .reflect .Array ;
2021import java .lang .reflect .Method ;
22+ import java .util .Collection ;
23+ import java .util .Set ;
2124
25+ import org .springframework .beans .BeanUtils ;
2226import org .springframework .beans .annotation .AnnotationBeanUtils ;
2327import org .springframework .beans .factory .BeanFactory ;
2428import org .springframework .beans .factory .BeanFactoryAware ;
2529import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
2630import org .springframework .core .annotation .AnnotationUtils ;
2731import org .springframework .jmx .export .metadata .InvalidMetadataException ;
2832import org .springframework .jmx .export .metadata .JmxAttributeSource ;
29- import org .springframework .jmx .export .metadata .ManagedAttribute ;
30- import org .springframework .jmx .export .metadata .ManagedMetric ;
31- import org .springframework .jmx .export .metadata .ManagedNotification ;
32- import org .springframework .jmx .export .metadata .ManagedOperation ;
33- import org .springframework .jmx .export .metadata .ManagedOperationParameter ;
34- import org .springframework .jmx .export .metadata .ManagedResource ;
3533import org .springframework .util .StringValueResolver ;
3634
3735/**
3836 * Implementation of the {@code JmxAttributeSource} interface that
39- * reads JDK 1.5+ annotations and exposes the corresponding attributes.
37+ * reads annotations and exposes the corresponding attributes.
4038 *
4139 * @author Rob Harrop
4240 * @author Juergen Hoeller
4341 * @author Jennifer Hickey
4442 * @since 1.2
45- * @see org.springframework.jmx.export.annotation. ManagedResource
46- * @see org.springframework.jmx.export.annotation. ManagedAttribute
47- * @see org.springframework.jmx.export.annotation. ManagedOperation
43+ * @see ManagedResource
44+ * @see ManagedAttribute
45+ * @see ManagedOperation
4846 */
4947public class AnnotationJmxAttributeSource implements JmxAttributeSource , BeanFactoryAware {
5048
@@ -66,25 +64,23 @@ public String resolveStringValue(String strVal) {
6664 }
6765
6866 @ Override
69- public ManagedResource getManagedResource (Class <?> beanClass ) throws InvalidMetadataException {
70- org .springframework .jmx .export .annotation .ManagedResource ann =
71- AnnotationUtils .getAnnotation (beanClass , org .springframework .jmx .export .annotation .ManagedResource .class );
67+ public org .springframework .jmx .export .metadata .ManagedResource getManagedResource (Class <?> beanClass ) throws InvalidMetadataException {
68+ ManagedResource ann = AnnotationUtils .findAnnotation (beanClass , ManagedResource .class );
7269 if (ann == null ) {
7370 return null ;
7471 }
75- ManagedResource managedResource = new ManagedResource ();
72+ org . springframework . jmx . export . metadata . ManagedResource managedResource = new org . springframework . jmx . export . metadata . ManagedResource ();
7673 AnnotationBeanUtils .copyPropertiesToBean (ann , managedResource , this .embeddedValueResolver );
7774 return managedResource ;
7875 }
7976
8077 @ Override
81- public ManagedAttribute getManagedAttribute (Method method ) throws InvalidMetadataException {
82- org .springframework .jmx .export .annotation .ManagedAttribute ann =
83- AnnotationUtils .findAnnotation (method , org .springframework .jmx .export .annotation .ManagedAttribute .class );
78+ public org .springframework .jmx .export .metadata .ManagedAttribute getManagedAttribute (Method method ) throws InvalidMetadataException {
79+ ManagedAttribute ann = AnnotationUtils .findAnnotation (method , ManagedAttribute .class );
8480 if (ann == null ) {
8581 return null ;
8682 }
87- ManagedAttribute managedAttribute = new ManagedAttribute ();
83+ org . springframework . jmx . export . metadata . ManagedAttribute managedAttribute = new org . springframework . jmx . export . metadata . ManagedAttribute ();
8884 AnnotationBeanUtils .copyPropertiesToBean (ann , managedAttribute , "defaultValue" );
8985 if (ann .defaultValue ().length () > 0 ) {
9086 managedAttribute .setDefaultValue (ann .defaultValue ());
@@ -93,65 +89,53 @@ public ManagedAttribute getManagedAttribute(Method method) throws InvalidMetadat
9389 }
9490
9591 @ Override
96- public ManagedMetric getManagedMetric (Method method ) throws InvalidMetadataException {
97- org .springframework .jmx .export .annotation .ManagedMetric ann =
98- AnnotationUtils .findAnnotation (method , org .springframework .jmx .export .annotation .ManagedMetric .class );
99- if (ann == null ) {
100- return null ;
101- }
102- ManagedMetric managedMetric = new ManagedMetric ();
103- AnnotationBeanUtils .copyPropertiesToBean (ann , managedMetric );
104- return managedMetric ;
92+ public org .springframework .jmx .export .metadata .ManagedMetric getManagedMetric (Method method ) throws InvalidMetadataException {
93+ ManagedMetric ann = AnnotationUtils .findAnnotation (method , ManagedMetric .class );
94+ return copyPropertiesToBean (ann , org .springframework .jmx .export .metadata .ManagedMetric .class );
10595 }
10696
10797 @ Override
108- public ManagedOperation getManagedOperation (Method method ) throws InvalidMetadataException {
109- Annotation ann = AnnotationUtils .findAnnotation (method , org .springframework .jmx .export .annotation .ManagedOperation .class );
110- if (ann == null ) {
111- return null ;
112- }
113- ManagedOperation op = new ManagedOperation ();
114- AnnotationBeanUtils .copyPropertiesToBean (ann , op );
115- return op ;
98+ public org .springframework .jmx .export .metadata .ManagedOperation getManagedOperation (Method method ) throws InvalidMetadataException {
99+ ManagedOperation ann = AnnotationUtils .findAnnotation (method , ManagedOperation .class );
100+ return copyPropertiesToBean (ann , org .springframework .jmx .export .metadata .ManagedOperation .class );
116101 }
117102
118103 @ Override
119- public ManagedOperationParameter [] getManagedOperationParameters (Method method )
104+ public org . springframework . jmx . export . metadata . ManagedOperationParameter [] getManagedOperationParameters (Method method )
120105 throws InvalidMetadataException {
121106
122- ManagedOperationParameters params = AnnotationUtils .findAnnotation (method , ManagedOperationParameters .class );
123- ManagedOperationParameter [] result = null ;
124- if (params == null ) {
125- result = new ManagedOperationParameter [0 ];
126- }
127- else {
128- Annotation [] paramData = params .value ();
129- result = new ManagedOperationParameter [paramData .length ];
130- for (int i = 0 ; i < paramData .length ; i ++) {
131- Annotation annotation = paramData [i ];
132- ManagedOperationParameter managedOperationParameter = new ManagedOperationParameter ();
133- AnnotationBeanUtils .copyPropertiesToBean (annotation , managedOperationParameter );
134- result [i ] = managedOperationParameter ;
135- }
136- }
137- return result ;
107+ Set <ManagedOperationParameter > anns = AnnotationUtils .getRepeatableAnnotations (
108+ method , ManagedOperationParameter .class , ManagedOperationParameters .class );
109+ return copyPropertiesToBeanArray (anns , org .springframework .jmx .export .metadata .ManagedOperationParameter .class );
138110 }
139111
140112 @ Override
141- public ManagedNotification [] getManagedNotifications (Class <?> clazz ) throws InvalidMetadataException {
142- ManagedNotifications notificationsAnn = AnnotationUtils .getAnnotation (clazz , ManagedNotifications .class );
143- if (notificationsAnn == null ) {
144- return new ManagedNotification [0 ];
113+ public org .springframework .jmx .export .metadata .ManagedNotification [] getManagedNotifications (Class <?> clazz )
114+ throws InvalidMetadataException {
115+
116+ Set <ManagedNotification > anns = AnnotationUtils .getRepeatableAnnotations (
117+ clazz , ManagedNotification .class , ManagedNotifications .class );
118+ return copyPropertiesToBeanArray (anns , org .springframework .jmx .export .metadata .ManagedNotification .class );
119+ }
120+
121+
122+ @ SuppressWarnings ("unchecked" )
123+ private static <T > T [] copyPropertiesToBeanArray (Collection <? extends Annotation > anns , Class <T > beanClass ) {
124+ T [] beans = (T []) Array .newInstance (beanClass , anns .size ());
125+ int i = 0 ;
126+ for (Annotation ann : anns ) {
127+ beans [i ++] = copyPropertiesToBean (ann , beanClass );
145128 }
146- Annotation [] notifications = notificationsAnn .value ();
147- ManagedNotification [] result = new ManagedNotification [notifications .length ];
148- for (int i = 0 ; i < notifications .length ; i ++) {
149- Annotation notification = notifications [i ];
150- ManagedNotification managedNotification = new ManagedNotification ();
151- AnnotationBeanUtils .copyPropertiesToBean (notification , managedNotification );
152- result [i ] = managedNotification ;
129+ return beans ;
130+ }
131+
132+ private static <T > T copyPropertiesToBean (Annotation ann , Class <T > beanClass ) {
133+ if (ann == null ) {
134+ return null ;
153135 }
154- return result ;
136+ T bean = BeanUtils .instantiate (beanClass );
137+ AnnotationBeanUtils .copyPropertiesToBean (ann , bean );
138+ return bean ;
155139 }
156140
157141}
0 commit comments