1 package org.e2etrace.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Enumeration;
23 import java.util.HashSet;
24 import java.util.Properties;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.e2etrace.trace.ITraceStepId;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public class PropertiesTraceConfig implements ITraceConfig {
61
62 private static final Log log = LogFactory.getLog(PropertiesTraceConfig.class);
63
64 private static final String KEY_ENABLETRACE = "global.enabletrace";
65 private static final String KEY_RELOADINTERVAL = "global.reloadinterval";
66 private static final String KEY_IDPREFIX = "id.";
67
68 private String fileName;
69 private boolean enabled;
70 private boolean configIsDirty;
71 private HashSet disabledForId;
72 private long reloadInterval;
73 private long lastReload;
74
75
76
77
78
79
80 public PropertiesTraceConfig() {
81
82 }
83
84
85 public boolean isTraceEnabled() {
86 reloadifIntervalExpired();
87
88 return this.enabled;
89 }
90
91
92 public boolean isTraceEnabledForId(ITraceStepId id) {
93 boolean ret = true;
94
95 reloadifIntervalExpired();
96
97 if (this.disabledForId.contains(id.asString())) {
98 ret = false;
99 }
100
101 return ret;
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public void loadConfigFile(String fileName) throws IOException {
128 this.fileName = fileName;
129 this.configIsDirty = true;
130
131 reloadConfiguration();
132
133 }
134
135
136
137
138
139
140 private void reloadifIntervalExpired() {
141 if (this.reloadInterval > 0) {
142 if (((System.currentTimeMillis() / 1000) - this.lastReload) >= this.reloadInterval) {
143 try {
144
145
146 this.configIsDirty = true;
147
148 reloadConfiguration();
149
150 } catch (IOException e) {
151 log
152 .error(
153 "Cannot reload configuration file. Will use the already loaded values instead.",
154 e);
155 }
156 }
157 }
158 }
159
160
161
162
163
164
165
166 private synchronized void reloadConfiguration() throws IOException {
167
168
169 if (!this.configIsDirty) {
170 return;
171 }
172
173 Properties loadedConfig;
174 loadedConfig = locateAndLoadProperties();
175
176 this.enabled = getPropertyAsBoolean(loadedConfig,
177 PropertiesTraceConfig.KEY_ENABLETRACE, true);
178 this.reloadInterval = getPropertyAsLong(loadedConfig,
179 PropertiesTraceConfig.KEY_RELOADINTERVAL, 0);
180
181
182 Enumeration e = loadedConfig.keys();
183 String key;
184 String id;
185 boolean idEnabled;
186
187 this.disabledForId = new HashSet();
188
189 while (e.hasMoreElements()) {
190 key = (String) e.nextElement();
191
192 if (key.startsWith(KEY_IDPREFIX)) {
193 id = key.substring(key.indexOf(".") + 1);
194 idEnabled = getPropertyAsBoolean(loadedConfig, key, true);
195
196 if (!idEnabled) {
197 this.disabledForId.add(id);
198 }
199 }
200 }
201
202
203 this.lastReload = System.currentTimeMillis() / 1000;
204
205
206 this.configIsDirty = false;
207
208 if (log.isInfoEnabled()) {
209 log.info("Loaded configuration file " + this.fileName);
210 log.info("Tracing is now " + (this.enabled ? "enabled" : "disabled"));
211 log.info("Reload interval is now " + this.reloadInterval + " seconds");
212 log.info("Tracing has been disabled for " + this.disabledForId.size() + " ids");
213 }
214
215 }
216
217
218
219
220
221
222
223
224
225 private Properties locateAndLoadProperties() throws IOException {
226 InputStream is = null;
227 Properties loadedConfig = new Properties();
228
229 try {
230
231 is = PropertiesTraceConfig.class.getResourceAsStream(this.fileName);
232
233 if (is == null) {
234
235
236 ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
237 is = ctxLoader.getResourceAsStream(this.fileName);
238
239 if (is == null) {
240
241 is = new FileInputStream(this.fileName);
242 }
243 }
244
245 if (is != null) {
246 loadedConfig.load(is);
247 } else {
248 throw new IOException("Cannot find or open file " + this.fileName);
249 }
250 } finally {
251 if (is != null) {
252 is.close();
253 }
254 }
255
256 return loadedConfig;
257 }
258
259
260
261
262
263
264
265
266
267
268
269 private boolean getPropertyAsBoolean(Properties loadedConfig, String key, boolean def) {
270 boolean ret = def;
271 String retValue;
272
273 retValue = loadedConfig.getProperty(key);
274
275 if (retValue != null) {
276 ret = new Boolean(retValue).booleanValue();
277
278 }
279
280 return ret;
281
282 }
283
284
285
286
287
288
289
290
291
292
293
294 private long getPropertyAsLong(Properties loadedConfig, String key, long def) {
295 long ret = def;
296 String retValue;
297
298 retValue = loadedConfig.getProperty(key);
299
300 if (retValue != null) {
301 ret = new Long(retValue).longValue();
302
303 }
304
305 return ret;
306
307 }
308
309 }