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 super(os, fileName, layout); 049 this.isAppend = append; 050 this.isLocking = locking; 051 this.advertiseURI = advertiseURI; 052 this.bufferSize = bufferSize; 053 } 054 055 /** 056 * Returns the FileManager. 057 * @param fileName The name of the file to manage. 058 * @param append true if the file should be appended to, false if it should be overwritten. 059 * @param locking true if the file should be locked while writing, false otherwise. 060 * @param bufferedIo true if the contents should be buffered as they are written. 061 * @param advertiseUri the URI to use when advertising the file 062 * @param layout The layout 063 * @param bufferSize buffer size for buffered IO 064 * @return A FileManager for the File. 065 */ 066 public static FileManager getFileManager(final String fileName, final boolean append, boolean locking, 067 final boolean bufferedIo, final String advertiseUri, final Layout<? extends Serializable> layout, 068 final int bufferSize) { 069 070 if (locking && bufferedIo) { 071 locking = false; 072 } 073 return (FileManager) getManager(fileName, new FactoryData(append, locking, bufferedIo, bufferSize, 074 advertiseUri, layout), FACTORY); 075 } 076 077 @Override 078 protected synchronized void write(final byte[] bytes, final int offset, final int length) { 079 080 if (isLocking) { 081 final FileChannel channel = ((FileOutputStream) getOutputStream()).getChannel(); 082 try { 083 /* Lock the whole file. This could be optimized to only lock from the current file 084 position. Note that locking may be advisory on some systems and mandatory on others, 085 so locking just from the current position would allow reading on systems where 086 locking is mandatory. Also, Java 6 will throw an exception if the region of the 087 file is already locked by another FileChannel in the same JVM. Hopefully, that will 088 be avoided since every file should have a single file manager - unless two different 089 files strings are configured that somehow map to the same file.*/ 090 final FileLock lock = channel.lock(0, Long.MAX_VALUE, false); 091 try { 092 super.write(bytes, offset, length); 093 } finally { 094 lock.release(); 095 } 096 } catch (final IOException ex) { 097 throw new AppenderLoggingException("Unable to obtain lock on " + getName(), ex); 098 } 099 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}