KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > logger > LoggerAwareOutputStream


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 package org.apache.avalon.framework.logger;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 /**
24  * Logger aware output stream, characters written to this {@link OutputStream}
25  * are buffered until a newline character is encountered, or a flush() is called.
26  *
27  * <p>
28  * Extend to specify the log method that the message should be invoked. eg:
29  * </p>
30  *
31  * <pre>
32  * setOutputStream( new LoggerAwareOutputStream( getLogger() ) {
33  * protected void logMessage( String message )
34  * {
35  * if ( m_logger.isDebugEnabled() )
36  * {
37  * m_logger.debug( message );
38  * }
39  * }
40  * } );
41  * </pre>
42  *
43  * @author <a HREF="mailto:crafterm@apache.org">Marcus Crafter</a>
44  * @version $Revision:$
45  * @since Nov 19, 2004 7:03:50 PM
46  */

47 public abstract class LoggerAwareOutputStream extends OutputStream JavaDoc
48 {
49     /**
50      * Constructor, creates instance of class.
51      *
52      * @param logger logger this output stream should use
53      */

54     public LoggerAwareOutputStream( Logger logger ) {
55         m_logger = logger;
56     }
57
58     /**
59      * Writes a byte to the internal buffer. If a newline character is
60      * encountered, then the buffer is sent to the logger.
61      *
62      * @param b character to write
63      * @throws IOException if an error occurs
64      * @see java.io.OutputStream#write(int)
65      */

66     public void write( int b ) throws IOException JavaDoc
67     {
68         if ( b == '\n' )
69         {
70             final byte[] content = bos.toByteArray();
71             logMessage( new String JavaDoc( content ) );
72             bos.reset();
73             return;
74         }
75
76         bos.write( b );
77     }
78
79     /**
80      * Flushes this output stream, writing any buffered content to the log
81      *
82      * @throws IOException on error
83      * @see java.io.OutputStream#flush()
84      */

85     public void flush() throws IOException JavaDoc
86     {
87         final byte[] content = bos.toByteArray();
88         logMessage( new String JavaDoc( content ) );
89         bos.reset();
90     }
91
92     /**
93      * Purposely flushes the stream, but doesn't close anything since the logger
94      * is managed by another class.
95      *
96      * @throws IOException if an IO error occurs
97      * @see java.io.OutputStream#close()
98      */

99     public void close() throws IOException JavaDoc
100     {
101         flush();
102     }
103
104     /**
105      * Writes the message to the log. Subclasses should override this method to
106      * send the message to the log level they require.
107      *
108      * @param message message to be written
109      */

110     protected abstract void logMessage( String JavaDoc message );
111
112     /** Message buffer */
113     private final ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
114
115     /** {@link Logger} reference */
116     protected final Logger m_logger;
117 }
Popular Tags