Interface FileHandler

  • All Superinterfaces:
    Loggable
    All Known Implementing Classes:
    DefaultFileHandler

    public interface FileHandler
    extends Loggable
    File operations that are performed in Cargo. All file operations must use this interface. This interface is also useful for unit testing as it's possible to create a mock implementation of it to prevent actual file operations from happening.
    • Field Detail

      • NEW_LINE

        static final String NEW_LINE
        New line character.
    • Method Detail

      • copyFile

        void copyFile​(String source,
                      String target)
        Copy a file from a source to a destination. If destination file already exists, it is not overwritten.
        Parameters:
        source - the file to copy from. Must not be null.
        target - the file to copy to. Must not be null.
      • copyFile

        void copyFile​(String source,
                      String target,
                      boolean overwrite)
        Copy a file from a source to a destination specifying if source files may overwrite newer destination files.
        Parameters:
        source - the file to copy from. Must not be null.
        target - the file to copy to. Must not be null.
        overwrite - to overwrite the file if it already exists.
      • copyFile

        void copyFile​(String source,
                      String target,
                      Map<String,​String> replacements,
                      Charset encoding)
        Copy a file from a source to a destination using token replacement.
        Parameters:
        source - the file to copy from. Must not be null
        target - the file to copy to. Must not be null
        replacements - the token replacements to use. Must not be null
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.
      • copyDirectory

        void copyDirectory​(String source,
                           String target)
        Copy a directory from a source to a destination.
        Parameters:
        source - the directory to copy from. Must not be null.
        target - the directory to copy to. Must not be null.
      • copyDirectory

        void copyDirectory​(String source,
                           String target,
                           List<String> excludes)
        Copy a directory from a source to a destination specifying files to be excluded.
        Parameters:
        source - the directory to copy from. Must not be null.
        target - the directory to copy to. Must not be null.
        excludes - the list of files to be excluded, can be null. Each entry in the list is an Ant file pattern.
      • copyDirectory

        void copyDirectory​(String source,
                           String target,
                           Map<String,​String> replacements,
                           Charset encoding)
        Copy a directory from a source to a destination using a token replacement.
        Parameters:
        source - the directory to copy from. Must not be null
        target - the directory to copy to. Must not be null
        replacements - the token replacements to use. Must not be null
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.
      • explode

        void explode​(String war,
                     String exploded)
        Extracts a war file into a directory.
        Parameters:
        war - the War archive to be extracted.
        exploded - the directory that receives files.
      • createDirectory

        String createDirectory​(String parentDir,
                               String name)
        Convenience method for creating a new directory inside another one. If the directory already exists, it will return the already created directory.
        Parameters:
        parentDir - The directory in which the new directory should be created
        name - The name of the directory to create
        Returns:
        The new directory
      • copy

        void copy​(InputStream in,
                  OutputStream out,
                  int bufSize)
        Copies data from an InputStream to an OutputStream.
        Parameters:
        in - InputStream to copy data from
        out - OutputStream to copy data to
        bufSize - size of the copy buffer
      • copy

        void copy​(InputStream in,
                  OutputStream out)
        Copies data from an InputStream to an OutputStream using a default buffer size.
        Parameters:
        in - InputStream to copy data from
        out - OutputStream to copy data to
      • replaceInFile

        void replaceInFile​(String file,
                           Map<String,​String> replacements,
                           Charset encoding)
                    throws CargoException
        Replaces using a map of replacements in a given file.
        Parameters:
        file - File to replace in.
        replacements - Map containing replacements.
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.
        Throws:
        CargoException - If anything fails, most notably if one of the replacements does not exist in the file.
      • replaceInFile

        void replaceInFile​(String file,
                           Map<String,​String> replacements,
                           Charset encoding,
                           boolean ignoreNonExistingProperties)
                    throws CargoException
        Replaces using a map of replacements in a given file.
        Parameters:
        file - File to replace in.
        replacements - Map containing replacements.
        ignoreNonExistingProperties - Whether to ignore if one of the keys cannot be found.
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.
        Throws:
        CargoException - If anything fails, most notably if one of the replacements does not exist in the file.
      • replaceInXmlFile

        void replaceInXmlFile​(XmlReplacement... xmlReplacements)
                       throws CargoException
        Replaces using a map of XML replacements in a given file.
        Parameters:
        xmlReplacements - Array of XML replacements.
        Throws:
        CargoException - If anything fails, most notably if one of the replacements does not exist in the file.
      • getTmpPath

        String getTmpPath​(String name)
        Compute the location of a temporary directory.
        Parameters:
        name - The name of the directory to compute inside the tmp directory
        Returns:
        the location of the computed temporary directory
      • createUniqueTmpDirectory

        String createUniqueTmpDirectory()
        Creates a unique temporary directory.
        Returns:
        the newly created temporary directory
      • delete

        void delete​(String path)
        Deletes a file or directory, removing any children as appropriate.
        Parameters:
        path - the path to the file or directory to remove
      • getSize

        long getSize​(String file)
        Parameters:
        file - the file for which to get the size
        Returns:
        long Size of the file, in bytes
      • getInputStream

        InputStream getInputStream​(String file)
        Parameters:
        file - the file for which to get an InputStream for
        Returns:
        an InputStream pointing to the file
      • getOutputStream

        OutputStream getOutputStream​(String file)
        Parameters:
        file - the file for which to get an OutputStream for
        Returns:
        an OutputStream pointing to the file
      • append

        String append​(String path,
                      String suffixToAppend)
        Append a suffix path to an existing path.
        Parameters:
        path - the path to append to
        suffixToAppend - the suffix to append to the path
        Returns:
        the new full path
      • mkdirs

        void mkdirs​(String path)
        Create directories for the passed path.
        Parameters:
        path - the path for which to create all directories leading to it
      • getParent

        String getParent​(String path)
        Parameters:
        path - the path for which to return its parent
        Returns:
        the parent path of the passed path
      • exists

        boolean exists​(String path)
        Parameters:
        path - the path to check the existence for
        Returns:
        true if the path exists or false otherwise
      • createFile

        void createFile​(String file)
        Create a file.
        Parameters:
        file - the file to create
      • isDirectoryEmpty

        boolean isDirectoryEmpty​(String dir)
        Parameters:
        dir - the directory to check
        Returns:
        true if the passed directory is empty
      • isDirectory

        boolean isDirectory​(String path)
        Parameters:
        path - the path to verify
        Returns:
        true if the path passed is a directory or false otherwise
      • getName

        String getName​(String file)
        Parameters:
        file - the file name for which to return the file name
        Returns:
        the file name (without path)
      • getURL

        String getURL​(String path)
        Parameters:
        path - the path for which to return the URL
        Returns:
        the URL for the passed path
      • getChildren

        String[] getChildren​(String directory)
        Parameters:
        directory - the directory for which to return all children
        Returns:
        the children of the passed directory
      • getChildren

        String[] getChildren​(String directory,
                             List<String> filters)
        Parameters:
        directory - the directory for which to return all children
        filters - any filters to apply when listing, can be null Each entry in the list is an Ant file pattern.
        Returns:
        the children of the passed directory
      • getAbsolutePath

        String getAbsolutePath​(String path)
        gets the absolute path from a file that may be relative to the current directory.
        Parameters:
        path - - what to extract the file path from
        Returns:
        - absolute path to the file
      • readTextFile

        String readTextFile​(String file,
                            Charset encoding)
        Parameters:
        file - the file for which to load into a String object.
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.
        Returns:
        a String with the file's contents.
      • writeTextFile

        void writeTextFile​(String file,
                           String content,
                           Charset encoding)
        Parameters:
        file - the file in which to write the String object.
        content - String to write into the file.
        encoding - The character encoding to use, may be null or empty to use the platform's default encoding.