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.servicemix.http.jetty;
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 static final char DELIM_START = '{';
031 static final char DELIM_STOP = '}';
032
033 private final String name;
034 private final Log log;
035
036 public JCLLogger() {
037 this("org.mortbay.jetty");
038 }
039
040 public JCLLogger(String name) {
041 this.name = name;
042 this.log = LogFactory.getLog(name);
043 }
044
045 public static void init() {
046 // TODO: use org.mortbay.log.Log#setLog when available (beta18)
047 String old = System.getProperty("org.mortbay.log.class");
048 try {
049 System.setProperty("org.mortbay.log.class", JCLLogger.class.getName());
050 // For the class to be loaded by invoking a public static method
051 Class<?> cl = Thread.currentThread().getContextClassLoader().loadClass("org.mortbay.log.Log");
052 cl.getMethod("isDebugEnabled", new Class[0]).invoke(null);
053 } catch (Exception e) {
054 e.printStackTrace();
055 } finally {
056 if (old != null) {
057 System.setProperty("org.mortbay.log.class", old);
058 } else {
059 System.getProperties().remove("org.mortbay.log.class");
060 }
061 }
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 loggerName) {
075 if (loggerName == null) {
076 return null;
077 }
078 return new JCLLogger(this.name + "." + loggerName);
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 public static String arrayFormat(String messagePattern, Object[] argArray) {
106 if (messagePattern == null) {
107 return null;
108 }
109 int i = 0;
110 int len = messagePattern.length();
111 int j = messagePattern.indexOf(DELIM_START);
112
113 StringBuffer sbuf = new StringBuffer(messagePattern.length() + 50);
114
115 for (int index = 0; index < argArray.length; index++) {
116
117 char escape = 'x';
118
119 j = messagePattern.indexOf(DELIM_START, i);
120
121 if (j == -1 || (j + 1 == len)) {
122 // no more variables
123 if (i == 0) { // this is a simple string
124 return messagePattern;
125 } else { // add the tail string which contains no variables
126 // and return the result.
127 sbuf.append(messagePattern.substring(i, messagePattern.length()));
128 return sbuf.toString();
129 }
130 } else {
131 char delimStop = messagePattern.charAt(j + 1);
132 if (j > 0) {
133 escape = messagePattern.charAt(j - 1);
134 }
135
136 if (escape == '\\') {
137 index--; // DELIM_START was escaped, thus should not be
138 // incremented
139 sbuf.append(messagePattern.substring(i, j - 1));
140 sbuf.append(DELIM_START);
141 i = j + 1;
142 } else if (delimStop != DELIM_STOP) {
143 // invalid DELIM_START/DELIM_STOP pair
144 sbuf.append(messagePattern.substring(i, messagePattern.length()));
145 return sbuf.toString();
146 } else {
147 // normal case
148 sbuf.append(messagePattern.substring(i, j));
149 sbuf.append(argArray[index]);
150 i = j + 2;
151 }
152 }
153 }
154 // append the characters following the second {} pair.
155 sbuf.append(messagePattern.substring(i, messagePattern.length()));
156 return sbuf.toString();
157 }
158 }