• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.commons.io.output;
18 
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.io.Writer;
25 import java.nio.charset.Charset;
26 import java.util.Objects;
27 
28 import org.apache.commons.io.Charsets;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.io.build.AbstractOrigin;
31 import org.apache.commons.io.build.AbstractOriginSupplier;
32 import org.apache.commons.io.build.AbstractStreamBuilder;
33 
34 /**
35  * FileWriter that will create and honor lock files to allow simple cross thread file lock handling.
36  * <p>
37  * This class provides a simple alternative to {@link FileWriter} that will use a lock file to prevent duplicate writes.
38  * </p>
39  * <p>
40  * <b>Note:</b> The lock file is deleted when {@link #close()} is called - or if the main file cannot be opened initially. In the (unlikely) event that the lock
41  * file cannot be deleted, an exception is thrown.
42  * </p>
43  * <p>
44  * By default, the file will be overwritten, but this may be changed to append. The lock directory may be specified, but defaults to the system property
45  * {@code java.io.tmpdir}. The encoding may also be specified, and defaults to the platform default.
46  * </p>
47  * <p>
48  * To build an instance, see {@link Builder}.
49  * </p>
50  */
51 public class LockableFileWriter extends Writer {
52 
53     /**
54      * Builds a new {@link LockableFileWriter} instance.
55      * <p>
56      * Using a CharsetEncoder:
57      * </p>
58      * <pre>{@code
59      * LockableFileWriter w = LockableFileWriter.builder()
60      *   .setPath(path)
61      *   .setAppend(false)
62      *   .setLockDirectory("Some/Directory")
63      *   .get();}
64      * </pre>
65      *
66      * @since 2.12.0
67      */
68     public static class Builder extends AbstractStreamBuilder<LockableFileWriter, Builder> {
69 
70         private boolean append;
71         private AbstractOrigin<?, ?> lockDirectory = AbstractOriginSupplier.newFileOrigin(FileUtils.getTempDirectoryPath());
72 
73         /**
74          * Constructs a new Builder.
75          */
Builder()76         public Builder() {
77             setBufferSizeDefault(AbstractByteArrayOutputStream.DEFAULT_SIZE);
78             setBufferSize(AbstractByteArrayOutputStream.DEFAULT_SIZE);
79         }
80 
81         /**
82          * Constructs a new instance.
83          * <p>
84          * This builder use the aspects File, Charset, append, and lockDirectory.
85          * </p>
86          * <p>
87          * You must provide an origin that can be converted to a File by this builder, otherwise, this call will throw an
88          * {@link UnsupportedOperationException}.
89          * </p>
90          *
91          * @return a new instance.
92          * @throws UnsupportedOperationException if the origin cannot provide a File.
93          * @throws IllegalStateException if the {@code origin} is {@code null}.
94          * @see AbstractOrigin#getFile()
95          */
96         @Override
get()97         public LockableFileWriter get() throws IOException {
98             return new LockableFileWriter(checkOrigin().getFile(), getCharset(), append, lockDirectory.getFile().toString());
99         }
100 
101         /**
102          * Sets whether to append (true) or overwrite (false).
103          *
104          * @param append whether to append (true) or overwrite (false).
105          * @return this
106          */
setAppend(final boolean append)107         public Builder setAppend(final boolean append) {
108             this.append = append;
109             return this;
110         }
111 
112         /**
113          * Sets the directory in which the lock file should be held.
114          *
115          * @param lockDirectory the directory in which the lock file should be held.
116          * @return this
117          */
setLockDirectory(final File lockDirectory)118         public Builder setLockDirectory(final File lockDirectory) {
119             this.lockDirectory = AbstractOriginSupplier.newFileOrigin(lockDirectory != null ? lockDirectory : FileUtils.getTempDirectory());
120             return this;
121         }
122 
123         /**
124          * Sets the directory in which the lock file should be held.
125          *
126          * @param lockDirectory the directory in which the lock file should be held.
127          * @return this
128          */
setLockDirectory(final String lockDirectory)129         public Builder setLockDirectory(final String lockDirectory) {
130             this.lockDirectory = AbstractOriginSupplier.newFileOrigin(lockDirectory != null ? lockDirectory : FileUtils.getTempDirectoryPath());
131             return this;
132         }
133 
134     }
135 
136     /** The extension for the lock file. */
137     private static final String LCK = ".lck";
138 
139     // Cannot extend ProxyWriter, as requires writer to be
140     // known when super() is called
141 
142     /**
143      * Constructs a new {@link Builder}.
144      *
145      * @return a new {@link Builder}.
146      * @since 2.12.0
147      */
builder()148     public static Builder builder() {
149         return new Builder();
150     }
151 
152     /** The writer to decorate. */
153     private final Writer out;
154 
155     /** The lock file. */
156     private final File lockFile;
157 
158     /**
159      * Constructs a LockableFileWriter. If the file exists, it is overwritten.
160      *
161      * @param file the file to write to, not null
162      * @throws NullPointerException if the file is null
163      * @throws IOException          in case of an I/O error
164      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
165      */
166     @Deprecated
LockableFileWriter(final File file)167     public LockableFileWriter(final File file) throws IOException {
168         this(file, false, null);
169     }
170 
171     /**
172      * Constructs a LockableFileWriter.
173      *
174      * @param file   the file to write to, not null
175      * @param append true if content should be appended, false to overwrite
176      * @throws NullPointerException if the file is null
177      * @throws IOException          in case of an I/O error
178      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
179      */
180     @Deprecated
LockableFileWriter(final File file, final boolean append)181     public LockableFileWriter(final File file, final boolean append) throws IOException {
182         this(file, append, null);
183     }
184 
185     /**
186      * Constructs a LockableFileWriter.
187      *
188      * @param file    the file to write to, not null
189      * @param append  true if content should be appended, false to overwrite
190      * @param lockDir the directory in which the lock file should be held
191      * @throws NullPointerException if the file is null
192      * @throws IOException          in case of an I/O error
193      * @deprecated 2.5 use {@link #LockableFileWriter(File, Charset, boolean, String)} instead
194      */
195     @Deprecated
LockableFileWriter(final File file, final boolean append, final String lockDir)196     public LockableFileWriter(final File file, final boolean append, final String lockDir) throws IOException {
197         this(file, Charset.defaultCharset(), append, lockDir);
198     }
199 
200     /**
201      * Constructs a LockableFileWriter with a file encoding.
202      *
203      * @param file    the file to write to, not null
204      * @param charset the charset to use, null means platform default
205      * @throws NullPointerException if the file is null
206      * @throws IOException          in case of an I/O error
207      * @since 2.3
208      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
209      */
210     @Deprecated
LockableFileWriter(final File file, final Charset charset)211     public LockableFileWriter(final File file, final Charset charset) throws IOException {
212         this(file, charset, false, null);
213     }
214 
215     /**
216      * Constructs a LockableFileWriter with a file encoding.
217      *
218      * @param file    the file to write to, not null
219      * @param charset the name of the requested charset, null means platform default
220      * @param append  true if content should be appended, false to overwrite
221      * @param lockDir the directory in which the lock file should be held
222      * @throws NullPointerException if the file is null
223      * @throws IOException          in case of an I/O error
224      * @since 2.3
225      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
226      */
227     @Deprecated
LockableFileWriter(final File file, final Charset charset, final boolean append, final String lockDir)228     public LockableFileWriter(final File file, final Charset charset, final boolean append, final String lockDir) throws IOException {
229         // init file to create/append
230         final File absFile = Objects.requireNonNull(file, "file").getAbsoluteFile();
231         if (absFile.getParentFile() != null) {
232             FileUtils.forceMkdir(absFile.getParentFile());
233         }
234         if (absFile.isDirectory()) {
235             throw new IOException("File specified is a directory");
236         }
237 
238         // init lock file
239         final File lockDirFile = new File(lockDir != null ? lockDir : FileUtils.getTempDirectoryPath());
240         FileUtils.forceMkdir(lockDirFile);
241         testLockDir(lockDirFile);
242         lockFile = new File(lockDirFile, absFile.getName() + LCK);
243 
244         // check if locked
245         createLock();
246 
247         // init wrapped writer
248         out = initWriter(absFile, charset, append);
249     }
250 
251     /**
252      * Constructs a LockableFileWriter with a file encoding.
253      *
254      * @param file        the file to write to, not null
255      * @param charsetName the name of the requested charset, null means platform default
256      * @throws NullPointerException                         if the file is null
257      * @throws IOException                                  in case of an I/O error
258      * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io.UnsupportedEncodingException} in version 2.2 if the encoding is not
259      *                                                      supported.
260      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
261      */
262     @Deprecated
LockableFileWriter(final File file, final String charsetName)263     public LockableFileWriter(final File file, final String charsetName) throws IOException {
264         this(file, charsetName, false, null);
265     }
266 
267     /**
268      * Constructs a LockableFileWriter with a file encoding.
269      *
270      * @param file        the file to write to, not null
271      * @param charsetName the encoding to use, null means platform default
272      * @param append      true if content should be appended, false to overwrite
273      * @param lockDir     the directory in which the lock file should be held
274      * @throws NullPointerException                         if the file is null
275      * @throws IOException                                  in case of an I/O error
276      * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io.UnsupportedEncodingException} in version 2.2 if the encoding is not
277      *                                                      supported.
278      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
279      */
280     @Deprecated
LockableFileWriter(final File file, final String charsetName, final boolean append, final String lockDir)281     public LockableFileWriter(final File file, final String charsetName, final boolean append, final String lockDir) throws IOException {
282         this(file, Charsets.toCharset(charsetName), append, lockDir);
283     }
284 
285     /**
286      * Constructs a LockableFileWriter. If the file exists, it is overwritten.
287      *
288      * @param fileName the file to write to, not null
289      * @throws NullPointerException if the file is null
290      * @throws IOException          in case of an I/O error
291      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
292      */
293     @Deprecated
LockableFileWriter(final String fileName)294     public LockableFileWriter(final String fileName) throws IOException {
295         this(fileName, false, null);
296     }
297 
298     /**
299      * Constructs a LockableFileWriter.
300      *
301      * @param fileName file to write to, not null
302      * @param append   true if content should be appended, false to overwrite
303      * @throws NullPointerException if the file is null
304      * @throws IOException          in case of an I/O error
305      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
306      */
307     @Deprecated
LockableFileWriter(final String fileName, final boolean append)308     public LockableFileWriter(final String fileName, final boolean append) throws IOException {
309         this(fileName, append, null);
310     }
311 
312     /**
313      * Constructs a LockableFileWriter.
314      *
315      * @param fileName the file to write to, not null
316      * @param append   true if content should be appended, false to overwrite
317      * @param lockDir  the directory in which the lock file should be held
318      * @throws NullPointerException if the file is null
319      * @throws IOException          in case of an I/O error
320      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
321      */
322     @Deprecated
LockableFileWriter(final String fileName, final boolean append, final String lockDir)323     public LockableFileWriter(final String fileName, final boolean append, final String lockDir) throws IOException {
324         this(new File(fileName), append, lockDir);
325     }
326 
327     /**
328      * Closes the file writer and deletes the lock file.
329      *
330      * @throws IOException if an I/O error occurs.
331      */
332     @Override
close()333     public void close() throws IOException {
334         try {
335             out.close();
336         } finally {
337             FileUtils.delete(lockFile);
338         }
339     }
340 
341     /**
342      * Creates the lock file.
343      *
344      * @throws IOException if we cannot create the file
345      */
createLock()346     private void createLock() throws IOException {
347         synchronized (LockableFileWriter.class) {
348             if (!lockFile.createNewFile()) {
349                 throw new IOException("Can't write file, lock " + lockFile.getAbsolutePath() + " exists");
350             }
351             lockFile.deleteOnExit();
352         }
353     }
354 
355     /**
356      * Flushes the stream.
357      *
358      * @throws IOException if an I/O error occurs.
359      */
360     @Override
flush()361     public void flush() throws IOException {
362         out.flush();
363     }
364 
365     /**
366      * Initializes the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.
367      *
368      * @param file    the file to be accessed
369      * @param charset the charset to use
370      * @param append  true to append
371      * @return The initialized writer
372      * @throws IOException if an error occurs
373      */
initWriter(final File file, final Charset charset, final boolean append)374     private Writer initWriter(final File file, final Charset charset, final boolean append) throws IOException {
375         final boolean fileExistedAlready = file.exists();
376         try {
377             return new OutputStreamWriter(new FileOutputStream(file.getAbsolutePath(), append), Charsets.toCharset(charset));
378 
379         } catch (final IOException | RuntimeException ex) {
380             FileUtils.deleteQuietly(lockFile);
381             if (!fileExistedAlready) {
382                 FileUtils.deleteQuietly(file);
383             }
384             throw ex;
385         }
386     }
387 
388     /**
389      * Tests that we can write to the lock directory.
390      *
391      * @param lockDir the File representing the lock directory
392      * @throws IOException if we cannot write to the lock directory
393      * @throws IOException if we cannot find the lock file
394      */
testLockDir(final File lockDir)395     private void testLockDir(final File lockDir) throws IOException {
396         if (!lockDir.exists()) {
397             throw new IOException("Could not find lockDir: " + lockDir.getAbsolutePath());
398         }
399         if (!lockDir.canWrite()) {
400             throw new IOException("Could not write to lockDir: " + lockDir.getAbsolutePath());
401         }
402     }
403 
404     /**
405      * Writes the characters from an array.
406      *
407      * @param cbuf the characters to write
408      * @throws IOException if an I/O error occurs.
409      */
410     @Override
write(final char[] cbuf)411     public void write(final char[] cbuf) throws IOException {
412         out.write(cbuf);
413     }
414 
415     /**
416      * Writes the specified characters from an array.
417      *
418      * @param cbuf the characters to write
419      * @param off  The start offset
420      * @param len  The number of characters to write
421      * @throws IOException if an I/O error occurs.
422      */
423     @Override
write(final char[] cbuf, final int off, final int len)424     public void write(final char[] cbuf, final int off, final int len) throws IOException {
425         out.write(cbuf, off, len);
426     }
427 
428     /**
429      * Writes a character.
430      *
431      * @param c the character to write
432      * @throws IOException if an I/O error occurs.
433      */
434     @Override
write(final int c)435     public void write(final int c) throws IOException {
436         out.write(c);
437     }
438 
439     /**
440      * Writes the characters from a string.
441      *
442      * @param str the string to write
443      * @throws IOException if an I/O error occurs.
444      */
445     @Override
write(final String str)446     public void write(final String str) throws IOException {
447         out.write(str);
448     }
449 
450     /**
451      * Writes the specified characters from a string.
452      *
453      * @param str the string to write
454      * @param off The start offset
455      * @param len The number of characters to write
456      * @throws IOException if an I/O error occurs.
457      */
458     @Override
write(final String str, final int off, final int len)459     public void write(final String str, final int off, final int len) throws IOException {
460         out.write(str, off, len);
461     }
462 
463 }
464