Class PathProcessorSupport

java.lang.Object
org.eclipse.aether.spi.io.PathProcessorSupport
All Implemented Interfaces:
PathProcessor
Direct Known Subclasses:
DefaultPathProcessor, TestPathProcessor

public class PathProcessorSupport extends Object implements PathProcessor
Utility class serving as base of PathProcessor implementations. This class can be extended or replaced (as component) when needed. Also, this class is published in Resolver implementation for path processor interface.
Since:
2.0.13
  • Field Details

    • IS_WINDOWS

      protected static final boolean IS_WINDOWS
      Logic borrowed from Commons-Lang3: we really need only this, to decide do we NIO2 file ops or not. On Windows the final move needs retry/staging logic, see retryingMove(Path, Path, StandardCopyOption[]).
    • ATOMIC_MOVE

      protected static final boolean ATOMIC_MOVE
      Escape hatch if atomic move is not desired on system we run on.
    • WINDOWS_MOVE_ATTEMPTS

      protected static final int WINDOWS_MOVE_ATTEMPTS
      The number of attempts for the final move on Windows, where the move target may be transiently locked by a virus scanner, an indexer or a concurrent reader.
    • WINDOWS_MOVE_RETRY_DELAY

      protected static final long WINDOWS_MOVE_RETRY_DELAY
      The delay in milliseconds applied between the move attempts on Windows.
  • Constructor Details

  • Method Details

    • setLastModified

      public boolean setLastModified(Path path, long value) throws IOException
      Description copied from interface: PathProcessor
      Sets last modified of path in milliseconds, if exists.
      Specified by:
      setLastModified in interface PathProcessor
      Parameters:
      path - The path, may be null.
      Throws:
      IOException - If an I/O error occurs. Some exceptions/reasons of failure to set mtime may be swallowed, and can be multiple, ranging from "file not found" to cases when FS does not support the setting the mtime.
    • write

      public void write(Path target, String data) throws IOException
      Description copied from interface: PathProcessor
      Writes the given data to a file. UTF-8 is assumed as encoding for the data. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
      Specified by:
      write in interface PathProcessor
      Parameters:
      target - The file to write to, must not be null. This file will be overwritten.
      data - The data to write, may be null.
      Throws:
      IOException - If an I/O error occurs.
    • write

      public void write(Path target, InputStream source) throws IOException
      Description copied from interface: PathProcessor
      Writes the given stream to a file. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
      Specified by:
      write in interface PathProcessor
      Parameters:
      target - The file to write to, must not be null. This file will be overwritten.
      source - The stream to write to the file, must not be null.
      Throws:
      IOException - If an I/O error occurs.
    • writeWithBackup

      public void writeWithBackup(Path target, String data) throws IOException
      Description copied from interface: PathProcessor
      Writes the given data to a file. UTF-8 is assumed as encoding for the data. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
      Specified by:
      writeWithBackup in interface PathProcessor
      Parameters:
      target - The file to write to, must not be null. This file will be overwritten.
      data - The data to write, may be null.
      Throws:
      IOException - If an I/O error occurs.
    • writeWithBackup

      public void writeWithBackup(Path target, InputStream source) throws IOException
      Description copied from interface: PathProcessor
      Writes the given stream to a file. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
      Specified by:
      writeWithBackup in interface PathProcessor
      Parameters:
      target - The file to write to, must not be null. This file will be overwritten.
      source - The stream to write to the file, must not be null.
      Throws:
      IOException - If an I/O error occurs.
    • writeFile

      public void writeFile(Path target, PathProcessorSupport.FileWriter writer, boolean doBackup) throws IOException
      Utility method to write out file to disk in "atomic" manner, with optional backups (".bak") if needed. This ensures that no other thread or process will be able to read not fully written files. Finally, this method may create the needed parent directories, if the passed in target parents does not exist.
      Parameters:
      target - that is the target file (must be an existing or non-existing file, the path must have parent)
      writer - the writer that will accept a Path to write content to
      doBackup - if true, and target file is about to be overwritten, a ".bak" file with old contents will be created/overwritten
      Throws:
      IOException - if at any step IO problem occurs
    • copy

      public long copy(Path source, Path target, PathProcessor.ProgressListener listener) throws IOException
      Description copied from interface: PathProcessor
      Copies the specified source file to the given target file. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
      Specified by:
      copy in interface PathProcessor
      Parameters:
      source - The file to copy from, must not be null.
      target - The file to copy to, must not be null.
      listener - The listener to notify about the copy progress, may be null.
      Returns:
      The number of copied bytes.
      Throws:
      IOException - If an I/O error occurs.
    • move

      public void move(Path source, Path target) throws IOException
      Description copied from interface: PathProcessor
      Moves the specified source file to the given target file. If the target file already exists, it is overwritten. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
      Specified by:
      move in interface PathProcessor
      Parameters:
      source - The file to move from, must not be null.
      target - The file to move to, must not be null.
      Throws:
      IOException - If an I/O error occurs.
    • newTempFile

      Description copied from interface: PathProcessor
      Creates a PathProcessor.TempFile instance and backing temporary file on file system. It will be located in the default temporary-file directory. Returned instance should be handled in try-with-resource construct and created temp file is removed (if exists) when returned instance is closed.

      This method uses Files.createTempFile(String, String, java.nio.file.attribute.FileAttribute[]) to create the temporary file on file system.

      Specified by:
      newTempFile in interface PathProcessor
      Throws:
      IOException
    • newTempFile

      Description copied from interface: PathProcessor
      Creates a PathProcessor.CollocatedTempFile instance for given file without backing file. The path will be located in same directory where given file is, and will reuse its name for generated (randomized) name. Returned instance should be handled in try-with-resource and created temp path is removed (if exists) when returned instance is closed. The PathProcessor.CollocatedTempFile.move() makes possible to atomically replace passed in file with the processed content written into a file backing the PathProcessor.CollocatedTempFile instance.

      The file nor it's parent directories have to exist. The parent directories are created if needed.

      This method uses Path.resolve(String) to create the temporary file path in passed in file parent directory, but it does NOT create backing file on file system.

      Specified by:
      newTempFile in interface PathProcessor
      Throws:
      IOException
    • retryingMove

      protected void retryingMove(Path source, Path target, StandardCopyOption[] copyOptions) throws IOException
      Moves the source file to the target path without ever truncating the target in place: the content visible at the target path is either the old file or the complete new file, never a partially written one.

      Historically, on Windows the final move was implemented by opening the target path with a truncating stream and copying the source into it (classicCopy(Path, Path)). That left a window during which a concurrent reader (for example a forked JVM resolving the same artifact) or an untimely process kill would observe, or durably leave behind, a truncated file at the final path - after checksum validation has already happened, so the torn content would from then on be trusted as verified. Instead, this method attempts the rename a bounded number of times (WINDOWS_MOVE_ATTEMPTS, file locking by virus scanners, indexers or concurrent readers is transient on Windows), and if the file system refuses an atomic move (for example when source and target are on different stores), the source is first staged into a collocated temporary file next to the target and then renamed into place - the target path itself is never opened for writing.

      Throws:
      IOException
    • fileSystemMove

      protected void fileSystemMove(Path source, Path target, StandardCopyOption... copyOptions) throws IOException
      Throws:
      IOException
    • classicCopy

      protected void classicCopy(Path source, Path target) throws IOException
      Pre-NIO2 way to copy files. Important: this method must never be pointed at a "final" (published) path, as it opens the target with truncation; callers stage into a temporary file and rename into place instead.
      Throws:
      IOException