KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > config > RollerConfig


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.config;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Properties JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.util.PropertyExpander;
29
30
31 /**
32  * This is the single entry point for accessing configuration properties in Roller.
33  */

34 public class RollerConfig {
35     
36     private static String JavaDoc default_config = "/roller.properties";
37     private static String JavaDoc custom_config = "/roller-custom.properties";
38     private static String JavaDoc custom_jvm_param = "roller.custom.config";
39     private static File JavaDoc custom_config_file = null;
40
41     private static Properties JavaDoc mConfig;
42
43     private static Log log = LogFactory.getLog(RollerConfig.class);
44     
45     
46     /*
47      * Static block run once at class loading
48      *
49      * We load the default properties and any custom properties we find
50      */

51     static {
52         mConfig = new Properties JavaDoc();
53
54         try {
55             // we'll need this to get at our properties files in the classpath
56
Class JavaDoc config_class = Class.forName("org.apache.roller.config.RollerConfig");
57
58             // first, lets load our default properties
59
InputStream JavaDoc is = config_class.getResourceAsStream(default_config);
60             mConfig.load(is);
61             log.info("successfully loaded default properties.");
62
63             // now, see if we can find our custom config
64
is = config_class.getResourceAsStream(custom_config);
65             if(is != null) {
66                 mConfig.load(is);
67                 log.info("successfully loaded custom properties file from classpath");
68             } else {
69                 log.info("no custom properties file found in classpath");
70             }
71
72             // finally, check for an external config file
73
String JavaDoc env_file = System.getProperty(custom_jvm_param);
74             if(env_file != null && env_file.length() > 0) {
75                 custom_config_file = new File JavaDoc(env_file);
76
77                 // make sure the file exists, then try and load it
78
if(custom_config_file != null && custom_config_file.exists()) {
79                     is = new FileInputStream JavaDoc(custom_config_file);
80                     mConfig.load(is);
81                     log.info("successfully loaded custom properties from "+
82                             custom_config_file.getAbsolutePath());
83                 } else {
84                     log.warn("failed to load custom properties from "+
85                             custom_config_file.getAbsolutePath());
86                 }
87
88             } else {
89                 log.info("no custom properties file specified via jvm option");
90             }
91
92             // Now expand system properties for properties in the config.expandedProperties list,
93
// replacing them by their expanded values.
94
String JavaDoc expandedPropertiesDef = (String JavaDoc) mConfig.get("config.expandedProperties");
95             if (expandedPropertiesDef != null) {
96                 String JavaDoc[] expandedProperties = expandedPropertiesDef.split(",");
97                 for (int i = 0; i < expandedProperties.length; i++) {
98                     String JavaDoc propName = expandedProperties[i].trim();
99                     String JavaDoc initialValue = (String JavaDoc) mConfig.get(propName);
100                     if (initialValue != null) {
101                         String JavaDoc expandedValue = PropertyExpander.expandSystemProperties(initialValue);
102                         mConfig.put(propName,expandedValue);
103                         if (log.isDebugEnabled()) {
104                             log.info("Expanded value of " + propName + " from '" +
105                                 initialValue + "' to '" + expandedValue + "'");
106                         }
107                     }
108                 }
109             }
110
111             // some debugging for those that want it
112
if(log.isDebugEnabled()) {
113                 log.debug("RollerConfig looks like this ...");
114
115                 String JavaDoc key = null;
116                 Enumeration JavaDoc keys = mConfig.keys();
117                 while(keys.hasMoreElements()) {
118                     key = (String JavaDoc) keys.nextElement();
119                     log.debug(key+"="+mConfig.getProperty(key));
120                 }
121             }
122
123         } catch (Exception JavaDoc e) {
124             e.printStackTrace();
125         }
126
127     }
128
129
130     // no, you may not instantiate this class :p
131
private RollerConfig() {}
132
133
134     /**
135      * Retrieve a property value
136      * @param key Name of the property
137      * @return String Value of property requested, null if not found
138      */

139     public static String JavaDoc getProperty(String JavaDoc key) {
140         log.debug("Fetching property ["+key+"="+mConfig.getProperty(key)+"]");
141         return mConfig.getProperty(key);
142     }
143     
144     /**
145      * Retrieve a property value
146      * @param key Name of the property
147      * @param defaultValue Default value of property if not found
148      * @return String Value of property requested or defaultValue
149      */

150     public static String JavaDoc getProperty(String JavaDoc key, String JavaDoc defaultValue) {
151         log.debug("Fetching property ["+key+"="+mConfig.getProperty(key)+",defaultValue="+defaultValue+"]");
152         String JavaDoc value = mConfig.getProperty(key);
153         if(value == null)
154           return defaultValue;
155         
156         return value;
157     }
158
159     /**
160      * Retrieve a property as a boolean ... defaults to false if not present.
161      */

162     public static boolean getBooleanProperty(String JavaDoc name) {
163         return getBooleanProperty(name,false);
164     }
165
166     /**
167      * Retrieve a property as a boolean ... with specified default if not present.
168      */

169     public static boolean getBooleanProperty(String JavaDoc name, boolean defaultValue) {
170         // get the value first, then convert
171
String JavaDoc value = RollerConfig.getProperty(name);
172
173         if(value == null)
174             return defaultValue;
175
176         return (new Boolean JavaDoc(value)).booleanValue();
177     }
178
179     /**
180      * Retrieve a property as an int ... defaults to 0 if not present.
181      */

182     public static int getIntProperty(String JavaDoc name) {
183         return getIntProperty(name, 0);
184     }
185
186     /**
187      * Retrieve a property as a int ... with specified default if not present.
188      */

189     public static int getIntProperty(String JavaDoc name, int defaultValue) {
190         // get the value first, then convert
191
String JavaDoc value = RollerConfig.getProperty(name);
192
193         if (value == null)
194             return defaultValue;
195
196         return (new Integer JavaDoc(value)).intValue();
197     }
198
199     /**
200      * Retrieve all property keys
201      * @return Enumeration A list of all keys
202      **/

203     public static Enumeration JavaDoc keys() {
204         return mConfig.keys();
205     }
206    
207
208     /**
209      * Set the "uploads.dir" property at runtime.
210      * <p />
211      * Properties are meant to be read-only, but we make this exception because
212      * we know that some people are still writing their uploads to the webapp
213      * context and we can only get that path at runtime (and for unit testing).
214      * <p />
215      * This property is *not* persisted in any way.
216      */

217     public static void setUploadsDir(String JavaDoc path) {
218         // only do this if the user wants to use the webapp context
219
if("${webapp.context}".equals(mConfig.getProperty("uploads.dir")))
220             mConfig.setProperty("uploads.dir", path);
221     }
222
223     /**
224      * Set the "context.realPath" property at runtime.
225      * <p />
226      * Properties are meant to be read-only, but we make this exception because
227      * there are some classes which rely on having filesystem access to files
228      * in theRoller webapp context (and for unit testing).
229      * <p />
230      * This property is *not* persisted in any way.
231      */

232     public static void setContextRealPath(String JavaDoc path) {
233         mConfig.setProperty("context.realPath", path);
234     }
235     
236     /**
237      * Set the "context.realpath" property at runtime.
238      * <p />
239      * Properties are meant to be read-only, but we make this exception to make
240      * it possible for unit tests to control the cache directory.
241      * <p />
242      * This property is *not* persisted in any way.
243      */

244     public static void setPlanetCachePath(String JavaDoc path) {
245         mConfig.setProperty("planet.aggregator.cache.dir", path);
246     }
247 }
248
Popular Tags