KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.util.Properties JavaDoc;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.config.runtime.RuntimeConfigDefs;
28 import org.apache.roller.config.runtime.RuntimeConfigDefsParser;
29 import org.apache.roller.model.PropertiesManager;
30 import org.apache.roller.model.RollerFactory;
31
32
33 /**
34  * This class acts as a convenience gateway for getting property values
35  * via the PropertiesManager. We do this because most calls to the
36  * PropertiesManager are just to get the value of a specific property and
37  * thus the caller doesn't need the full RollerPropertyData object.
38  *
39  * We also provide some methods for converting to different data types.
40  */

41 public class RollerRuntimeConfig {
42     
43     private static Log log = LogFactory.getLog(RollerRuntimeConfig.class);
44     
45     private static String JavaDoc runtime_config = "/rollerRuntimeConfigDefs.xml";
46     private static RuntimeConfigDefs configDefs = null;
47     
48     // special case for our context urls
49
private static String JavaDoc relativeContextURL = null;
50     private static String JavaDoc absoluteContextURL = null;
51     
52     
53     // prevent instantiations
54
private RollerRuntimeConfig() {}
55     
56     
57     /**
58      * Retrieve a single property from the PropertiesManager ... returns null
59      * if there is an error
60      **/

61     public static String JavaDoc getProperty(String JavaDoc name) {
62         
63         String JavaDoc value = null;
64         
65         try {
66             PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager();
67             value = pmgr.getProperty(name).getValue();
68         } catch(Exception JavaDoc e) {
69             log.warn("Trouble accessing property: "+name, e);
70         }
71         
72         log.debug("fetched property ["+name+"="+value+"]");
73
74         return value;
75     }
76     
77     
78     /**
79      * Retrieve a property as a boolean ... defaults to false if there is an error
80      **/

81     public static boolean getBooleanProperty(String JavaDoc name) {
82         
83         // get the value first, then convert
84
String JavaDoc value = RollerRuntimeConfig.getProperty(name);
85         
86         if(value == null)
87             return false;
88         
89         return (new Boolean JavaDoc(value)).booleanValue();
90     }
91     
92     
93     /**
94      * Retrieve a property as an int ... defaults to -1 if there is an error
95      **/

96     public static int getIntProperty(String JavaDoc name) {
97         
98         // get the value first, then convert
99
String JavaDoc value = RollerRuntimeConfig.getProperty(name);
100         
101         if(value == null)
102             return -1;
103         
104         int intval = -1;
105         try {
106             intval = Integer.parseInt(value);
107         } catch(Exception JavaDoc e) {
108             log.warn("Trouble converting to int: "+name, e);
109         }
110         
111         return intval;
112     }
113     
114     
115     public static RuntimeConfigDefs getRuntimeConfigDefs() {
116         
117         if(configDefs == null) {
118             
119             // unmarshall the config defs file
120
try {
121                 InputStream JavaDoc is =
122                         RollerRuntimeConfig.class.getResourceAsStream(runtime_config);
123                 
124                 RuntimeConfigDefsParser parser = new RuntimeConfigDefsParser();
125                 configDefs = parser.unmarshall(is);
126                 
127             } catch(Exception JavaDoc e) {
128                 // error while parsing :(
129
log.error("Error parsing runtime config defs", e);
130             }
131             
132         }
133         
134         return configDefs;
135     }
136     
137     
138     /**
139      * Get the runtime configuration definitions XML file as a string.
140      *
141      * This is basically a convenience method for accessing this file.
142      * The file itself contains meta-data about what configuration
143      * properties we change at runtime via the UI and how to setup
144      * the display for editing those properties.
145      */

146     public static String JavaDoc getRuntimeConfigDefsAsString() {
147         
148         log.debug("Trying to load runtime config defs file");
149         
150         try {
151             InputStreamReader JavaDoc reader =
152                     new InputStreamReader JavaDoc(RollerConfig.class.getResourceAsStream(runtime_config));
153             StringWriter JavaDoc configString = new StringWriter JavaDoc();
154             
155             char[] buf = new char[8196];
156             int length = 0;
157             while((length = reader.read(buf)) > 0)
158                 configString.write(buf, 0, length);
159             
160             reader.close();
161             
162             return configString.toString();
163         } catch(Exception JavaDoc e) {
164             log.error("Error loading runtime config defs file", e);
165         }
166         
167         return "";
168     }
169     
170     
171     /**
172      * Special method which sets the non-persisted absolute url to this site.
173      *
174      * This property is *not* persisted in any way.
175      */

176     public static void setAbsoluteContextURL(String JavaDoc url) {
177         absoluteContextURL = url;
178     }
179     
180     
181     /**
182      * Get the absolute url to this site.
183      *
184      * This method will just return the value of the "site.absoluteurl"
185      * property if it is set, otherwise it will return the non-persisted
186      * value which is set by the InitFilter.
187      */

188     public static String JavaDoc getAbsoluteContextURL() {
189         
190         // db prop takes priority if it exists
191
String JavaDoc absURL = getProperty("site.absoluteurl");
192         if(absURL != null && absURL.trim().length() > 0) {
193             return absURL;
194         }
195         
196         return absoluteContextURL;
197     }
198     
199     
200     /**
201      * Special method which sets the non-persisted relative url to this site.
202      *
203      * This property is *not* persisted in any way.
204      */

205     public static void setRelativeContextURL(String JavaDoc url) {
206         relativeContextURL = url;
207     }
208     
209     
210     public static String JavaDoc getRelativeContextURL() {
211         return relativeContextURL;
212     }
213     
214     
215     /**
216      * Convenience method for Roller classes trying to determine if a given
217      * weblog handle represents the front page blog.
218      */

219     public static boolean isFrontPageWeblog(String JavaDoc weblogHandle) {
220         
221         String JavaDoc frontPageHandle = getProperty("site.frontpage.weblog.handle");
222         
223         return (frontPageHandle.equals(weblogHandle));
224     }
225     
226     
227     /**
228      * Convenience method for Roller classes trying to determine if a given
229      * weblog handle represents the front page blog configured to render
230      * site-wide data.
231      */

232     public static boolean isSiteWideWeblog(String JavaDoc weblogHandle) {
233         
234         boolean siteWide = getBooleanProperty("site.frontpage.weblog.aggregated");
235         
236         return (isFrontPageWeblog(weblogHandle) && siteWide);
237     }
238     
239 }
240
Popular Tags