1 16 17 package org.springframework.jmx.export.naming; 18 19 import java.io.IOException ; 20 import java.util.Properties ; 21 22 import javax.management.MalformedObjectNameException ; 23 import javax.management.ObjectName ; 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 53 public class KeyNamingStrategy implements ObjectNamingStrategy, InitializingBean { 54 55 58 protected final Log logger = LogFactory.getLog(getClass()); 59 60 63 private Properties mappings; 64 65 70 private Resource[] mappingLocations; 71 72 76 private Properties mergedMappings; 77 78 79 84 public void setMappings(Properties mappings) { 85 this.mappings = mappings; 86 } 87 88 92 public void setMappingLocation(Resource location) { 93 this.mappingLocations = new Resource[]{location}; 94 } 95 96 100 public void setMappingLocations(Resource[] mappingLocations) { 101 this.mappingLocations = mappingLocations; 102 } 103 104 105 111 public void afterPropertiesSet() throws IOException { 112 this.mergedMappings = new Properties (); 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 132 public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException { 133 String 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 |