1 5 package com.tc.properties; 6 7 import com.tc.logging.TCLogger; 8 import com.tc.logging.TCLogging; 9 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileNotFoundException ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Properties ; 21 import java.util.Map.Entry; 22 23 29 public class TCPropertiesImpl implements TCProperties { 30 31 public static final String SYSTEM_PROP_PREFIX = "com.tc"; 32 33 private static final LogBuffer LOG_BUFFER = new LogBuffer(); 34 35 private static final String DEFAULT_TC_PROPERTIES_FILE = "tc.properties"; 37 38 private static final String TC_PROPERTIES_FILE = "tc.properties"; 40 41 private static final String TC_PROPERTIES_SYSTEM_PROP = "com.tc.properties"; 43 44 private static final TCPropertiesImpl INSTANCE; 45 46 private final Properties props = new Properties (); 47 48 static { 49 INSTANCE = new TCPropertiesImpl(); 50 } 51 52 private TCPropertiesImpl() { 53 super(); 54 55 loadDefaults(DEFAULT_TC_PROPERTIES_FILE); 56 String tcJarDir = getTCJarRootDirectory(); 57 if (tcJarDir != null) { 58 loadOverrides(tcJarDir, TC_PROPERTIES_FILE); 59 } 60 String tcPropFile = System.getProperty(TC_PROPERTIES_SYSTEM_PROP); 61 if(tcPropFile != null) { 62 loadOverrides(tcPropFile); 63 } 64 65 applySystemPropertyOverrides(); 66 } 67 68 private void applySystemPropertyOverrides() { 69 for (Iterator i = props.entrySet().iterator(); i.hasNext();) { 70 Map.Entry e = (Entry) i.next(); 71 String key = (String ) e.getKey(); 72 String sysPropOverride = System.getProperty(SYSTEM_PROP_PREFIX + "." + key); 73 if (sysPropOverride != null) { 74 e.setValue(sysPropOverride); 75 } 76 } 77 } 78 79 public Properties addAllPropertiesTo(Properties properties) { 80 return addAllPropertiesTo(properties, null); 81 } 82 83 Properties addAllPropertiesTo(Properties properties, String filter) { 84 if (filter == null) { 85 properties.putAll(props); 86 return properties; 87 } 88 for (Iterator i = props.entrySet().iterator(); i.hasNext();) { 89 Map.Entry e = (Entry) i.next(); 90 String key = (String ) e.getKey(); 91 if (key.startsWith(filter)) { 92 properties.put(key.substring(filter.length()), e.getValue()); 93 } 94 } 95 return properties; 96 } 97 98 private void loadOverrides(String propDir, String propFile) { 99 File file = new File (propDir, propFile); 100 loadOverrides(file); 101 } 102 103 private void loadOverrides(String propFile) { 104 File file = new File (propFile); 105 loadOverrides(file); 106 } 107 108 private void loadOverrides(File file) { 109 if (file.canRead()) { 110 try { 111 FileInputStream fin = new FileInputStream (file); 112 LOG_BUFFER.addLog("Loading override properties from : " + file); 113 props.load(fin); 114 } catch (FileNotFoundException e) { 115 LOG_BUFFER.addLog("Couldnt find " + file + ". Ignoring it", e); 116 } catch (IOException e) { 117 LOG_BUFFER.addLog("Couldnt read " + file + ". Ignoring it", e); 118 } 119 } 120 } 121 122 private String getTCJarRootDirectory() { 123 URL url = TCPropertiesImpl.class.getProtectionDomain().getCodeSource().getLocation(); 124 String path = url.getPath(); 125 if (!path.toLowerCase().endsWith(".jar")) { return null; } 126 File jarFile = new File (path); 127 String dir = jarFile.getParent(); 128 return dir; 129 } 130 131 private void loadDefaults(String propFile) { 132 InputStream in = TCPropertiesImpl.class.getResourceAsStream(propFile); 133 if (in == null) { throw new AssertionError ("TC Property file " + propFile + " not Found"); } 134 try { 135 LOG_BUFFER.addLog("Loading default properties from " + propFile); 136 props.load(in); 137 } catch (IOException e) { 138 throw new AssertionError (e); 139 } 140 } 141 142 public static TCProperties getProperties() { 143 return INSTANCE; 144 } 145 146 public TCProperties getPropertiesFor(String category) { 147 if (category == null) { throw new AssertionError ("Category cant be null"); } 148 return new TCSubProperties(INSTANCE, category); 149 } 150 151 public String getProperty(String key) { 152 return getProperty(key, false); 153 } 154 155 public String getProperty(String key, boolean missingOkay) { 156 LoggingWorkaround.doLog(); 157 String val = props.getProperty(key); 158 if (val == null && !missingOkay) { throw new AssertionError ("TCProperties : Property not found for " + key); } 159 return val; 160 } 161 162 public String toString() { 163 return "TCProperties=" + props.toString(); 164 } 165 166 public boolean getBoolean(String key) { 167 String val = getProperty(key); 168 return Boolean.valueOf(val).booleanValue(); 169 } 170 171 public int getInt(String key) { 172 String val = getProperty(key); 173 return Integer.valueOf(val).intValue(); 174 } 175 176 public long getLong(String key) { 177 String val = getProperty(key); 178 return Long.valueOf(val).longValue(); 179 } 180 181 public float getFloat(String key) { 182 String val = getProperty(key); 183 return Float.valueOf(val).floatValue(); 184 } 185 186 static class LogBuffer { 187 189 private final List logs = new ArrayList (); 190 191 void addLog(String msg) { 192 logs.add(new Entry(msg)); 193 } 194 195 void addLog(String msg, Throwable t) { 196 logs.add(new Entry(msg, t)); 197 } 198 199 void logTo(TCLogger logger) { 200 for (Iterator iter = logs.iterator(); iter.hasNext();) { 201 Entry e = (Entry) iter.next(); 202 if (e.t != null) { 203 logger.info(e.msg, e.t); 204 } else { 205 logger.info(e.msg); 206 } 207 } 208 logs.clear(); 209 } 210 211 static class Entry { 212 final String msg; 213 final Throwable t; 214 215 Entry(String msg) { 216 this(msg, null); 217 } 218 219 Entry(String msg, Throwable t) { 220 this.msg = msg; 221 this.t = t; 222 } 223 } 224 } 225 226 private static class LoggingWorkaround { 227 static { 228 TCLogger logger = TCLogging.getLogger(TCProperties.class); 229 LOG_BUFFER.logTo(logger); 230 logger.info("Loaded TCProperties : " + INSTANCE); 231 } 232 233 static void doLog() { 234 } 236 237 } 238 } 239 | Popular Tags |