KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > io > StreamTarget


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.output.io;
18
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import org.apache.log.format.Formatter;
22 import org.apache.log.output.AbstractOutputTarget;
23
24 /**
25  * A basic target that writes to an OutputStream.
26  *
27  * @author Peter Donald
28  */

29 public class StreamTarget
30     extends AbstractOutputTarget
31 {
32     /** OutputStream we are writing to. */
33     private OutputStream JavaDoc m_outputStream;
34
35     /** The encoding to use when creating byte array for string, may be null. */
36     private String JavaDoc m_encoding;
37
38     /**
39      * Constructor that writes to a stream and uses a particular formatter.
40      *
41      * @param outputStream the OutputStream to write to
42      * @param formatter the Formatter to use
43      * @param encoding Desired encoding to use when writing to the log, null
44      * implies the default system encoding.
45      */

46     public StreamTarget( final OutputStream JavaDoc outputStream,
47                          final Formatter formatter,
48                          final String JavaDoc encoding )
49     {
50         super( formatter );
51         m_encoding = encoding;
52
53         if( null != outputStream )
54         {
55             setOutputStream( outputStream );
56             open();
57         }
58     }
59
60     /**
61      * Constructor that writes to a stream and uses a particular formatter.
62      *
63      * @param outputStream the OutputStream to write to
64      * @param formatter the Formatter to use
65      */

66     public StreamTarget( final OutputStream JavaDoc outputStream,
67                          final Formatter formatter )
68     {
69         // We can get the default system encoding by calling the following
70
// method, but it is not a standard API so we work around it by
71
// allowing encoding to be null.
72
// sun.io.Converters.getDefaultEncodingName();
73

74         this( outputStream, formatter, null );
75     }
76
77     /**
78      * Set the output stream.
79      * Close down old stream and write tail if appropriate.
80      *
81      * @param outputStream the new OutputStream
82      */

83     protected synchronized void setOutputStream( final OutputStream JavaDoc outputStream )
84     {
85         if( null == outputStream )
86         {
87             throw new NullPointerException JavaDoc( "outputStream property must not be null" );
88         }
89
90         m_outputStream = outputStream;
91     }
92
93     /**
94      * Abstract method that will output event.
95      *
96      * @param data the data to be output
97      */

98     protected synchronized void write( final String JavaDoc data )
99     {
100         //Cache method local version
101
//so that can be replaced in another thread
102
final OutputStream JavaDoc outputStream = m_outputStream;
103
104         if( null == outputStream )
105         {
106             final String JavaDoc message = "Attempted to write data '" + data + "' to Null OutputStream";
107             getErrorHandler().error( message, null, null );
108             return;
109         }
110
111         try
112         {
113             byte[] bytes;
114             if( m_encoding == null )
115             {
116                 bytes = data.getBytes();
117             }
118             else
119             {
120                 bytes = data.getBytes( m_encoding );
121             }
122             outputStream.write( bytes );
123             outputStream.flush();
124         }
125         catch( final IOException JavaDoc ioe )
126         {
127             final String JavaDoc message = "Error writing data '" + data + "' to OutputStream";
128             getErrorHandler().error( message, ioe, null );
129         }
130     }
131
132     /**
133      * Shutdown target.
134      * Attempting to write to target after close() will cause errors to be logged.
135      *
136      */

137     public synchronized void close()
138     {
139         super.close();
140         shutdownStream();
141     }
142
143     /**
144      * Shutdown output stream.
145      */

146     protected synchronized void shutdownStream()
147     {
148         final OutputStream JavaDoc outputStream = m_outputStream;
149         m_outputStream = null;
150
151         try
152         {
153             if( null != outputStream )
154             {
155                 // Never close System Streams
156
if( ! ( System.out == outputStream && System.err == outputStream ) )
157                 {
158                     outputStream.close();
159                 }
160             }
161         }
162         catch( final IOException JavaDoc ioe )
163         {
164             getErrorHandler().error( "Error closing OutputStream", ioe, null );
165         }
166     }
167 }
168
Popular Tags