1 18 19 package org.apache.roller.config; 20 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.StringWriter ; 24 import java.util.Properties ; 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 41 public class RollerRuntimeConfig { 42 43 private static Log log = LogFactory.getLog(RollerRuntimeConfig.class); 44 45 private static String runtime_config = "/rollerRuntimeConfigDefs.xml"; 46 private static RuntimeConfigDefs configDefs = null; 47 48 private static String relativeContextURL = null; 50 private static String absoluteContextURL = null; 51 52 53 private RollerRuntimeConfig() {} 55 56 57 61 public static String getProperty(String name) { 62 63 String value = null; 64 65 try { 66 PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager(); 67 value = pmgr.getProperty(name).getValue(); 68 } catch(Exception 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 81 public static boolean getBooleanProperty(String name) { 82 83 String value = RollerRuntimeConfig.getProperty(name); 85 86 if(value == null) 87 return false; 88 89 return (new Boolean (value)).booleanValue(); 90 } 91 92 93 96 public static int getIntProperty(String name) { 97 98 String 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 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 try { 121 InputStream is = 122 RollerRuntimeConfig.class.getResourceAsStream(runtime_config); 123 124 RuntimeConfigDefsParser parser = new RuntimeConfigDefsParser(); 125 configDefs = parser.unmarshall(is); 126 127 } catch(Exception e) { 128 log.error("Error parsing runtime config defs", e); 130 } 131 132 } 133 134 return configDefs; 135 } 136 137 138 146 public static String getRuntimeConfigDefsAsString() { 147 148 log.debug("Trying to load runtime config defs file"); 149 150 try { 151 InputStreamReader reader = 152 new InputStreamReader (RollerConfig.class.getResourceAsStream(runtime_config)); 153 StringWriter configString = new StringWriter (); 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 e) { 164 log.error("Error loading runtime config defs file", e); 165 } 166 167 return ""; 168 } 169 170 171 176 public static void setAbsoluteContextURL(String url) { 177 absoluteContextURL = url; 178 } 179 180 181 188 public static String getAbsoluteContextURL() { 189 190 String absURL = getProperty("site.absoluteurl"); 192 if(absURL != null && absURL.trim().length() > 0) { 193 return absURL; 194 } 195 196 return absoluteContextURL; 197 } 198 199 200 205 public static void setRelativeContextURL(String url) { 206 relativeContextURL = url; 207 } 208 209 210 public static String getRelativeContextURL() { 211 return relativeContextURL; 212 } 213 214 215 219 public static boolean isFrontPageWeblog(String weblogHandle) { 220 221 String frontPageHandle = getProperty("site.frontpage.weblog.handle"); 222 223 return (frontPageHandle.equals(weblogHandle)); 224 } 225 226 227 232 public static boolean isSiteWideWeblog(String weblogHandle) { 233 234 boolean siteWide = getBooleanProperty("site.frontpage.weblog.aggregated"); 235 236 return (isFrontPageWeblog(weblogHandle) && siteWide); 237 } 238 239 } 240 | Popular Tags |