1 17 package org.apache.avalon.excalibur.logger.log4j; 18 19 import org.apache.avalon.framework.context.Context; 20 import org.apache.avalon.framework.context.ContextException; 21 import org.apache.log4j.helpers.LogLog; 22 import org.apache.log4j.xml.DOMConfigurator; 23 24 29 public class Log4JConfigurator extends DOMConfigurator { 30 31 protected Context context; 32 33 public Log4JConfigurator(Context context) { 34 this.context = context; 35 } 36 37 protected String subst(String value) { 38 try { 39 return this.substVars(value); 40 } catch (IllegalArgumentException e) { 41 LogLog.warn("Could not perform variable substitution.", e); 42 43 return value; 44 } 45 } 46 47 static String DELIM_START = "${"; 48 static char DELIM_STOP = '}'; 49 static int DELIM_START_LEN = 2; 50 static int DELIM_STOP_LEN = 1; 51 52 56 public String substVars(String val) 57 throws IllegalArgumentException { 58 59 StringBuffer sbuf = new StringBuffer (); 60 61 int i = 0; 62 int j, k; 63 64 while(true) { 65 j=val.indexOf(DELIM_START, i); 66 if (j == -1) { 67 if(i==0) { return val; 70 } else { sbuf.append(val.substring(i, val.length())); 72 return sbuf.toString(); 73 } 74 } else { 75 sbuf.append(val.substring(i, j)); 76 k = val.indexOf(DELIM_STOP, j); 77 if(k == -1) { 78 throw new IllegalArgumentException ('"'+val+ 79 "\" has no closing brace. Opening brace at position " + j 80 + '.'); 81 } else { 82 j += DELIM_START_LEN; 83 String key = val.substring(j, k); 84 String replacement = this.getSystemProperty(key); 86 if (replacement == null && this.context != null) { 88 try { 89 Object o = this.context.get(key); 90 if ( o != null ) { 91 replacement = o.toString(); 92 } 93 } catch (ContextException ce) { 94 LogLog.debug("Was not allowed to read context property \""+key+"\"."); 95 } 96 } 97 98 if (replacement != null) { 99 String recursiveReplacement = substVars(replacement); 105 sbuf.append(recursiveReplacement); 106 } 107 i = k + DELIM_STOP_LEN; 108 } 109 } 110 } 111 } 112 113 117 public String getSystemProperty(String key) { 118 try { 119 return System.getProperty(key, null); 120 } catch(Throwable e) { LogLog.debug("Was not allowed to read system property \""+key+"\"."); 122 return null; 123 } 124 } 125 } 126 | Popular Tags |