View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender;
18  
19  import java.io.File;
20  import java.io.FileNotFoundException;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.io.Serializable;
25  import java.nio.ByteBuffer;
26  import java.nio.channels.FileChannel;
27  import java.nio.channels.FileLock;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import org.apache.logging.log4j.core.Layout;
32  import org.apache.logging.log4j.core.util.Constants;
33  
34  
35  /**
36   * Manages actual File I/O for File Appenders.
37   */
38  public class FileManager extends OutputStreamManager {
39  
40      private static final FileManagerFactory FACTORY = new FileManagerFactory();
41  
42      private final boolean isAppend;
43      private final boolean isLocking;
44      private final String advertiseURI;
45      private final int bufferSize;
46  
47      @Deprecated
48      protected FileManager(final String fileName, final OutputStream os, final boolean append, final boolean locking,
49              final String advertiseURI, final Layout<? extends Serializable> layout, final int bufferSize,
50              final boolean writeHeader) {
51          this(fileName, os, append, locking, advertiseURI, layout, writeHeader, ByteBuffer.wrap(new byte[bufferSize]));
52      }
53  
54      /** @since 2.6 */
55      protected FileManager(final String fileName, final OutputStream os, final boolean append, final boolean locking,
56              final String advertiseURI, final Layout<? extends Serializable> layout, final boolean writeHeader,
57              final ByteBuffer buffer) {
58          super(os, fileName, layout, writeHeader, buffer);
59          this.isAppend = append;
60          this.isLocking = locking;
61          this.advertiseURI = advertiseURI;
62          this.bufferSize = buffer.capacity();
63      }
64  
65      /**
66       * Returns the FileManager.
67       * @param fileName The name of the file to manage.
68       * @param append true if the file should be appended to, false if it should be overwritten.
69       * @param locking true if the file should be locked while writing, false otherwise.
70       * @param bufferedIo true if the contents should be buffered as they are written.
71       * @param advertiseUri the URI to use when advertising the file
72       * @param layout The layout
73       * @param bufferSize buffer size for buffered IO
74       * @param immediateFlush true if the contents should be flushed on every write, false otherwise.
75       * @return A FileManager for the File.
76       */
77      public static FileManager getFileManager(final String fileName, final boolean append, boolean locking,
78              final boolean bufferedIo, final String advertiseUri, final Layout<? extends Serializable> layout,
79              final int bufferSize, final boolean immediateFlush) {
80  
81          if (locking && bufferedIo) {
82              locking = false;
83          }
84          return (FileManager) getManager(fileName, new FactoryData(append, locking, bufferedIo, bufferSize,
85                  immediateFlush, advertiseUri, layout), FACTORY);
86      }
87  
88      @Override
89      protected synchronized void write(final byte[] bytes, final int offset, final int length, final boolean immediateFlush)  {
90  
91          if (isLocking) {
92              final FileChannel channel = ((FileOutputStream) getOutputStream()).getChannel();
93              try {
94                  /* Lock the whole file. This could be optimized to only lock from the current file
95                     position. Note that locking may be advisory on some systems and mandatory on others,
96                     so locking just from the current position would allow reading on systems where
97                     locking is mandatory.  Also, Java 6 will throw an exception if the region of the
98                     file is already locked by another FileChannel in the same JVM. Hopefully, that will
99                     be avoided since every file should have a single file manager - unless two different
100                    files strings are configured that somehow map to the same file.*/
101                 final FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
102                 try {
103                     super.write(bytes, offset, length, immediateFlush);
104                 } finally {
105                     lock.release();
106                 }
107             } catch (final IOException ex) {
108                 throw new AppenderLoggingException("Unable to obtain lock on " + getName(), ex);
109             }
110 
111         } else {
112             super.write(bytes, offset, length, immediateFlush);
113         }
114     }
115 
116     /**
117      * Returns the name of the File being managed.
118      * @return The name of the File being managed.
119      */
120     public String getFileName() {
121         return getName();
122     }
123 
124     /**
125      * Returns the append status.
126      * @return true if the file will be appended to, false if it is overwritten.
127      */
128     public boolean isAppend() {
129         return isAppend;
130     }
131 
132     /**
133      * Returns the lock status.
134      * @return true if the file will be locked when writing, false otherwise.
135      */
136     public boolean isLocking() {
137         return isLocking;
138     }
139 
140     /**
141      * Returns the buffer size to use if the appender was configured with BufferedIO=true, otherwise returns a negative
142      * number.
143      * @return the buffer size, or a negative number if the output stream is not buffered
144      */
145     public int getBufferSize() {
146         return bufferSize;
147     }
148 
149     /**
150      * FileManager's content format is specified by: <code>Key: "fileURI" Value: provided "advertiseURI" param</code>.
151      *
152      * @return Map of content format keys supporting FileManager
153      */
154     @Override
155     public Map<String, String> getContentFormat() {
156         final Map<String, String> result = new HashMap<>(super.getContentFormat());
157         result.put("fileURI", advertiseURI);
158         return result;
159     }
160 
161     /**
162      * Factory Data.
163      */
164     private static class FactoryData {
165         private final boolean append;
166         private final boolean locking;
167         private final boolean bufferedIO;
168         private final int bufferSize;
169         private final boolean immediateFlush;
170         private final String advertiseURI;
171         private final Layout<? extends Serializable> layout;
172 
173         /**
174          * Constructor.
175          * @param append Append status.
176          * @param locking Locking status.
177          * @param bufferedIO Buffering flag.
178          * @param bufferSize Buffer size.
179          * @param immediateFlush flush on every write or not
180          * @param advertiseURI the URI to use when advertising the file
181          */
182         public FactoryData(final boolean append, final boolean locking, final boolean bufferedIO, final int bufferSize,
183                 final boolean immediateFlush, final String advertiseURI, final Layout<? extends Serializable> layout) {
184             this.append = append;
185             this.locking = locking;
186             this.bufferedIO = bufferedIO;
187             this.bufferSize = bufferSize;
188             this.immediateFlush = immediateFlush;
189             this.advertiseURI = advertiseURI;
190             this.layout = layout;
191         }
192     }
193 
194     /**
195      * Factory to create a FileManager.
196      */
197     private static class FileManagerFactory implements ManagerFactory<FileManager, FactoryData> {
198 
199         /**
200          * Create a FileManager.
201          * @param name The name of the File.
202          * @param data The FactoryData
203          * @return The FileManager for the File.
204          */
205         @Override
206         public FileManager createManager(final String name, final FactoryData data) {
207             final File file = new File(name);
208             final File parent = file.getParentFile();
209             if (null != parent && !parent.exists()) {
210                 parent.mkdirs();
211             }
212 
213             final boolean writeHeader = !data.append || !file.exists();
214             OutputStream os;
215             try {
216                 os = new FileOutputStream(name, data.append);
217                 final int actualSize = data.bufferedIO ? data.bufferSize : Constants.ENCODER_BYTE_BUFFER_SIZE;
218                 final ByteBuffer buffer = ByteBuffer.wrap(new byte[actualSize]);
219                 return new FileManager(name, os, data.append, data.locking, data.advertiseURI, data.layout,
220                         writeHeader, buffer);
221             } catch (final FileNotFoundException ex) {
222                 LOGGER.error("FileManager (" + name + ") " + ex, ex);
223             }
224             return null;
225         }
226     }
227 
228 }