KickJava   Java API By Example, From Geeks To Geeks.

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


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.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  * <code>AbstractReflectiveMBeanInfoAssembler</code> subclass that allows
32  * method names to be explicitly excluded as MBean operations and attributes.
33  *
34  * <p>Any method not explicitly excluded from the management interface will be exposed to
35  * JMX. JavaBean getters and setters will automatically be exposed as JMX attributes.
36  *
37  * <p>You can supply an array of method names via the <code>ignoredMethods</code>
38  * property. If you have multiple beans and you wish each bean to use a different
39  * set of method names, then you can map bean keys (that is the name used to pass
40  * the bean to the <code>MBeanExporter</code>) to a list of method names using the
41  * <code>ignoredMethodMappings</code> property.
42  *
43  * <p>If you specify values for both <code>ignoredMethodMappings</code> and
44  * <code>ignoredMethods</code>, Spring will attempt to find method names in the
45  * mappings first. If no method names for the bean are found, it will use the
46  * method names defined by <code>ignoredMethods</code>.
47  *
48  * @author Rob Harrop
49  * @author Seth Ladd
50  * @since 1.2.5
51  * @see #setIgnoredMethods
52  * @see #setIgnoredMethodMappings
53  * @see InterfaceBasedMBeanInfoAssembler
54  * @see SimpleReflectiveMBeanInfoAssembler
55  * @see MethodNameBasedMBeanInfoAssembler
56  * @see org.springframework.jmx.export.MBeanExporter
57  */

58 public class MethodExclusionMBeanInfoAssembler extends AbstractConfigurableMBeanInfoAssembler {
59
60     private Set JavaDoc ignoredMethods;
61
62     private Map JavaDoc ignoredMethodMappings;
63
64
65     /**
66      * Set the array of method names to be <b>ignored</b> when creating the management info.
67      * <p>These method names will be used for a bean if no entry corresponding to
68      * that bean is found in the <code>ignoredMethodsMappings</code> property.
69      * @see #setIgnoredMethodMappings(java.util.Properties)
70      */

71     public void setIgnoredMethods(String JavaDoc[] ignoredMethodNames) {
72         this.ignoredMethods = new HashSet JavaDoc(Arrays.asList(ignoredMethodNames));
73     }
74
75     /**
76      * Set the mappings of bean keys to a comma-separated list of method names.
77      * <p>These method names are <b>ignored</b> when creating the management interface.
78      * <p>The property key must match the bean key and the property value must match
79      * the list of method names. When searching for method names to ignore for a bean,
80      * Spring will check these mappings first.
81      */

82     public void setIgnoredMethodMappings(Properties JavaDoc mappings) {
83         this.ignoredMethodMappings = new HashMap JavaDoc();
84         for (Enumeration JavaDoc en = mappings.keys(); en.hasMoreElements();) {
85             String JavaDoc beanKey = (String JavaDoc) en.nextElement();
86             String JavaDoc[] methodNames = StringUtils.commaDelimitedListToStringArray(mappings.getProperty(beanKey));
87             this.ignoredMethodMappings.put(beanKey, new HashSet JavaDoc(Arrays.asList(methodNames)));
88         }
89     }
90
91
92     protected boolean includeReadAttribute(Method JavaDoc method, String JavaDoc beanKey) {
93         return isNotIgnored(method, beanKey);
94     }
95
96     protected boolean includeWriteAttribute(Method JavaDoc method, String JavaDoc beanKey) {
97         return isNotIgnored(method, beanKey);
98     }
99
100     protected boolean includeOperation(Method JavaDoc method, String JavaDoc beanKey) {
101         return isNotIgnored(method, beanKey);
102     }
103
104     /**
105      * Determine whether the given method is supposed to be included,
106      * that is, not configured as to be ignored.
107      * @param method the operation method
108      * @param beanKey the key associated with the MBean in the beans map
109      * of the <code>MBeanExporter</code>
110      */

111     protected boolean isNotIgnored(Method JavaDoc method, String JavaDoc beanKey) {
112         if (this.ignoredMethodMappings != null) {
113             Set JavaDoc methodNames = (Set JavaDoc) this.ignoredMethodMappings.get(beanKey);
114             if (methodNames != null) {
115                 return !methodNames.contains(method.getName());
116             }
117         }
118         if (this.ignoredMethods != null) {
119             return !this.ignoredMethods.contains(method.getName());
120         }
121         return true;
122     }
123
124 }
125
Popular Tags