KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RollerRuntimeConfig.java
3  *
4  * Created on May 4, 2005, 3:00 PM
5  */

6
7 package org.roller.config;
8
9 import java.io.InputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.io.StringWriter JavaDoc;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.roller.config.runtime.RuntimeConfigDefs;
15 import org.roller.config.runtime.RuntimeConfigDefsParser;
16 import org.roller.model.PropertiesManager;
17 import org.roller.model.RollerFactory;
18
19 /**
20  * This class acts as a convenience gateway for getting property values
21  * via the PropertiesManager. We do this because most calls to the
22  * PropertiesManager are just to get the value of a specific property and
23  * thus the caller doesn't need the full RollerPropertyData object.
24  *
25  * We also provide some methods for converting to different data types.
26  *
27  * @author Allen Gilliland
28  */

29 public class RollerRuntimeConfig {
30     
31     private static String JavaDoc runtime_config = "/rollerRuntimeConfigDefs.xml";
32     private static RuntimeConfigDefs configDefs = null;
33     
34     private static Log mLogger =
35         LogFactory.getFactory().getInstance(RollerRuntimeConfig.class);
36     
37     
38     // prevent instantiations
39
private RollerRuntimeConfig() {}
40     
41     
42     /**
43      * Retrieve a single property from the PropertiesManager ... returns null
44      * if there is an error
45      **/

46     public static String JavaDoc getProperty(String JavaDoc name) {
47         
48         String JavaDoc value = null;
49         try {
50             PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager();
51             value = pmgr.getProperty(name).getValue();
52         } catch(Exception JavaDoc e) {
53             mLogger.warn("Trouble accessing property: "+name, e);
54         }
55         
56         mLogger.debug("fetched property ["+name+"="+value+"]");
57         
58         return value;
59     }
60     
61     
62     /**
63      * Retrieve a property as a boolean ... defaults to false if there is an error
64      **/

65     public static boolean getBooleanProperty(String JavaDoc name) {
66         
67         // get the value first, then convert
68
String JavaDoc value = RollerRuntimeConfig.getProperty(name);
69         
70         if(value == null)
71             return false;
72         
73         return (new Boolean JavaDoc(value)).booleanValue();
74     }
75     
76     
77     /**
78      * Retrieve a property as an int ... defaults to -1 if there is an error
79      **/

80     public static int getIntProperty(String JavaDoc name) {
81         
82         // get the value first, then convert
83
String JavaDoc value = RollerRuntimeConfig.getProperty(name);
84         
85         if(value == null)
86             return -1;
87         
88         int intval = -1;
89         try {
90             intval = Integer.parseInt(value);
91         } catch(Exception JavaDoc e) {
92             mLogger.warn("Trouble converting to int: "+name, e);
93         }
94         
95         return intval;
96     }
97     
98     
99     public static RuntimeConfigDefs getRuntimeConfigDefs() {
100         
101         if(configDefs == null) {
102             
103             // unmarshall the config defs file
104
try {
105                 InputStream JavaDoc is =
106                         RollerConfig.class.getResourceAsStream(runtime_config);
107                 
108                 RuntimeConfigDefsParser parser = new RuntimeConfigDefsParser();
109                 configDefs = parser.unmarshall(is);
110                 
111             } catch(Exception JavaDoc e) {
112                 // error while parsing :(
113
mLogger.error("Error parsing runtime config defs", e);
114             }
115             
116         }
117         
118         return configDefs;
119     }
120     
121     
122     /**
123      * Get the runtime configuration definitions XML file as a string.
124      *
125      * This is basically a convenience method for accessing this file.
126      * The file itself contains meta-data about what configuration
127      * properties we change at runtime via the UI and how to setup
128      * the display for editing those properties.
129      */

130     public static String JavaDoc getRuntimeConfigDefsAsString() {
131         
132         mLogger.debug("Trying to load runtime config defs file");
133         
134         try {
135             InputStreamReader JavaDoc reader =
136                     new InputStreamReader JavaDoc(RollerConfig.class.getResourceAsStream(runtime_config));
137             StringWriter JavaDoc configString = new StringWriter JavaDoc();
138             
139             char[] buf = new char[8196];
140             int length = 0;
141             while((length = reader.read(buf)) > 0)
142                 configString.write(buf, 0, length);
143             
144             reader.close();
145             
146             return configString.toString();
147         } catch(Exception JavaDoc e) {
148             mLogger.error("Error loading runtime config defs file", e);
149         }
150         
151         return "";
152     }
153     
154 }
155
Popular Tags