001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender;
018
019import java.io.IOException;
020import java.io.Writer;
021import java.util.concurrent.TimeUnit;
022
023import org.apache.logging.log4j.core.StringLayout;
024
025/**
026 * Manages a Writer so that it can be shared by multiple Appenders and will
027 * allow appenders to reconfigure without requiring a new writer.
028 */
029public class WriterManager extends AbstractManager {
030
031    /**
032     * Creates a Manager.
033     *
034     * @param name The name of the stream to manage.
035     * @param data The data to pass to the Manager.
036     * @param factory The factory to use to create the Manager.
037     * @param <T> The type of the WriterManager.
038     * @return A WriterManager.
039     */
040    public static <T> WriterManager getManager(final String name, final T data,
041                                                 final ManagerFactory<? extends WriterManager, T> factory) {
042        return AbstractManager.getManager(name, factory, data);
043    }
044    protected final StringLayout layout;
045
046    private volatile Writer writer;
047
048    public WriterManager(final Writer writer, final String streamName, final StringLayout layout,
049            final boolean writeHeader) {
050        super(null, streamName);
051        this.writer = writer;
052        this.layout = layout;
053        if (writeHeader && layout != null) {
054            final byte[] header = layout.getHeader();
055            if (header != null) {
056                try {
057                    this.writer.write(new String(header, layout.getCharset()));
058                } catch (final IOException e) {
059                    logError("Unable to write header", e);
060                }
061            }
062        }
063    }
064
065    protected synchronized void closeWriter() {
066        final Writer w = writer; // access volatile field only once per method
067        try {
068            w.close();
069        } catch (final IOException ex) {
070            logError("Unable to close stream", ex);
071        }
072    }
073
074    /**
075     * Flushes any buffers.
076     */
077    public synchronized void flush() {
078        try {
079            writer.flush();
080        } catch (final IOException ex) {
081            final String msg = "Error flushing stream " + getName();
082            throw new AppenderLoggingException(msg, ex);
083        }
084    }
085
086    protected Writer getWriter() {
087        return writer;
088    }
089
090    /**
091     * Returns the status of the stream.
092     * @return true if the stream is open, false if it is not.
093     */
094    public boolean isOpen() {
095        return getCount() > 0;
096    }
097
098    /**
099     * Default hook to write footer during close.
100     */
101    @Override
102    public boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
103        writeFooter();
104        closeWriter();
105        return true;
106    }
107
108    protected void setWriter(final Writer writer) {
109        final byte[] header = layout.getHeader();
110        if (header != null) {
111            try {
112                writer.write(new String(header, layout.getCharset()));
113                this.writer = writer; // only update field if writer.write() succeeded
114            } catch (final IOException ioe) {
115                logError("Unable to write header", ioe);
116            }
117        } else {
118            this.writer = writer;
119        }
120    }
121
122    /**
123     * Some output streams synchronize writes while others do not. Synchronizing here insures that
124     * log events won't be intertwined.
125     * @param str the string to write
126     * @throws AppenderLoggingException if an error occurs.
127     */
128    protected synchronized void write(final String str)  {
129        try {
130            writer.write(str);
131        } catch (final IOException ex) {
132            final String msg = "Error writing to stream " + getName();
133            throw new AppenderLoggingException(msg, ex);
134        }
135    }
136
137    /**
138     * Writes the footer.
139     */
140    protected void writeFooter() {
141        if (layout == null) {
142            return;
143        }
144        final byte[] footer = layout.getFooter();
145        if (footer != null && footer.length > 0) {
146            write(new String(footer, layout.getCharset()));
147        }
148    }
149}