KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > log > Log4JConfigurator


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

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 /**
24  * This is a configurator for log4j that supports variable substitution
25  *
26  * @since 2.1.6
27  * @deprecated This class is in the latest Excalibur version and therefore will be removed in 2.2.
28  * @version CVS $Id: Log4JConfigurator.java 30941 2004-07-29 19:56:58Z vgritsenko $
29  */

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

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

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