001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.jasper.util;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.PrintStream;
022: import java.util.Hashtable;
023:
024: /**
025: * This helper class may be used to do sophisticated redirection of
026: * System.out and System.err.
027: *
028: * @author Remy Maucherat
029: */
030: public class SystemLogHandler extends PrintStream {
031:
032: // ----------------------------------------------------------- Constructors
033:
034: /**
035: * Construct the handler to capture the output of the given steam.
036: */
037: public SystemLogHandler(PrintStream wrapped) {
038: super (wrapped);
039: this .wrapped = wrapped;
040: }
041:
042: // ----------------------------------------------------- Instance Variables
043:
044: /**
045: * Wrapped PrintStream.
046: */
047: protected PrintStream wrapped = null;
048:
049: /**
050: * Thread <-> PrintStream associations.
051: */
052: protected static Hashtable streams = new Hashtable();
053:
054: /**
055: * Thread <-> ByteArrayOutputStream associations.
056: */
057: protected static Hashtable data = new Hashtable();
058:
059: // --------------------------------------------------------- Public Methods
060:
061: public PrintStream getWrapped() {
062: return wrapped;
063: }
064:
065: /**
066: * Start capturing thread's output.
067: */
068: public static void setThread() {
069: ByteArrayOutputStream baos = new ByteArrayOutputStream();
070: PrintStream ps = new PrintStream(baos);
071: data.put(Thread.currentThread(), baos);
072: streams.put(Thread.currentThread(), ps);
073: }
074:
075: /**
076: * Stop capturing thread's output and return captured data as a String.
077: */
078: public static String unsetThread() {
079: ByteArrayOutputStream baos = (ByteArrayOutputStream) data
080: .get(Thread.currentThread());
081: if (baos == null) {
082: return null;
083: }
084: streams.remove(Thread.currentThread());
085: data.remove(Thread.currentThread());
086: return baos.toString();
087: }
088:
089: // ------------------------------------------------------ Protected Methods
090:
091: /**
092: * Find PrintStream to which the output must be written to.
093: */
094: protected PrintStream findStream() {
095: PrintStream ps = (PrintStream) streams.get(Thread
096: .currentThread());
097: if (ps == null) {
098: ps = wrapped;
099: }
100: return ps;
101: }
102:
103: // ---------------------------------------------------- PrintStream Methods
104:
105: public void flush() {
106: findStream().flush();
107: }
108:
109: public void close() {
110: findStream().close();
111: }
112:
113: public boolean checkError() {
114: return findStream().checkError();
115: }
116:
117: protected void setError() {
118: //findStream().setError();
119: }
120:
121: public void write(int b) {
122: findStream().write(b);
123: }
124:
125: public void write(byte[] b) throws IOException {
126: findStream().write(b);
127: }
128:
129: public void write(byte[] buf, int off, int len) {
130: findStream().write(buf, off, len);
131: }
132:
133: public void print(boolean b) {
134: findStream().print(b);
135: }
136:
137: public void print(char c) {
138: findStream().print(c);
139: }
140:
141: public void print(int i) {
142: findStream().print(i);
143: }
144:
145: public void print(long l) {
146: findStream().print(l);
147: }
148:
149: public void print(float f) {
150: findStream().print(f);
151: }
152:
153: public void print(double d) {
154: findStream().print(d);
155: }
156:
157: public void print(char[] s) {
158: findStream().print(s);
159: }
160:
161: public void print(String s) {
162: findStream().print(s);
163: }
164:
165: public void print(Object obj) {
166: findStream().print(obj);
167: }
168:
169: public void println() {
170: findStream().println();
171: }
172:
173: public void println(boolean x) {
174: findStream().println(x);
175: }
176:
177: public void println(char x) {
178: findStream().println(x);
179: }
180:
181: public void println(int x) {
182: findStream().println(x);
183: }
184:
185: public void println(long x) {
186: findStream().println(x);
187: }
188:
189: public void println(float x) {
190: findStream().println(x);
191: }
192:
193: public void println(double x) {
194: findStream().println(x);
195: }
196:
197: public void println(char[] x) {
198: findStream().println(x);
199: }
200:
201: public void println(String x) {
202: findStream().println(x);
203: }
204:
205: public void println(Object x) {
206: findStream().println(x);
207: }
208:
209: }
|