public class SimpleDirectoryWatchService extends java.lang.Object implements DirectoryWatchService, java.lang.Runnable
| Modifier and Type | Field and Description |
|---|---|
private static org.slf4j.Logger |
logger
Logging object.
|
private java.util.Map<java.nio.file.Path,java.util.Set<OnFileChangeListener>> |
mDirPathToListenersMap
|
private java.util.concurrent.atomic.AtomicBoolean |
mIsRunning
Whether or not this
DirectoryWatchService is running. |
private java.util.Map<OnFileChangeListener,java.util.Set<java.nio.file.PathMatcher>> |
mListenerToFilePatternsMap
Mapping of
OnFileChangeListeners to the
Set of patterns being used to observe changes in
Paths of interest. |
private java.util.Map<java.nio.file.WatchKey,java.nio.file.Path> |
mWatchKeyToDirPathMap
Mapping of monitoring "registration" keys to the
Path that it will be watching. |
private java.nio.file.WatchService |
mWatchService
WatchService used to monitor changes in various
Paths. |
| Constructor and Description |
|---|
SimpleDirectoryWatchService()
A simple no argument constructor for creating a
SimpleDirectoryWatchService. |
| Modifier and Type | Method and Description |
|---|---|
private static <T> java.nio.file.WatchEvent<T> |
cast(java.nio.file.WatchEvent<?> event)
Utility method used to make "valid" casts of the given
event to a specific type of WatchEvent. |
private java.nio.file.Path |
getDir(OnFileChangeListener listener)
Get the
Path associated with the given
OnFileChangeListener. |
private java.nio.file.Path |
getDirPath(java.nio.file.WatchKey key)
Get the path associated with the given
WatchKey. |
private java.util.Set<OnFileChangeListener> |
getListeners(java.nio.file.Path path)
Get the
OnFileChangeListeners associated
with the given path. |
private java.util.Set<java.nio.file.PathMatcher> |
getPatterns(OnFileChangeListener listener)
Get the
Set of patterns associated with the given
OnFileChangeListener. |
private java.nio.file.WatchKey |
getWatchKey(java.nio.file.Path dir)
Get the monitoring "registration" key associated with the
given
Path. |
boolean |
isRunning()
Checks to see if the service is still running.
|
private java.util.Set<OnFileChangeListener> |
matchedListeners(java.nio.file.Path dir,
java.nio.file.Path file)
|
private static java.nio.file.PathMatcher |
matcherForGlobExpression(java.lang.String globPattern)
Returns a
PathMatcher that performs "glob" matches
with the given globPattern against the String
representation of Path objects. |
static boolean |
matches(java.nio.file.Path input,
java.nio.file.PathMatcher pattern)
Check the given
input Path against the given pattern. |
private static boolean |
matchesAny(java.nio.file.Path input,
java.util.Set<java.nio.file.PathMatcher> patterns)
Check the given
input Path against all of the
specified patterns. |
private void |
notifyListeners(java.nio.file.WatchKey key)
Method responsible for notifying listeners when a file matching their
relevant pattern has changed.
|
private void |
notifyListenersOfInvalidation(java.nio.file.WatchKey key)
Method responsible for notifying listeners when the path they are
watching has been deleted (or otherwise "invalidated"
somehow).
|
void |
register(OnFileChangeListener listener,
java.lang.String dirPath,
java.lang.String... globPatterns)
Notifies the implementation of this interface that
dirPath should be monitored for file system events. |
void |
run() |
void |
start()
Start this
SimpleDirectoryWatchService instance by spawning a
new thread. |
void |
stop()
Stop this
SimpleDirectoryWatchService thread. |
void |
unregister(OnFileChangeListener listener)
Notifies the implementation of this interface that
listener should cease to be notified of file system events. |
void |
unregisterAll()
Unregisters all listeners.
|
private static final org.slf4j.Logger logger
private final java.nio.file.WatchService mWatchService
WatchService used to monitor changes in various
Paths.private final java.util.concurrent.atomic.AtomicBoolean mIsRunning
DirectoryWatchService is running.private final java.util.Map<java.nio.file.WatchKey,java.nio.file.Path> mWatchKeyToDirPathMap
Path that it will be watching.private final java.util.Map<java.nio.file.Path,java.util.Set<OnFileChangeListener>> mDirPathToListenersMap
private final java.util.Map<OnFileChangeListener,java.util.Set<java.nio.file.PathMatcher>> mListenerToFilePatternsMap
OnFileChangeListeners to the
Set of patterns being used to observe changes in
Paths of interest.public SimpleDirectoryWatchService() throws java.io.IOException
SimpleDirectoryWatchService.java.io.IOException - If an I/O error occurs.private static <T> java.nio.file.WatchEvent<T> cast(java.nio.file.WatchEvent<?> event)
event to a specific type of WatchEvent.T - Type to which event will be casted.event - Event to cast.event casted to WatchEvent<T>.private static java.nio.file.PathMatcher matcherForGlobExpression(java.lang.String globPattern) throws java.io.IOException
PathMatcher that performs "glob" matches
with the given globPattern against the String
representation of Path objects.globPattern - Pattern to match against. null or empty
String values will be converted to *.globPattern.java.io.IOException - if there was a problem creating the
PathMatcher.public static boolean matches(java.nio.file.Path input, java.nio.file.PathMatcher pattern)
input Path against the given pattern.input - Path to check.pattern - Pattern to check against. Cannot be null.input matches pattern.private static boolean matchesAny(java.nio.file.Path input, java.util.Set<java.nio.file.PathMatcher> patterns)
input Path against all of the
specified patterns.input - Path to check.patterns - Set of patterns to attempt to match
input against. Cannot be null.input matches any of the given
patterns.private java.nio.file.Path getDirPath(java.nio.file.WatchKey key)
WatchKey.key - WatchKey whose corresponding Path is being
requested.Path or null.private java.util.Set<OnFileChangeListener> getListeners(java.nio.file.Path path)
OnFileChangeListeners associated
with the given path.path - Path whose listeners should be returned. Cannot be
null.Set of listeners associated with path
or null.private java.util.Set<java.nio.file.PathMatcher> getPatterns(OnFileChangeListener listener)
Set of patterns associated with the given
OnFileChangeListener.listener - Listener of interest.Set of patterns associated with
listener or null.private java.nio.file.Path getDir(OnFileChangeListener listener)
Path associated with the given
OnFileChangeListener.listener - Listener whose path is requested.Path associated with listener or
null.private java.nio.file.WatchKey getWatchKey(java.nio.file.Path dir)
Path.dir - Path whose WatchKey is requested.WatchKey corresponding to dir or
null.private java.util.Set<OnFileChangeListener> matchedListeners(java.nio.file.Path dir, java.nio.file.Path file)
dir - Directory containing file.file - File that changed.Set of listeners that should be notified that
file has changed.private void notifyListeners(java.nio.file.WatchKey key)
key - mWatchService "registration" key for
one of the Paths being watched. Cannot be
null.run()private void notifyListenersOfInvalidation(java.nio.file.WatchKey key)
key - Key that has become invalid. Cannot be null.public void register(OnFileChangeListener listener, java.lang.String dirPath, java.lang.String... globPatterns) throws java.io.IOException
dirPath should be monitored for file system events. If the
changed file matches any of the globPatterns, listener
should be notified.register in interface DirectoryWatchServicelistener - The listener.dirPath - The directory path.globPatterns - Zero or more file patterns to be matched against
file names. If none provided, matches any
file.java.io.IOException - If dirPath is not a directory.public void unregister(OnFileChangeListener listener)
listener should cease to be notified of file system events.unregister in interface DirectoryWatchServicelistener - Listener to unregister. Cannot be null.public void unregisterAll()
unregisterAll in interface DirectoryWatchServicepublic void start()
SimpleDirectoryWatchService instance by spawning a
new thread.start in interface DirectoryWatchServicestart in interface Servicestop()public void stop()
SimpleDirectoryWatchService thread.
The killing happens lazily, giving the running thread an opportunity to finish the work at hand.
public boolean isRunning()
public void run()
run in interface java.lang.Runnable