001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.config;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.InputStream;
024: import java.util.Enumeration;
025: import java.util.Properties;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.roller.util.PropertyExpander;
029:
030: /**
031: * This is the single entry point for accessing configuration properties in Roller.
032: */
033: public class RollerConfig {
034:
035: private static String default_config = "/roller.properties";
036: private static String custom_config = "/roller-custom.properties";
037: private static String custom_jvm_param = "roller.custom.config";
038: private static File custom_config_file = null;
039:
040: private static Properties mConfig;
041:
042: private static Log log = LogFactory.getLog(RollerConfig.class);
043:
044: /*
045: * Static block run once at class loading
046: *
047: * We load the default properties and any custom properties we find
048: */
049: static {
050: mConfig = new Properties();
051:
052: try {
053: // we'll need this to get at our properties files in the classpath
054: Class config_class = Class
055: .forName("org.apache.roller.config.RollerConfig");
056:
057: // first, lets load our default properties
058: InputStream is = config_class
059: .getResourceAsStream(default_config);
060: mConfig.load(is);
061: log.info("successfully loaded default properties.");
062:
063: // now, see if we can find our custom config
064: is = config_class.getResourceAsStream(custom_config);
065: if (is != null) {
066: mConfig.load(is);
067: log
068: .info("successfully loaded custom properties file from classpath");
069: } else {
070: log
071: .info("no custom properties file found in classpath");
072: }
073:
074: // finally, check for an external config file
075: String env_file = System.getProperty(custom_jvm_param);
076: if (env_file != null && env_file.length() > 0) {
077: custom_config_file = new File(env_file);
078:
079: // make sure the file exists, then try and load it
080: if (custom_config_file != null
081: && custom_config_file.exists()) {
082: is = new FileInputStream(custom_config_file);
083: mConfig.load(is);
084: log
085: .info("successfully loaded custom properties from "
086: + custom_config_file
087: .getAbsolutePath());
088: } else {
089: log.warn("failed to load custom properties from "
090: + custom_config_file.getAbsolutePath());
091: }
092:
093: } else {
094: log
095: .info("no custom properties file specified via jvm option");
096: }
097:
098: // Now expand system properties for properties in the config.expandedProperties list,
099: // replacing them by their expanded values.
100: String expandedPropertiesDef = (String) mConfig
101: .get("config.expandedProperties");
102: if (expandedPropertiesDef != null) {
103: String[] expandedProperties = expandedPropertiesDef
104: .split(",");
105: for (int i = 0; i < expandedProperties.length; i++) {
106: String propName = expandedProperties[i].trim();
107: String initialValue = (String) mConfig
108: .get(propName);
109: if (initialValue != null) {
110: String expandedValue = PropertyExpander
111: .expandSystemProperties(initialValue);
112: mConfig.put(propName, expandedValue);
113: if (log.isDebugEnabled()) {
114: log.info("Expanded value of " + propName
115: + " from '" + initialValue
116: + "' to '" + expandedValue + "'");
117: }
118: }
119: }
120: }
121:
122: // some debugging for those that want it
123: if (log.isDebugEnabled()) {
124: log.debug("RollerConfig looks like this ...");
125:
126: String key = null;
127: Enumeration keys = mConfig.keys();
128: while (keys.hasMoreElements()) {
129: key = (String) keys.nextElement();
130: log.debug(key + "=" + mConfig.getProperty(key));
131: }
132: }
133:
134: } catch (Exception e) {
135: e.printStackTrace();
136: }
137:
138: }
139:
140: // no, you may not instantiate this class :p
141: private RollerConfig() {
142: }
143:
144: /**
145: * Retrieve a property value
146: * @param key Name of the property
147: * @return String Value of property requested, null if not found
148: */
149: public static String getProperty(String key) {
150: log.debug("Fetching property [" + key + "="
151: + mConfig.getProperty(key) + "]");
152: return mConfig.getProperty(key);
153: }
154:
155: /**
156: * Retrieve a property value
157: * @param key Name of the property
158: * @param defaultValue Default value of property if not found
159: * @return String Value of property requested or defaultValue
160: */
161: public static String getProperty(String key, String defaultValue) {
162: log.debug("Fetching property [" + key + "="
163: + mConfig.getProperty(key) + ",defaultValue="
164: + defaultValue + "]");
165: String value = mConfig.getProperty(key);
166: if (value == null)
167: return defaultValue;
168:
169: return value;
170: }
171:
172: /**
173: * Retrieve a property as a boolean ... defaults to false if not present.
174: */
175: public static boolean getBooleanProperty(String name) {
176: return getBooleanProperty(name, false);
177: }
178:
179: /**
180: * Retrieve a property as a boolean ... with specified default if not present.
181: */
182: public static boolean getBooleanProperty(String name,
183: boolean defaultValue) {
184: // get the value first, then convert
185: String value = RollerConfig.getProperty(name);
186:
187: if (value == null)
188: return defaultValue;
189:
190: return (new Boolean(value)).booleanValue();
191: }
192:
193: /**
194: * Retrieve a property as an int ... defaults to 0 if not present.
195: */
196: public static int getIntProperty(String name) {
197: return getIntProperty(name, 0);
198: }
199:
200: /**
201: * Retrieve a property as a int ... with specified default if not present.
202: */
203: public static int getIntProperty(String name, int defaultValue) {
204: // get the value first, then convert
205: String value = RollerConfig.getProperty(name);
206:
207: if (value == null)
208: return defaultValue;
209:
210: return (new Integer(value)).intValue();
211: }
212:
213: /**
214: * Retrieve all property keys
215: * @return Enumeration A list of all keys
216: **/
217: public static Enumeration keys() {
218: return mConfig.keys();
219: }
220:
221: /**
222: * Set the "uploads.dir" property at runtime.
223: * <p />
224: * Properties are meant to be read-only, but we make this exception because
225: * we know that some people are still writing their uploads to the webapp
226: * context and we can only get that path at runtime (and for unit testing).
227: * <p />
228: * This property is *not* persisted in any way.
229: */
230: public static void setUploadsDir(String path) {
231: // only do this if the user wants to use the webapp context
232: if ("${webapp.context}".equals(mConfig
233: .getProperty("uploads.dir")))
234: mConfig.setProperty("uploads.dir", path);
235: }
236:
237: /**
238: * Set the "themes.dir" property at runtime.
239: * <p />
240: * Properties are meant to be read-only, but we make this exception because
241: * we know that some people are still using their themes in the webapp
242: * context and we can only get that path at runtime (and for unit testing).
243: * <p />
244: * This property is *not* persisted in any way.
245: */
246: public static void setThemesDir(String path) {
247: // only do this if the user wants to use the webapp context
248: if ("${webapp.context}".equals(mConfig
249: .getProperty("themes.dir")))
250: mConfig.setProperty("themes.dir", path);
251: }
252:
253: }
|