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.assembler;
018:
019: import java.lang.reflect.Method;
020: import java.util.Arrays;
021: import java.util.Enumeration;
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.Map;
025: import java.util.Properties;
026: import java.util.Set;
027:
028: import org.springframework.util.StringUtils;
029:
030: /**
031: * <code>AbstractReflectiveMBeanInfoAssembler</code> subclass that allows
032: * method names to be explicitly excluded as MBean operations and attributes.
033: *
034: * <p>Any method not explicitly excluded from the management interface will be exposed to
035: * JMX. JavaBean getters and setters will automatically be exposed as JMX attributes.
036: *
037: * <p>You can supply an array of method names via the <code>ignoredMethods</code>
038: * property. If you have multiple beans and you wish each bean to use a different
039: * set of method names, then you can map bean keys (that is the name used to pass
040: * the bean to the <code>MBeanExporter</code>) to a list of method names using the
041: * <code>ignoredMethodMappings</code> property.
042: *
043: * <p>If you specify values for both <code>ignoredMethodMappings</code> and
044: * <code>ignoredMethods</code>, Spring will attempt to find method names in the
045: * mappings first. If no method names for the bean are found, it will use the
046: * method names defined by <code>ignoredMethods</code>.
047: *
048: * @author Rob Harrop
049: * @author Seth Ladd
050: * @since 1.2.5
051: * @see #setIgnoredMethods
052: * @see #setIgnoredMethodMappings
053: * @see InterfaceBasedMBeanInfoAssembler
054: * @see SimpleReflectiveMBeanInfoAssembler
055: * @see MethodNameBasedMBeanInfoAssembler
056: * @see org.springframework.jmx.export.MBeanExporter
057: */
058: public class MethodExclusionMBeanInfoAssembler extends
059: AbstractConfigurableMBeanInfoAssembler {
060:
061: private Set ignoredMethods;
062:
063: private Map ignoredMethodMappings;
064:
065: /**
066: * Set the array of method names to be <b>ignored</b> when creating the management info.
067: * <p>These method names will be used for a bean if no entry corresponding to
068: * that bean is found in the <code>ignoredMethodsMappings</code> property.
069: * @see #setIgnoredMethodMappings(java.util.Properties)
070: */
071: public void setIgnoredMethods(String[] ignoredMethodNames) {
072: this .ignoredMethods = new HashSet(Arrays
073: .asList(ignoredMethodNames));
074: }
075:
076: /**
077: * Set the mappings of bean keys to a comma-separated list of method names.
078: * <p>These method names are <b>ignored</b> when creating the management interface.
079: * <p>The property key must match the bean key and the property value must match
080: * the list of method names. When searching for method names to ignore for a bean,
081: * Spring will check these mappings first.
082: */
083: public void setIgnoredMethodMappings(Properties mappings) {
084: this .ignoredMethodMappings = new HashMap();
085: for (Enumeration en = mappings.keys(); en.hasMoreElements();) {
086: String beanKey = (String) en.nextElement();
087: String[] methodNames = StringUtils
088: .commaDelimitedListToStringArray(mappings
089: .getProperty(beanKey));
090: this .ignoredMethodMappings.put(beanKey, new HashSet(Arrays
091: .asList(methodNames)));
092: }
093: }
094:
095: protected boolean includeReadAttribute(Method method, String beanKey) {
096: return isNotIgnored(method, beanKey);
097: }
098:
099: protected boolean includeWriteAttribute(Method method,
100: String beanKey) {
101: return isNotIgnored(method, beanKey);
102: }
103:
104: protected boolean includeOperation(Method method, String beanKey) {
105: return isNotIgnored(method, beanKey);
106: }
107:
108: /**
109: * Determine whether the given method is supposed to be included,
110: * that is, not configured as to be ignored.
111: * @param method the operation method
112: * @param beanKey the key associated with the MBean in the beans map
113: * of the <code>MBeanExporter</code>
114: */
115: protected boolean isNotIgnored(Method method, String beanKey) {
116: if (this .ignoredMethodMappings != null) {
117: Set methodNames = (Set) this .ignoredMethodMappings
118: .get(beanKey);
119: if (methodNames != null) {
120: return !methodNames.contains(method.getName());
121: }
122: }
123: if (this .ignoredMethods != null) {
124: return !this .ignoredMethods.contains(method.getName());
125: }
126: return true;
127: }
128:
129: }
|