1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
package org.webmacro.util; |
25 | |
|
26 | |
import org.webmacro.InitException; |
27 | |
|
28 | |
import java.io.IOException; |
29 | |
import java.io.InputStream; |
30 | |
import java.net.MalformedURLException; |
31 | |
import java.net.URL; |
32 | |
import java.util.Enumeration; |
33 | |
import java.util.Properties; |
34 | |
import java.util.StringTokenizer; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class Settings |
40 | |
{ |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 567 | abstract public static class ListSettingHandler |
46 | |
{ |
47 | |
|
48 | |
abstract public void processSetting (String settingKey, |
49 | |
String settingValue); |
50 | |
} |
51 | |
|
52 | 41 | protected static final String[] stringArray = new String[0]; |
53 | |
|
54 | |
private Properties _props; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public Settings () |
60 | 992 | { |
61 | 992 | _props = new Properties(); |
62 | 992 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public Settings (String settingsFile) |
69 | |
throws InitException, IOException |
70 | |
{ |
71 | 40 | this(); |
72 | 40 | load(settingsFile); |
73 | 40 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public Settings (URL settingsFile) |
80 | |
throws InitException, IOException |
81 | |
{ |
82 | 0 | this(); |
83 | 0 | load(settingsFile); |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
protected Settings (Properties values) |
92 | |
{ |
93 | 0 | this(); |
94 | 0 | load(values); |
95 | 0 | } |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public Settings (Settings defaults) |
102 | |
{ |
103 | 0 | this(); |
104 | 0 | load(defaults); |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public void load (String fileName) throws InitException, IOException |
113 | |
{ |
114 | 44 | ClassLoader cl = this.getClass().getClassLoader(); |
115 | 44 | URL u = (cl == null) ? null : cl.getResource(fileName); |
116 | 44 | if (u == null) |
117 | 0 | u = ClassLoader.getSystemResource(fileName); |
118 | 44 | if (u == null) |
119 | |
try |
120 | |
{ |
121 | 0 | u = new URL("file:" + fileName); |
122 | |
} |
123 | 0 | catch (MalformedURLException e) |
124 | |
{ |
125 | |
|
126 | 0 | } |
127 | |
; |
128 | |
|
129 | 44 | if (u == null) |
130 | |
{ |
131 | 0 | StringBuffer error = new StringBuffer(); |
132 | 0 | error.append("Unable to load the properties file: "); |
133 | 0 | error.append(fileName); |
134 | 0 | error.append(" via the class path\n"); |
135 | 0 | error.append("This indicates a configuration error."); |
136 | 0 | throw new InitException(error.toString()); |
137 | |
} |
138 | 44 | load(u); |
139 | 44 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public void load (URL u) throws IOException |
145 | |
{ |
146 | 55 | InputStream in = u.openStream(); |
147 | 55 | _props.load(in); |
148 | 55 | in.close(); |
149 | 55 | } |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public void load (InputStream in) throws IOException |
155 | |
{ |
156 | 0 | _props.load(in); |
157 | 0 | } |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public void load (Settings defaults) |
163 | |
{ |
164 | 63 | String keys[] = defaults.getKeys(); |
165 | 6363 | for (int i = 0; i < keys.length; i++) |
166 | |
{ |
167 | 6300 | _props.setProperty(keys[i], defaults.getSetting(keys[i])); |
168 | |
} |
169 | 63 | } |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public void load (Properties props, String prefix) |
176 | |
{ |
177 | 73 | Enumeration e = props.propertyNames(); |
178 | 73 | String dotPrefix = (prefix == null) ? "" : prefix + "."; |
179 | 3360 | while (e.hasMoreElements()) |
180 | |
{ |
181 | 3287 | String key = (String) e.nextElement(); |
182 | 3287 | if (prefix == null) |
183 | 9 | setProperty(_props,key, props.getProperty(key)); |
184 | 3278 | else if (key.startsWith(dotPrefix)) { |
185 | 65 | setProperty(_props,key.substring(dotPrefix.length()), |
186 | |
props.getProperty(key)); |
187 | |
} |
188 | 3287 | } |
189 | 73 | } |
190 | |
|
191 | |
private static void setProperty(Properties props, String key, String value) { |
192 | |
|
193 | 74 | props.setProperty(key, value); |
194 | 74 | } |
195 | |
|
196 | |
|
197 | |
|
198 | |
public void load (Properties props) |
199 | |
{ |
200 | 6 | load(props, null); |
201 | 6 | } |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
public boolean containsKey (String key) |
207 | |
{ |
208 | 2890 | return _props.containsKey(key); |
209 | |
} |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
public String getSetting (String key) |
217 | |
{ |
218 | 15288 | String prop = _props.getProperty(key); |
219 | 15288 | if (prop != null) |
220 | 13567 | prop = prop.trim(); |
221 | 15288 | if (prop != null |
222 | |
&& prop.length() > 2 |
223 | |
&& prop.charAt(0) == '"' |
224 | |
&& prop.charAt(prop.length() - 1) == '"') |
225 | 4 | prop = prop.substring(1, prop.length() - 1); |
226 | |
|
227 | 15288 | return prop; |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
public String getSetting (String key, String defaultValue) |
234 | |
{ |
235 | 1394 | String ret = getSetting(key); |
236 | 1394 | return (ret != null) ? ret : defaultValue; |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public int getIntegerSetting (String key) |
243 | |
{ |
244 | 4 | return getIntegerSetting(key, 0); |
245 | |
} |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
public int getIntegerSetting (String key, int defaultValue) |
251 | |
{ |
252 | 2386 | if (containsKey(key)) |
253 | |
{ |
254 | |
try |
255 | |
{ |
256 | 1881 | return Integer.parseInt(getSetting(key)); |
257 | |
} |
258 | 0 | catch (Exception e) |
259 | |
{ |
260 | 0 | return defaultValue; |
261 | |
} |
262 | |
} |
263 | |
else |
264 | |
{ |
265 | 505 | return defaultValue; |
266 | |
} |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
public boolean getBooleanSetting (String key) |
275 | |
{ |
276 | 260 | String setting = getSetting(key); |
277 | 260 | return ((setting != null) && |
278 | |
((setting.equalsIgnoreCase("on")) |
279 | |
|| (setting.equalsIgnoreCase("true")) |
280 | |
|| (setting.equalsIgnoreCase("yes")))); |
281 | |
} |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public boolean getBooleanSetting (String key, boolean defaultValue) |
287 | |
{ |
288 | 0 | if (containsKey(key)) |
289 | |
{ |
290 | 0 | return getBooleanSetting(key); |
291 | |
} |
292 | |
else |
293 | |
{ |
294 | 0 | return defaultValue; |
295 | |
} |
296 | |
} |
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
public String[] getKeys () |
302 | |
{ |
303 | 696 | return (String[]) _props.keySet().toArray(stringArray); |
304 | |
} |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
public Properties getAsProperties () |
310 | |
{ |
311 | 2 | String[] keys = getKeys(); |
312 | 2 | Properties p = new Properties(); |
313 | |
|
314 | 202 | for (int i = 0; i < keys.length; i++) |
315 | 200 | p.setProperty(keys[i], getSetting(keys[i])); |
316 | |
|
317 | 2 | return p; |
318 | |
} |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
public void processListSetting (String settingName, ListSettingHandler h) |
333 | |
{ |
334 | |
|
335 | 567 | String listNames = getSetting(settingName); |
336 | 567 | if (listNames != null) |
337 | |
{ |
338 | 316 | Enumeration denum = new StringTokenizer(listNames); |
339 | 317 | while (denum.hasMoreElements()) |
340 | 1 | h.processSetting("", ((String) denum.nextElement())); |
341 | |
} |
342 | |
|
343 | 567 | Settings s = new SubSettings(this, settingName); |
344 | 567 | String[] keys = s.getKeys(); |
345 | 3151 | for (int i = 0; i < keys.length; i++) |
346 | |
{ |
347 | 2584 | String value = s.getSetting(keys[i]).trim(); |
348 | 2584 | if (value.equals("")) |
349 | 0 | continue; |
350 | 2584 | h.processSetting(keys[i], value); |
351 | |
} |
352 | 567 | } |
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
public static void main (String arg[]) throws Exception |
359 | |
{ |
360 | |
|
361 | 0 | Settings s = new Settings(); |
362 | 0 | s.load("Test.properties"); |
363 | |
|
364 | 0 | Settings sb = new SubSettings(s, "b"); |
365 | 0 | String[] keys = sb.getKeys(); |
366 | 0 | for (int i = 0; i < keys.length; i++) |
367 | |
{ |
368 | 0 | System.out.println("sub-prop " + keys[i] + " = " + sb.getSetting(keys[i])); |
369 | |
} |
370 | |
|
371 | 0 | Settings ssb = new SubSettings(sb, "b"); |
372 | 0 | keys = ssb.getKeys(); |
373 | 0 | for (int i = 0; i < keys.length; i++) |
374 | |
{ |
375 | 0 | System.out.println("sub-sub-prop " + keys[i] + " = " + ssb.getSetting(keys[i])); |
376 | |
} |
377 | |
|
378 | 0 | System.out.println("LogTraceExceptions is: " + s.getBooleanSetting("LogTraceExceptions")); |
379 | |
|
380 | 0 | } |
381 | |
} |
382 | |
|