17
17
package org .springframework .jmx .export .annotation ;
18
18
19
19
import java .lang .annotation .Annotation ;
20
+ import java .lang .reflect .Array ;
20
21
import java .lang .reflect .Method ;
22
+ import java .util .Collection ;
23
+ import java .util .Set ;
21
24
25
+ import org .springframework .beans .BeanUtils ;
22
26
import org .springframework .beans .annotation .AnnotationBeanUtils ;
23
27
import org .springframework .beans .factory .BeanFactory ;
24
28
import org .springframework .beans .factory .BeanFactoryAware ;
25
29
import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
26
30
import org .springframework .core .annotation .AnnotationUtils ;
27
31
import org .springframework .jmx .export .metadata .InvalidMetadataException ;
28
32
import 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 ;
35
33
import org .springframework .util .StringValueResolver ;
36
34
37
35
/**
38
36
* 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.
40
38
*
41
39
* @author Rob Harrop
42
40
* @author Juergen Hoeller
43
41
* @author Jennifer Hickey
44
42
* @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
48
46
*/
49
47
public class AnnotationJmxAttributeSource implements JmxAttributeSource , BeanFactoryAware {
50
48
@@ -66,25 +64,23 @@ public String resolveStringValue(String strVal) {
66
64
}
67
65
68
66
@ 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 );
72
69
if (ann == null ) {
73
70
return null ;
74
71
}
75
- ManagedResource managedResource = new ManagedResource ();
72
+ org . springframework . jmx . export . metadata . ManagedResource managedResource = new org . springframework . jmx . export . metadata . ManagedResource ();
76
73
AnnotationBeanUtils .copyPropertiesToBean (ann , managedResource , this .embeddedValueResolver );
77
74
return managedResource ;
78
75
}
79
76
80
77
@ 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 );
84
80
if (ann == null ) {
85
81
return null ;
86
82
}
87
- ManagedAttribute managedAttribute = new ManagedAttribute ();
83
+ org . springframework . jmx . export . metadata . ManagedAttribute managedAttribute = new org . springframework . jmx . export . metadata . ManagedAttribute ();
88
84
AnnotationBeanUtils .copyPropertiesToBean (ann , managedAttribute , "defaultValue" );
89
85
if (ann .defaultValue ().length () > 0 ) {
90
86
managedAttribute .setDefaultValue (ann .defaultValue ());
@@ -93,65 +89,53 @@ public ManagedAttribute getManagedAttribute(Method method) throws InvalidMetadat
93
89
}
94
90
95
91
@ 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 );
105
95
}
106
96
107
97
@ 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 );
116
101
}
117
102
118
103
@ Override
119
- public ManagedOperationParameter [] getManagedOperationParameters (Method method )
104
+ public org . springframework . jmx . export . metadata . ManagedOperationParameter [] getManagedOperationParameters (Method method )
120
105
throws InvalidMetadataException {
121
106
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 );
138
110
}
139
111
140
112
@ 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 );
145
128
}
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 ;
153
135
}
154
- return result ;
136
+ T bean = BeanUtils .instantiate (beanClass );
137
+ AnnotationBeanUtils .copyPropertiesToBean (ann , bean );
138
+ return bean ;
155
139
}
156
140
157
141
}
0 commit comments