- java.lang.Object
-
- net.morimekta.file.DirWatcher
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class DirWatcher extends Object implements Closeable
A dir watcher uses a native watch service and watch directories for any change. Each listener is registered to be listening to individual directories, but can be registered for more than one. The listener will then be notified of changes to the content of the directory. Including:- Files, sub-directories or symlinks being created
- Files, sub-directories or symlinks being deleted
- Files being written to. This MAY cause multiple events, as each event is triggered by file-system writes.
-
When a file is moved into or out from the directory.
Handled as a
FileEvent.CREATED
orFileEvent.DELETED
event respectively.
- The listened to directory being deleted itself.
- Changes to metadata and permissions
-
-
Constructor Summary
Constructors Modifier Constructor Description DirWatcher()
Create a FileWatcher with default watch service.DirWatcher(WatchService watchService)
Create a FileWatcher using the provided watch service.protected
DirWatcher(WatchService watchService, ExecutorService watcherExecutor, ExecutorService callbackExecutor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DirWatcher
addWatcher(Path dir, FileEventListener listener)
Start watching file path and notify watcher for updates on that file.void
close()
static FileEvent
forWatchEventKind(WatchEvent.Kind<?> kind)
boolean
removeWatcher(Path dir, FileEventListener listener)
Remove a watcher from the list of listeners.boolean
removeWatcher(FileEventListener listener)
Remove a watcher from the list of listeners.DirWatcher
weakAddWatcher(Path dir, FileEventListener listener)
Start watching file path and notify watcher for updates on that file.
-
-
-
Constructor Detail
-
DirWatcher
public DirWatcher()
Create a FileWatcher with default watch service.
-
DirWatcher
public DirWatcher(WatchService watchService)
Create a FileWatcher using the provided watch service.- Parameters:
watchService
- Watcher service to use.
-
DirWatcher
protected DirWatcher(WatchService watchService, ExecutorService watcherExecutor, ExecutorService callbackExecutor)
-
-
Method Detail
-
addWatcher
public DirWatcher addWatcher(Path dir, FileEventListener listener)
Start watching file path and notify watcher for updates on that file.- Parameters:
dir
- The file path to watch.listener
- The listener to be notified.- Returns:
- The watcher.
-
weakAddWatcher
public DirWatcher weakAddWatcher(Path dir, FileEventListener listener)
Start watching file path and notify watcher for updates on that file.- Parameters:
dir
- The file path to watch.listener
- The listener to be notified.- Returns:
- The watcher.
-
removeWatcher
public boolean removeWatcher(Path dir, FileEventListener listener)
Remove a watcher from the list of listeners.- Parameters:
dir
- The file path to no longer watch.listener
- The listener to be removed.- Returns:
- True if the listener was removed from the list.
-
removeWatcher
public boolean removeWatcher(FileEventListener listener)
Remove a watcher from the list of listeners.- Parameters:
listener
- The listener to be removed.- Returns:
- True if the listener was removed from the list.
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
forWatchEventKind
public static FileEvent forWatchEventKind(WatchEvent.Kind<?> kind)
-
-