KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > output > io > StreamTarget


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.jivesoftware.util.log.output.io;
9
10 import org.jivesoftware.util.log.format.Formatter;
11 import org.jivesoftware.util.log.output.AbstractOutputTarget;
12 import java.io.IOException JavaDoc;
13 import java.io.OutputStream JavaDoc;
14
15 /**
16  * A basic target that writes to an OutputStream.
17  *
18  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
19  */

20 public class StreamTarget extends AbstractOutputTarget {
21     ///OutputStream we are writing to
22
private OutputStream JavaDoc m_outputStream;
23
24     /**
25      * Constructor that writes to a stream and uses a particular formatter.
26      *
27      * @param outputStream the OutputStream to send to
28      * @param formatter the Formatter to use
29      */

30     public StreamTarget(final OutputStream JavaDoc outputStream, final Formatter formatter) {
31         super(formatter);
32
33         if (null != outputStream) {
34             setOutputStream(outputStream);
35             open();
36         }
37     }
38
39     /**
40      * Set the output stream.
41      * Close down old stream and send tail if appropriate.
42      *
43      * @param outputStream the new OutputStream
44      */

45     protected synchronized void setOutputStream(final OutputStream JavaDoc outputStream) {
46         if (null == outputStream) {
47             throw new NullPointerException JavaDoc("outputStream property must not be null");
48         }
49
50         m_outputStream = outputStream;
51     }
52
53     /**
54      * Abstract method that will output event.
55      *
56      * @param data the data to be output
57      */

58     protected synchronized void write(final String JavaDoc data) {
59         //Cache method local version
60
//so that can be replaced in another thread
61
final OutputStream JavaDoc outputStream = m_outputStream;
62
63         if (null == outputStream) {
64             final String JavaDoc message = "Attempted to send data '" + data + "' to Null OutputStream";
65             getErrorHandler().error(message, null, null);
66             return;
67         }
68
69         try {
70             //TODO: We should be able to specify encoding???
71
outputStream.write(data.getBytes("UTF-8"));
72             outputStream.flush();
73         }
74         catch (final IOException JavaDoc ioe) {
75             final String JavaDoc message = "Error writing data '" + data + "' to OutputStream";
76             getErrorHandler().error(message, ioe, null);
77         }
78     }
79
80     /**
81      * Shutdown target.
82      * Attempting to send to target after close() will cause errors to be logged.
83      */

84     public synchronized void close() {
85         super.close();
86         shutdownStream();
87     }
88
89     /**
90      * Shutdown output stream.
91      */

92     protected synchronized void shutdownStream() {
93         final OutputStream JavaDoc outputStream = m_outputStream;
94         m_outputStream = null;
95
96         try {
97             if (null != outputStream) {
98                 outputStream.close();
99             }
100         }
101         catch (final IOException JavaDoc ioe) {
102             getErrorHandler().error("Error closing OutputStream", ioe, null);
103         }
104     }
105 }
106
Popular Tags