KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > properties > TCPropertiesImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.properties;
6
7 import com.tc.logging.TCLogger;
8 import com.tc.logging.TCLogging;
9
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.FileNotFoundException JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21 import java.util.Map.Entry;
22
23 /**
24  * This class is an easy way to read properties that will help tune DSO. It first loads properties from the
25  * tc.properties file in the same package as this file and these properties can be overloaded by tc.properites in the
26  * base directory where tc.jar is present. TODO:: Improve tcbuild to aggregate properties from different directories
27  * during build time.
28  */

29 public class TCPropertiesImpl implements TCProperties {
30
31   public static final String JavaDoc SYSTEM_PROP_PREFIX = "com.tc";
32
33   private static final LogBuffer LOG_BUFFER = new LogBuffer();
34
35   // This file resides in src.resource/com/tc/properties directory
36
private static final String JavaDoc DEFAULT_TC_PROPERTIES_FILE = "tc.properties";
37
38   // This file,if present, overrides the default properties and resides in the same directory as tc.jar
39
private static final String JavaDoc TC_PROPERTIES_FILE = "tc.properties";
40   
41   // This is the system property that can be set to point to a tc.properties file
42
private static final String JavaDoc TC_PROPERTIES_SYSTEM_PROP = "com.tc.properties";
43
44   private static final TCPropertiesImpl INSTANCE;
45
46   private final Properties JavaDoc props = new Properties JavaDoc();
47
48   static {
49     INSTANCE = new TCPropertiesImpl();
50   }
51
52   private TCPropertiesImpl() {
53     super();
54
55     loadDefaults(DEFAULT_TC_PROPERTIES_FILE);
56     String JavaDoc tcJarDir = getTCJarRootDirectory();
57     if (tcJarDir != null) {
58       loadOverrides(tcJarDir, TC_PROPERTIES_FILE);
59     }
60     String JavaDoc 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 JavaDoc i = props.entrySet().iterator(); i.hasNext();) {
70       Map.Entry JavaDoc e = (Entry) i.next();
71       String JavaDoc key = (String JavaDoc) e.getKey();
72       String JavaDoc sysPropOverride = System.getProperty(SYSTEM_PROP_PREFIX + "." + key);
73       if (sysPropOverride != null) {
74         e.setValue(sysPropOverride);
75       }
76     }
77   }
78
79   public Properties JavaDoc addAllPropertiesTo(Properties JavaDoc properties) {
80     return addAllPropertiesTo(properties, null);
81   }
82
83   Properties JavaDoc addAllPropertiesTo(Properties JavaDoc properties, String JavaDoc filter) {
84     if (filter == null) {
85       properties.putAll(props);
86       return properties;
87     }
88     for (Iterator JavaDoc i = props.entrySet().iterator(); i.hasNext();) {
89       Map.Entry JavaDoc e = (Entry) i.next();
90       String JavaDoc key = (String JavaDoc) 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 JavaDoc propDir, String JavaDoc propFile) {
99     File JavaDoc file = new File JavaDoc(propDir, propFile);
100     loadOverrides(file);
101   }
102   
103   private void loadOverrides(String JavaDoc propFile) {
104     File JavaDoc file = new File JavaDoc(propFile);
105     loadOverrides(file);
106   }
107   
108   private void loadOverrides(File JavaDoc file) {
109     if (file.canRead()) {
110       try {
111         FileInputStream JavaDoc fin = new FileInputStream JavaDoc(file);
112         LOG_BUFFER.addLog("Loading override properties from : " + file);
113         props.load(fin);
114       } catch (FileNotFoundException JavaDoc e) {
115         LOG_BUFFER.addLog("Couldnt find " + file + ". Ignoring it", e);
116       } catch (IOException JavaDoc e) {
117         LOG_BUFFER.addLog("Couldnt read " + file + ". Ignoring it", e);
118       }
119     }
120   }
121
122   private String JavaDoc getTCJarRootDirectory() {
123     URL JavaDoc url = TCPropertiesImpl.class.getProtectionDomain().getCodeSource().getLocation();
124     String JavaDoc path = url.getPath();
125     if (!path.toLowerCase().endsWith(".jar")) { return null; }
126     File JavaDoc jarFile = new File JavaDoc(path);
127     String JavaDoc dir = jarFile.getParent();
128     return dir;
129   }
130
131   private void loadDefaults(String JavaDoc propFile) {
132     InputStream JavaDoc in = TCPropertiesImpl.class.getResourceAsStream(propFile);
133     if (in == null) { throw new AssertionError JavaDoc("TC Property file " + propFile + " not Found"); }
134     try {
135       LOG_BUFFER.addLog("Loading default properties from " + propFile);
136       props.load(in);
137     } catch (IOException JavaDoc e) {
138       throw new AssertionError JavaDoc(e);
139     }
140   }
141
142   public static TCProperties getProperties() {
143     return INSTANCE;
144   }
145
146   public TCProperties getPropertiesFor(String JavaDoc category) {
147     if (category == null) { throw new AssertionError JavaDoc("Category cant be null"); }
148     return new TCSubProperties(INSTANCE, category);
149   }
150
151   public String JavaDoc getProperty(String JavaDoc key) {
152     return getProperty(key, false);
153   }
154
155   public String JavaDoc getProperty(String JavaDoc key, boolean missingOkay) {
156     LoggingWorkaround.doLog();
157     String JavaDoc val = props.getProperty(key);
158     if (val == null && !missingOkay) { throw new AssertionError JavaDoc("TCProperties : Property not found for " + key); }
159     return val;
160   }
161
162   public String JavaDoc toString() {
163     return "TCProperties=" + props.toString();
164   }
165
166   public boolean getBoolean(String JavaDoc key) {
167     String JavaDoc val = getProperty(key);
168     return Boolean.valueOf(val).booleanValue();
169   }
170
171   public int getInt(String JavaDoc key) {
172     String JavaDoc val = getProperty(key);
173     return Integer.valueOf(val).intValue();
174   }
175
176   public long getLong(String JavaDoc key) {
177     String JavaDoc val = getProperty(key);
178     return Long.valueOf(val).longValue();
179   }
180
181   public float getFloat(String JavaDoc key) {
182     String JavaDoc val = getProperty(key);
183     return Float.valueOf(val).floatValue();
184   }
185
186   static class LogBuffer {
187     // This class could be made fancier if it needs to log message at different levels (ie. INFO vs ERROR, etc)
188

189     private final List JavaDoc logs = new ArrayList JavaDoc();
190
191     void addLog(String JavaDoc msg) {
192       logs.add(new Entry(msg));
193     }
194
195     void addLog(String JavaDoc msg, Throwable JavaDoc t) {
196       logs.add(new Entry(msg, t));
197     }
198
199     void logTo(TCLogger logger) {
200       for (Iterator JavaDoc 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 JavaDoc msg;
213       final Throwable JavaDoc t;
214
215       Entry(String JavaDoc msg) {
216         this(msg, null);
217       }
218
219       Entry(String JavaDoc msg, Throwable JavaDoc 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       // the only reason this method is here is to trigger the static initilizer of this inner class one (and only once)
235
}
236
237   }
238 }
239
Popular Tags