KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > log > EnvironmentStream


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.log;
31
32 import com.caucho.loader.EnvironmentLocal;
33 import com.caucho.util.Alarm;
34 import com.caucho.util.QDate;
35 import com.caucho.vfs.StderrStream;
36 import com.caucho.vfs.StdoutStream;
37 import com.caucho.vfs.StreamImpl;
38 import com.caucho.vfs.Vfs;
39 import com.caucho.vfs.WriteStream;
40
41 import java.io.IOException JavaDoc;
42 import java.io.OutputStream JavaDoc;
43 import java.io.PrintStream JavaDoc;
44
45 /**
46  * A stream that varies depending on the environment class loader.
47  */

48 public class EnvironmentStream extends StreamImpl {
49   // original stdout stream
50
private final static WriteStream _origSystemOut = Vfs.openWrite(System.out);
51   // original stderr stream
52
private final static WriteStream _origSystemErr = Vfs.openWrite(System.err);
53   
54   // static stdout stream
55
private static PrintStream JavaDoc _systemOut;
56   // static stderr stream
57
private static PrintStream JavaDoc _systemErr;
58   
59   // static stdout stream
60
private static EnvironmentStream _stdoutStream;
61   // static stderr stream
62
private static EnvironmentStream _stderrStream;
63   
64   // context variable storing the per-environment stream.
65
private EnvironmentLocal<OutputStream JavaDoc> _environmentStream;
66
67   /**
68    * Create the environment stream.
69    *
70    * @param envVariable the variable for the underlying stream
71    * @param defaultStream the stream used if outside an environment
72    */

73   public EnvironmentStream(String JavaDoc envVariable, OutputStream JavaDoc defaultStream)
74   {
75     _environmentStream = new EnvironmentLocal<OutputStream JavaDoc>(envVariable);
76     _environmentStream.setGlobal(defaultStream);
77   }
78
79   /**
80    * Create the environment stream.
81    *
82    * @param defaultStream the stream used if outside an environment
83    */

84   public EnvironmentStream(OutputStream JavaDoc defaultStream)
85   {
86     _environmentStream = new EnvironmentLocal<OutputStream JavaDoc>();
87     _environmentStream.setGlobal(defaultStream);
88   }
89
90   /**
91    * Returns the context stream's variable.
92    */

93   public String JavaDoc getVariable()
94   {
95     return _environmentStream.getVariable();
96   }
97
98   /**
99    * Returns the global stream
100    */

101   public OutputStream JavaDoc getGlobalStream()
102   {
103     return (OutputStream JavaDoc) _environmentStream.getGlobal();
104   }
105
106   /**
107    * Returns the context stream's variable.
108    */

109   public Object JavaDoc setGlobalStream(OutputStream JavaDoc defaultStream)
110   {
111     return _environmentStream.setGlobal(defaultStream);
112   }
113
114   /**
115    * Returns the global stream
116    */

117   public OutputStream JavaDoc getStream()
118   {
119     return (OutputStream JavaDoc) _environmentStream.get();
120   }
121
122   /**
123    * Returns the context stream's variable.
124    */

125   public Object JavaDoc setStream(OutputStream JavaDoc os)
126   {
127     return _environmentStream.set(os);
128   }
129
130   /**
131    * True if the stream can write
132    */

133   public boolean canWrite()
134   {
135     OutputStream JavaDoc stream = getStream();
136
137     return stream != null;
138   }
139
140   /**
141    * Write data to the stream.
142    */

143   public void write(byte []buf, int offset, int length, boolean isEnd)
144     throws IOException JavaDoc
145   {
146     OutputStream JavaDoc stream = getStream();
147
148     if (stream == null)
149       return;
150
151     synchronized (stream) {
152       stream.write(buf, offset, length);
153       if (isEnd)
154         stream.flush();
155     }
156   }
157
158   /**
159    * Flush data to the stream.
160    */

161   public void flush()
162     throws IOException JavaDoc
163   {
164     OutputStream JavaDoc stream = getStream();
165
166     if (stream == null)
167       return;
168
169     synchronized (stream) {
170       stream.flush();
171     }
172   }
173
174   /**
175    * Flush data to the stream.
176    */

177   public void close()
178     throws IOException JavaDoc
179   {
180     OutputStream JavaDoc stream = getStream();
181
182     if (stream == null)
183       return;
184
185     synchronized (stream) {
186       stream.flush();
187     }
188   }
189
190   /**
191    * Sets the backing stream for System.out
192    */

193   public static void setStdout(OutputStream JavaDoc os)
194   {
195     if (_stdoutStream == null) {
196       OutputStream JavaDoc systemOut = System.out;
197       
198       _stdoutStream = new EnvironmentStream("caucho.stdout.stream",
199                                             systemOut);
200       WriteStream out = new WriteStream(_stdoutStream);
201       out.setDisableClose(true);
202       _systemOut = new PrintStream JavaDoc(out, true);
203       System.setOut(_systemOut);
204
205       if (os == systemOut)
206         return;
207     }
208
209     if (os == _systemErr || os == _systemOut)
210       return;
211
212     if (os instanceof WriteStream) {
213       WriteStream out = (WriteStream) os;
214
215       if (out.getSource() == StdoutStream.create() ||
216       out.getSource() == StderrStream.create())
217     return;
218     }
219     
220     _stdoutStream.setStream(os);
221   }
222
223   /**
224    * Returns the environment stream for System.out
225    */

226   public static EnvironmentStream getStdout()
227   {
228     return _stdoutStream;
229   }
230
231   /**
232    * Returns the original System.out writer
233    */

234   public static WriteStream getOriginalSystemOut()
235   {
236     return _origSystemOut;
237   }
238
239   /**
240    * Sets path as the backing stream for System.err
241    */

242   public static void setStderr(OutputStream JavaDoc os)
243   {
244     if (_stderrStream == null) {
245       OutputStream JavaDoc systemErr = System.err;
246       
247       _stderrStream = new EnvironmentStream("caucho.stderr.stream",
248                                             systemErr);
249       WriteStream err = new WriteStream(_stderrStream);
250       err.setDisableClose(true);
251       _systemErr = new PrintStream JavaDoc(err, true);
252       System.setErr(_systemErr);
253       
254       if (os == systemErr)
255         return;
256     }
257
258     if (os == _systemErr || os == _systemOut)
259       return;
260
261     if (os instanceof WriteStream) {
262       WriteStream out = (WriteStream) os;
263
264       if (out.getSource() == StdoutStream.create() ||
265       out.getSource() == StderrStream.create())
266     return;
267     }
268
269     _stderrStream.setStream(os);
270   }
271
272   /**
273    * Returns the environment stream for System.err
274    */

275   public static EnvironmentStream getStderr()
276   {
277     return _stderrStream;
278   }
279
280   /**
281    * Returns the original System.out writer
282    */

283   public static WriteStream getOriginalSystemErr()
284   {
285     return _origSystemErr;
286   }
287
288   /**
289    * Logs a message to the original stderr in cases where java.util.logging
290    * is dangerous, e.g. in the logging code itself.
291    */

292   public static void logStderr(String JavaDoc msg, Throwable JavaDoc e)
293   {
294     try {
295       long now = Alarm.getCurrentTime();
296     
297       msg = QDate.formatLocal(now, "[%Y-%m-%d %H:%M:%S] ") + msg;
298
299       _origSystemErr.println(msg);
300
301       e.printStackTrace(_origSystemErr.getPrintWriter());
302
303       _origSystemErr.flush();
304     } catch (Throwable JavaDoc e1) {
305     }
306   }
307
308   /**
309    * Logs a message to the original stderr in cases where java.util.logging
310    * is dangerous, e.g. in the logging code itself.
311    */

312   public static void logStderr(String JavaDoc msg)
313   {
314     try {
315       long now = Alarm.getCurrentTime();
316     
317       msg = QDate.formatLocal(now, "[%Y-%m-%d %H:%M:%S] ") + msg;
318
319       _origSystemErr.println(msg);
320     } catch (Throwable JavaDoc e1) {
321     }
322   }
323
324   static {
325     _origSystemOut.setFlushOnNewline(true);
326     _origSystemErr.setFlushOnNewline(true);
327   }
328 }
329
Popular Tags