Package net.morimekta.util
Class FileWatcher
- java.lang.Object
-
- net.morimekta.util.FileWatcher
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class FileWatcher extends java.lang.Object implements java.lang.AutoCloseable
File watcher helper for use with simple callbacks. It monitors two types of changes: Actual file changes, and symlink changes. Note that whenever a file or symlink is added to the whole, it will be monitored at least until the file or symlink is deleted. There are essentially two types of watchers. File specific watchers will always resolve back to the "requested" file, and it is triggered whenever any of:- The current canonical file content changed.
- The link starts pointing to a different file, this is only calculated based on the file being a symlink, or being in a symlinked directory. Everything else is treated as canonical locations.
So if this is the case (ref configMaps in kubernetes):
If you listen to '/volume/map/config.txt', then you are notified if:/volume/map/..2018-01/config.txt (old file) /volume/map/..2018-02/config.txt (new file) /volume/map/..data -> symlink to '..2018-01' /volume/map/config.txt -> symlink to '..data/config.txt'
- '/volume/map/..2018-01/config.txt' is changed
- '/volume/map/..data' symlink is updated to '..2018-02'
- '/volume/map/config.txt' symlink is updated to '..2018-02/config.txt'
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
FileWatcher.Listener
static interface
FileWatcher.Watcher
Deprecated.UseFileWatcher.Listener
instead.
-
Constructor Summary
Constructors Modifier Constructor Description FileWatcher()
Create a FileWatcher with default watch service.FileWatcher(java.nio.file.WatchService watchService)
Create a FileWatcher using the provided watch service.protected
FileWatcher(java.nio.file.WatchService watchService, java.util.concurrent.ExecutorService watcherExecutor, java.util.concurrent.ExecutorService callbackExecutor)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addWatcher(java.io.File file, FileWatcher.Listener watcher)
Deprecated.void
addWatcher(java.io.File file, FileWatcher.Watcher watcher)
Deprecated.void
addWatcher(java.nio.file.Path file, FileWatcher.Listener watcher)
Start watching file path and notify watcher for updates on that file.void
addWatcher(java.nio.file.Path file, FileWatcher.Watcher watcher)
Deprecated.void
addWatcher(FileWatcher.Listener watcher)
Deprecated.void
addWatcher(FileWatcher.Watcher watcher)
Deprecated.void
close()
boolean
removeWatcher(FileWatcher.Listener watcher)
Remove a watcher from the list of listeners.boolean
removeWatcher(FileWatcher.Watcher watcher)
Deprecated.void
startWatching(java.io.File file)
Deprecated.void
stopWatching(java.io.File file)
Deprecated.UseremoveWatcher(Listener)
and let it clean up watched files itself.void
weakAddWatcher(java.io.File file, FileWatcher.Listener watcher)
Deprecated.void
weakAddWatcher(java.io.File file, FileWatcher.Watcher watcher)
Deprecated.void
weakAddWatcher(java.nio.file.Path file, FileWatcher.Listener watcher)
Start watching file path and notify watcher for updates on that file.void
weakAddWatcher(java.nio.file.Path file, FileWatcher.Watcher watcher)
Deprecated.void
weakAddWatcher(FileWatcher.Listener watcher)
Deprecated.void
weakAddWatcher(FileWatcher.Watcher watcher)
Deprecated.
-
-
-
Constructor Detail
-
FileWatcher
public FileWatcher()
Create a FileWatcher with default watch service.
-
FileWatcher
public FileWatcher(java.nio.file.WatchService watchService)
Create a FileWatcher using the provided watch service.- Parameters:
watchService
- Watcher service to use.
-
FileWatcher
protected FileWatcher(java.nio.file.WatchService watchService, java.util.concurrent.ExecutorService watcherExecutor, java.util.concurrent.ExecutorService callbackExecutor)
-
-
Method Detail
-
addWatcher
@Deprecated public void addWatcher(java.io.File file, FileWatcher.Watcher watcher)
Deprecated.Start watching file path and notify watcher for updates on that file.- Parameters:
file
- The file path to watch.watcher
- The watcher to be notified.
-
addWatcher
@Deprecated public void addWatcher(java.io.File file, FileWatcher.Listener watcher)
Deprecated.Start watching file path and notify watcher for updates on that file.- Parameters:
file
- The file path to watch.watcher
- The watcher to be notified.
-
addWatcher
@Deprecated public void addWatcher(java.nio.file.Path file, FileWatcher.Watcher watcher)
Deprecated.Start watching file path and notify watcher for updates on that file.- Parameters:
file
- The file path to watch.watcher
- The watcher to be notified.
-
addWatcher
public void addWatcher(java.nio.file.Path file, FileWatcher.Listener watcher)
Start watching file path and notify watcher for updates on that file.- Parameters:
file
- The file path to watch.watcher
- The watcher to be notified.
-
addWatcher
@Deprecated public void addWatcher(FileWatcher.Watcher watcher)
Deprecated.Add a file watcher that is persistent. If the reference from the file watcher to the watcher itself should not prevent garbage collection, use theweakAddWatcher(Listener)
method.- Parameters:
watcher
- The watcher to add.
-
addWatcher
@Deprecated public void addWatcher(FileWatcher.Listener watcher)
Deprecated.Add a file watcher that is persistent. If the reference from the file watcher to the watcher itself should not prevent garbage collection, use theweakAddWatcher(Listener)
method.- Parameters:
watcher
- The watcher to add.
-
weakAddWatcher
@Deprecated public void weakAddWatcher(java.io.File file, FileWatcher.Watcher watcher)
Deprecated.Start watching file path and notify watcher for updates on that file. The watcher will be kept in a weak reference and will allow GC to delete the instance.- Parameters:
file
- The file path to watch.watcher
- The watcher to be notified.
-
weakAddWatcher
@Deprecated public void weakAddWatcher(java.io.File file, FileWatcher.Listener watcher)
Deprecated.Start watching file path and notify watcher for updates on that file. The watcher will be kept in a weak reference and will allow GC to delete the instance.- Parameters:
file
- The file path to watch.watcher
- The watcher to be notified.
-
weakAddWatcher
@Deprecated public void weakAddWatcher(java.nio.file.Path file, FileWatcher.Watcher watcher)
Deprecated.Start watching file path and notify watcher for updates on that file. The watcher will be kept in a weak reference and will allow GC to delete the instance.- Parameters:
file
- The file path to watch.watcher
- The watcher to be notified.
-
weakAddWatcher
public void weakAddWatcher(java.nio.file.Path file, FileWatcher.Listener watcher)
Start watching file path and notify watcher for updates on that file. The watcher will be kept in a weak reference and will allow GC to delete the instance.- Parameters:
file
- The file path to watch.watcher
- The watcher to be notified.
-
weakAddWatcher
@Deprecated public void weakAddWatcher(FileWatcher.Watcher watcher)
Deprecated.Add a non-persistent file watcher.- Parameters:
watcher
- The watcher to add.
-
weakAddWatcher
@Deprecated public void weakAddWatcher(FileWatcher.Listener watcher)
Deprecated.Add a non-persistent file watcher.- Parameters:
watcher
- The watcher to add.
-
removeWatcher
@Deprecated public boolean removeWatcher(FileWatcher.Watcher watcher)
Deprecated.Remove a watcher from the list of listeners.- Parameters:
watcher
- The watcher to be removed.- Returns:
- True if the watcher was removed from the list.
-
removeWatcher
public boolean removeWatcher(FileWatcher.Listener watcher)
Remove a watcher from the list of listeners.- Parameters:
watcher
- The watcher to be removed.- Returns:
- True if the watcher was removed from the list.
-
startWatching
@Deprecated public void startWatching(java.io.File file)
Deprecated.Start watching a specific file. Note that this will watch the file as seen in the directory as it is pointed to. This means that if the file itself is a symlink, then change events will notify changes to the symlink definition, not the content of the file.- Parameters:
file
- The file to be watched.
-
stopWatching
@Deprecated public void stopWatching(java.io.File file)
Deprecated.UseremoveWatcher(Listener)
and let it clean up watched files itself.Stop watching a specific file.- Parameters:
file
- The file to be watched.
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
-
-