KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > log4j > Log4JConfigurator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 /**
25  * This is a configurator for log4j that supports variable substitution
26  *
27  * @version CVS $Id: Log4JConfigurator.java,v 1.2 2004/06/14 14:07:09 cziegeler Exp $
28  */

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 JavaDoc subst(String JavaDoc value) {
38         try {
39           return this.substVars(value);
40         } catch (IllegalArgumentException JavaDoc e) {
41           LogLog.warn("Could not perform variable substitution.", e);
42
43           return value;
44         }
45     }
46     
47     static String JavaDoc DELIM_START = "${";
48     static char DELIM_STOP = '}';
49     static int DELIM_START_LEN = 2;
50     static int DELIM_STOP_LEN = 1;
51
52     /**
53      * This is directly copied from log4j's OptionConverter class.
54      * The only difference is the getting of a property.
55      */

56     public String JavaDoc substVars(String JavaDoc val)
57     throws IllegalArgumentException JavaDoc {
58
59         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
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                 // no more variables
68
if(i==0) { // this is a simple string
69
return val;
70                 } else { // add the tail string which contails no variables and return the result.
71
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 JavaDoc('"'+val+
79                              "\" has no closing brace. Opening brace at position " + j
80                              + '.');
81                 } else {
82                     j += DELIM_START_LEN;
83                     String JavaDoc key = val.substring(j, k);
84                     // first try in System properties
85
String JavaDoc replacement = this.getSystemProperty(key);
86                     // then try props parameter
87
if (replacement == null && this.context != null) {
88                         try {
89                             Object JavaDoc 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                         // Do variable substitution on the replacement string
100
// such that we can solve "Hello ${x2}" as "Hello p1"
101
// the where the properties are
102
// x1=p1
103
// x2=${x1}
104
String JavaDoc recursiveReplacement = substVars(replacement);
105                         sbuf.append(recursiveReplacement);
106                     }
107                     i = k + DELIM_STOP_LEN;
108                 }
109             }
110         }
111     }
112     
113     /**
114      * This is directly copied from log4j's OptionConverter class.
115      * The only difference is the getting of a property.
116      */

117     public String JavaDoc getSystemProperty(String JavaDoc key) {
118         try {
119             return System.getProperty(key, null);
120         } catch(Throwable JavaDoc e) { // MS-Java throws com.ms.security.SecurityExceptionEx
121
LogLog.debug("Was not allowed to read system property \""+key+"\".");
122             return null;
123         }
124     }
125 }
126
Popular Tags