KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > util > SystemLogHandler


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
18 package org.apache.jasper.util;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.PrintStream JavaDoc;
23
24
25 /**
26  * This helper class may be used to do sophisticated redirection of
27  * System.out and System.err.
28  *
29  * @author Remy Maucherat
30  */

31 public class SystemLogHandler extends PrintStream JavaDoc {
32
33
34     // ----------------------------------------------------------- Constructors
35

36
37     /**
38      * Construct the handler to capture the output of the given steam.
39      */

40     public SystemLogHandler(PrintStream JavaDoc wrapped) {
41         super(wrapped);
42         this.wrapped = wrapped;
43     }
44
45
46     // ----------------------------------------------------- Instance Variables
47

48
49     /**
50      * Wrapped PrintStream.
51      */

52     protected PrintStream JavaDoc wrapped = null;
53
54
55     /**
56      * Thread <-> PrintStream associations.
57      */

58     protected static ThreadLocal JavaDoc streams = new ThreadLocal JavaDoc();
59
60
61     /**
62      * Thread <-> ByteArrayOutputStream associations.
63      */

64     protected static ThreadLocal JavaDoc data = new ThreadLocal JavaDoc();
65
66
67     // --------------------------------------------------------- Public Methods
68

69
70     public PrintStream JavaDoc getWrapped() {
71       return wrapped;
72     }
73
74     /**
75      * Start capturing thread's output.
76      */

77     public static void setThread() {
78         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
79         data.set(baos);
80         streams.set(new PrintStream JavaDoc(baos));
81     }
82
83
84     /**
85      * Stop capturing thread's output and return captured data as a String.
86      */

87     public static String JavaDoc unsetThread() {
88         ByteArrayOutputStream JavaDoc baos =
89             (ByteArrayOutputStream JavaDoc) data.get();
90         if (baos == null) {
91             return null;
92         }
93         streams.set(null);
94         data.set(null);
95         return baos.toString();
96     }
97
98
99     // ------------------------------------------------------ Protected Methods
100

101
102     /**
103      * Find PrintStream to which the output must be written to.
104      */

105     protected PrintStream JavaDoc findStream() {
106         PrintStream JavaDoc ps = (PrintStream JavaDoc) streams.get();
107         if (ps == null) {
108             ps = wrapped;
109         }
110         return ps;
111     }
112
113
114     // ---------------------------------------------------- PrintStream Methods
115

116
117     public void flush() {
118         findStream().flush();
119     }
120
121     public void close() {
122         findStream().close();
123     }
124
125     public boolean checkError() {
126         return findStream().checkError();
127     }
128
129     protected void setError() {
130         //findStream().setError();
131
}
132
133     public void write(int b) {
134         findStream().write(b);
135     }
136
137     public void write(byte[] b)
138         throws IOException JavaDoc {
139         findStream().write(b);
140     }
141
142     public void write(byte[] buf, int off, int len) {
143         findStream().write(buf, off, len);
144     }
145
146     public void print(boolean b) {
147         findStream().print(b);
148     }
149
150     public void print(char c) {
151         findStream().print(c);
152     }
153
154     public void print(int i) {
155         findStream().print(i);
156     }
157
158     public void print(long l) {
159         findStream().print(l);
160     }
161
162     public void print(float f) {
163         findStream().print(f);
164     }
165
166     public void print(double d) {
167         findStream().print(d);
168     }
169
170     public void print(char[] s) {
171         findStream().print(s);
172     }
173
174     public void print(String JavaDoc s) {
175         findStream().print(s);
176     }
177
178     public void print(Object JavaDoc obj) {
179         findStream().print(obj);
180     }
181
182     public void println() {
183         findStream().println();
184     }
185
186     public void println(boolean x) {
187         findStream().println(x);
188     }
189
190     public void println(char x) {
191         findStream().println(x);
192     }
193
194     public void println(int x) {
195         findStream().println(x);
196     }
197
198     public void println(long x) {
199         findStream().println(x);
200     }
201
202     public void println(float x) {
203         findStream().println(x);
204     }
205
206     public void println(double x) {
207         findStream().println(x);
208     }
209
210     public void println(char[] x) {
211         findStream().println(x);
212     }
213
214     public void println(String JavaDoc x) {
215         findStream().println(x);
216     }
217
218     public void println(Object JavaDoc x) {
219         findStream().println(x);
220     }
221
222 }
223
Popular Tags