Module xaos.core

Class DirectoryWatcher

  • All Implemented Interfaces:
    AutoCloseable

    public class DirectoryWatcher
    extends Object
    implements AutoCloseable
    Watches for changes in files and directories, and allows for standard operations on both files and directories.

    Usage:

       ExecutorService executor = Executors.newSingleThreadExecutor();
       DirectoryWatcher watcher = build(executor);
    
       watcher.events().subscribe(event -> {
         event.getEvents().stream().forEach(e -> {
           if ( StandardWatchEventKinds.ENTRY_CREATE.equals(e.kind()) ) {
             ...
           } else if ( StandardWatchEventKinds.ENTRY_DELETE.equals(e.kind()) ) {
             ...
           } else if ( StandardWatchEventKinds.ENTRY_MODIFY.equals(e.kind()) ) {
             ...
           }
         });
       });
    
       Path root = ...
    
       watcher.watch(root);
    Author:
    claudio.rosati@esss.se
    See Also:
    LiveDirsFX:org.fxmisc.livedirs.DirWatcher
    • Method Detail

      • close

        public void close()
        Shutdown this watcher.

        Note: this method will not shutdown the event thread Executor used to create this watcher.

        Specified by:
        close in interface AutoCloseable
      • createDirectories

        public void createDirectories​(Path dir,
                                      Consumer<Path> onSuccess,
                                      Consumer<Throwable> onError,
                                      FileAttribute<?>... attrs)
        Create a new directory by creating all nonexistent parent directories first. One of the two given Consumers will be called on success or on failure.
        Parameters:
        dir - The pathname of the file to be created.
        onSuccess - The Consumer called on success, where the passed parameter is the new directory Path.
        onError - The Consumer called on failure.
        attrs - An optional list of file attributes to set atomically when creating the directory.
      • createDirectory

        public void createDirectory​(Path dir,
                                    Consumer<Path> onSuccess,
                                    Consumer<Throwable> onError,
                                    FileAttribute<?>... attrs)
        Create a new directory using the given path. One of the two given Consumers will be called on success or on failure.

        The createDirectories method should be used where it is required to create all nonexistent parent directories first.

        Parameters:
        dir - The pathname of the file to be created.
        onSuccess - The Consumer called on success, where the passed parameter is the new directory Path.
        onError - The Consumer called on failure.
        attrs - An optional list of file attributes to set atomically when creating the directory.
      • deleteTree

        public void deleteTree​(Path root,
                               Consumer<Void> onSuccess,
                               Consumer<Throwable> onError)
        Deletes a file tree rooted at the given path. One of the two given Consumers will be called on success or on failure.
        Parameters:
        root - The path to the file tree root to be deleted.
        onSuccess - The Consumer called on success.
        onError - The Consumer called on failure.
      • errors

        public io.reactivex.Observable<Throwable> errors()
        Returns:
        The Subject of thrown errors.
      • isCloseComplete

        public final boolean isCloseComplete()
        Returns true if this watcher was shutdown, and the shutdown process is completed (i.e. the I/O thread is terminated).
        Returns:
        true if this watcher's shutdown completed.
      • isClosed

        public final boolean isClosed()
        Returns true if this watcher was shutdown, meaning that no elements will be posted to the errors and events streams.
        Returns:
        true if this watcher was shutdown.
      • unwatch

        public void unwatch​(Path dir)
        Unwatch the given directory Path.
        Parameters:
        dir - The directory to be unwatched.
      • unwatchUp

        public void unwatchUp​(Path dir)
        Unwatch the given directory and all its parents.
        Parameters:
        dir - The directory to be watched.
      • unwatchUp

        public void unwatchUp​(Path dir,
                              Path to)
        Unwatch the given directory and all its parents up to the given ancestor.
        Parameters:
        dir - The directory to be watched.
        to - The dir's ancestor Path (exclusive) where unwatching will be stopped. Can be null.
      • watch

        public void watch​(Path dir)
                   throws IOException
        Watch the given directory for entry create, delete, and modify events.
        Parameters:
        dir - The directory to be watched.
        Throws:
        IOException - If an I/O error occurs.
      • watchOrStreamError

        public void watchOrStreamError​(Path dir)
        Watch the given directory for entry create, delete, and modify events. If an I/O error occurs, the exception is logged (emitted).
        Parameters:
        dir - The directory to be watched.
      • watchUp

        public void watchUp​(Path dir)
                     throws IOException
        Watch the given directory and all its parents for entry create, delete, and modify events.

        This method is equivalent to calling watchUp(Path, Path) passing null to its second parameter.

        Parameters:
        dir - The directory to be watched.
        Throws:
        IOException - If an I/O error occurs.
      • watchUp

        public void watchUp​(Path dir,
                            Path to)
                     throws IOException,
                            IllegalArgumentException
        Watch the given directory and all its parents up to the given ancestor, for entry create, delete, and modify events.
        Parameters:
        dir - The directory to be watched.
        to - The dir's ancestor Path (exclusive) where watching will be stopped. Can be null.
        Throws:
        IOException - If an I/O error occurs.
        IllegalArgumentException - If to is not an ancestor of dir.
      • watchUpOrStreamError

        public void watchUpOrStreamError​(Path dir)
        Watch the given directory and all its parents for entry create, delete, and modify events. If an I/O error occurs, the exception is logged (emitted).

        This method is equivalent to calling watchUpOrStreamError(Path, Path) passing null to its second parameter.

        Parameters:
        dir - The directory to be watched.
      • watchUpOrStreamError

        public void watchUpOrStreamError​(Path dir,
                                         Path to)
        Watch the given directory and all its parents up to the given ancestor, for entry create, delete, and modify events. If an I/O error occurs, the exception is logged (emitted).
        Parameters:
        dir - The directory to be watched.
        to - The dir's ancestor Path (exclusive) where watching will be stopped. Can be null.