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.BufferedOutputStream;
020import java.io.File;
021import java.io.FileNotFoundException;
022import java.io.FileOutputStream;
023import java.io.IOException;
024import java.io.OutputStream;
025import java.io.Serializable;
026import java.nio.channels.FileChannel;
027import java.nio.channels.FileLock;
028import java.util.HashMap;
029import java.util.Map;
030
031import org.apache.logging.log4j.core.Layout;
032
033
034/**
035 * Manages actual File I/O for File Appenders.
036 */
037public class FileManager extends OutputStreamManager {
038
039    private static final FileManagerFactory FACTORY = new FileManagerFactory();
040
041    private final boolean isAppend;
042    private final boolean isLocking;
043    private final String advertiseURI;
044    private final int bufferSize;
045
046    protected FileManager(final String fileName, final OutputStream os, final boolean append, final boolean locking,
047            final String advertiseURI, final Layout<? extends Serializable> layout, final int bufferSize,
048            final boolean writeHeader) {
049        super(os, fileName, layout, writeHeader);
050        this.isAppend = append;
051        this.isLocking = locking;
052        this.advertiseURI = advertiseURI;
053        this.bufferSize = bufferSize;
054    }
055
056    /**
057     * Returns the FileManager.
058     * @param fileName The name of the file to manage.
059     * @param append true if the file should be appended to, false if it should be overwritten.
060     * @param locking true if the file should be locked while writing, false otherwise.
061     * @param bufferedIo true if the contents should be buffered as they are written.
062     * @param advertiseUri the URI to use when advertising the file
063     * @param layout The layout
064     * @param bufferSize buffer size for buffered IO
065     * @return A FileManager for the File.
066     */
067    public static FileManager getFileManager(final String fileName, final boolean append, boolean locking,
068            final boolean bufferedIo, final String advertiseUri, final Layout<? extends Serializable> layout,
069            final int bufferSize) {
070
071        if (locking && bufferedIo) {
072            locking = false;
073        }
074        return (FileManager) getManager(fileName, new FactoryData(append, locking, bufferedIo, bufferSize,
075                advertiseUri, layout), FACTORY);
076    }
077
078    @Override
079    protected synchronized void write(final byte[] bytes, final int offset, final int length)  {
080
081        if (isLocking) {
082            final FileChannel channel = ((FileOutputStream) getOutputStream()).getChannel();
083            try {
084                /* Lock the whole file. This could be optimized to only lock from the current file
085                   position. Note that locking may be advisory on some systems and mandatory on others,
086                   so locking just from the current position would allow reading on systems where
087                   locking is mandatory.  Also, Java 6 will throw an exception if the region of the
088                   file is already locked by another FileChannel in the same JVM. Hopefully, that will
089                   be avoided since every file should have a single file manager - unless two different
090                   files strings are configured that somehow map to the same file.*/
091                final FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
092                try {
093                    super.write(bytes, offset, length);
094                } finally {
095                    lock.release();
096                }
097            } catch (final IOException ex) {
098                throw new AppenderLoggingException("Unable to obtain lock on " + getName(), ex);
099            }
100
101        } else {
102            super.write(bytes, offset, length);
103        }
104    }
105
106    /**
107     * Returns the name of the File being managed.
108     * @return The name of the File being managed.
109     */
110    public String getFileName() {
111        return getName();
112    }
113
114    /**
115     * Returns the append status.
116     * @return true if the file will be appended to, false if it is overwritten.
117     */
118    public boolean isAppend() {
119        return isAppend;
120    }
121
122    /**
123     * Returns the lock status.
124     * @return true if the file will be locked when writing, false otherwise.
125     */
126    public boolean isLocking() {
127        return isLocking;
128    }
129    
130    /**
131     * Returns the buffer size to use if the appender was configured with BufferedIO=true, otherwise returns a negative
132     * number.
133     * @return the buffer size, or a negative number if the output stream is not buffered
134     */
135    public int getBufferSize() {
136        return bufferSize;
137    }
138
139    /**
140     * FileManager's content format is specified by: <code>Key: "fileURI" Value: provided "advertiseURI" param</code>.
141     *
142     * @return Map of content format keys supporting FileManager
143     */
144    @Override
145    public Map<String, String> getContentFormat() {
146        final Map<String, String> result = new HashMap<>(super.getContentFormat());
147        result.put("fileURI", advertiseURI);
148        return result;
149    }
150
151    /**
152     * Factory Data.
153     */
154    private static class FactoryData {
155        private final boolean append;
156        private final boolean locking;
157        private final boolean bufferedIO;
158        private final int bufferSize;
159        private final String advertiseURI;
160        private final Layout<? extends Serializable> layout;
161
162        /**
163         * Constructor.
164         * @param append Append status.
165         * @param locking Locking status.
166         * @param bufferedIO Buffering flag.
167         * @param bufferSize Buffer size.
168         * @param advertiseURI the URI to use when advertising the file
169         */
170        public FactoryData(final boolean append, final boolean locking, final boolean bufferedIO, final int bufferSize,
171                final String advertiseURI, final Layout<? extends Serializable> layout) {
172            this.append = append;
173            this.locking = locking;
174            this.bufferedIO = bufferedIO;
175            this.bufferSize = bufferSize;
176            this.advertiseURI = advertiseURI;
177            this.layout = layout;
178        }
179    }
180
181    /**
182     * Factory to create a FileManager.
183     */
184    private static class FileManagerFactory implements ManagerFactory<FileManager, FactoryData> {
185
186        /**
187         * Create a FileManager.
188         * @param name The name of the File.
189         * @param data The FactoryData
190         * @return The FileManager for the File.
191         */
192        @Override
193        public FileManager createManager(final String name, final FactoryData data) {
194            final File file = new File(name);
195            final File parent = file.getParentFile();
196            if (null != parent && !parent.exists()) {
197                parent.mkdirs();
198            }
199
200            final boolean writeHeader = !data.append || !file.exists();
201            OutputStream os;
202            try {
203                os = new FileOutputStream(name, data.append);
204                int bufferSize = data.bufferSize;
205                if (data.bufferedIO) {
206                    os = new BufferedOutputStream(os, bufferSize);
207                } else {
208                    bufferSize = -1; // signals to RollingFileManager not to use BufferedOutputStream
209                }
210                return new FileManager(name, os, data.append, data.locking, data.advertiseURI, data.layout, bufferSize,
211                        writeHeader);
212            } catch (final FileNotFoundException ex) {
213                LOGGER.error("FileManager (" + name + ") " + ex);
214            }
215            return null;
216        }
217    }
218
219}