StreamTarget.java 3.08 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package org.jivesoftware.util.log.output.io;

import org.jivesoftware.util.log.format.Formatter;
import org.jivesoftware.util.log.output.AbstractOutputTarget;
import java.io.IOException;
import java.io.OutputStream;

/**
 * A basic target that writes to an OutputStream.
 *
 * @author <a href="mailto:peter@apache.org">Peter Donald</a>
 */
public class StreamTarget extends AbstractOutputTarget {
    ///OutputStream we are writing to
    private OutputStream m_outputStream;

    /**
     * Constructor that writes to a stream and uses a particular formatter.
     *
     * @param outputStream the OutputStream to send to
     * @param formatter    the Formatter to use
     */
    public StreamTarget(final OutputStream outputStream, final Formatter formatter) {
        super(formatter);

        if (null != outputStream) {
            setOutputStream(outputStream);
            open();
        }
    }

    /**
     * Set the output stream.
     * Close down old stream and send tail if appropriate.
     *
     * @param outputStream the new OutputStream
     */
    protected synchronized void setOutputStream(final OutputStream outputStream) {
        if (null == outputStream) {
            throw new NullPointerException("outputStream property must not be null");
        }

        m_outputStream = outputStream;
    }

    /**
     * Abstract method that will output event.
     *
     * @param data the data to be output
     */
    protected synchronized void write(final String data) {
        //Cache method local version
        //so that can be replaced in another thread
        final OutputStream outputStream = m_outputStream;

        if (null == outputStream) {
            final String message = "Attempted to send data '" + data + "' to Null OutputStream";
            getErrorHandler().error(message, null, null);
            return;
        }

        try {
            //TODO: We should be able to specify encoding???
            outputStream.write(data.getBytes());
            outputStream.flush();
        }
        catch (final IOException ioe) {
            final String message = "Error writing data '" + data + "' to OutputStream";
            getErrorHandler().error(message, ioe, null);
        }
    }

    /**
     * Shutdown target.
     * Attempting to send to target after close() will cause errors to be logged.
     */
    public synchronized void close() {
        super.close();
        shutdownStream();
    }

    /**
     * Shutdown output stream.
     */
    protected synchronized void shutdownStream() {
        final OutputStream outputStream = m_outputStream;
        m_outputStream = null;

        try {
            if (null != outputStream) {
                outputStream.close();
            }
        }
        catch (final IOException ioe) {
            getErrorHandler().error("Error closing OutputStream", ioe, null);
        }
    }
}