KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RollerConfig.java
3  *
4  */

5
6 package org.roller.config;
7
8 import java.io.File JavaDoc;
9 import java.io.FileInputStream JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.InputStreamReader JavaDoc;
12 import java.io.StringWriter JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19
20 /**
21  * This is the single entry point for accessing configuration properties
22  * in Roller.
23  *
24  * @author Allen Gilliland
25  */

26 public class RollerConfig {
27     
28     private static String JavaDoc default_config = "/roller.properties";
29     private static String JavaDoc custom_config = "/roller-custom.properties";
30     private static String JavaDoc custom_jvm_param = "roller.custom.config";
31     private static File JavaDoc custom_config_file = null;
32     
33     private static Properties JavaDoc mConfig;
34     
35     private static Log mLogger =
36             LogFactory.getFactory().getInstance(RollerConfig.class);
37     
38     
39     /*
40      * Static block run once at class loading
41      *
42      * We load the default properties and any custom properties we find
43      */

44     static {
45         mConfig = new Properties JavaDoc();
46         
47         try {
48             // we'll need this to get at our properties files in the classpath
49
Class JavaDoc config_class = Class.forName("org.roller.config.RollerConfig");
50             
51             // first, lets load our default properties
52
InputStream JavaDoc is = config_class.getResourceAsStream(default_config);
53             mConfig.load(is);
54             mLogger.info("successfully loaded default properties.");
55             
56             // now, see if we can find our custom config
57
is = config_class.getResourceAsStream(custom_config);
58             if(is != null) {
59                 mConfig.load(is);
60                 mLogger.info("successfully loaded custom properties file from classpath");
61             } else {
62                 mLogger.info("no custom properties file found in classpath");
63             }
64             
65             // finally, check for an external config file
66
String JavaDoc env_file = System.getProperty(custom_jvm_param);
67             if(env_file != null && env_file.length() > 0) {
68                 custom_config_file = new File JavaDoc(env_file);
69                 
70                 // make sure the file exists, then try and load it
71
if(custom_config_file != null && custom_config_file.exists()) {
72                     is = new FileInputStream JavaDoc(custom_config_file);
73                     mConfig.load(is);
74                     mLogger.info("successfully loaded custom properties from "+
75                             custom_config_file.getAbsolutePath());
76                 } else {
77                     mLogger.warn("failed to load custom properties from "+
78                             custom_config_file.getAbsolutePath());
79                 }
80                 
81             } else {
82                 mLogger.info("no custom properties file specified via jvm option");
83             }
84             
85             // some debugging for those that want it
86
if(mLogger.isDebugEnabled()) {
87                 mLogger.debug("RollerConfig looks like this ...");
88                 
89                 String JavaDoc key = null;
90                 Enumeration JavaDoc keys = mConfig.keys();
91                 while(keys.hasMoreElements()) {
92                     key = (String JavaDoc) keys.nextElement();
93                     mLogger.debug(key+"="+mConfig.getProperty(key));
94                 }
95             }
96             
97         } catch (Exception JavaDoc e) {
98             e.printStackTrace();
99         }
100         
101     }
102     
103     
104     // no, you may not instantiate this class :p
105
private RollerConfig() {}
106     
107     
108     /**
109      * Retrieve a property value
110      *
111      * @param key Name of the property
112      * @return String Value of property requested, null if not found
113      **/

114     public static String JavaDoc getProperty(String JavaDoc key) {
115         mLogger.debug("Fetching property ["+key+"="+mConfig.getProperty(key)+"]");
116         return mConfig.getProperty(key);
117     }
118     
119     
120     /**
121      * Retrieve a property as a boolean ... defaults to false if there is an error
122      **/

123     public static boolean getBooleanProperty(String JavaDoc name) {
124         
125         // get the value first, then convert
126
String JavaDoc value = RollerConfig.getProperty(name);
127         
128         if(value == null)
129             return false;
130         
131         return (new Boolean JavaDoc(value)).booleanValue();
132     }
133     
134     
135     /**
136      * Retrieve all property keys
137      *
138      * @return Enumeration A list of all keys
139      **/

140     public static Enumeration JavaDoc keys() {
141         return mConfig.keys();
142     }
143     
144     
145     /**
146      * Set the "uploads.dir" property at runtime.
147      *
148      * Properties are meant to be read-only, but we make this one exception
149      * for now because we know that some people are still writing their
150      * uploads to the webapp context and we can only get that path at runtime.
151      */

152     public static void setUploadsDir(String JavaDoc path) {
153         
154         // only do this if the user wants to use the webapp context
155
if("${webapp.context}".equals(mConfig.getProperty("uploads.dir")))
156             mConfig.setProperty("uploads.dir", path);
157     }
158     
159 }
160
Popular Tags