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