KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > annotation > AnnotationJmxAttributeSource


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jmx.export.annotation;
18
19 import java.beans.PropertyDescriptor JavaDoc;
20 import java.lang.annotation.Annotation JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
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 /**
35  * Implementation of the <code>JmxAttributeSource</code> interface that
36  * reads JDK 1.5+ annotations and exposes the corresponding attributes.
37  *
38  * <p>This is a direct alternative to <code>AttributesJmxAttributeSource</code>,
39  * which is able to read in source-level attributes via Commons Attributes.
40  *
41  * @author Rob Harrop
42  * @author Juergen Hoeller
43  * @since 1.2
44  * @see org.springframework.jmx.export.annotation.ManagedResource
45  * @see org.springframework.jmx.export.annotation.ManagedAttribute
46  * @see org.springframework.jmx.export.annotation.ManagedOperation
47  * @see org.springframework.jmx.export.metadata.AttributesJmxAttributeSource
48  * @see org.springframework.metadata.commons.CommonsAttributes
49  */

50 public class AnnotationJmxAttributeSource implements JmxAttributeSource {
51
52     public ManagedResource getManagedResource(Class JavaDoc beanClass) throws InvalidMetadataException {
53         Annotation JavaDoc 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 JavaDoc 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 JavaDoc method) throws InvalidMetadataException {
77         PropertyDescriptor JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[] paramData = params.value();
103             result = new ManagedOperationParameter[paramData.length];
104             for (int i = 0; i < paramData.length; i++) {
105                 Annotation JavaDoc 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 JavaDoc clazz) throws InvalidMetadataException {
115         ManagedNotifications notificationsAnn = (ManagedNotifications) clazz.getAnnotation(ManagedNotifications.class);
116         if(notificationsAnn == null) {
117             return new ManagedNotification[0];
118         }
119         Annotation JavaDoc[] notifications = notificationsAnn.value();
120         ManagedNotification[] result = new ManagedNotification[notifications.length];
121         for (int i = 0; i < notifications.length; i++) {
122             Annotation JavaDoc 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