KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > jetty > JCLLogger


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.servicemix.http.jetty;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.mortbay.log.Logger;
22
23 /**
24  * A jetty6 logger implementation on top of commons-logging
25  *
26  * @author gnodet
27  */

28 public class JCLLogger implements Logger {
29
30     private final String JavaDoc name;
31     private final Log log;
32     
33     public static void init() {
34         // TODO: use org.mortbay.log.Log#setLog when available (beta18)
35
String JavaDoc old = System.getProperty("org.mortbay.log.class");
36         try {
37             System.setProperty("org.mortbay.log.class", JCLLogger.class.getName());
38             // For the class to be loaded by invoking a public static method
39
Class JavaDoc cl = Thread.currentThread().getContextClassLoader().loadClass("org.mortbay.log.Log");
40             cl.getMethod("isDebugEnabled", new Class JavaDoc[0]).invoke(null, null);
41         } catch (Exception JavaDoc e) {
42             e.printStackTrace();
43         } finally {
44             if (old != null) {
45                 System.setProperty("org.mortbay.log.class", old);
46             } else {
47                 System.getProperties().remove("org.mortbay.log.class");
48             }
49         }
50     }
51     
52     public JCLLogger() {
53         this("org.mortbay.jetty");
54     }
55     
56     public JCLLogger(String JavaDoc name) {
57         this.name = name;
58         this.log = LogFactory.getLog(name);
59     }
60     
61     public void debug(String JavaDoc msg, Throwable JavaDoc th) {
62         log.debug(msg, th);
63     }
64
65     public void debug(String JavaDoc msg, Object JavaDoc arg0, Object JavaDoc arg1) {
66         if (log.isDebugEnabled()) {
67             log.debug(arrayFormat(msg, new Object JavaDoc[] {arg0, arg1}));
68         }
69     }
70
71     public Logger getLogger(String JavaDoc name) {
72         if (name == null) {
73             return null;
74         }
75         return new JCLLogger(this.name + "." + name);
76     }
77
78     public void info(String JavaDoc msg, Object JavaDoc arg0, Object JavaDoc arg1) {
79         if (log.isInfoEnabled()) {
80             log.info(arrayFormat(msg, new Object JavaDoc[] {arg0, arg1}));
81         }
82     }
83
84     public boolean isDebugEnabled() {
85         return log.isDebugEnabled();
86     }
87
88     public void setDebugEnabled(boolean enabled) {
89         log.warn("setDebugEnabled not supported");
90     }
91
92     public void warn(String JavaDoc msg, Throwable JavaDoc th) {
93         log.warn(msg, th);
94     }
95
96     public void warn(String JavaDoc msg, Object JavaDoc arg0, Object JavaDoc arg1) {
97         if (log.isWarnEnabled()) {
98             log.warn(arrayFormat(msg, new Object JavaDoc[] {arg0, arg1}));
99         }
100     }
101     
102     static final char DELIM_START = '{';
103     static final char DELIM_STOP = '}';
104     
105     public static String JavaDoc arrayFormat(String JavaDoc messagePattern, Object JavaDoc[] 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 JavaDoc sbuf = new StringBuffer JavaDoc(messagePattern.length() + 50);
114
115         for (int L = 0; L < argArray.length; L++) {
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                     L--; // 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[L]);
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 }
159
Popular Tags