KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > assembler > MethodNameBasedMBeanInfoAssembler


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.assembler;
18
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.springframework.util.StringUtils;
29
30 /**
31  * Subclass of <code>AbstractReflectiveMBeanInfoAssembler</code> that allows
32  * to specify method names to be exposed as MBean operations and attributes.
33  * JavaBean getters and setters will automatically be exposed as JMX attributes.
34  *
35  * <p>You can supply an array of method names via the <code>managedMethods</code>
36  * property. If you have multiple beans and you wish each bean to use a different
37  * set of method names, then you can map bean keys (that is the name used to pass
38  * the bean to the <code>MBeanExporter</code>) to a list of method names using the
39  * <code>methodMappings</code> property.
40  *
41  * <p>If you specify values for both <code>methodMappings</code> and
42  * <code>managedMethods</code>, Spring will attempt to find method names in the
43  * mappings first. If no method names for the bean are found, it will use the
44  * method names defined by <code>managedMethods</code>.
45  *
46  * @author Juergen Hoeller
47  * @since 1.2
48  * @see #setManagedMethods
49  * @see #setMethodMappings
50  * @see InterfaceBasedMBeanInfoAssembler
51  * @see SimpleReflectiveMBeanInfoAssembler
52  * @see MethodExclusionMBeanInfoAssembler
53  * @see org.springframework.jmx.export.MBeanExporter
54  */

55 public class MethodNameBasedMBeanInfoAssembler extends AbstractConfigurableMBeanInfoAssembler {
56
57     /**
58      * Stores the set of method names to use for creating the management interface.
59      */

60     private Set JavaDoc managedMethods;
61
62     /**
63      * Stores the mappings of bean keys to an array of method names.
64      */

65     private Map JavaDoc methodMappings;
66
67
68     /**
69      * Set the array of method names to use for creating the management info.
70      * These method names will be used for a bean if no entry corresponding to
71      * that bean is found in the <code>methodMappings</code> property.
72      * @param methodNames an array of method names indicating the methods to use
73      * @see #setMethodMappings
74      */

75     public void setManagedMethods(String JavaDoc[] methodNames) {
76         this.managedMethods = new HashSet JavaDoc(Arrays.asList(methodNames));
77     }
78
79     /**
80      * Set the mappings of bean keys to a comma-separated list of method names.
81      * The property key should match the bean key and the property value should match
82      * the list of method names. When searching for method names for a bean, Spring
83      * will check these mappings first.
84      * @param mappings the mappins of bean keys to method names
85      */

86     public void setMethodMappings(Properties JavaDoc mappings) {
87         this.methodMappings = new HashMap JavaDoc();
88         for (Enumeration JavaDoc en = mappings.keys(); en.hasMoreElements();) {
89             String JavaDoc beanKey = (String JavaDoc) en.nextElement();
90             String JavaDoc[] methodNames = StringUtils.commaDelimitedListToStringArray(mappings.getProperty(beanKey));
91             this.methodMappings.put(beanKey, new HashSet JavaDoc(Arrays.asList(methodNames)));
92         }
93     }
94
95
96     protected boolean includeReadAttribute(Method JavaDoc method, String JavaDoc beanKey) {
97         return isMatch(method, beanKey);
98     }
99
100     protected boolean includeWriteAttribute(Method JavaDoc method, String JavaDoc beanKey) {
101         return isMatch(method, beanKey);
102     }
103
104     protected boolean includeOperation(Method JavaDoc method, String JavaDoc beanKey) {
105         return isMatch(method, beanKey);
106     }
107
108     protected boolean isMatch(Method JavaDoc method, String JavaDoc beanKey) {
109         if (this.methodMappings != null) {
110             Set JavaDoc methodNames = (Set JavaDoc) this.methodMappings.get(beanKey);
111             if (methodNames != null) {
112                 return methodNames.contains(method.getName());
113             }
114         }
115         return (this.managedMethods != null && this.managedMethods.contains(method.getName()));
116     }
117
118 }
119
Popular Tags