Package org.forgerock.util
Class DirectoryWatcher
java.lang.Object
org.forgerock.util.DirectoryWatcher
High-level interface to the
WatchService API for detecting filesystem change events.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassA change listener for a single directory that can be used to register notification callbacks and to cancel observing this directory. -
Constructor Summary
ConstructorsConstructorDescriptionDirectoryWatcher(ScheduledExecutorService scheduledExecutorService, WatchEvent.Modifier... modifiers) Initializes the directory watcher. -
Method Summary
Modifier and TypeMethodDescriptionvoidshutdown()Shuts down the directory watcher, cancelling all registered watch tasks and closing the underlying watch service.watch(Path directory, WatchEvent.Kind<Path>... eventKinds) Registers a directory to be watched for change notifications.
-
Constructor Details
-
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 Details
-
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. SeeStandardWatchEventKindsfor 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.
-