KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > metadata > AttributesJmxAttributeSource


1 /*
2  * Copyright 2002-2005 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.metadata;
18
19 import java.beans.PropertyDescriptor JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.springframework.beans.BeanUtils;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.metadata.Attributes;
27 import org.springframework.util.Assert;
28
29 /**
30  * Implementation of the <code>JmxAttributeSource</code> interface that
31  * reads metadata via Spring's <code>Attributes</code> abstraction.
32  *
33  * <p>Typically used for reading in source-level attributes via
34  * Commons Attributes.
35  *
36  * @author Rob Harrop
37  * @since 1.2
38  * @see org.springframework.metadata.Attributes
39  * @see org.springframework.metadata.commons.CommonsAttributes
40  */

41 public class AttributesJmxAttributeSource implements JmxAttributeSource, InitializingBean {
42
43     /**
44      * Underlying Attributes implementation that we're using.
45      */

46     private Attributes attributes;
47
48
49     /**
50      * Create a new AttributesJmxAttributeSource.
51      * @see #setAttributes
52      */

53     public AttributesJmxAttributeSource() {
54     }
55
56     /**
57      * Create a new AttributesJmxAttributeSource.
58      * @param attributes the Attributes implementation to use
59      * @see org.springframework.metadata.commons.CommonsAttributes
60      */

61     public AttributesJmxAttributeSource(Attributes attributes) {
62         if (attributes == null) {
63             throw new IllegalArgumentException JavaDoc("Attributes is required");
64         }
65         this.attributes = attributes;
66     }
67
68     /**
69      * Set the Attributes implementation to use.
70      * @see org.springframework.metadata.commons.CommonsAttributes
71      */

72     public void setAttributes(Attributes attributes) {
73         this.attributes = attributes;
74     }
75
76     public void afterPropertiesSet() {
77         if (this.attributes == null) {
78             throw new IllegalArgumentException JavaDoc("'attributes' is required");
79         }
80     }
81
82
83     /**
84      * If the specified class has a <code>ManagedResource</code> attribute,
85      * then it is returned. Otherwise returns null.
86      * @param clazz the class to read the attribute data from
87      * @return the attribute, or <code>null</code> if not found
88      * @throws InvalidMetadataException if more than one attribute exists
89      */

90     public ManagedResource getManagedResource(Class JavaDoc clazz) {
91         Assert.notNull(this.attributes, "'attributes' is required");
92         Collection JavaDoc attrs = this.attributes.getAttributes(clazz, ManagedResource.class);
93         if (attrs.isEmpty()) {
94             return null;
95         }
96         else if (attrs.size() == 1) {
97             return (ManagedResource) attrs.iterator().next();
98         }
99         else {
100             throw new InvalidMetadataException("A Class can have only one ManagedResource attribute");
101         }
102     }
103
104     /**
105      * If the specified method has a <code>ManagedAttribute</code> attribute,
106      * then it is returned. Otherwise returns null.
107      * @param method the method to read the attribute data from
108      * @return the attribute, or <code>null</code> if not found
109      * @throws InvalidMetadataException if more than one attribute exists,
110      * or if the supplied method does not represent a JavaBean property
111      */

112     public ManagedAttribute getManagedAttribute(Method JavaDoc method) throws InvalidMetadataException {
113         Assert.notNull(this.attributes, "'attributes' is required");
114         PropertyDescriptor JavaDoc pd = BeanUtils.findPropertyForMethod(method);
115         if (pd == null) {
116             throw new InvalidMetadataException(
117                     "The ManagedAttribute attribute is only valid for JavaBean properties: " +
118                     "use ManagedOperation for methods");
119         }
120         Collection JavaDoc attrs = this.attributes.getAttributes(method, ManagedAttribute.class);
121         if (attrs.isEmpty()) {
122             return null;
123         }
124         else if (attrs.size() == 1) {
125             return (ManagedAttribute) attrs.iterator().next();
126         }
127         else {
128             throw new InvalidMetadataException("A Method can have only one ManagedAttribute attribute");
129         }
130     }
131
132     /**
133      * If the specified method has a <code>ManagedOperation</code> attribute,
134      * then it is returned. Otherwise return null.
135      * @param method the method to read the attribute data from
136      * @return the attribute, or <code>null</code> if not found
137      * @throws InvalidMetadataException if more than one attribute exists,
138      * or if the supplied method represents a JavaBean property
139      */

140     public ManagedOperation getManagedOperation(Method JavaDoc method) {
141         Assert.notNull(this.attributes, "'attributes' is required");
142         PropertyDescriptor JavaDoc pd = BeanUtils.findPropertyForMethod(method);
143         if (pd != null) {
144             throw new InvalidMetadataException(
145                     "The ManagedOperation attribute is not valid for JavaBean properties: " +
146                     "use ManagedAttribute instead");
147         }
148         Collection JavaDoc attrs = this.attributes.getAttributes(method, ManagedOperation.class);
149         if (attrs.isEmpty()) {
150             return null;
151         }
152         else if (attrs.size() == 1) {
153             return (ManagedOperation) attrs.iterator().next();
154         }
155         else {
156             throw new InvalidMetadataException("A Method can have only one ManagedAttribute attribute");
157         }
158     }
159
160     /**
161      * If the specified method has <code>ManagedOperationParameter</code> attributes,
162      * then these are returned, otherwise a zero length array is returned.
163      * @param method the method to get the managed operation parameters for
164      * @return the array of ManagedOperationParameter objects
165      * @throws InvalidMetadataException if the number of ManagedOperationParameter
166      * attributes does not match the number of parameters in the method
167      */

168     public ManagedOperationParameter[] getManagedOperationParameters(Method JavaDoc method)
169             throws InvalidMetadataException {
170
171         Assert.notNull(this.attributes, "'attributes' is required");
172         Collection JavaDoc attrs = this.attributes.getAttributes(method, ManagedOperationParameter.class);
173         if (attrs.size() == 0) {
174             return new ManagedOperationParameter[0];
175         }
176         else if (attrs.size() != method.getParameterTypes().length) {
177             throw new InvalidMetadataException(
178                     "Method [" + method + "] has an incorrect number of ManagedOperationParameters specified");
179         }
180         else {
181             ManagedOperationParameter[] params = new ManagedOperationParameter[attrs.size()];
182             for (Iterator JavaDoc it = attrs.iterator(); it.hasNext();) {
183                 ManagedOperationParameter param = (ManagedOperationParameter) it.next();
184                 if (param.getIndex() < 0 || param.getIndex() >= params.length) {
185                     throw new InvalidMetadataException(
186                             "ManagedOperationParameter index for [" + param.getName() + "] is out of bounds");
187                 }
188                 params[param.getIndex()] = param;
189             }
190             return params;
191         }
192     }
193
194     /**
195      * If the specified has {@link ManagedNotification} attributes these are returned, otherwise
196      * a zero-length array is returned.
197      */

198     public ManagedNotification[] getManagedNotifications(Class JavaDoc clazz) {
199         Assert.notNull(this.attributes, "'attributes' is required");
200         Collection JavaDoc attrs = this.attributes.getAttributes(clazz, ManagedNotification.class);
201         return attrs.isEmpty() ? new ManagedNotification[0] : (ManagedNotification[]) attrs.toArray(new ManagedNotification[attrs.size()]);
202     }
203 }
204
Popular Tags