001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.util.log;
018:
019: import org.apache.avalon.framework.context.Context;
020: import org.apache.avalon.framework.context.ContextException;
021: import org.apache.log4j.helpers.LogLog;
022: import org.apache.log4j.xml.DOMConfigurator;
023:
024: /**
025: * This is a configurator for log4j that supports variable substitution
026: *
027: * @since 2.1.6
028: * @deprecated This class is in the latest Excalibur version and therefore will be removed in 2.2.
029: * @version CVS $Id: Log4JConfigurator.java 30941 2004-07-29 19:56:58Z vgritsenko $
030: */
031: public class Log4JConfigurator extends DOMConfigurator {
032:
033: protected Context context;
034:
035: public Log4JConfigurator(Context context) {
036: this .context = context;
037: }
038:
039: protected String subst(String value) {
040: try {
041: return this .substVars(value);
042: } catch (IllegalArgumentException e) {
043: LogLog.warn("Could not perform variable substitution.", e);
044:
045: return value;
046: }
047: }
048:
049: static String DELIM_START = "${";
050: static char DELIM_STOP = '}';
051: static int DELIM_START_LEN = 2;
052: static int DELIM_STOP_LEN = 1;
053:
054: /**
055: * This is directly copied from log4j's OptionConverter class.
056: * The only difference is the getting of a property.
057: */
058: public String substVars(String val) throws IllegalArgumentException {
059:
060: StringBuffer sbuf = new StringBuffer();
061:
062: int i = 0;
063: int j, k;
064:
065: while (true) {
066: j = val.indexOf(DELIM_START, i);
067: if (j == -1) {
068: // no more variables
069: if (i == 0) { // this is a simple string
070: return val;
071: } else { // add the tail string which contails no variables and return the result.
072: sbuf.append(val.substring(i, val.length()));
073: return sbuf.toString();
074: }
075: } else {
076: sbuf.append(val.substring(i, j));
077: k = val.indexOf(DELIM_STOP, j);
078: if (k == -1) {
079: throw new IllegalArgumentException(
080: '"'
081: + val
082: + "\" has no closing brace. Opening brace at position "
083: + j + '.');
084: } else {
085: j += DELIM_START_LEN;
086: String key = val.substring(j, k);
087: // first try in System properties
088: String replacement = this .getSystemProperty(key);
089: // then try props parameter
090: if (replacement == null && this .context != null) {
091: try {
092: Object o = this .context.get(key);
093: if (o != null) {
094: replacement = o.toString();
095: }
096: } catch (ContextException ce) {
097: LogLog
098: .debug("Was not allowed to read context property \""
099: + key + "\".");
100: }
101: }
102:
103: if (replacement != null) {
104: // Do variable substitution on the replacement string
105: // such that we can solve "Hello ${x2}" as "Hello p1"
106: // the where the properties are
107: // x1=p1
108: // x2=${x1}
109: String recursiveReplacement = substVars(replacement);
110: sbuf.append(recursiveReplacement);
111: }
112: i = k + DELIM_STOP_LEN;
113: }
114: }
115: }
116: }
117:
118: /**
119: * This is directly copied from log4j's OptionConverter class.
120: * The only difference is the getting of a property.
121: */
122: public String getSystemProperty(String key) {
123: try {
124: return System.getProperty(key, null);
125: } catch (Throwable e) { // MS-Java throws com.ms.security.SecurityExceptionEx
126: LogLog.debug("Was not allowed to read system property \""
127: + key + "\".");
128: return null;
129: }
130: }
131: }
|