1 6 7 package org.roller.config; 8 9 import java.io.InputStream ; 10 import java.io.InputStreamReader ; 11 import java.io.StringWriter ; 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 29 public class RollerRuntimeConfig { 30 31 private static String 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 private RollerRuntimeConfig() {} 40 41 42 46 public static String getProperty(String name) { 47 48 String value = null; 49 try { 50 PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager(); 51 value = pmgr.getProperty(name).getValue(); 52 } catch(Exception 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 65 public static boolean getBooleanProperty(String name) { 66 67 String value = RollerRuntimeConfig.getProperty(name); 69 70 if(value == null) 71 return false; 72 73 return (new Boolean (value)).booleanValue(); 74 } 75 76 77 80 public static int getIntProperty(String name) { 81 82 String 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 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 try { 105 InputStream is = 106 RollerConfig.class.getResourceAsStream(runtime_config); 107 108 RuntimeConfigDefsParser parser = new RuntimeConfigDefsParser(); 109 configDefs = parser.unmarshall(is); 110 111 } catch(Exception e) { 112 mLogger.error("Error parsing runtime config defs", e); 114 } 115 116 } 117 118 return configDefs; 119 } 120 121 122 130 public static String getRuntimeConfigDefsAsString() { 131 132 mLogger.debug("Trying to load runtime config defs file"); 133 134 try { 135 InputStreamReader reader = 136 new InputStreamReader (RollerConfig.class.getResourceAsStream(runtime_config)); 137 StringWriter configString = new StringWriter (); 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 e) { 148 mLogger.error("Error loading runtime config defs file", e); 149 } 150 151 return ""; 152 } 153 154 } 155 | Popular Tags |