1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.io.RandomAccessFile;
23 import java.io.Serializable;
24 import java.nio.ByteBuffer;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.apache.logging.log4j.core.Layout;
29
30
31
32
33
34
35 public class RandomAccessFileManager extends OutputStreamManager {
36 static final int DEFAULT_BUFFER_SIZE = 256 * 1024;
37
38 private static final RandomAccessFileManagerFactory FACTORY = new RandomAccessFileManagerFactory();
39
40 private final boolean isImmediateFlush;
41 private final String advertiseURI;
42 private final RandomAccessFile randomAccessFile;
43 private final ByteBuffer buffer;
44 private final ThreadLocal<Boolean> isEndOfBatch = new ThreadLocal<Boolean>();
45
46 protected RandomAccessFileManager(final RandomAccessFile file,
47 final String fileName, final OutputStream os,
48 final boolean immediateFlush, final int bufferSize,
49 final String advertiseURI, final Layout<? extends Serializable> layout) {
50 super(os, fileName, layout);
51 this.isImmediateFlush = immediateFlush;
52 this.randomAccessFile = file;
53 this.advertiseURI = advertiseURI;
54 this.isEndOfBatch.set(Boolean.FALSE);
55 this.buffer = ByteBuffer.allocate(bufferSize);
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public static RandomAccessFileManager getFileManager(final String fileName, final boolean append,
72 final boolean isFlush, final int bufferSize, final String advertiseURI,
73 final Layout<? extends Serializable> layout) {
74 return (RandomAccessFileManager) getManager(fileName, new FactoryData(append,
75 isFlush, bufferSize, advertiseURI, layout), FACTORY);
76 }
77
78 public Boolean isEndOfBatch() {
79 return isEndOfBatch.get();
80 }
81
82 public void setEndOfBatch(final boolean isEndOfBatch) {
83 this.isEndOfBatch.set(Boolean.valueOf(isEndOfBatch));
84 }
85
86 @Override
87 protected synchronized void write(final byte[] bytes, int offset, int length) {
88 super.write(bytes, offset, length);
89
90 int chunk = 0;
91 do {
92 if (length > buffer.remaining()) {
93 flush();
94 }
95 chunk = Math.min(length, buffer.remaining());
96 buffer.put(bytes, offset, chunk);
97 offset += chunk;
98 length -= chunk;
99 } while (length > 0);
100
101 if (isImmediateFlush || isEndOfBatch.get() == Boolean.TRUE) {
102 flush();
103 }
104 }
105
106 @Override
107 public synchronized void flush() {
108 buffer.flip();
109 try {
110 randomAccessFile.write(buffer.array(), 0, buffer.limit());
111 } catch (final IOException ex) {
112 final String msg = "Error writing to RandomAccessFile " + getName();
113 throw new AppenderLoggingException(msg, ex);
114 }
115 buffer.clear();
116 }
117
118 @Override
119 public synchronized void close() {
120 flush();
121 try {
122 randomAccessFile.close();
123 } catch (final IOException ex) {
124 LOGGER.error("Unable to close RandomAccessFile " + getName() + ". "
125 + ex);
126 }
127 }
128
129
130
131
132
133
134 public String getFileName() {
135 return getName();
136 }
137
138
139
140
141
142 public int getBufferSize() {
143 return buffer.capacity();
144 }
145
146
147 static class DummyOutputStream extends OutputStream {
148 @Override
149 public void write(final int b) throws IOException {
150 }
151
152 @Override
153 public void write(final byte[] b, final int off, final int len) throws IOException {
154 }
155 }
156
157
158
159
160
161
162
163
164 @Override
165 public Map<String, String> getContentFormat() {
166 final Map<String, String> result = new HashMap<String, String>(
167 super.getContentFormat());
168 result.put("fileURI", advertiseURI);
169 return result;
170 }
171
172
173
174
175 private static class FactoryData {
176 private final boolean append;
177 private final boolean immediateFlush;
178 private final int bufferSize;
179 private final String advertiseURI;
180 private final Layout<? extends Serializable> layout;
181
182
183
184
185
186
187
188 public FactoryData(final boolean append, final boolean immediateFlush,
189 final int bufferSize, final String advertiseURI, final Layout<? extends Serializable> layout) {
190 this.append = append;
191 this.immediateFlush = immediateFlush;
192 this.bufferSize = bufferSize;
193 this.advertiseURI = advertiseURI;
194 this.layout = layout;
195 }
196 }
197
198
199
200
201 private static class RandomAccessFileManagerFactory implements
202 ManagerFactory<RandomAccessFileManager, FactoryData> {
203
204
205
206
207
208
209
210
211 @Override
212 public RandomAccessFileManager createManager(final String name, final FactoryData data) {
213 final File file = new File(name);
214 final File parent = file.getParentFile();
215 if (null != parent && !parent.exists()) {
216 parent.mkdirs();
217 }
218 if (!data.append) {
219 file.delete();
220 }
221
222 final OutputStream os = new DummyOutputStream();
223 RandomAccessFile raf;
224 try {
225 raf = new RandomAccessFile(name, "rw");
226 if (data.append) {
227 raf.seek(raf.length());
228 } else {
229 raf.setLength(0);
230 }
231 return new RandomAccessFileManager(raf, name, os, data.immediateFlush,
232 data.bufferSize, data.advertiseURI, data.layout);
233 } catch (final Exception ex) {
234 LOGGER.error("RandomAccessFileManager (" + name + ") " + ex);
235 }
236 return null;
237 }
238 }
239
240 }