LockableFileWriter.java

  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. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.FileWriter;
  21. import java.io.IOException;
  22. import java.io.OutputStreamWriter;
  23. import java.io.UnsupportedEncodingException;
  24. import java.io.Writer;
  25. import java.nio.charset.Charset;
  26. import java.util.Objects;

  27. import org.apache.commons.io.Charsets;
  28. import org.apache.commons.io.FileUtils;
  29. import org.apache.commons.io.build.AbstractOrigin;
  30. import org.apache.commons.io.build.AbstractStreamBuilder;

  31. /**
  32.  * FileWriter that will create and honor lock files to allow simple cross thread file lock handling.
  33.  * <p>
  34.  * This class provides a simple alternative to {@link FileWriter} that will use a lock file to prevent duplicate writes.
  35.  * </p>
  36.  * <p>
  37.  * <strong>Note:</strong> The lock file is deleted when {@link #close()} is called - or if the main file cannot be opened initially. In the (unlikely) event
  38.  * that the lock file cannot be deleted, an exception is thrown.
  39.  * </p>
  40.  * <p>
  41.  * 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
  42.  * {@code java.io.tmpdir}. The encoding may also be specified, and defaults to the platform default.
  43.  * </p>
  44.  * <p>
  45.  * To build an instance, use {@link Builder}.
  46.  * </p>
  47.  *
  48.  * @see Builder
  49.  */
  50. public class LockableFileWriter extends Writer {

  51.     // @formatter:off
  52.     /**
  53.      * Builds a new {@link LockableFileWriter}.
  54.      *
  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.      * @see #get()
  67.      * @since 2.12.0
  68.      */
  69.     // @formatter:on
  70.     public static class Builder extends AbstractStreamBuilder<LockableFileWriter, Builder> {

  71.         private boolean append;
  72.         private AbstractOrigin<?, ?> lockDirectory = newFileOrigin(FileUtils.getTempDirectoryPath());

  73.         /**
  74.          * Constructs a new builder of {@link LockableFileWriter}.
  75.          */
  76.         public Builder() {
  77.             setBufferSizeDefault(AbstractByteArrayOutputStream.DEFAULT_SIZE);
  78.             setBufferSize(AbstractByteArrayOutputStream.DEFAULT_SIZE);
  79.         }

  80.         /**
  81.          * Constructs a new instance.
  82.          * <p>
  83.          * You must set an aspect that supports {@link File} on this builder, otherwise, this method throws an exception.
  84.          * </p>
  85.          * <p>
  86.          * This builder uses the following aspects:
  87.          * </p>
  88.          * <ul>
  89.          * <li>{@link File} is the target aspect.</li>
  90.          * <li>{@link #getCharset()}</li>
  91.          * <li>append</li>
  92.          * <li>lockDirectory</li>
  93.          * </ul>
  94.          *
  95.          * @return a new instance.
  96.          * @throws UnsupportedOperationException if the origin cannot provide a File.
  97.          * @throws IllegalStateException         if the {@code origin} is {@code null}.
  98.          * @throws IOException                   if an I/O error occurs converting to an {@link File} using {@link #getFile()}.
  99.          * @see AbstractOrigin#getFile()
  100.          * @see #getUnchecked()
  101.          */
  102.         @Override
  103.         public LockableFileWriter get() throws IOException {
  104.             return new LockableFileWriter(checkOrigin().getFile(), getCharset(), append, lockDirectory.getFile().toString());
  105.         }

  106.         /**
  107.          * Sets whether to append (true) or overwrite (false).
  108.          *
  109.          * @param append whether to append (true) or overwrite (false).
  110.          * @return {@code this} instance.
  111.          */
  112.         public Builder setAppend(final boolean append) {
  113.             this.append = append;
  114.             return this;
  115.         }

  116.         /**
  117.          * Sets the directory in which the lock file should be held.
  118.          *
  119.          * @param lockDirectory the directory in which the lock file should be held.
  120.          * @return {@code this} instance.
  121.          */
  122.         public Builder setLockDirectory(final File lockDirectory) {
  123.             this.lockDirectory = newFileOrigin(lockDirectory != null ? lockDirectory : FileUtils.getTempDirectory());
  124.             return this;
  125.         }

  126.         /**
  127.          * Sets the directory in which the lock file should be held.
  128.          *
  129.          * @param lockDirectory the directory in which the lock file should be held.
  130.          * @return {@code this} instance.
  131.          */
  132.         public Builder setLockDirectory(final String lockDirectory) {
  133.             this.lockDirectory = newFileOrigin(lockDirectory != null ? lockDirectory : FileUtils.getTempDirectoryPath());
  134.             return this;
  135.         }

  136.     }

  137.     /** The extension for the lock file. */
  138.     private static final String LCK = ".lck";

  139.     // Cannot extend ProxyWriter, as requires writer to be
  140.     // known when super() is called

  141.     /**
  142.      * Constructs a new {@link Builder}.
  143.      *
  144.      * @return a new {@link Builder}.
  145.      * @since 2.12.0
  146.      */
  147.     public static Builder builder() {
  148.         return new Builder();
  149.     }

  150.     /** The writer to decorate. */
  151.     private final Writer out;

  152.     /** The lock file. */
  153.     private final File lockFile;

  154.     /**
  155.      * Constructs a LockableFileWriter. If the file exists, it is overwritten.
  156.      *
  157.      * @param file the file to write to, not null
  158.      * @throws NullPointerException if the file is null
  159.      * @throws IOException          in case of an I/O error
  160.      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
  161.      */
  162.     @Deprecated
  163.     public LockableFileWriter(final File file) throws IOException {
  164.         this(file, false, null);
  165.     }

  166.     /**
  167.      * Constructs a LockableFileWriter.
  168.      *
  169.      * @param file   the file to write to, not null
  170.      * @param append true if content should be appended, false to overwrite
  171.      * @throws NullPointerException if the file is null
  172.      * @throws IOException          in case of an I/O error
  173.      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
  174.      */
  175.     @Deprecated
  176.     public LockableFileWriter(final File file, final boolean append) throws IOException {
  177.         this(file, append, null);
  178.     }

  179.     /**
  180.      * Constructs a LockableFileWriter.
  181.      * <p>
  182.      * The new instance uses the virtual machine's {@link Charset#defaultCharset() default charset}.
  183.      * </p>
  184.      *
  185.      * @param file    the file to write to, not null
  186.      * @param append  true if content should be appended, false to overwrite
  187.      * @param lockDir the directory in which the lock file should be held
  188.      * @throws NullPointerException if the file is null
  189.      * @throws IOException          in case of an I/O error
  190.      * @deprecated Use {@link #LockableFileWriter(File, Charset, boolean, String)} instead
  191.      */
  192.     @Deprecated
  193.     public LockableFileWriter(final File file, final boolean append, final String lockDir) throws IOException {
  194.         this(file, Charset.defaultCharset(), append, lockDir);
  195.     }

  196.     /**
  197.      * Constructs a LockableFileWriter with a file encoding.
  198.      *
  199.      * @param file    the file to write to, not null
  200.      * @param charset the charset to use, null means platform default
  201.      * @throws NullPointerException if the file is null
  202.      * @throws IOException          in case of an I/O error
  203.      * @since 2.3
  204.      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
  205.      */
  206.     @Deprecated
  207.     public LockableFileWriter(final File file, final Charset charset) throws IOException {
  208.         this(file, charset, false, null);
  209.     }

  210.     /**
  211.      * Constructs a LockableFileWriter with a file encoding.
  212.      *
  213.      * @param file    the file to write to, not null
  214.      * @param charset the name of the requested charset, null means platform default
  215.      * @param append  true if content should be appended, false to overwrite
  216.      * @param lockDir the directory in which the lock file should be held
  217.      * @throws NullPointerException if the file is null
  218.      * @throws IOException          in case of an I/O error
  219.      * @since 2.3
  220.      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
  221.      */
  222.     @Deprecated
  223.     public LockableFileWriter(final File file, final Charset charset, final boolean append, final String lockDir) throws IOException {
  224.         // init file to create/append
  225.         final File absFile = Objects.requireNonNull(file, "file").getAbsoluteFile();
  226.         if (absFile.getParentFile() != null) {
  227.             FileUtils.forceMkdir(absFile.getParentFile());
  228.         }
  229.         if (absFile.isDirectory()) {
  230.             throw new IOException("File specified is a directory");
  231.         }

  232.         // init lock file
  233.         final File lockDirFile = new File(lockDir != null ? lockDir : FileUtils.getTempDirectoryPath());
  234.         FileUtils.forceMkdir(lockDirFile);
  235.         testLockDir(lockDirFile);
  236.         lockFile = new File(lockDirFile, absFile.getName() + LCK);

  237.         // check if locked
  238.         createLock();

  239.         // init wrapped writer
  240.         out = initWriter(absFile, charset, append);
  241.     }

  242.     /**
  243.      * Constructs a LockableFileWriter with a file encoding.
  244.      *
  245.      * @param file        the file to write to, not null
  246.      * @param charsetName the name of the requested charset, null means platform default
  247.      * @throws NullPointerException                         if the file is null
  248.      * @throws IOException                                  in case of an I/O error
  249.      * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
  250.      *                                                      supported.
  251.      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
  252.      */
  253.     @Deprecated
  254.     public LockableFileWriter(final File file, final String charsetName) throws IOException {
  255.         this(file, charsetName, false, null);
  256.     }

  257.     /**
  258.      * Constructs a LockableFileWriter with a file encoding.
  259.      *
  260.      * @param file        the file to write to, not null
  261.      * @param charsetName the encoding to use, null means platform default
  262.      * @param append      true if content should be appended, false to overwrite
  263.      * @param lockDir     the directory in which the lock file should be held
  264.      * @throws NullPointerException                         if the file is null
  265.      * @throws IOException                                  in case of an I/O error
  266.      * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
  267.      *                                                      supported.
  268.      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
  269.      */
  270.     @Deprecated
  271.     public LockableFileWriter(final File file, final String charsetName, final boolean append, final String lockDir) throws IOException {
  272.         this(file, Charsets.toCharset(charsetName), append, lockDir);
  273.     }

  274.     /**
  275.      * Constructs a LockableFileWriter. If the file exists, it is overwritten.
  276.      *
  277.      * @param fileName the file to write to, not null
  278.      * @throws NullPointerException if the file is null
  279.      * @throws IOException          in case of an I/O error
  280.      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
  281.      */
  282.     @Deprecated
  283.     public LockableFileWriter(final String fileName) throws IOException {
  284.         this(fileName, false, null);
  285.     }

  286.     /**
  287.      * Constructs a LockableFileWriter.
  288.      *
  289.      * @param fileName file to write to, not null
  290.      * @param append   true if content should be appended, false to overwrite
  291.      * @throws NullPointerException if the file is null
  292.      * @throws IOException          in case of an I/O error
  293.      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
  294.      */
  295.     @Deprecated
  296.     public LockableFileWriter(final String fileName, final boolean append) throws IOException {
  297.         this(fileName, append, null);
  298.     }

  299.     /**
  300.      * Constructs a LockableFileWriter.
  301.      *
  302.      * @param fileName the file to write to, not null
  303.      * @param append   true if content should be appended, false to overwrite
  304.      * @param lockDir  the directory in which the lock file should be held
  305.      * @throws NullPointerException if the file is null
  306.      * @throws IOException          in case of an I/O error
  307.      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
  308.      */
  309.     @Deprecated
  310.     public LockableFileWriter(final String fileName, final boolean append, final String lockDir) throws IOException {
  311.         this(new File(fileName), append, lockDir);
  312.     }

  313.     /**
  314.      * Closes the file writer and deletes the lock file.
  315.      *
  316.      * @throws IOException if an I/O error occurs.
  317.      */
  318.     @Override
  319.     public void close() throws IOException {
  320.         try {
  321.             out.close();
  322.         } finally {
  323.             FileUtils.delete(lockFile);
  324.         }
  325.     }

  326.     /**
  327.      * Creates the lock file.
  328.      *
  329.      * @throws IOException if we cannot create the file
  330.      */
  331.     private void createLock() throws IOException {
  332.         synchronized (LockableFileWriter.class) {
  333.             if (!lockFile.createNewFile()) {
  334.                 throw new IOException("Can't write file, lock " + lockFile.getAbsolutePath() + " exists");
  335.             }
  336.             lockFile.deleteOnExit();
  337.         }
  338.     }

  339.     /**
  340.      * Flushes the stream.
  341.      *
  342.      * @throws IOException if an I/O error occurs.
  343.      */
  344.     @Override
  345.     public void flush() throws IOException {
  346.         out.flush();
  347.     }

  348.     /**
  349.      * Initializes the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.
  350.      *
  351.      * @param file    the file to be accessed
  352.      * @param charset the charset to use
  353.      * @param append  true to append
  354.      * @return The initialized writer
  355.      * @throws IOException if an error occurs
  356.      */
  357.     private Writer initWriter(final File file, final Charset charset, final boolean append) throws IOException {
  358.         final boolean fileExistedAlready = file.exists();
  359.         try {
  360.             return new OutputStreamWriter(new FileOutputStream(file.getAbsolutePath(), append), Charsets.toCharset(charset));

  361.         } catch (final IOException | RuntimeException ex) {
  362.             FileUtils.deleteQuietly(lockFile);
  363.             if (!fileExistedAlready) {
  364.                 FileUtils.deleteQuietly(file);
  365.             }
  366.             throw ex;
  367.         }
  368.     }

  369.     /**
  370.      * Tests that we can write to the lock directory.
  371.      *
  372.      * @param lockDir the File representing the lock directory
  373.      * @throws IOException if we cannot write to the lock directory
  374.      * @throws IOException if we cannot find the lock file
  375.      */
  376.     private void testLockDir(final File lockDir) throws IOException {
  377.         if (!lockDir.exists()) {
  378.             throw new IOException("Could not find lockDir: " + lockDir.getAbsolutePath());
  379.         }
  380.         if (!lockDir.canWrite()) {
  381.             throw new IOException("Could not write to lockDir: " + lockDir.getAbsolutePath());
  382.         }
  383.     }

  384.     /**
  385.      * Writes the characters from an array.
  386.      *
  387.      * @param cbuf the characters to write
  388.      * @throws IOException if an I/O error occurs.
  389.      */
  390.     @Override
  391.     public void write(final char[] cbuf) throws IOException {
  392.         out.write(cbuf);
  393.     }

  394.     /**
  395.      * Writes the specified characters from an array.
  396.      *
  397.      * @param cbuf the characters to write
  398.      * @param off  The start offset
  399.      * @param len  The number of characters to write
  400.      * @throws IOException if an I/O error occurs.
  401.      */
  402.     @Override
  403.     public void write(final char[] cbuf, final int off, final int len) throws IOException {
  404.         out.write(cbuf, off, len);
  405.     }

  406.     /**
  407.      * Writes a character.
  408.      *
  409.      * @param c the character to write
  410.      * @throws IOException if an I/O error occurs.
  411.      */
  412.     @Override
  413.     public void write(final int c) throws IOException {
  414.         out.write(c);
  415.     }

  416.     /**
  417.      * Writes the characters from a string.
  418.      *
  419.      * @param str the string to write
  420.      * @throws IOException if an I/O error occurs.
  421.      */
  422.     @Override
  423.     public void write(final String str) throws IOException {
  424.         out.write(str);
  425.     }

  426.     /**
  427.      * Writes the specified characters from a string.
  428.      *
  429.      * @param str the string to write
  430.      * @param off The start offset
  431.      * @param len The number of characters to write
  432.      * @throws IOException if an I/O error occurs.
  433.      */
  434.     @Override
  435.     public void write(final String str, final int off, final int len) throws IOException {
  436.         out.write(str, off, len);
  437.     }

  438. }