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 com.bostechcorp.cbesb.runtime.component.http.server;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.mortbay.log.Logger;
022:
023: /**
024: * A jetty6 logger implementation on top of commons-logging
025: *
026: * @author gnodet
027: */
028: public class JCLLogger implements Logger {
029:
030: private final String name;
031: private final Log log;
032:
033: public static void init() {
034: // TODO: use org.mortbay.log.Log#setLog when available (beta18)
035: String old = System.getProperty("org.mortbay.log.class");
036: try {
037: System.setProperty("org.mortbay.log.class", JCLLogger.class
038: .getName());
039: // For the class to be loaded by invoking a public static method
040: Class cl = Thread.currentThread().getContextClassLoader()
041: .loadClass("org.mortbay.log.Log");
042: cl.getMethod("isDebugEnabled", new Class[0]).invoke(null,
043: null);
044: } catch (Exception e) {
045: e.printStackTrace();
046: } finally {
047: if (old != null) {
048: System.setProperty("org.mortbay.log.class", old);
049: } else {
050: System.getProperties().remove("org.mortbay.log.class");
051: }
052: }
053: }
054:
055: public JCLLogger() {
056: this ("org.mortbay.jetty");
057: }
058:
059: public JCLLogger(String name) {
060: this .name = name;
061: this .log = LogFactory.getLog(name);
062: }
063:
064: public void debug(String msg, Throwable th) {
065: log.debug(msg, th);
066: }
067:
068: public void debug(String msg, Object arg0, Object arg1) {
069: if (log.isDebugEnabled()) {
070: log.debug(arrayFormat(msg, new Object[] { arg0, arg1 }));
071: }
072: }
073:
074: public Logger getLogger(String name) {
075: if (name == null) {
076: return null;
077: }
078: return new JCLLogger(this .name + "." + name);
079: }
080:
081: public void info(String msg, Object arg0, Object arg1) {
082: if (log.isInfoEnabled()) {
083: log.info(arrayFormat(msg, new Object[] { arg0, arg1 }));
084: }
085: }
086:
087: public boolean isDebugEnabled() {
088: return log.isDebugEnabled();
089: }
090:
091: public void setDebugEnabled(boolean enabled) {
092: log.warn("setDebugEnabled not supported");
093: }
094:
095: public void warn(String msg, Throwable th) {
096: log.warn(msg, th);
097: }
098:
099: public void warn(String msg, Object arg0, Object arg1) {
100: if (log.isWarnEnabled()) {
101: log.warn(arrayFormat(msg, new Object[] { arg0, arg1 }));
102: }
103: }
104:
105: static final char DELIM_START = '{';
106: static final char DELIM_STOP = '}';
107:
108: public static String arrayFormat(String messagePattern,
109: Object[] argArray) {
110: if (messagePattern == null) {
111: return null;
112: }
113: int i = 0;
114: int len = messagePattern.length();
115: int j = messagePattern.indexOf(DELIM_START);
116:
117: StringBuffer sbuf = new StringBuffer(
118: messagePattern.length() + 50);
119:
120: for (int L = 0; L < argArray.length; L++) {
121:
122: char escape = 'x';
123:
124: j = messagePattern.indexOf(DELIM_START, i);
125:
126: if (j == -1 || (j + 1 == len)) {
127: // no more variables
128: if (i == 0) { // this is a simple string
129: return messagePattern;
130: } else { // add the tail string which contains no variables
131: // and return the result.
132: sbuf.append(messagePattern.substring(i,
133: messagePattern.length()));
134: return sbuf.toString();
135: }
136: } else {
137: char delimStop = messagePattern.charAt(j + 1);
138: if (j > 0) {
139: escape = messagePattern.charAt(j - 1);
140: }
141:
142: if (escape == '\\') {
143: L--; // DELIM_START was escaped, thus should not be
144: // incremented
145: sbuf.append(messagePattern.substring(i, j - 1));
146: sbuf.append(DELIM_START);
147: i = j + 1;
148: } else if ((delimStop != DELIM_STOP)) {
149: // invalid DELIM_START/DELIM_STOP pair
150: sbuf.append(messagePattern.substring(i,
151: messagePattern.length()));
152: return sbuf.toString();
153: } else {
154: // normal case
155: sbuf.append(messagePattern.substring(i, j));
156: sbuf.append(argArray[L]);
157: i = j + 2;
158: }
159: }
160: }
161: // append the characters following the second {} pair.
162: sbuf.append(messagePattern
163: .substring(i, messagePattern.length()));
164: return sbuf.toString();
165: }
166: }
|