1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.jasper.util;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.PrintStream;
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 {
32
33
34
35
36
37 /***
38 * Construct the handler to capture the output of the given steam.
39 */
40 public SystemLogHandler(PrintStream wrapped) {
41 super(wrapped);
42 this.wrapped = wrapped;
43 }
44
45
46
47
48
49 /***
50 * Wrapped PrintStream.
51 */
52 protected PrintStream wrapped = null;
53
54
55 /***
56 * Thread <-> PrintStream associations.
57 */
58 protected static ThreadLocal streams = new ThreadLocal();
59
60
61 /***
62 * Thread <-> ByteArrayOutputStream associations.
63 */
64 protected static ThreadLocal data = new ThreadLocal();
65
66
67
68
69
70 public PrintStream getWrapped() {
71 return wrapped;
72 }
73
74 /***
75 * Start capturing thread's output.
76 */
77 public static void setThread() {
78 ByteArrayOutputStream baos = new ByteArrayOutputStream();
79 data.set(baos);
80 streams.set(new PrintStream(baos));
81 }
82
83
84 /***
85 * Stop capturing thread's output and return captured data as a String.
86 */
87 public static String unsetThread() {
88 ByteArrayOutputStream baos =
89 (ByteArrayOutputStream) 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
100
101
102 /***
103 * Find PrintStream to which the output must be written to.
104 */
105 protected PrintStream findStream() {
106 PrintStream ps = (PrintStream) streams.get();
107 if (ps == null) {
108 ps = wrapped;
109 }
110 return ps;
111 }
112
113
114
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
131 }
132
133 public void write(int b) {
134 findStream().write(b);
135 }
136
137 public void write(byte[] b)
138 throws IOException {
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 s) {
175 findStream().print(s);
176 }
177
178 public void print(Object 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 x) {
215 findStream().println(x);
216 }
217
218 public void println(Object x) {
219 findStream().println(x);
220 }
221
222 }