001: /*
002: * Copyright 2002-2007 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.core.annotation.AnnotationUtils;
026: import org.springframework.jmx.export.metadata.InvalidMetadataException;
027: import org.springframework.jmx.export.metadata.JmxAttributeSource;
028: import org.springframework.jmx.export.metadata.ManagedAttribute;
029: import org.springframework.jmx.export.metadata.ManagedNotification;
030: import org.springframework.jmx.export.metadata.ManagedOperation;
031: import org.springframework.jmx.export.metadata.ManagedOperationParameter;
032: import org.springframework.jmx.export.metadata.ManagedResource;
033: import org.springframework.util.StringUtils;
034:
035: /**
036: * Implementation of the <code>JmxAttributeSource</code> interface that
037: * reads JDK 1.5+ annotations and exposes the corresponding attributes.
038: *
039: * <p>This is a direct alternative to <code>AttributesJmxAttributeSource</code>,
040: * which is able to read in source-level attributes via Commons Attributes.
041: *
042: * @author Rob Harrop
043: * @author Juergen Hoeller
044: * @since 1.2
045: * @see org.springframework.jmx.export.annotation.ManagedResource
046: * @see org.springframework.jmx.export.annotation.ManagedAttribute
047: * @see org.springframework.jmx.export.annotation.ManagedOperation
048: * @see org.springframework.jmx.export.metadata.AttributesJmxAttributeSource
049: * @see org.springframework.metadata.commons.CommonsAttributes
050: */
051: public class AnnotationJmxAttributeSource implements JmxAttributeSource {
052:
053: public ManagedResource getManagedResource(Class beanClass)
054: throws InvalidMetadataException {
055: org.springframework.jmx.export.annotation.ManagedResource ann = ((Class<?>) beanClass)
056: .getAnnotation(org.springframework.jmx.export.annotation.ManagedResource.class);
057: if (ann == null) {
058: return null;
059: }
060: ManagedResource managedResource = new ManagedResource();
061: AnnotationBeanUtils.copyPropertiesToBean(ann, managedResource);
062: if (!"".equals(ann.value())
063: && !StringUtils.hasLength(managedResource
064: .getObjectName())) {
065: managedResource.setObjectName(ann.value());
066: }
067: return managedResource;
068: }
069:
070: public ManagedAttribute getManagedAttribute(Method method)
071: throws InvalidMetadataException {
072: org.springframework.jmx.export.annotation.ManagedAttribute ann = AnnotationUtils
073: .getAnnotation(
074: method,
075: org.springframework.jmx.export.annotation.ManagedAttribute.class);
076: if (ann == null) {
077: return null;
078: }
079: ManagedAttribute managedAttribute = new ManagedAttribute();
080: AnnotationBeanUtils.copyPropertiesToBean(ann, managedAttribute,
081: "defaultValue");
082: if (ann.defaultValue().length() > 0) {
083: managedAttribute.setDefaultValue(ann.defaultValue());
084: }
085: return managedAttribute;
086: }
087:
088: public ManagedOperation getManagedOperation(Method method)
089: throws InvalidMetadataException {
090: PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
091: if (pd != null) {
092: throw new InvalidMetadataException(
093: "The ManagedOperation attribute is not valid for JavaBean properties. Use ManagedAttribute instead.");
094: }
095:
096: Annotation ann = AnnotationUtils
097: .getAnnotation(
098: method,
099: org.springframework.jmx.export.annotation.ManagedOperation.class);
100: if (ann == null) {
101: return null;
102: }
103:
104: ManagedOperation op = new ManagedOperation();
105: AnnotationBeanUtils.copyPropertiesToBean(ann, op);
106: return op;
107: }
108:
109: public ManagedOperationParameter[] getManagedOperationParameters(
110: Method method) throws InvalidMetadataException {
111:
112: ManagedOperationParameters params = AnnotationUtils
113: .getAnnotation(method, ManagedOperationParameters.class);
114: ManagedOperationParameter[] result = null;
115: if (params == null) {
116: result = new ManagedOperationParameter[0];
117: } else {
118: Annotation[] paramData = params.value();
119: result = new ManagedOperationParameter[paramData.length];
120: for (int i = 0; i < paramData.length; i++) {
121: Annotation annotation = paramData[i];
122: ManagedOperationParameter managedOperationParameter = new ManagedOperationParameter();
123: AnnotationBeanUtils.copyPropertiesToBean(annotation,
124: managedOperationParameter);
125: result[i] = managedOperationParameter;
126: }
127: }
128: return result;
129: }
130:
131: public ManagedNotification[] getManagedNotifications(Class clazz)
132: throws InvalidMetadataException {
133: ManagedNotifications notificationsAnn = (ManagedNotifications) clazz
134: .getAnnotation(ManagedNotifications.class);
135: if (notificationsAnn == null) {
136: return new ManagedNotification[0];
137: }
138: Annotation[] notifications = notificationsAnn.value();
139: ManagedNotification[] result = new ManagedNotification[notifications.length];
140: for (int i = 0; i < notifications.length; i++) {
141: Annotation notification = notifications[i];
142:
143: ManagedNotification managedNotification = new ManagedNotification();
144: AnnotationBeanUtils.copyPropertiesToBean(notification,
145: managedNotification);
146: result[i] = managedNotification;
147: }
148: return result;
149: }
150:
151: }
|