1 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 28 public class JCLLogger implements Logger { 29 30 private final String name; 31 private final Log log; 32 33 public static void init() { 34 String old = System.getProperty("org.mortbay.log.class"); 36 try { 37 System.setProperty("org.mortbay.log.class", JCLLogger.class.getName()); 38 Class cl = Thread.currentThread().getContextClassLoader().loadClass("org.mortbay.log.Log"); 40 cl.getMethod("isDebugEnabled", new Class [0]).invoke(null, null); 41 } catch (Exception 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 name) { 57 this.name = name; 58 this.log = LogFactory.getLog(name); 59 } 60 61 public void debug(String msg, Throwable th) { 62 log.debug(msg, th); 63 } 64 65 public void debug(String msg, Object arg0, Object arg1) { 66 if (log.isDebugEnabled()) { 67 log.debug(arrayFormat(msg, new Object [] {arg0, arg1})); 68 } 69 } 70 71 public Logger getLogger(String name) { 72 if (name == null) { 73 return null; 74 } 75 return new JCLLogger(this.name + "." + name); 76 } 77 78 public void info(String msg, Object arg0, Object arg1) { 79 if (log.isInfoEnabled()) { 80 log.info(arrayFormat(msg, new Object [] {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 msg, Throwable th) { 93 log.warn(msg, th); 94 } 95 96 public void warn(String msg, Object arg0, Object arg1) { 97 if (log.isWarnEnabled()) { 98 log.warn(arrayFormat(msg, new Object [] {arg0, arg1})); 99 } 100 } 101 102 static final char DELIM_START = '{'; 103 static final char DELIM_STOP = '}'; 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 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 if (i == 0) { return messagePattern; 125 } else { 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--; sbuf.append(messagePattern.substring(i, j - 1)); 140 sbuf.append(DELIM_START); 141 i = j + 1; 142 } else if ((delimStop != DELIM_STOP)) { 143 sbuf.append(messagePattern.substring(i, messagePattern.length())); 145 return sbuf.toString(); 146 } else { 147 sbuf.append(messagePattern.substring(i, j)); 149 sbuf.append(argArray[L]); 150 i = j + 2; 151 } 152 } 153 } 154 sbuf.append(messagePattern.substring(i, messagePattern.length())); 156 return sbuf.toString(); 157 } 158 } 159 | Popular Tags |