1 5 6 package org.roller.config; 7 8 import java.io.File ; 9 import java.io.FileInputStream ; 10 import java.io.InputStream ; 11 import java.io.InputStreamReader ; 12 import java.io.StringWriter ; 13 import java.util.Enumeration ; 14 import java.util.Properties ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 20 26 public class RollerConfig { 27 28 private static String default_config = "/roller.properties"; 29 private static String custom_config = "/roller-custom.properties"; 30 private static String custom_jvm_param = "roller.custom.config"; 31 private static File custom_config_file = null; 32 33 private static Properties mConfig; 34 35 private static Log mLogger = 36 LogFactory.getFactory().getInstance(RollerConfig.class); 37 38 39 44 static { 45 mConfig = new Properties (); 46 47 try { 48 Class config_class = Class.forName("org.roller.config.RollerConfig"); 50 51 InputStream is = config_class.getResourceAsStream(default_config); 53 mConfig.load(is); 54 mLogger.info("successfully loaded default properties."); 55 56 is = config_class.getResourceAsStream(custom_config); 58 if(is != null) { 59 mConfig.load(is); 60 mLogger.info("successfully loaded custom properties file from classpath"); 61 } else { 62 mLogger.info("no custom properties file found in classpath"); 63 } 64 65 String env_file = System.getProperty(custom_jvm_param); 67 if(env_file != null && env_file.length() > 0) { 68 custom_config_file = new File (env_file); 69 70 if(custom_config_file != null && custom_config_file.exists()) { 72 is = new FileInputStream (custom_config_file); 73 mConfig.load(is); 74 mLogger.info("successfully loaded custom properties from "+ 75 custom_config_file.getAbsolutePath()); 76 } else { 77 mLogger.warn("failed to load custom properties from "+ 78 custom_config_file.getAbsolutePath()); 79 } 80 81 } else { 82 mLogger.info("no custom properties file specified via jvm option"); 83 } 84 85 if(mLogger.isDebugEnabled()) { 87 mLogger.debug("RollerConfig looks like this ..."); 88 89 String key = null; 90 Enumeration keys = mConfig.keys(); 91 while(keys.hasMoreElements()) { 92 key = (String ) keys.nextElement(); 93 mLogger.debug(key+"="+mConfig.getProperty(key)); 94 } 95 } 96 97 } catch (Exception e) { 98 e.printStackTrace(); 99 } 100 101 } 102 103 104 private RollerConfig() {} 106 107 108 114 public static String getProperty(String key) { 115 mLogger.debug("Fetching property ["+key+"="+mConfig.getProperty(key)+"]"); 116 return mConfig.getProperty(key); 117 } 118 119 120 123 public static boolean getBooleanProperty(String name) { 124 125 String value = RollerConfig.getProperty(name); 127 128 if(value == null) 129 return false; 130 131 return (new Boolean (value)).booleanValue(); 132 } 133 134 135 140 public static Enumeration keys() { 141 return mConfig.keys(); 142 } 143 144 145 152 public static void setUploadsDir(String path) { 153 154 if("${webapp.context}".equals(mConfig.getProperty("uploads.dir"))) 156 mConfig.setProperty("uploads.dir", path); 157 } 158 159 } 160 | Popular Tags |