1 5 package com.opensymphony.oscache.base; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 import java.io.InputStream ; 11 12 import java.util.Properties ; 13 14 22 public class Config implements java.io.Serializable { 23 private static final transient Log log = LogFactory.getLog(Config.class); 24 25 28 private final static String PROPERTIES_FILENAME = "/oscache.properties"; 29 30 33 private Properties properties = null; 34 35 41 public Config() { 42 this(null); 43 } 44 45 56 public Config(Properties p) { 57 if (log.isDebugEnabled()) { 58 log.debug("Config() called"); 59 } 60 61 if (p == null) { 62 loadProps(); 63 } else { 64 this.properties = p; 65 } 66 } 67 68 78 public String getProperty(String key) { 79 if (key == null) { 80 throw new IllegalArgumentException ("key is null"); 81 } 82 83 if (properties == null) { 84 return null; 85 } 86 87 String value = properties.getProperty(key); 88 return value; 89 } 90 91 97 public Properties getProperties() { 98 return properties; 99 } 100 101 public Object get(Object key) { 102 return properties.get(key); 103 } 104 105 113 public void set(Object key, Object value) { 114 if (key == null) { 115 throw new IllegalArgumentException ("key is null"); 116 } 117 118 if (value == null) { 119 return; 120 } 121 122 if (properties == null) { 123 properties = new Properties (); 124 } 125 126 properties.put(key, value); 127 } 128 129 134 private void loadProps() { 135 if (log.isDebugEnabled()) { 136 log.debug("Getting Config"); 137 } 138 139 properties = new Properties (); 140 141 InputStream in = null; 142 143 try { 144 in = Config.class.getResourceAsStream(PROPERTIES_FILENAME); 145 properties.load(in); 146 log.info("Properties " + properties); 147 } catch (Exception e) { 148 log.error("Error reading " + PROPERTIES_FILENAME + " in CacheAdministrator.loadProps() " + e); 149 log.error("Ensure the " + PROPERTIES_FILENAME + " file is readable and in your classpath."); 150 } finally { 151 try { 152 in.close(); 153 } catch (Exception e) { 154 } 156 } 157 } 158 } 159 | Popular Tags |