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.naming;
018:
019: import java.io.IOException;
020: import java.util.Properties;
021:
022: import javax.management.MalformedObjectNameException;
023: import javax.management.ObjectName;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import org.springframework.beans.factory.InitializingBean;
029: import org.springframework.core.io.Resource;
030: import org.springframework.core.io.support.PropertiesLoaderUtils;
031: import org.springframework.jmx.support.ObjectNameManager;
032: import org.springframework.util.CollectionUtils;
033:
034: /**
035: * <code>ObjectNamingStrategy</code> implementation that builds
036: * <code>ObjectName</code> instances from the key used in the
037: * "beans" map passed to <code>MBeanExporter</code>.
038: *
039: * <p>Can also check object name mappings, given as <code>Properties</code>
040: * or as <code>mappingLocations</code> of properties files. The key used
041: * to look up is the key used in <code>MBeanExporter</code>'s "beans" map.
042: * If no mapping is found for a given key, the key itself is used to
043: * build an <code>ObjectName</code>.
044: *
045: * @author Rob Harrop
046: * @author Juergen Hoeller
047: * @since 1.2
048: * @see #setMappings
049: * @see #setMappingLocation
050: * @see #setMappingLocations
051: * @see org.springframework.jmx.export.MBeanExporter#setBeans
052: */
053: public class KeyNamingStrategy implements ObjectNamingStrategy,
054: InitializingBean {
055:
056: /**
057: * <code>Log</code> instance for this class.
058: */
059: protected final Log logger = LogFactory.getLog(getClass());
060:
061: /**
062: * Stores the mappings of bean key to <code>ObjectName</code>.
063: */
064: private Properties mappings;
065:
066: /**
067: * Stores the <code>Resource</code>s containing properties that should be loaded
068: * into the final merged set of <code>Properties</code> used for <code>ObjectName</code>
069: * resolution.
070: */
071: private Resource[] mappingLocations;
072:
073: /**
074: * Stores the result of merging the <code>mappings</code> <code>Properties</code>
075: * with the the properties stored in the resources defined by <code>mappingLocations</code>.
076: */
077: private Properties mergedMappings;
078:
079: /**
080: * Set local properties, containing object name mappings, e.g. via
081: * the "props" tag in XML bean definitions. These can be considered
082: * defaults, to be overridden by properties loaded from files.
083: */
084: public void setMappings(Properties mappings) {
085: this .mappings = mappings;
086: }
087:
088: /**
089: * Set a location of a properties file to be loaded,
090: * containing object name mappings.
091: */
092: public void setMappingLocation(Resource location) {
093: this .mappingLocations = new Resource[] { location };
094: }
095:
096: /**
097: * Set location of properties files to be loaded,
098: * containing object name mappings.
099: */
100: public void setMappingLocations(Resource[] mappingLocations) {
101: this .mappingLocations = mappingLocations;
102: }
103:
104: /**
105: * Merges the <code>Properties</code> configured in the <code>mappings</code> and
106: * <code>mappingLocations</code> into the final <code>Properties</code> instance
107: * used for <code>ObjectName</code> resolution.
108: * @throws IOException
109: */
110: public void afterPropertiesSet() throws IOException {
111: this .mergedMappings = new Properties();
112:
113: CollectionUtils.mergePropertiesIntoMap(this .mappings,
114: this .mergedMappings);
115:
116: if (this .mappingLocations != null) {
117: for (int i = 0; i < this .mappingLocations.length; i++) {
118: Resource location = this .mappingLocations[i];
119: if (logger.isInfoEnabled()) {
120: logger
121: .info("Loading JMX object name mappings file from "
122: + location);
123: }
124: PropertiesLoaderUtils.fillProperties(
125: this .mergedMappings, location);
126: }
127: }
128: }
129:
130: /**
131: * Attempts to retrieve the <code>ObjectName</code> via the given key, trying to
132: * find a mapped value in the mappings first.
133: */
134: public ObjectName getObjectName(Object managedBean, String beanKey)
135: throws MalformedObjectNameException {
136: String objectName = null;
137: if (this.mergedMappings != null) {
138: objectName = this.mergedMappings.getProperty(beanKey);
139: }
140: if (objectName == null) {
141: objectName = beanKey;
142: }
143: return ObjectNameManager.getInstance(objectName);
144: }
145:
146: }
|