001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jmx.export.annotation;
018:
019: import java.beans.PropertyDescriptor;
020: import java.lang.annotation.Annotation;
021: import java.lang.reflect.Method;
022:
023: import org.springframework.beans.BeanUtils;
024: import org.springframework.beans.annotation.AnnotationBeanUtils;
025: import org.springframework.jmx.export.metadata.InvalidMetadataException;
026: import org.springframework.jmx.export.metadata.JmxAttributeSource;
027: import org.springframework.jmx.export.metadata.ManagedAttribute;
028: import org.springframework.jmx.export.metadata.ManagedNotification;
029: import org.springframework.jmx.export.metadata.ManagedOperation;
030: import org.springframework.jmx.export.metadata.ManagedOperationParameter;
031: import org.springframework.jmx.export.metadata.ManagedResource;
032: import org.springframework.core.annotation.AnnotationUtils;
033:
034: /**
035: * Implementation of the <code>JmxAttributeSource</code> interface that
036: * reads JDK 1.5+ annotations and exposes the corresponding attributes.
037: *
038: * <p>This is a direct alternative to <code>AttributesJmxAttributeSource</code>,
039: * which is able to read in source-level attributes via Commons Attributes.
040: *
041: * @author Rob Harrop
042: * @author Juergen Hoeller
043: * @since 1.2
044: * @see org.springframework.jmx.export.annotation.ManagedResource
045: * @see org.springframework.jmx.export.annotation.ManagedAttribute
046: * @see org.springframework.jmx.export.annotation.ManagedOperation
047: * @see org.springframework.jmx.export.metadata.AttributesJmxAttributeSource
048: * @see org.springframework.metadata.commons.CommonsAttributes
049: */
050: public class AnnotationJmxAttributeSource implements JmxAttributeSource {
051:
052: public ManagedResource getManagedResource(Class beanClass)
053: throws InvalidMetadataException {
054: Annotation ann = beanClass
055: .getAnnotation(org.springframework.jmx.export.annotation.ManagedResource.class);
056: if (ann == null) {
057: return null;
058: }
059: ManagedResource managedResource = new ManagedResource();
060: AnnotationBeanUtils.copyPropertiesToBean(ann, managedResource);
061: return managedResource;
062: }
063:
064: public ManagedAttribute getManagedAttribute(Method method)
065: throws InvalidMetadataException {
066: org.springframework.jmx.export.annotation.ManagedAttribute ann = AnnotationUtils
067: .getAnnotation(
068: method,
069: org.springframework.jmx.export.annotation.ManagedAttribute.class);
070: if (ann == null) {
071: return null;
072: }
073: ManagedAttribute managedAttribute = new ManagedAttribute();
074: AnnotationBeanUtils.copyPropertiesToBean(ann, managedAttribute,
075: "defaultValue");
076: if (ann.defaultValue().length() > 0) {
077: managedAttribute.setDefaultValue(ann.defaultValue());
078: }
079: return managedAttribute;
080: }
081:
082: public ManagedOperation getManagedOperation(Method method)
083: throws InvalidMetadataException {
084: PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
085: if (pd != null) {
086: throw new InvalidMetadataException(
087: "The ManagedOperation attribute is not valid for JavaBean properties. Use ManagedAttribute instead.");
088: }
089:
090: Annotation ann = AnnotationUtils
091: .getAnnotation(
092: method,
093: org.springframework.jmx.export.annotation.ManagedOperation.class);
094: if (ann == null) {
095: return null;
096: }
097:
098: ManagedOperation op = new ManagedOperation();
099: AnnotationBeanUtils.copyPropertiesToBean(ann, op);
100: return op;
101: }
102:
103: public ManagedOperationParameter[] getManagedOperationParameters(
104: Method method) throws InvalidMetadataException {
105:
106: ManagedOperationParameters params = AnnotationUtils
107: .getAnnotation(method, ManagedOperationParameters.class);
108: ManagedOperationParameter[] result = null;
109: if (params == null) {
110: result = new ManagedOperationParameter[0];
111: } else {
112: Annotation[] paramData = params.value();
113: result = new ManagedOperationParameter[paramData.length];
114: for (int i = 0; i < paramData.length; i++) {
115: Annotation annotation = paramData[i];
116: ManagedOperationParameter managedOperationParameter = new ManagedOperationParameter();
117: AnnotationBeanUtils.copyPropertiesToBean(annotation,
118: managedOperationParameter);
119: result[i] = managedOperationParameter;
120: }
121: }
122: return result;
123: }
124:
125: public ManagedNotification[] getManagedNotifications(Class clazz)
126: throws InvalidMetadataException {
127: ManagedNotifications notificationsAnn = (ManagedNotifications) clazz
128: .getAnnotation(ManagedNotifications.class);
129: if (notificationsAnn == null) {
130: return new ManagedNotification[0];
131: }
132: Annotation[] notifications = notificationsAnn.value();
133: ManagedNotification[] result = new ManagedNotification[notifications.length];
134: for (int i = 0; i < notifications.length; i++) {
135: Annotation notification = notifications[i];
136:
137: ManagedNotification managedNotification = new ManagedNotification();
138: AnnotationBeanUtils.copyPropertiesToBean(notification,
139: managedNotification);
140: result[i] = managedNotification;
141: }
142: return result;
143: }
144:
145: }
|