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.BufferedOutputStream;
20  import java.io.File;
21  import java.io.FileNotFoundException;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.io.Serializable;
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  
33  
34  /**
35   * Manages actual File I/O for File Appenders.
36   */
37  public class FileManager extends OutputStreamManager {
38  
39      private static final FileManagerFactory FACTORY = new FileManagerFactory();
40  
41      private final boolean isAppend;
42      private final boolean isLocking;
43      private final String advertiseURI;
44      private final int bufferSize;
45  
46      protected FileManager(final String fileName, final OutputStream os, final boolean append, final boolean locking,
47              final String advertiseURI, final Layout<? extends Serializable> layout, final int bufferSize) {
48          super(os, fileName, layout);
49          this.isAppend = append;
50          this.isLocking = locking;
51          this.advertiseURI = advertiseURI;
52          this.bufferSize = bufferSize;
53      }
54  
55      /**
56       * Returns the FileManager.
57       * @param fileName The name of the file to manage.
58       * @param append true if the file should be appended to, false if it should be overwritten.
59       * @param locking true if the file should be locked while writing, false otherwise.
60       * @param bufferedIo true if the contents should be buffered as they are written.
61       * @param advertiseUri the URI to use when advertising the file
62       * @param layout The layout
63       * @param bufferSize buffer size for buffered IO
64       * @return A FileManager for the File.
65       */
66      public static FileManager getFileManager(final String fileName, final boolean append, boolean locking,
67              final boolean bufferedIo, final String advertiseUri, final Layout<? extends Serializable> layout,
68              final int bufferSize) {
69  
70          if (locking && bufferedIo) {
71              locking = false;
72          }
73          return (FileManager) getManager(fileName, new FactoryData(append, locking, bufferedIo, bufferSize,
74                  advertiseUri, layout), FACTORY);
75      }
76  
77      @Override
78      protected synchronized void write(final byte[] bytes, final int offset, final int length)  {
79  
80          if (isLocking) {
81              final FileChannel channel = ((FileOutputStream) getOutputStream()).getChannel();
82              try {
83                  /* Lock the whole file. This could be optimized to only lock from the current file
84                     position. Note that locking may be advisory on some systems and mandatory on others,
85                     so locking just from the current position would allow reading on systems where
86                     locking is mandatory.  Also, Java 6 will throw an exception if the region of the
87                     file is already locked by another FileChannel in the same JVM. Hopefully, that will
88                     be avoided since every file should have a single file manager - unless two different
89                     files strings are configured that somehow map to the same file.*/
90                  final FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
91                  try {
92                      super.write(bytes, offset, length);
93                  } finally {
94                      lock.release();
95                  }
96              } catch (final IOException ex) {
97                  throw new AppenderLoggingException("Unable to obtain lock on " + getName(), ex);
98              }
99  
100         } else {
101             super.write(bytes, offset, length);
102         }
103     }
104 
105     /**
106      * Returns the name of the File being managed.
107      * @return The name of the File being managed.
108      */
109     public String getFileName() {
110         return getName();
111     }
112 
113     /**
114      * Returns the append status.
115      * @return true if the file will be appended to, false if it is overwritten.
116      */
117     public boolean isAppend() {
118         return isAppend;
119     }
120 
121     /**
122      * Returns the lock status.
123      * @return true if the file will be locked when writing, false otherwise.
124      */
125     public boolean isLocking() {
126         return isLocking;
127     }
128     
129     /**
130      * Returns the buffer size to use if the appender was configured with BufferedIO=true, otherwise returns a negative
131      * number.
132      * @return the buffer size, or a negative number if the output stream is not buffered
133      */
134     public int getBufferSize() {
135         return bufferSize;
136     }
137 
138     /**
139      * FileManager's content format is specified by: <code>Key: "fileURI" Value: provided "advertiseURI" param</code>.
140      *
141      * @return Map of content format keys supporting FileManager
142      */
143     @Override
144     public Map<String, String> getContentFormat() {
145         final Map<String, String> result = new HashMap<String, String>(super.getContentFormat());
146         result.put("fileURI", advertiseURI);
147         return result;
148     }
149 
150     /**
151      * Factory Data.
152      */
153     private static class FactoryData {
154         private final boolean append;
155         private final boolean locking;
156         private final boolean bufferedIO;
157         private final int bufferSize;
158         private final String advertiseURI;
159         private final Layout<? extends Serializable> layout;
160 
161         /**
162          * Constructor.
163          * @param append Append status.
164          * @param locking Locking status.
165          * @param bufferedIO Buffering flag.
166          * @param bufferSize Buffer size.
167          * @param advertiseURI the URI to use when advertising the file
168          */
169         public FactoryData(final boolean append, final boolean locking, final boolean bufferedIO, final int bufferSize,
170                 final String advertiseURI, final Layout<? extends Serializable> layout) {
171             this.append = append;
172             this.locking = locking;
173             this.bufferedIO = bufferedIO;
174             this.bufferSize = bufferSize;
175             this.advertiseURI = advertiseURI;
176             this.layout = layout;
177         }
178     }
179 
180     /**
181      * Factory to create a FileManager.
182      */
183     private static class FileManagerFactory implements ManagerFactory<FileManager, FactoryData> {
184 
185         /**
186          * Create a FileManager.
187          * @param name The name of the File.
188          * @param data The FactoryData
189          * @return The FileManager for the File.
190          */
191         @Override
192         public FileManager createManager(final String name, final FactoryData data) {
193             final File file = new File(name);
194             final File parent = file.getParentFile();
195             if (null != parent && !parent.exists()) {
196                 parent.mkdirs();
197             }
198 
199             OutputStream os;
200             try {
201                 os = new FileOutputStream(name, data.append);
202                 int bufferSize = data.bufferSize;
203                 if (data.bufferedIO) {
204                     os = new BufferedOutputStream(os, bufferSize);
205                 } else {
206                     bufferSize = -1; // signals to RollingFileManager not to use BufferedOutputStream
207                 }
208                 return new FileManager(name, os, data.append, data.locking, data.advertiseURI, data.layout, bufferSize);
209             } catch (final FileNotFoundException ex) {
210                 LOGGER.error("FileManager (" + name + ") " + ex);
211             }
212             return null;
213         }
214     }
215 
216 }