Package org.forgerock.util
Class DirectoryWatcher
- java.lang.Object
-
- org.forgerock.util.DirectoryWatcher
-
public class DirectoryWatcher extends Object
High-level interface to theWatchService
API for detecting filesystem change events.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
DirectoryWatcher.ChangeListener
A change listener for a single directory that can be used to register notification callbacks and to cancel observing this directory.
-
Constructor Summary
Constructors Constructor Description DirectoryWatcher(ScheduledExecutorService scheduledExecutorService, WatchEvent.Modifier... modifiers)
Initializes the directory watcher.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
shutdown()
Shuts down the directory watcher, cancelling all registered watch tasks and closing the underlying watch service.DirectoryWatcher.ChangeListener
watch(Path directory, WatchEvent.Kind<Path>... eventKinds)
Registers a directory to be watched for change notifications.
-
-
-
Constructor Detail
-
DirectoryWatcher
public DirectoryWatcher(ScheduledExecutorService scheduledExecutorService, WatchEvent.Modifier... modifiers) throws IOException
Initializes the directory watcher. The watcher will use the given executor service to execute a periodic task polling for filesystem changes. The watch service from the default filesystem will be used to wait for events.- Parameters:
scheduledExecutorService
- the scheduled executor service (never null). A periodic task will be added that polls for filesystem events every second.modifiers
- how urgently events should be delivered. A higher value will result in more frequent polling of the filesystem in some implementations. The allowed values are JVM specific. On Hotspot usecom.sun.nio.file.SensitivityWatchEventModifier
. Leave unspecified to use the default sensitivity. Other modifiers may also be allowed depending on the platform.- Throws:
IOException
- if an I/O error occurs when trying to construct the watch service.
-
-
Method Detail
-
watch
@SafeVarargs public final DirectoryWatcher.ChangeListener watch(Path directory, WatchEvent.Kind<Path>... eventKinds) throws IOException
Registers a directory to be watched for change notifications.- Parameters:
directory
- the directory to watch.eventKinds
- the kinds of events to watch. SeeStandardWatchEventKinds
for values.- Returns:
- a change listener that can be used to add callbacks.
- Throws:
IOException
- if an I/O error prevents the directory being watched.IllegalStateException
- if the watch service has already been shut down.
-
shutdown
public void shutdown()
Shuts down the directory watcher, cancelling all registered watch tasks and closing the underlying watch service.
-
-