KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > ThreadLocalPrintStream


1 /*
2
3   Copyright 2004, Martian Software, Inc.
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   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
19 package com.martiansoftware.nailgun;
20
21 import java.io.IOException JavaDoc;
22 import java.io.PrintStream JavaDoc;
23
24 /**
25  * The class name is pretty descriptive. This creates a PrintStream
26  * much like a FilterOutputStream, but with the wrapped PrintStream
27  * being local to the current Thread. By setting System.out to a
28  * ThreadLocalPrintStream, different Threads can write to different
29  * PrintStreams simply by using System.out. Of course, the init()
30  * method must be called by the Thread that wishes to use the
31  * wrapped stream.
32  *
33  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
34  */

35 class ThreadLocalPrintStream extends PrintStream JavaDoc {
36
37     /**
38      * The PrintStreams for the various threads
39      */

40     private InheritableThreadLocal JavaDoc streams = null;
41
42     private PrintStream JavaDoc defaultPrintStream = null;
43     
44     /**
45      * Creates a new InheritedThreadLocalPrintStream
46      * @param defaultPrintStream the PrintStream that will be used if the
47      * current thread has not called init()
48      */

49     public ThreadLocalPrintStream(PrintStream JavaDoc defaultPrintStream) {
50         super(defaultPrintStream);
51         streams = new InheritableThreadLocal JavaDoc();
52         this.defaultPrintStream = defaultPrintStream;
53         init(null);
54     }
55
56     /**
57      * Sets the PrintStream for the current thread
58      * @param streamForCurrentThread the PrintStream for the current thread
59      */

60     void init(PrintStream JavaDoc streamForCurrentThread) {
61         streams.set(streamForCurrentThread);
62     }
63
64     /**
65      * Returns this thread's PrintStream
66      * @return this thread's PrintStream
67      */

68     PrintStream JavaDoc getPrintStream() {
69         PrintStream JavaDoc result = (PrintStream JavaDoc) streams.get();
70         return ((result == null) ? defaultPrintStream : result);
71     }
72
73 // BEGIN delegated java.io.PrintStream methods
74

75     /**
76      * @see java.io.PrintStream#checkError()
77      */

78     public boolean checkError() {
79         return (getPrintStream().checkError());
80     }
81
82     /**
83      * @see java.io.PrintStream#close()
84      */

85     public void close() {
86         getPrintStream().close();
87     }
88
89     /**
90      * @see java.io.PrintStream#flush()
91      */

92     public void flush() {
93         getPrintStream().flush();
94     }
95
96     /**
97      * @see java.io.PrintStream#print(boolean)
98      */

99     public void print(boolean b) {
100         getPrintStream().print(b);
101     }
102
103     /**
104      * @see java.io.PrintStream#print(char)
105      */

106     public void print(char c) {
107         getPrintStream().print(c);
108     }
109
110     /**
111      * @see java.io.PrintStream#print(char[])
112      */

113     public void print(char[] s) {
114         getPrintStream().print(s);
115     }
116
117     /**
118      * @see java.io.PrintStream#print(double)
119      */

120     public void print(double d) {
121         getPrintStream().print(d);
122     }
123
124     /**
125      * @see java.io.PrintStream#print(float)
126      */

127     public void print(float f) {
128         getPrintStream().print(f);
129     }
130
131     /**
132      * @see java.io.PrintStream#print(int)
133      */

134     public void print(int i) {
135         getPrintStream().print(i);
136     }
137
138     /**
139      * @see java.io.PrintStream#print(long)
140      */

141     public void print(long l) {
142         getPrintStream().print(l);
143     }
144
145     /**
146      * @see java.io.PrintStream#print(Object)
147      */

148     public void print(Object JavaDoc obj) {
149         getPrintStream().print(obj);
150     }
151
152     /**
153      * @see java.io.PrintStream#print(String)
154      */

155     public void print(String JavaDoc s) {
156         getPrintStream().print(s);
157     }
158
159     /**
160      * @see java.io.PrintStream#println()
161      */

162     public void println() {
163         getPrintStream().println();
164     }
165
166     /**
167      * @see java.io.PrintStream#println(boolean)
168      */

169     public void println(boolean x) {
170         getPrintStream().println(x);
171     }
172
173     /**
174      * @see java.io.PrintStream#println(char)
175      */

176     public void println(char x) {
177         getPrintStream().println(x);
178     }
179
180     /**
181      * @see java.io.PrintStream#println(char[])
182      */

183     public void println(char[] x) {
184         getPrintStream().println(x);
185     }
186
187     /**
188      * @see java.io.PrintStream#println(double)
189      */

190     public void println(double x) {
191         getPrintStream().println(x);
192     }
193
194     /**
195      * @see java.io.PrintStream#println(float)
196      */

197     public void println(float x) {
198         getPrintStream().println(x);
199     }
200
201     /**
202      * @see java.io.PrintStream#println(int)
203      */

204     public void println(int x) {
205         getPrintStream().println(x);
206     }
207
208     /**
209      * @see java.io.PrintStream#println(long)
210      */

211     public void println(long x) {
212         getPrintStream().println(x);
213     }
214
215     /**
216      * @see java.io.PrintStream#println(Object)
217      */

218     public void println(Object JavaDoc x) {
219         getPrintStream().println(x);
220     }
221
222     /**
223      * @see java.io.PrintStream#println(String)
224      */

225     public void println(String JavaDoc x) {
226         getPrintStream().println(x);
227     }
228
229     /**
230      * @see java.io.PrintStream#write(byte[],int,int)
231      */

232     public void write(byte[] buf, int off, int len) {
233         getPrintStream().write(buf, off, len);
234     }
235
236     /**
237      * @see java.io.PrintStream#write(int)
238      */

239     public void write(int b) {
240         getPrintStream().write(b);
241     }
242
243 // END delegated java.io.PrintStream methods
244

245 // BEGIN delegated java.io.FilterOutputStream methods
246

247     /**
248      * @see java.io.FilterOutputStream#write(byte[])
249      */

250     public void write(byte[] b) throws IOException JavaDoc {
251         getPrintStream().write(b);
252     }
253
254 // END delegated java.io.FilterOutputStream methods
255

256 // Note: Should java.lang.Object methods be delegated? If not, and
257
// someone synchronizes on this stream, processes might be blocked
258
// that shouldn't be. It would certainly be stupid to delegate
259
// finalize(). Not so clear are hashcode(), equals(), notify(), and
260
// the wait() methods.
261
}
262
Popular Tags