1 23 24 package com.sun.enterprise.server.logging; 25 import com.sun.enterprise.J2EESecurityManager; 26 27 import java.util.logging.Logger ; 28 import java.util.logging.Level ; 29 import java.util.logging.Handler ; 30 import java.util.logging.Filter ; 31 import java.util.logging.ConsoleHandler ; 32 import java.util.logging.ErrorManager ; 33 import java.util.logging.LogManager ; 34 35 import java.io.*; 36 import java.util.*; 37 38 import com.sun.logging.LogDomains; 39 40 41 49 public class SystemOutandErrHandler { 50 51 private static final String SYSTEMERR_LOGGER = "javax.enterprise.system.stream.err"; 52 private static final String SYSTEMOUT_LOGGER = "javax.enterprise.system.stream.out"; 53 private static PrintStream originalSystemErr; 54 private static Logger soLogger = null; 55 private static Logger seLogger = null; 56 private static Level errLogLevel = Level.WARNING; 57 private static Level outLogLevel = Level.INFO; 58 private static String lineSeparator; 59 private static int lineSeparatorSize; 60 private LoggingPrintStream lout, lerr; 61 62 public SystemOutandErrHandler() { 63 if (originalSystemErr != null) { 64 RuntimeException e = new RuntimeException ("recursivecall"); 65 originalSystemErr.println("recursive call into SystemOutandErrhandler"); 66 e.printStackTrace(originalSystemErr); 67 return; 68 } 69 70 LoggingByteArrayOutputStream buf = new LoggingByteArrayOutputStream(); 71 seLogger = java.util.logging.Logger.getLogger(SYSTEMERR_LOGGER); 73 buf.setLogger(seLogger, errLogLevel); 75 originalSystemErr = System.err; 76 lerr = (new LoggingPrintStream(buf)); 77 lerr.setLogger(seLogger); 78 System.setErr(lerr); 79 80 buf = new LoggingByteArrayOutputStream(); 81 soLogger = java.util.logging.Logger.getLogger(SYSTEMOUT_LOGGER); 83 buf.setLogger(soLogger, outLogLevel); 84 lout = (new LoggingPrintStream(buf)); 85 lout.setLogger(soLogger); 86 System.setOut(lout); 87 88 lineSeparator = (String ) java.security.AccessController.doPrivileged( 89 new sun.security.action.GetPropertyAction("line.separator")); 90 lineSeparatorSize = lineSeparator.length(); 91 92 } 93 94 117 118 private class LoggingPrintStream extends PrintStream { 119 private ByteArrayOutputStream bufOut; 120 private Logger logger=null; 121 LogManager logManager=null; 122 private ThreadLocal perThreadStObjects = new ThreadLocal (); 123 124 125 public LoggingPrintStream(LoggingByteArrayOutputStream buf) { 126 super(buf, true); 128 bufOut = buf; 129 logManager = LogManager.getLogManager( ); 130 } 131 132 public void setLogger(Logger l) { 133 logger=l; 134 } 135 136 public void println(Object x) { 137 if (!checkLocks()) return; 138 139 StackTraceObjects sTO; 140 141 if ( (sTO = (StackTraceObjects) perThreadStObjects.get()) != null ) { 142 153 perThreadStObjects.set(null); 154 } 155 156 if ( !(x instanceof java.lang.Throwable ) ) { 157 super.println(x); 159 return; 160 } 161 162 sTO = new StackTraceObjects((Throwable )x); 164 perThreadStObjects.set(sTO); 165 super.println(sTO.toString()); 166 } 167 public void println(String str) { 168 if (!checkLocks()) return; 169 170 StackTraceObjects sTO; 171 sTO = (StackTraceObjects) perThreadStObjects.get(); 172 if ( sTO == null ) { 173 super.println(str); 175 return; 176 } 177 178 if ( !sTO.ignorePrintln(str) ) { 179 perThreadStObjects.set(null); 180 super.println(str); 181 return; 182 } 183 184 if (sTO.checkCompletion()) { 185 perThreadStObjects.set(null); 186 return; 187 } 188 } 189 190 public void print(String x) { 191 if (checkLocks()) 192 super.print(x); 193 } 194 195 196 public void print(Object x) { 197 if (checkLocks()) 198 super.print(x); 199 } 200 201 public void print(boolean x) { 202 if (checkLocks()) { 203 super.print(x); 204 } 205 } 206 public void println(boolean x) { 207 if (checkLocks()) 208 super.println(x); 209 } 210 211 public void print(char x) { 212 if (checkLocks()) { 213 super.print(x); 214 } 215 } 216 public void println(char x) { 217 if (checkLocks()) 218 super.println(x); 219 } 220 221 public void print(int x) { 222 if (checkLocks()) { 223 super.print(x); 224 } 225 } 226 public void println(int x) { 227 if (checkLocks()) 228 super.println(x); 229 } 230 231 public void print(long x) { 232 if (checkLocks()) { 233 super.print(x); 234 } 235 } 236 public void println(long x) { 237 if (checkLocks()) 238 super.println(x); 239 } 240 241 public void print(float x) { 242 if (checkLocks()) { 243 super.print(x); 244 } 245 } 246 public void println(float x) { 247 if (checkLocks()) 248 super.println(x); 249 } 250 251 public void print(double x) { 252 if (checkLocks()) { 253 super.print(x); 254 } 255 } 256 public void println(double x) { 257 if (checkLocks()) 258 super.println(x); 259 } 260 261 public void print(char[] x) { 262 if (checkLocks()) { 263 super.print(x); 264 } 265 } 266 public void println(char[] x) { 267 if (checkLocks()) 268 super.println(x); 269 } 270 271 272 public void println() { 273 if (checkLocks()) { 274 super.println(); 275 } 276 } 277 278 public void write(byte[] buf, int off, int len) { 279 if (checkLocks()) { 280 super.write(buf,off,len); 281 } 282 } 283 284 public void write(int b) { 285 if (checkLocks()) { 286 super.write(b); 287 } 288 } 289 290 309 private boolean checkLocks() { 310 Thread t = Thread.currentThread(); 311 if ( !t.holdsLock(logger) && !t.holdsLock(logManager) ) { 312 return true; 313 } 314 return false; 315 } 316 } 317 318 319 323 private class LoggingByteArrayOutputStream extends ByteArrayOutputStream { 324 private Level logLevel; 325 private Logger logger; 326 private boolean recursiveWarnIssued = false; 327 private boolean inFlush = false; 328 329 public LoggingByteArrayOutputStream() { 330 super(); 331 } 332 public void flush() throws IOException { 333 if (super.size() == 0) { 334 return; 336 } 337 if ((super.size() == lineSeparatorSize) && 338 super.toString().equals(lineSeparator)) { 339 return; 341 } 342 synchronized (this) { 343 if (inFlush) { 344 if (!recursiveWarnIssued) { 346 RuntimeException e = new RuntimeException ("recursivecall"); 347 originalSystemErr.println("recursive call into SystemOutandErrhandler"); 348 e.printStackTrace(originalSystemErr); 349 recursiveWarnIssued = true; 350 } 351 return; 352 } 353 inFlush = true; 354 super.flush(); 355 logger.log(logLevel, this.toString()); 356 inFlush = false; 357 super.reset(); 358 } 359 } 360 public void setLogger(Logger systemlogger, Level loglevel) { 361 logger = systemlogger; 362 logLevel = loglevel; 363 } 364 } 365 366 367 373 private class StackTraceObjects { 374 375 private ByteArrayOutputStream stackTraceBuf; 376 private PrintStream stStream; 377 private String stString; 378 private ByteArrayOutputStream comparisonBuf; 379 private int comparisonBufIndex = 0; 380 private PrintStream cbStream; 381 private int stackTraceBufBytes = 0; 382 private int charsIgnored = 0; 383 384 private StackTraceObjects(Throwable x) { 385 stackTraceBuf = new ByteArrayOutputStream(); 387 stStream = new PrintStream(stackTraceBuf, true); 388 comparisonBuf = new ByteArrayOutputStream(); 389 cbStream = new PrintStream(comparisonBuf, true); 390 ((Throwable )x).printStackTrace(stStream); 391 stString = stackTraceBuf.toString(); 392 stackTraceBufBytes = stackTraceBuf.size(); 393 cbStream.println(x); 395 } 396 397 public String toString() { 398 return stString; 399 } 400 401 boolean ignorePrintln(String str) { 402 String cbString; 403 int cbLen; 404 cbStream.println(str); 405 cbString = comparisonBuf.toString(); 406 cbLen = cbString.length(); 407 if (stString.regionMatches(charsIgnored, cbString, 0, cbLen)) { 408 charsIgnored+= cbLen; 409 comparisonBuf.reset(); 410 return true; 411 } 412 413 return false; 414 415 } 416 417 boolean checkCompletion() { 418 if ( charsIgnored >= stackTraceBufBytes ) { 419 return true; 420 } else { 421 return false; 422 } 423 } 424 } 425 } 426 | Popular Tags |