1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.helpers;
19
20 import java.util.Properties;
21 import java.net.URL;
22 import java.io.InterruptedIOException;
23
24 import org.apache.log4j.Level;
25 import org.apache.log4j.spi.Configurator;
26 import org.apache.log4j.spi.LoggerRepository;
27 import org.apache.log4j.PropertyConfigurator;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class OptionConverter {
41
42 static String DELIM_START = "${";
43 static char DELIM_STOP = '}';
44 static int DELIM_START_LEN = 2;
45 static int DELIM_STOP_LEN = 1;
46
47
48 private OptionConverter() {}
49
50 public
51 static
52 String[] concatanateArrays(String[] l, String[] r) {
53 int len = l.length + r.length;
54 String[] a = new String[len];
55
56 System.arraycopy(l, 0, a, 0, l.length);
57 System.arraycopy(r, 0, a, l.length, r.length);
58
59 return a;
60 }
61
62 public
63 static
64 String convertSpecialChars(String s) {
65 char c;
66 int len = s.length();
67 StringBuffer sbuf = new StringBuffer(len);
68
69 int i = 0;
70 while(i < len) {
71 c = s.charAt(i++);
72 if (c == '\\') {
73 c = s.charAt(i++);
74 if(c == 'n') c = '\n';
75 else if(c == 'r') c = '\r';
76 else if(c == 't') c = '\t';
77 else if(c == 'f') c = '\f';
78 else if(c == '\b') c = '\b';
79 else if(c == '\"') c = '\"';
80 else if(c == '\'') c = '\'';
81 else if(c == '\\') c = '\\';
82 }
83 sbuf.append(c);
84 }
85 return sbuf.toString();
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99 public
100 static
101 String getSystemProperty(String key, String def) {
102 try {
103 return System.getProperty(key, def);
104 } catch(Throwable e) {
105 LogLog.debug("Was not allowed to read system property \""+key+"\".");
106 return def;
107 }
108 }
109
110
111 public
112 static
113 Object instantiateByKey(Properties props, String key, Class superClass,
114 Object defaultValue) {
115
116
117 String className = findAndSubst(key, props);
118 if(className == null) {
119 LogLog.error("Could not find value for key " + key);
120 return defaultValue;
121 }
122
123 return OptionConverter.instantiateByClassName(className.trim(), superClass,
124 defaultValue);
125 }
126
127
128
129
130
131
132
133
134 public
135 static
136 boolean toBoolean(String value, boolean dEfault) {
137 if(value == null)
138 return dEfault;
139 String trimmedVal = value.trim();
140 if("true".equalsIgnoreCase(trimmedVal))
141 return true;
142 if("false".equalsIgnoreCase(trimmedVal))
143 return false;
144 return dEfault;
145 }
146
147 public
148 static
149 int toInt(String value, int dEfault) {
150 if(value != null) {
151 String s = value.trim();
152 try {
153 return Integer.valueOf(s).intValue();
154 }
155 catch (NumberFormatException e) {
156 LogLog.error("[" + s + "] is not in proper int form.");
157 e.printStackTrace();
158 }
159 }
160 return dEfault;
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 public
184 static
185 Level toLevel(String value, Level defaultValue) {
186 if(value == null)
187 return defaultValue;
188
189 value = value.trim();
190
191 int hashIndex = value.indexOf('#');
192 if (hashIndex == -1) {
193 if("NULL".equalsIgnoreCase(value)) {
194 return null;
195 } else {
196
197 return(Level) Level.toLevel(value, defaultValue);
198 }
199 }
200
201 Level result = defaultValue;
202
203 String clazz = value.substring(hashIndex+1);
204 String levelName = value.substring(0, hashIndex);
205
206
207 if("NULL".equalsIgnoreCase(levelName)) {
208 return null;
209 }
210
211 LogLog.debug("toLevel" + ":class=[" + clazz + "]"
212 + ":pri=[" + levelName + "]");
213
214 try {
215 Class customLevel = Loader.loadClass(clazz);
216
217
218
219 Class[] paramTypes = new Class[] { String.class,
220 org.apache.log4j.Level.class
221 };
222 java.lang.reflect.Method toLevelMethod =
223 customLevel.getMethod("toLevel", paramTypes);
224
225
226 Object[] params = new Object[] {levelName, defaultValue};
227 Object o = toLevelMethod.invoke(null, params);
228
229 result = (Level) o;
230 } catch(ClassNotFoundException e) {
231 LogLog.warn("custom level class [" + clazz + "] not found.");
232 } catch(NoSuchMethodException e) {
233 LogLog.warn("custom level class [" + clazz + "]"
234 + " does not have a class function toLevel(String, Level)", e);
235 } catch(java.lang.reflect.InvocationTargetException e) {
236 if (e.getTargetException() instanceof InterruptedException
237 || e.getTargetException() instanceof InterruptedIOException) {
238 Thread.currentThread().interrupt();
239 }
240 LogLog.warn("custom level class [" + clazz + "]"
241 + " could not be instantiated", e);
242 } catch(ClassCastException e) {
243 LogLog.warn("class [" + clazz
244 + "] is not a subclass of org.apache.log4j.Level", e);
245 } catch(IllegalAccessException e) {
246 LogLog.warn("class ["+clazz+
247 "] cannot be instantiated due to access restrictions", e);
248 } catch(RuntimeException e) {
249 LogLog.warn("class ["+clazz+"], level ["+levelName+
250 "] conversion failed.", e);
251 }
252 return result;
253 }
254
255 public
256 static
257 long toFileSize(String value, long dEfault) {
258 if(value == null)
259 return dEfault;
260
261 String s = value.trim().toUpperCase();
262 long multiplier = 1;
263 int index;
264
265 if((index = s.indexOf("KB")) != -1) {
266 multiplier = 1024;
267 s = s.substring(0, index);
268 }
269 else if((index = s.indexOf("MB")) != -1) {
270 multiplier = 1024*1024;
271 s = s.substring(0, index);
272 }
273 else if((index = s.indexOf("GB")) != -1) {
274 multiplier = 1024*1024*1024;
275 s = s.substring(0, index);
276 }
277 if(s != null) {
278 try {
279 return Long.valueOf(s).longValue() * multiplier;
280 }
281 catch (NumberFormatException e) {
282 LogLog.error("[" + s + "] is not in proper int form.");
283 LogLog.error("[" + value + "] not in expected format.", e);
284 }
285 }
286 return dEfault;
287 }
288
289
290
291
292
293
294
295 public
296 static
297 String findAndSubst(String key, Properties props) {
298 String value = props.getProperty(key);
299 if(value == null)
300 return null;
301
302 try {
303 return substVars(value, props);
304 } catch(IllegalArgumentException e) {
305 LogLog.error("Bad option value ["+value+"].", e);
306 return value;
307 }
308 }
309
310
311
312
313
314
315
316
317
318
319
320 public
321 static
322 Object instantiateByClassName(String className, Class superClass,
323 Object defaultValue) {
324 if(className != null) {
325 try {
326 Class classObj = Loader.loadClass(className);
327 if(!superClass.isAssignableFrom(classObj)) {
328 LogLog.error("A \""+className+"\" object is not assignable to a \""+
329 superClass.getName() + "\" variable.");
330 LogLog.error("The class \""+ superClass.getName()+"\" was loaded by ");
331 LogLog.error("["+superClass.getClassLoader()+"] whereas object of type ");
332 LogLog.error("\"" +classObj.getName()+"\" was loaded by ["
333 +classObj.getClassLoader()+"].");
334 return defaultValue;
335 }
336 return classObj.newInstance();
337 } catch (ClassNotFoundException e) {
338 LogLog.error("Could not instantiate class [" + className + "].", e);
339 } catch (IllegalAccessException e) {
340 LogLog.error("Could not instantiate class [" + className + "].", e);
341 } catch (InstantiationException e) {
342 LogLog.error("Could not instantiate class [" + className + "].", e);
343 } catch (RuntimeException e) {
344 LogLog.error("Could not instantiate class [" + className + "].", e);
345 }
346 }
347 return defaultValue;
348 }
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387 public static
388 String substVars(String val, Properties props) throws
389 IllegalArgumentException {
390
391 StringBuffer sbuf = new StringBuffer();
392
393 int i = 0;
394 int j, k;
395
396 while(true) {
397 j=val.indexOf(DELIM_START, i);
398 if(j == -1) {
399
400 if(i==0) {
401 return val;
402 } else {
403 sbuf.append(val.substring(i, val.length()));
404 return sbuf.toString();
405 }
406 } else {
407 sbuf.append(val.substring(i, j));
408 k = val.indexOf(DELIM_STOP, j);
409 if(k == -1) {
410 throw new IllegalArgumentException('"'+val+
411 "\" has no closing brace. Opening brace at position " + j
412 + '.');
413 } else {
414 j += DELIM_START_LEN;
415 String key = val.substring(j, k);
416
417 String replacement = getSystemProperty(key, null);
418
419 if(replacement == null && props != null) {
420 replacement = props.getProperty(key);
421 }
422
423 if(replacement != null) {
424
425
426
427
428
429 String recursiveReplacement = substVars(replacement, props);
430 sbuf.append(recursiveReplacement);
431 }
432 i = k + DELIM_STOP_LEN;
433 }
434 }
435 }
436 }
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460 static
461 public
462 void selectAndConfigure(URL url, String clazz, LoggerRepository hierarchy) {
463 Configurator configurator = null;
464 String filename = url.getFile();
465
466 if(clazz == null && filename != null && filename.endsWith(".xml")) {
467 clazz = "org.apache.log4j.xml.DOMConfigurator";
468 }
469
470 if(clazz != null) {
471 LogLog.debug("Preferred configurator class: " + clazz);
472 configurator = (Configurator) instantiateByClassName(clazz,
473 Configurator.class,
474 null);
475 if(configurator == null) {
476 LogLog.error("Could not instantiate configurator ["+clazz+"].");
477 return;
478 }
479 } else {
480 configurator = new PropertyConfigurator();
481 }
482
483 configurator.doConfigure(url, hierarchy);
484 }
485 }