View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: ScopeConfig.java,v 1.12 2002/09/12 10:51:34 ludovicc Exp $ 37 */ 38 package org.scopemvc.util; 39 40 import java.util.Enumeration; 41 import java.util.HashMap; 42 import java.util.HashSet; 43 import java.util.Iterator; 44 import java.util.MissingResourceException; 45 import java.util.Properties; 46 import java.util.ResourceBundle; 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 50 /*** 51 * <P> 52 * 53 * Loads the Scope config in such a way that custom properties can be set from 54 * the Java command line (-D switch) or from a custom ResourceBundle. Any 55 * properties not supplied are loaded with the default settings in {@link 56 * DefaultScopeConfig}. </P> <P> 57 * 58 * A custom ResourceBundle name can be specified with {@link #setPropertiesName} 59 * during application initialisation, although the default "scope.config" 60 * resource is always loaded if it can be found. </P> <P> 61 * 62 * Property definitions have the following priority (from highest to lowest): 63 * 64 * <OL> 65 * <LI> System properties</LI> 66 * <LI> setPropertiesName() properties</LI> 67 * <LI> scope.properties</LI> 68 * <LI> {@link DefaultScopeConfig}</LI> 69 * </OL> 70 * </P> 71 * 72 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 73 * @created 04 September 2002 74 * @version $Revision: 1.12 $ $Date: 2002/09/12 10:51:34 $ 75 * @see DefaultScopeConfig 76 */ 77 public final class ScopeConfig { 78 79 private static final Log LOG = LogFactory.getLog(ScopeConfig.class); 80 81 private static final String DEFAULT_CUSTOM_CONFIG_NAME = "scope"; 82 private static final String DEFAULT_CONFIG_NAME = "org.scopemvc.util.DefaultScopeConfig"; 83 private static final String DEFAULT_PROPERTY_PREFIX = "org.scopemvc."; 84 85 // -------------------- Initialisation ---------------------------------- 86 87 private static ScopeConfig instance; 88 89 /*** 90 * The Scope config properties loaded at first access of config. 91 */ 92 private HashMap properties = new HashMap(); 93 94 95 private ScopeConfig() { 96 initialise(); 97 } 98 99 100 // --------------------------- Get properties API ---------------------- 101 102 /*** 103 * Gets the instance of the Scope config 104 * 105 * @return The singleton of the ScopeConfig 106 */ 107 public static ScopeConfig getInstance() { 108 if (instance == null) { 109 instance = new ScopeConfig(); 110 } 111 return instance; 112 } 113 114 115 /*** 116 * Gets the string value of the property 117 * 118 * @param inKey The property name 119 * @return The string value of the property 120 */ 121 public static String getString(String inKey) { 122 if (LOG.isDebugEnabled()) { 123 LOG.debug("getString: " + inKey + ", result " + getProperties().get(inKey)); 124 } 125 return getProperties().get(inKey).toString(); 126 } 127 128 129 /*** 130 * Gets the object value of the property 131 * 132 * @param inKey The property name 133 * @return The object value of the property 134 */ 135 public static Object getObject(String inKey) { 136 if (LOG.isDebugEnabled()) { 137 LOG.debug("getObject: " + inKey + ", result " + getProperties().get(inKey)); 138 } 139 return getProperties().get(inKey); 140 } 141 142 143 /*** 144 * Gets the char value of the property 145 * 146 * @param inKey The property name 147 * @return The char value of the property 148 */ 149 public static char getChar(String inKey) { 150 String string = getProperties().get(inKey).toString(); 151 char result = 0; 152 if (string == null || string.length() < 1) { 153 if (LOG.isDebugEnabled()) { 154 LOG.debug("getChar: no char property for: " + inKey); 155 } 156 } else { 157 result = string.charAt(0); 158 } 159 160 if (LOG.isErrorEnabled() && string.length() > 1) { 161 LOG.error("Char property for (" + inKey + ") longer than a single char: " + string); 162 } 163 164 if (LOG.isDebugEnabled()) { 165 LOG.debug("getChar: " + inKey + ", result " + result); 166 } 167 return result; 168 } 169 170 171 /*** 172 * Gets the class value of the property 173 * 174 * @param inKey The property name 175 * @return The class value of the property 176 */ 177 public static Class getClass(String inKey) { 178 Object o = getProperties().get(inKey); 179 180 if (o == null) { 181 if (LOG.isDebugEnabled()) { 182 LOG.debug("getClass: " + inKey + ", result null"); 183 } 184 return null; 185 } 186 187 if (LOG.isDebugEnabled()) { 188 LOG.debug("getClass: " + inKey + ", result " + o); 189 } 190 if (o instanceof Class) { 191 return (Class) o; 192 } 193 if (!(o instanceof String)) { 194 throw new IllegalArgumentException("Config property: " + inKey + " is not a Class or String: " + o.getClass()); 195 } 196 try { 197 return Class.forName((String) o); 198 } catch (Exception e) { 199 LOG.warn("Class not found: " + o + " for property " + inKey); 200 return null; 201 } 202 } 203 204 205 /*** 206 * Gets the integer value of the property 207 * 208 * @param inKey The property name 209 * @return The integer value of the property 210 */ 211 public static Integer getInteger(String inKey) { 212 Object o = getProperties().get(inKey); 213 214 if (o == null) { 215 if (LOG.isDebugEnabled()) { 216 LOG.debug("getInteger: " + inKey + ", result null"); 217 } 218 return null; 219 } 220 221 if (LOG.isDebugEnabled()) { 222 LOG.debug("getInteger: " + inKey + ", result " + o); 223 } 224 if (o instanceof Integer) { 225 return (Integer) o; 226 } 227 if (!(o instanceof String)) { 228 throw new IllegalArgumentException("Config property: " + inKey + " is not an Integer or String: " + o.getClass()); 229 } 230 try { 231 return new Integer((String) o); 232 } catch (Exception e) { 233 LOG.warn("Not an integer value: " + o + " for property " + inKey); 234 return null; 235 } 236 } 237 238 239 /*** 240 * Gets the keys starting with the prefix 241 * 242 * @param inKeyPrefix The prefix of the keys to match 243 * @return an Iterator over the matching keys 244 */ 245 public static Iterator getKeysMatching(String inKeyPrefix) { 246 HashSet matchingKeys = new HashSet(); 247 for (Iterator i = getProperties().keySet().iterator(); i.hasNext(); ) { 248 String key = (String) i.next(); 249 if (key.startsWith(inKeyPrefix)) { 250 matchingKeys.add(key); 251 } 252 } 253 return matchingKeys.iterator(); 254 } 255 256 257 // ------------------ Set custom properties API ------------------------- 258 259 /*** 260 * Set name of custom properties that will override the DefaultScopeConfig 261 * and "scope.properties", but not override any System properties. 262 * 263 * @param inName The new propertiesName value 264 */ 265 public static void setPropertiesName(String inName) { 266 if (LOG.isDebugEnabled()) { 267 LOG.debug("setPropertiesName: " + inName); 268 } 269 if (inName == null) { 270 throw new IllegalArgumentException("Can't set PropertiesName to null. Pass in a resource name."); 271 } 272 273 ResourceBundle bundle = ResourceLoader.getProperties(inName); 274 if (bundle != null) { 275 getInstance().putAll(bundle); 276 } 277 278 // Load system properties over the top 279 getInstance().loadSystemConfig(); 280 } 281 282 283 /*** 284 * Gets the properties 285 * 286 * @return The properties value 287 */ 288 protected static HashMap getProperties() { 289 return getInstance().properties; 290 } 291 292 293 /*** 294 * Returns the name of the default custom configuration bundle. 295 * 296 * @return The defaultCustomConfigName value 297 */ 298 protected String getDefaultCustomConfigName() { 299 return DEFAULT_CUSTOM_CONFIG_NAME; 300 } 301 302 303 /*** 304 * Returns the name of the default configuration bundle. 305 * 306 * @return The defaultConfigName value 307 */ 308 protected String getDefaultConfigName() { 309 return DEFAULT_CONFIG_NAME; 310 } 311 312 313 /*** 314 * Returns the property name prefix. 315 * 316 * @return The propertyPrefix value 317 */ 318 protected String getPropertyPrefix() { 319 return DEFAULT_PROPERTY_PREFIX; 320 } 321 322 323 /*** 324 * Initialises the class by loading in the configuration. 325 */ 326 protected void initialise() { 327 loadDefaultConfig(); 328 loadCustomConfig(); 329 loadSystemConfig(); 330 if (LOG.isDebugEnabled()) { 331 LOG.debug("--- Scope properties ---"); 332 for (Iterator i = properties.keySet().iterator(); i.hasNext(); ) { 333 String key = (String) i.next(); 334 Object value = properties.get(key); 335 LOG.debug(key + " = " + value); 336 } 337 LOG.debug("------------------------"); 338 } 339 } 340 341 342 /*** 343 * Load Scope config properties from System properties. 344 */ 345 private void loadSystemConfig() { 346 try { 347 Properties systemProperties = System.getProperties(); 348 for (Enumeration e = systemProperties.keys(); e.hasMoreElements(); ) { 349 String key = (String) e.nextElement(); 350 if (key.startsWith(DEFAULT_PROPERTY_PREFIX)) { 351 Object value = systemProperties.getProperty(key); 352 properties.put(key, value); 353 if (LOG.isDebugEnabled()) { 354 LOG.debug("Override Scope property " + key + " with " + value); 355 } 356 } 357 } 358 } catch (SecurityException e) { 359 if (LOG.isDebugEnabled()) { 360 LOG.debug("Can't access System properties", e); 361 } 362 } 363 } 364 365 366 /*** 367 * Attempt to load a custom ResourceBundle called "scope.properties" if it 368 * can be found. 369 */ 370 private void loadCustomConfig() { 371 ResourceBundle bundle = ResourceLoader.getProperties(DEFAULT_CUSTOM_CONFIG_NAME); 372 if (bundle != null) { 373 putAll(bundle); 374 LOG.info("Load custom Scope config from " + DEFAULT_CUSTOM_CONFIG_NAME + ".properties"); 375 } else { 376 LOG.warn("Can't load custom Scope config from " + DEFAULT_CUSTOM_CONFIG_NAME + ".properties"); 377 } 378 } 379 380 381 /*** 382 * Load the default config. 383 */ 384 private void loadDefaultConfig() { 385 ResourceBundle bundle = ResourceLoader.getProperties(DEFAULT_CONFIG_NAME); 386 if (bundle != null) { 387 putAll(bundle); 388 } else { 389 LOG.error("Can't load default Scope config from: " + DEFAULT_CONFIG_NAME); 390 } 391 } 392 393 394 /*** 395 * Put properties from the ResourceBundle. 396 * 397 * @param inBundle The resource bundle to get the properties from. 398 */ 399 private void putAll(ResourceBundle inBundle) { 400 for (Enumeration i = inBundle.getKeys(); i.hasMoreElements(); ) { 401 String key = (String) i.nextElement(); 402 Object value = inBundle.getObject(key); 403 properties.put(key, value); 404 } 405 } 406 }

This page was automatically generated by Maven