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