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.InputStream;
022: import java.io.InputStreamReader;
023: import java.io.StringWriter;
024: import java.util.Properties;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.roller.config.runtime.RuntimeConfigDefs;
028: import org.apache.roller.config.runtime.RuntimeConfigDefsParser;
029: import org.apache.roller.business.PropertiesManager;
030: import org.apache.roller.business.RollerFactory;
031:
032: /**
033: * This class acts as a convenience gateway for getting property values
034: * via the PropertiesManager. We do this because most calls to the
035: * PropertiesManager are just to get the value of a specific property and
036: * thus the caller doesn't need the full RollerPropertyData object.
037: *
038: * We also provide some methods for converting to different data types.
039: */
040: public class RollerRuntimeConfig {
041:
042: private static Log log = LogFactory
043: .getLog(RollerRuntimeConfig.class);
044:
045: private static String runtime_config = "/rollerRuntimeConfigDefs.xml";
046: private static RuntimeConfigDefs configDefs = null;
047:
048: // special case for our context urls
049: private static String relativeContextURL = null;
050: private static String absoluteContextURL = null;
051:
052: // prevent instantiations
053: private RollerRuntimeConfig() {
054: }
055:
056: /**
057: * Retrieve a single property from the PropertiesManager ... returns null
058: * if there is an error
059: **/
060: public static String getProperty(String name) {
061:
062: String value = null;
063:
064: try {
065: PropertiesManager pmgr = RollerFactory.getRoller()
066: .getPropertiesManager();
067: value = pmgr.getProperty(name).getValue();
068: } catch (Exception e) {
069: log.warn("Trouble accessing property: " + name, e);
070: }
071:
072: log.debug("fetched property [" + name + "=" + value + "]");
073:
074: return value;
075: }
076:
077: /**
078: * Retrieve a property as a boolean ... defaults to false if there is an error
079: **/
080: public static boolean getBooleanProperty(String name) {
081:
082: // get the value first, then convert
083: String value = RollerRuntimeConfig.getProperty(name);
084:
085: if (value == null)
086: return false;
087:
088: return (new Boolean(value)).booleanValue();
089: }
090:
091: /**
092: * Retrieve a property as an int ... defaults to -1 if there is an error
093: **/
094: public static int getIntProperty(String name) {
095:
096: // get the value first, then convert
097: String value = RollerRuntimeConfig.getProperty(name);
098:
099: if (value == null)
100: return -1;
101:
102: int intval = -1;
103: try {
104: intval = Integer.parseInt(value);
105: } catch (Exception e) {
106: log.warn("Trouble converting to int: " + name, e);
107: }
108:
109: return intval;
110: }
111:
112: public static RuntimeConfigDefs getRuntimeConfigDefs() {
113:
114: if (configDefs == null) {
115:
116: // unmarshall the config defs file
117: try {
118: InputStream is = RollerRuntimeConfig.class
119: .getResourceAsStream(runtime_config);
120:
121: RuntimeConfigDefsParser parser = new RuntimeConfigDefsParser();
122: configDefs = parser.unmarshall(is);
123:
124: } catch (Exception e) {
125: // error while parsing :(
126: log.error("Error parsing runtime config defs", e);
127: }
128:
129: }
130:
131: return configDefs;
132: }
133:
134: /**
135: * Get the runtime configuration definitions XML file as a string.
136: *
137: * This is basically a convenience method for accessing this file.
138: * The file itself contains meta-data about what configuration
139: * properties we change at runtime via the UI and how to setup
140: * the display for editing those properties.
141: */
142: public static String getRuntimeConfigDefsAsString() {
143:
144: log.debug("Trying to load runtime config defs file");
145:
146: try {
147: InputStreamReader reader = new InputStreamReader(
148: RollerConfig.class
149: .getResourceAsStream(runtime_config));
150: StringWriter configString = new StringWriter();
151:
152: char[] buf = new char[8196];
153: int length = 0;
154: while ((length = reader.read(buf)) > 0)
155: configString.write(buf, 0, length);
156:
157: reader.close();
158:
159: return configString.toString();
160: } catch (Exception e) {
161: log.error("Error loading runtime config defs file", e);
162: }
163:
164: return "";
165: }
166:
167: /**
168: * Special method which sets the non-persisted absolute url to this site.
169: *
170: * This property is *not* persisted in any way.
171: */
172: public static void setAbsoluteContextURL(String url) {
173: absoluteContextURL = url;
174: }
175:
176: /**
177: * Get the absolute url to this site.
178: *
179: * This method will just return the value of the "site.absoluteurl"
180: * property if it is set, otherwise it will return the non-persisted
181: * value which is set by the InitFilter.
182: */
183: public static String getAbsoluteContextURL() {
184:
185: // db prop takes priority if it exists
186: String absURL = getProperty("site.absoluteurl");
187: if (absURL != null && absURL.trim().length() > 0) {
188: return absURL;
189: }
190:
191: return absoluteContextURL;
192: }
193:
194: /**
195: * Special method which sets the non-persisted relative url to this site.
196: *
197: * This property is *not* persisted in any way.
198: */
199: public static void setRelativeContextURL(String url) {
200: relativeContextURL = url;
201: }
202:
203: public static String getRelativeContextURL() {
204: return relativeContextURL;
205: }
206:
207: /**
208: * Convenience method for Roller classes trying to determine if a given
209: * weblog handle represents the front page blog.
210: */
211: public static boolean isFrontPageWeblog(String weblogHandle) {
212:
213: String frontPageHandle = getProperty("site.frontpage.weblog.handle");
214:
215: return (frontPageHandle.equals(weblogHandle));
216: }
217:
218: /**
219: * Convenience method for Roller classes trying to determine if a given
220: * weblog handle represents the front page blog configured to render
221: * site-wide data.
222: */
223: public static boolean isSiteWideWeblog(String weblogHandle) {
224:
225: boolean siteWide = getBooleanProperty("site.frontpage.weblog.aggregated");
226:
227: return (isFrontPageWeblog(weblogHandle) && siteWide);
228: }
229:
230: }
|