KickJava   Java API By Example, From Geeks To Geeks.

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


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.Writer JavaDoc;
21 import org.apache.log.format.Formatter;
22 import org.apache.log.output.AbstractOutputTarget;
23
24 /**
25  * This target outputs to a writer.
26  *
27  * @author Peter Donald
28  */

29 public class WriterTarget
30     extends AbstractOutputTarget
31 {
32     private Writer JavaDoc m_output;
33
34     /**
35      * Construct target with a specific writer and formatter.
36      *
37      * @param writer the writer
38      * @param formatter the formatter
39      */

40     public WriterTarget( final Writer JavaDoc writer, final Formatter formatter )
41     {
42         super( formatter );
43
44         if( null != writer )
45         {
46             setWriter( writer );
47             open();
48         }
49     }
50
51     /**
52      * Set the writer.
53      * Close down writer and write tail if appropriate.
54      *
55      * @param writer the new writer
56      */

57     protected synchronized void setWriter( final Writer JavaDoc writer )
58     {
59         if( null == writer )
60         {
61             throw new NullPointerException JavaDoc( "writer property must not be null" );
62         }
63
64         m_output = writer;
65     }
66
67     /**
68      * Concrete implementation of output that writes out to underlying writer.
69      *
70      * @param data the data to output
71      */

72     protected void write( final String JavaDoc data )
73     {
74         try
75         {
76             m_output.write( data );
77             m_output.flush();
78         }
79         catch( final IOException JavaDoc ioe )
80         {
81             getErrorHandler().error( "Caught an IOException", ioe, null );
82         }
83     }
84
85     /**
86      * Shutdown target.
87      * Attempting to write to target after close() will cause errors to be logged.
88      */

89     public synchronized void close()
90     {
91         super.close();
92         shutdownWriter();
93     }
94
95     /**
96      * Shutdown Writer.
97      */

98     protected synchronized void shutdownWriter()
99     {
100         final Writer JavaDoc writer = m_output;
101         m_output = null;
102
103         try
104         {
105             if( null != writer )
106             {
107                 writer.close();
108             }
109         }
110         catch( final IOException JavaDoc ioe )
111         {
112             getErrorHandler().error( "Error closing Writer", ioe, null );
113         }
114     }
115 }
116
Popular Tags