001: /*
002: * Copyright (C) The Apache Software Foundation. All rights reserved.
003: *
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008: package org.jivesoftware.util.log.output.io;
009:
010: import org.jivesoftware.util.log.format.Formatter;
011: import org.jivesoftware.util.log.output.AbstractOutputTarget;
012: import java.io.IOException;
013: import java.io.OutputStream;
014:
015: /**
016: * A basic target that writes to an OutputStream.
017: *
018: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
019: */
020: public class StreamTarget extends AbstractOutputTarget {
021: ///OutputStream we are writing to
022: private OutputStream m_outputStream;
023:
024: /**
025: * Constructor that writes to a stream and uses a particular formatter.
026: *
027: * @param outputStream the OutputStream to send to
028: * @param formatter the Formatter to use
029: */
030: public StreamTarget(final OutputStream outputStream,
031: final Formatter formatter) {
032: super (formatter);
033:
034: if (null != outputStream) {
035: setOutputStream(outputStream);
036: open();
037: }
038: }
039:
040: /**
041: * Set the output stream.
042: * Close down old stream and send tail if appropriate.
043: *
044: * @param outputStream the new OutputStream
045: */
046: protected synchronized void setOutputStream(
047: final OutputStream outputStream) {
048: if (null == outputStream) {
049: throw new NullPointerException(
050: "outputStream property must not be null");
051: }
052:
053: m_outputStream = outputStream;
054: }
055:
056: /**
057: * Abstract method that will output event.
058: *
059: * @param data the data to be output
060: */
061: protected synchronized void write(final String data) {
062: //Cache method local version
063: //so that can be replaced in another thread
064: final OutputStream outputStream = m_outputStream;
065:
066: if (null == outputStream) {
067: final String message = "Attempted to send data '" + data
068: + "' to Null OutputStream";
069: getErrorHandler().error(message, null, null);
070: return;
071: }
072:
073: try {
074: //TODO: We should be able to specify encoding???
075: outputStream.write(data.getBytes("UTF-8"));
076: outputStream.flush();
077: } catch (final IOException ioe) {
078: final String message = "Error writing data '" + data
079: + "' to OutputStream";
080: getErrorHandler().error(message, ioe, null);
081: }
082: }
083:
084: /**
085: * Shutdown target.
086: * Attempting to send to target after close() will cause errors to be logged.
087: */
088: public synchronized void close() {
089: super .close();
090: shutdownStream();
091: }
092:
093: /**
094: * Shutdown output stream.
095: */
096: protected synchronized void shutdownStream() {
097: final OutputStream outputStream = m_outputStream;
098: m_outputStream = null;
099:
100: try {
101: if (null != outputStream) {
102: outputStream.close();
103: }
104: } catch (final IOException ioe) {
105: getErrorHandler().error("Error closing OutputStream", ioe,
106: null);
107: }
108: }
109: }
|