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.nio.channels.FileChannel;
26  import java.nio.channels.FileLock;
27  
28  
29  /**
30   * Manages actual File I/O for File Appenders.
31   */
32  public class FileManager extends OutputStreamManager {
33  
34      private static final FileManagerFactory factory = new FileManagerFactory();
35  
36      private final boolean isAppend;
37      private final boolean isLocking;
38  
39      protected FileManager(String fileName, OutputStream os, boolean append, boolean locking) {
40          super(os, fileName);
41          this.isAppend = append;
42          this.isLocking = locking;
43      }
44  
45      /**
46       * Returns the FileManager.
47       * @param fileName The name of the file to manage.
48       * @param append true if the file should be appended to, false if it should be overwritten.
49       * @param locking true if the file should be locked while writing, false otherwise.
50       * @param bufferedIO true if the contents should be buffered as they are written.
51       * @return A FileManager for the File.
52       */
53      public static FileManager getFileManager(String fileName, boolean append, boolean locking, boolean bufferedIO) {
54  
55          if (locking && bufferedIO) {
56              locking = false;
57          }
58          return (FileManager) getManager(fileName, new FactoryData(append, locking, bufferedIO), factory);
59      }
60  
61      @Override
62      protected synchronized void write(byte[] bytes, int offset, int length)  {
63  
64          if (isLocking) {
65              FileChannel channel = ((FileOutputStream) getOutputStream()).getChannel();
66              try {
67                  /* Lock the whole file. This could be optimized to only lock from the current file
68                     position. Note that locking may be advisory on some systems and mandatory on others,
69                     so locking just from the current position would allow reading on systems where
70                     locking is mandatory.  Also, Java 6 will throw an exception if the region of the
71                     file is already locked by another FileChannel in the same JVM. Hopefully, that will
72                     be avoided since every file should have a single file manager - unless two different
73                     files strings are configured that somehow map to the same file.*/
74                  FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
75                  try {
76                      super.write(bytes, offset, length);
77                  } finally {
78                      lock.release();
79                  }
80              } catch (IOException ex) {
81                  throw new AppenderRuntimeException("Unable to obtain lock on " + getName(), ex);
82              }
83  
84          } else {
85              super.write(bytes, offset, length);
86          }
87      }
88  
89      /**
90       * Returns the name of the File being managed.
91       * @return The name of the File being managed.
92       */
93      public String getFileName() {
94          return getName();
95      }
96  
97      /**
98       * Returns the append status.
99       * @return true if the file will be appended to, false if it is overwritten.
100      */
101     public boolean isAppend() {
102         return isAppend;
103     }
104 
105     /**
106      * Returns the lock status.
107      * @return true if the file will be locked when writing, false otherwise.
108      */
109     public boolean isLocking() {
110         return isLocking;
111     }
112 
113     /**
114      * Factory Data.
115      */
116     private static class FactoryData {
117         private final boolean append;
118         private final boolean locking;
119         private final boolean bufferedIO;
120 
121         /**
122          * Constructor.
123          * @param append Append status.
124          * @param locking Locking status.
125          * @param bufferedIO Buffering flag.
126          */
127         public FactoryData(boolean append, boolean locking, boolean bufferedIO) {
128             this.append = append;
129             this.locking = locking;
130             this.bufferedIO = bufferedIO;
131         }
132     }
133 
134     /**
135      * Factory to create a FileManager.
136      */
137     private static class FileManagerFactory implements ManagerFactory<FileManager, FactoryData> {
138 
139         /**
140          * Create a FileManager.
141          * @param name The name of the File.
142          * @param data The FactoryData
143          * @return The FileManager for the File.
144          */
145         public FileManager createManager(String name, FactoryData data) {
146             File file = new File(name);
147             final File parent = file.getParentFile();
148             if (null != parent && !parent.exists()) {
149                 parent.mkdirs();
150             }
151 
152             OutputStream os;
153             try {
154                 os = new FileOutputStream(name, data.append);
155                 if (data.bufferedIO) {
156                     os = new BufferedOutputStream(os);
157                 }
158                 return new FileManager(name, os, data.append, data.locking);
159             } catch (FileNotFoundException ex) {
160                 LOGGER.error("FileManager (" + name + ") " + ex);
161             }
162             return null;
163         }
164     }
165 
166 }