KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > naming > KeyNamingStrategy


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.naming;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.management.MalformedObjectNameException JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.core.io.Resource;
30 import org.springframework.core.io.support.PropertiesLoaderUtils;
31 import org.springframework.jmx.support.ObjectNameManager;
32 import org.springframework.util.CollectionUtils;
33
34 /**
35  * <code>ObjectNamingStrategy</code> implementation that builds
36  * <code>ObjectName</code> instances from the key used in the
37  * "beans" map passed to <code>MBeanExporter</code>.
38  *
39  * <p>Can also check object name mappings, given as <code>Properties</code>
40  * or as <code>mappingLocations</code> of properties files. The key used
41  * to look up is the key used in <code>MBeanExporter</code>'s "beans" map.
42  * If no mapping is found for a given key, the key itself is used to
43  * build an <code>ObjectName</code>.
44  *
45  * @author Rob Harrop
46  * @author Juergen Hoeller
47  * @since 1.2
48  * @see #setMappings
49  * @see #setMappingLocation
50  * @see #setMappingLocations
51  * @see org.springframework.jmx.export.MBeanExporter#setBeans
52  */

53 public class KeyNamingStrategy implements ObjectNamingStrategy, InitializingBean {
54
55     /**
56      * <code>Log</code> instance for this class.
57      */

58     protected final Log logger = LogFactory.getLog(getClass());
59
60     /**
61      * Stores the mappings of bean key to <code>ObjectName</code>.
62      */

63     private Properties JavaDoc mappings;
64
65     /**
66      * Stores the <code>Resource</code>s containing properties that should be loaded
67      * into the final merged set of <code>Properties</code> used for <code>ObjectName</code>
68      * resolution.
69      */

70     private Resource[] mappingLocations;
71
72     /**
73      * Stores the result of merging the <code>mappings</code> <code>Properties</code>
74      * with the the properties stored in the resources defined by <code>mappingLocations</code>.
75      */

76     private Properties JavaDoc mergedMappings;
77
78
79     /**
80      * Set local properties, containing object name mappings, e.g. via
81      * the "props" tag in XML bean definitions. These can be considered
82      * defaults, to be overridden by properties loaded from files.
83      */

84     public void setMappings(Properties JavaDoc mappings) {
85         this.mappings = mappings;
86     }
87
88     /**
89      * Set a location of a properties file to be loaded,
90      * containing object name mappings.
91      */

92     public void setMappingLocation(Resource location) {
93         this.mappingLocations = new Resource[]{location};
94     }
95
96     /**
97      * Set location of properties files to be loaded,
98      * containing object name mappings.
99      */

100     public void setMappingLocations(Resource[] mappingLocations) {
101         this.mappingLocations = mappingLocations;
102     }
103
104
105     /**
106      * Merges the <code>Properties</code> configured in the <code>mappings</code> and
107      * <code>mappingLocations</code> into the final <code>Properties</code> instance
108      * used for <code>ObjectName</code> resolution.
109      * @throws IOException
110      */

111     public void afterPropertiesSet() throws IOException JavaDoc {
112         this.mergedMappings = new Properties JavaDoc();
113
114         CollectionUtils.mergePropertiesIntoMap(this.mappings, 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.info("Loading JMX object name mappings file from " + location);
121                 }
122                 PropertiesLoaderUtils.fillProperties(this.mergedMappings, location);
123             }
124         }
125     }
126     
127
128     /**
129      * Attempts to retrieve the <code>ObjectName</code> via the given key, trying to
130      * find a mapped value in the mappings first.
131      */

132     public ObjectName JavaDoc getObjectName(Object JavaDoc managedBean, String JavaDoc beanKey) throws MalformedObjectNameException JavaDoc {
133         String JavaDoc objectName = null;
134         if (this.mergedMappings != null) {
135             objectName = this.mergedMappings.getProperty(beanKey);
136         }
137         if (objectName == null) {
138             objectName = beanKey;
139         }
140         return ObjectNameManager.getInstance(objectName);
141     }
142
143 }
144
Popular Tags