1 package org.ocltf.common; 2 3 import java.util.Iterator; 4 5 import org.apache.commons.configuration.ClassPropertiesConfiguration; 6 import org.apache.commons.lang.StringUtils; 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import org.ocltf.utils.ExceptionUtils; 10 11 /*** 12 * Finds and loads Configuration objects 13 * 14 * @author Chad Brandon 15 */ 16 public class Configuration { 17 18 private static Log logger = LogFactory.getLog(Configuration.class); 19 20 /*** 21 * Where all configuration files will be found. 22 */ 23 private static final String CONFIGURATION_DIR = "META-INF/configuration/"; 24 25 /*** 26 * The underlying configuration object. 27 */ 28 private ClassPropertiesConfiguration config; 29 30 /*** 31 * The path to the configuration file 32 */ 33 private String path; 34 35 /*** 36 * Returns an instance of this Configuration for the 37 * specified forClass 38 * @param forClass 39 * @return Configuration 40 */ 41 public static Configuration getInstance(Class forClass) { 42 return new Configuration(forClass); 43 } 44 45 /*** 46 * The private constuctor which creates the underlying config. 47 * 48 * @param forClass 49 */ 50 private Configuration(Class forClass) { 51 String methodName = "Configuration"; 52 ExceptionUtils.checkNull(methodName, "forClass", forClass); 53 String resource = findPath(forClass); 54 try { 55 this.config = 56 new ClassPropertiesConfiguration(forClass, resource); 57 this.path = resource; 58 } catch (Exception ex) { 59 String errMsg = "Could not find configuration resource --> " + resource; 60 logger.error(errMsg, ex); 61 throw new ConfigurationException(errMsg, ex); 62 } 63 } 64 65 /*** 66 * Returns the path to the configuration file 67 * @param forClass the Class which the Configuration path is for. 68 * @return String 69 */ 70 public String findPath(Class forClass) { 71 String methodName = "findPath"; 72 ExceptionUtils.checkNull(methodName, "clazz", forClass); 73 return CONFIGURATION_DIR + forClass.getName(); 74 } 75 76 /*** 77 * Retrieves the property as a String 78 * @param key the key for the property 79 * @return String the value of the property or null if it can't be found. 80 */ 81 public String getOptionalString(String key) { 82 String property = null; 83 key = StringUtils.trimToEmpty(key); 84 if (StringUtils.isNotEmpty(key)) { 85 property = config.getString(key); 86 } 87 return property; 88 } 89 90 /*** 91 * Retrieves the property has a array of Strings 92 * @param key the key for the property 93 * @return String the value of the property or null if it can't be found. 94 */ 95 public String[] getOptionalStringArray(String key) { 96 String[] properties = null; 97 key = StringUtils.trimToEmpty(key); 98 if (StringUtils.isNotEmpty(key)) { 99 properties = config.getStringArray(key); 100 } 101 return properties; 102 } 103 104 /*** 105 * Retrieves the property has a array of Strings 106 * @param key the key for the property 107 * @return String the value of the property or null if it can't be found. 108 */ 109 public String[] getRequiredStringArray(String key) { 110 String methodName = "getOptionalStringArray"; 111 String[] properties = null; 112 key = StringUtils.trimToEmpty(key); 113 if (StringUtils.isNotEmpty(key)) { 114 properties = config.getStringArray(key); 115 } 116 if (properties == null || properties.length == 0) { 117 throw new ConfigurationException(methodName 118 + " the configuration property '" 119 + key 120 + " can not be found (or has no corresponding values)," 121 + " please check the configuration file --> " + this.getPath()); 122 } 123 return properties; 124 } 125 126 /*** 127 * Retrieves the property has a string. Will 128 * throw the runtime ConfigurationException 129 * if the property can not be found. 130 * @param key 131 * @return String 132 */ 133 public String getRequiredString(String key) { 134 String methodName = "getRequiredString"; 135 String property = this.getOptionalString(key); 136 if (StringUtils.isEmpty(property)) { 137 throw new ConfigurationException(methodName 138 + " the configuration property '" 139 + key 140 + " can not be found (or has no corresponding value)," 141 + " please check the configuration file --> " + this.getPath()); 142 } 143 return property; 144 } 145 146 /*** 147 * Get the list of the keys contained in the configuration 148 * repository that match the specified prefix. 149 * @param prefix the prefix to look for. 150 * @return Iterator 151 */ 152 public Iterator getKeys(String prefix) { 153 return this.config.getKeys(prefix); 154 } 155 156 /*** 157 * Get the list of the keys contained in the configuration repository. 158 * @return Iterator 159 */ 160 public Iterator getKeys() { 161 return this.config.getKeys(); 162 } 163 164 /*** 165 * Returns the path to the file for this Configuration 166 * @return String 167 */ 168 public String getPath() { 169 return this.path; 170 } 171 172 }