001: /*
002: * $Id: BexeeProperties.java,v 1.6 2004/12/09 12:34:45 kowap Exp $
003: *
004: * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
005: * Berne University of Applied Sciences
006: * School of Engineering and Information Technology
007: * All rights reserved.
008: */
009: package bexee.util;
010:
011: import java.io.IOException;
012: import java.io.InputStream;
013: import java.util.Properties;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017:
018: /**
019: * Provides a convenient way for accessing system properties and bexee
020: * properties of which the latter are stored in a file called
021: * <code>bexee.properties</code> and must be somewhere in the classpath.
022: * <p>
023: * Note that system properties take precedence over all other properties unless
024: * a property is explicitly programmaticly set.
025: * <p>
026: * The system properties are always checked for changes whereas the properties
027: * from the <code>bexee.properties</code> are only read once upon creation.
028: * <p>
029: * The class provides a number of static methods that always check first if the
030: * properties had been loaded from the properties file.
031: *
032: * @version $Revision: 1.6 $, $Date: 2004/12/09 12:34:45 $
033: * @author Patric Fornasier
034: */
035: public class BexeeProperties {
036:
037: protected static final String FILE_NAME = "bexee.properties";
038:
039: private static Log log = LogFactory.getLog(BexeeProperties.class);
040:
041: // union of bexee and system properties
042: protected static Properties props;
043:
044: // properties from bexee.properties file
045: protected static Properties bexee;
046:
047: /**
048: * Searches for the property with the specified key in this property list.
049: * The method returns null if the property is not found.
050: *
051: * @param key
052: * the property key.
053: * @return the value in this property list with the specified key value.
054: */
055: public static String getProperty(String key) {
056: init();
057: return props.getProperty(key);
058: }
059:
060: /**
061: * Searches for the property with the specified key in this property list.
062: * The method returns the default value argument if the property is not
063: * found.
064: *
065: * @param key
066: * the hashtable key
067: * @param defaultValue
068: * a default value
069: * @return the value in this property list with the specified key value
070: */
071: public static String getProperty(String key, String defaultValue) {
072: init();
073: return props.getProperty(key, defaultValue);
074: }
075:
076: /**
077: * Returns the initialized underlying property file.
078: *
079: * @return a <code>Property</code> object.
080: */
081: public static Properties getProperties() {
082: init();
083: return props;
084: }
085:
086: /**
087: * Calls the Hashtable method put. Provided for parallelism with the
088: * getProperty method. Enforces use of strings for property keys and values.
089: * The value returned is the result of the Hashtable call to put.
090: *
091: * @param key
092: * the key to be placed into this property list
093: * @param value
094: * the value corresponding to key
095: * @return the previous value of the specified key in this property list, or
096: * null if it did not have one
097: */
098: public static Object setProperty(String key, String value) {
099: init();
100: return props.setProperty(key, value);
101: }
102:
103: /**
104: * Makes sure the properties are initialized. This method is the first thing
105: * called in every method inside this class.
106: * <p>
107: * If created for the first time this method tries to load the properties
108: * from a property file and then adds system properties, which means that
109: * application specified properties may be overriden.
110: */
111: private static void init() {
112: if (props == null) {
113:
114: props = new Properties();
115: bexee = new Properties();
116:
117: // find properties file
118: ClassLoader loader = BexeeProperties.class.getClassLoader();
119: InputStream is = loader.getResourceAsStream(FILE_NAME);
120:
121: // try to load it
122: if (is == null) {
123: log.info("unable to locate file: " + FILE_NAME);
124: } else {
125: try {
126: // load properties from file once and store them in bexee
127: bexee.load(is);
128: // copy properties (they can be overriden by system props)
129: props.putAll(bexee);
130: } catch (IOException e) {
131: log.info("unable to load file: " + FILE_NAME, e);
132: }
133: }
134: }
135:
136: // always override with latest system properties
137: props.putAll(System.getProperties());
138: }
139: }
|