public class SimpleDirectoryWatchService extends Object implements DirectoryWatchService, Runnable
Modifier and Type | Field and Description |
---|---|
private static org.slf4j.Logger |
logger
Logging object.
|
private Map<Path,Set<OnFileChangeListener>> |
mDirPathToListenersMap
|
private AtomicBoolean |
mIsRunning
Whether or not this
DirectoryWatchService is running. |
private Map<OnFileChangeListener,Set<PathMatcher>> |
mListenerToFilePatternsMap
Mapping of
OnFileChangeListeners to the
Set of patterns being used to observe changes in
Paths of interest. |
private Map<WatchKey,Path> |
mWatchKeyToDirPathMap
Mapping of monitoring "registration" keys to the
Path that it will be watching. |
private 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> WatchEvent<T> |
cast(WatchEvent<?> event)
Utility method used to make "valid" casts of the given
event to a specific type of WatchEvent . |
private Path |
getDir(OnFileChangeListener listener)
Get the
Path associated with the given
OnFileChangeListener . |
private Path |
getDirPath(WatchKey key)
Get the path associated with the given
WatchKey . |
private Set<OnFileChangeListener> |
getListeners(Path path)
Get the
OnFileChangeListeners associated
with the given path . |
private Set<PathMatcher> |
getPatterns(OnFileChangeListener listener)
Get the
Set of patterns associated with the given
OnFileChangeListener . |
private WatchKey |
getWatchKey(Path dir)
Get the monitoring "registration" key associated with the
given
Path . |
boolean |
isRunning()
Checks to see if the service is still running.
|
private Set<OnFileChangeListener> |
matchedListeners(Path dir,
Path file)
|
private static PathMatcher |
matcherForGlobExpression(String globPattern)
Returns a
PathMatcher that performs "glob" matches
with the given globPattern against the String
representation of Path objects. |
static boolean |
matches(Path input,
PathMatcher pattern)
|
private static boolean |
matchesAny(Path input,
Set<PathMatcher> patterns)
|
private void |
notifyListeners(WatchKey key)
Method responsible for notifying listeners when a file matching their
relevant pattern has changed.
|
void |
register(OnFileChangeListener listener,
String dirPath,
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 WatchService mWatchService
WatchService
used to monitor changes in various
Paths
.private final AtomicBoolean mIsRunning
DirectoryWatchService
is running.private final Map<WatchKey,Path> mWatchKeyToDirPathMap
Path
that it will be watching.private final Map<Path,Set<OnFileChangeListener>> mDirPathToListenersMap
private final Map<OnFileChangeListener,Set<PathMatcher>> mListenerToFilePatternsMap
OnFileChangeListeners
to the
Set
of patterns being used to observe changes in
Paths
of interest.public SimpleDirectoryWatchService() throws IOException
SimpleDirectoryWatchService
.IOException
- If an I/O error occurs.private static <T> WatchEvent<T> cast(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 PathMatcher matcherForGlobExpression(String globPattern) throws 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
.IOException
- if there was a problem creating the
PathMatcher
.public static boolean matches(Path input, PathMatcher pattern)
input
- Path to check.pattern
- Pattern to check against. Cannot be null
.input
matches pattern
.private static boolean matchesAny(Path input, Set<PathMatcher> 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 Path getDirPath(WatchKey key)
WatchKey
.key
- WatchKey
whose corresponding Path
is being
requested.Path
or null
.private Set<OnFileChangeListener> getListeners(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 Set<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 Path getDir(OnFileChangeListener listener)
Path
associated with the given
OnFileChangeListener
.listener
- Listener whose path is requested.Path
associated with listener
or
null
.private WatchKey getWatchKey(Path dir)
Path
.dir
- Path
whose WatchKey
is requested.WatchKey
corresponding to dir
or
null
.private Set<OnFileChangeListener> matchedListeners(Path dir, Path file)
dir
- Directory containing file
.file
- File that changed.Set
of listeners that should be notified that
file
has changed.private void notifyListeners(WatchKey key)
Note: "change" means one of:
key
- mWatchService
"registration" key for
one of the Paths
being watched. Cannot be
null
.run()
public void register(OnFileChangeListener listener, String dirPath, String... globPatterns) throws 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 DirectoryWatchService
listener
- The listener.dirPath
- The directory path.globPatterns
- Zero or more file patterns to be matched against
file names. If none provided, matches any
file.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 DirectoryWatchService
listener
- Listener to unregister. Cannot be null
.public void unregisterAll()
unregisterAll
in interface DirectoryWatchService
public void start()
SimpleDirectoryWatchService
instance by spawning a
new thread.start
in interface DirectoryWatchService
start
in interface Service
stop()
public void stop()
SimpleDirectoryWatchService
thread.
The killing happens lazily, giving the running thread an opportunity to finish the work at hand.
public boolean isRunning()