1 18 19 package org.apache.roller.config; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.InputStream ; 24 import java.util.Enumeration ; 25 import java.util.Properties ; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.roller.util.PropertyExpander; 29 30 31 34 public class RollerConfig { 35 36 private static String default_config = "/roller.properties"; 37 private static String custom_config = "/roller-custom.properties"; 38 private static String custom_jvm_param = "roller.custom.config"; 39 private static File custom_config_file = null; 40 41 private static Properties mConfig; 42 43 private static Log log = LogFactory.getLog(RollerConfig.class); 44 45 46 51 static { 52 mConfig = new Properties (); 53 54 try { 55 Class config_class = Class.forName("org.apache.roller.config.RollerConfig"); 57 58 InputStream is = config_class.getResourceAsStream(default_config); 60 mConfig.load(is); 61 log.info("successfully loaded default properties."); 62 63 is = config_class.getResourceAsStream(custom_config); 65 if(is != null) { 66 mConfig.load(is); 67 log.info("successfully loaded custom properties file from classpath"); 68 } else { 69 log.info("no custom properties file found in classpath"); 70 } 71 72 String env_file = System.getProperty(custom_jvm_param); 74 if(env_file != null && env_file.length() > 0) { 75 custom_config_file = new File (env_file); 76 77 if(custom_config_file != null && custom_config_file.exists()) { 79 is = new FileInputStream (custom_config_file); 80 mConfig.load(is); 81 log.info("successfully loaded custom properties from "+ 82 custom_config_file.getAbsolutePath()); 83 } else { 84 log.warn("failed to load custom properties from "+ 85 custom_config_file.getAbsolutePath()); 86 } 87 88 } else { 89 log.info("no custom properties file specified via jvm option"); 90 } 91 92 String expandedPropertiesDef = (String ) mConfig.get("config.expandedProperties"); 95 if (expandedPropertiesDef != null) { 96 String [] expandedProperties = expandedPropertiesDef.split(","); 97 for (int i = 0; i < expandedProperties.length; i++) { 98 String propName = expandedProperties[i].trim(); 99 String initialValue = (String ) mConfig.get(propName); 100 if (initialValue != null) { 101 String expandedValue = PropertyExpander.expandSystemProperties(initialValue); 102 mConfig.put(propName,expandedValue); 103 if (log.isDebugEnabled()) { 104 log.info("Expanded value of " + propName + " from '" + 105 initialValue + "' to '" + expandedValue + "'"); 106 } 107 } 108 } 109 } 110 111 if(log.isDebugEnabled()) { 113 log.debug("RollerConfig looks like this ..."); 114 115 String key = null; 116 Enumeration keys = mConfig.keys(); 117 while(keys.hasMoreElements()) { 118 key = (String ) keys.nextElement(); 119 log.debug(key+"="+mConfig.getProperty(key)); 120 } 121 } 122 123 } catch (Exception e) { 124 e.printStackTrace(); 125 } 126 127 } 128 129 130 private RollerConfig() {} 132 133 134 139 public static String getProperty(String key) { 140 log.debug("Fetching property ["+key+"="+mConfig.getProperty(key)+"]"); 141 return mConfig.getProperty(key); 142 } 143 144 150 public static String getProperty(String key, String defaultValue) { 151 log.debug("Fetching property ["+key+"="+mConfig.getProperty(key)+",defaultValue="+defaultValue+"]"); 152 String value = mConfig.getProperty(key); 153 if(value == null) 154 return defaultValue; 155 156 return value; 157 } 158 159 162 public static boolean getBooleanProperty(String name) { 163 return getBooleanProperty(name,false); 164 } 165 166 169 public static boolean getBooleanProperty(String name, boolean defaultValue) { 170 String value = RollerConfig.getProperty(name); 172 173 if(value == null) 174 return defaultValue; 175 176 return (new Boolean (value)).booleanValue(); 177 } 178 179 182 public static int getIntProperty(String name) { 183 return getIntProperty(name, 0); 184 } 185 186 189 public static int getIntProperty(String name, int defaultValue) { 190 String value = RollerConfig.getProperty(name); 192 193 if (value == null) 194 return defaultValue; 195 196 return (new Integer (value)).intValue(); 197 } 198 199 203 public static Enumeration keys() { 204 return mConfig.keys(); 205 } 206 207 208 217 public static void setUploadsDir(String path) { 218 if("${webapp.context}".equals(mConfig.getProperty("uploads.dir"))) 220 mConfig.setProperty("uploads.dir", path); 221 } 222 223 232 public static void setContextRealPath(String path) { 233 mConfig.setProperty("context.realPath", path); 234 } 235 236 244 public static void setPlanetCachePath(String path) { 245 mConfig.setProperty("planet.aggregator.cache.dir", path); 246 } 247 } 248 | Popular Tags |