Class SimpleDirectoryWatchService
java.lang.Object
edu.wisc.ssec.mcidasv.util.pathwatcher.SimpleDirectoryWatchService
- All Implemented Interfaces:
DirectoryWatchService
,Service
,Runnable
A simple class which can monitor files and notify interested parties
(i.e. listeners) of file changes.
This class is kept lean by only keeping methods that are actually being
called.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final org.slf4j.Logger
Logging object.private final Map
<Path, Set<OnFileChangeListener>> private final AtomicBoolean
Whether or not thisDirectoryWatchService
is running.private final Map
<OnFileChangeListener, Set<PathMatcher>> Mapping ofOnFileChangeListeners
to theSet
of patterns being used to observe changes inPaths
of interest.Mapping of monitoring "registration" keys to thePath
that it will be watching.private final WatchService
WatchService
used to monitor changes in variousPaths
. -
Constructor Summary
ConstructorsConstructorDescriptionA simple no argument constructor for creating aSimpleDirectoryWatchService
. -
Method Summary
Modifier and TypeMethodDescriptionprivate static <T> WatchEvent
<T> cast
(WatchEvent<?> event) Utility method used to make "valid" casts of the givenevent
to a specific type ofWatchEvent
.private Path
getDir
(OnFileChangeListener listener) Get thePath
associated with the givenOnFileChangeListener
.private Path
getDirPath
(WatchKey key) Get the path associated with the givenWatchKey
.private Set
<OnFileChangeListener> getListeners
(Path path) Get theOnFileChangeListeners
associated with the givenpath
.private Set
<PathMatcher> getPatterns
(OnFileChangeListener listener) Get theSet
of patterns associated with the givenOnFileChangeListener
.private WatchKey
getWatchKey
(Path dir) Get the monitoring "registration" key associated with the givenPath
.boolean
Checks to see if the service is still running.private Set
<OnFileChangeListener> matchedListeners
(Path dir, Path file) private static PathMatcher
matcherForGlobExpression
(String globPattern) Returns aPathMatcher
that performs "glob" matches with the givenglobPattern
against theString
representation ofPath
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.private void
Method responsible for notifying listeners when the path they are watching has been deleted (or otherwise "invalidated" somehow).void
register
(OnFileChangeListener listener, String dirPath, String... globPatterns) Notifies the implementation of this interface thatdirPath
should be monitored for file system events.void
run()
void
start()
Start thisSimpleDirectoryWatchService
instance by spawning a new thread.void
stop()
Stop thisSimpleDirectoryWatchService
thread.void
unregister
(OnFileChangeListener listener) Notifies the implementation of this interface thatlistener
should cease to be notified of file system events.void
Unregisters all listeners.
-
Field Details
-
logger
Logging object. -
mWatchService
WatchService
used to monitor changes in variousPaths
. -
mIsRunning
Whether or not thisDirectoryWatchService
is running. -
mWatchKeyToDirPathMap
Mapping of monitoring "registration" keys to thePath
that it will be watching. -
mDirPathToListenersMap
-
mListenerToFilePatternsMap
Mapping ofOnFileChangeListeners
to theSet
of patterns being used to observe changes inPaths
of interest.
-
-
Constructor Details
-
SimpleDirectoryWatchService
A simple no argument constructor for creating aSimpleDirectoryWatchService
.- Throws:
IOException
- If an I/O error occurs.
-
-
Method Details
-
cast
Utility method used to make "valid" casts of the givenevent
to a specific type ofWatchEvent
.- Type Parameters:
T
- Type to whichevent
will be casted.- Parameters:
event
- Event to cast.- Returns:
event
casted toWatchEvent<T>
.
-
matcherForGlobExpression
Returns aPathMatcher
that performs "glob" matches with the givenglobPattern
against theString
representation ofPath
objects.- Parameters:
globPattern
- Pattern to match against.null
or emptyString
values will be converted to*
.- Returns:
- Path matching object for the given
globPattern
. - Throws:
IOException
- if there was a problem creating thePathMatcher
.
-
matches
- Parameters:
input
- Path to check.pattern
- Pattern to check against. Cannot benull
.- Returns:
- Whether or not
input
matchespattern
.
-
matchesAny
- Parameters:
input
- Path to check.patterns
-Set
of patterns to attempt to matchinput
against. Cannot benull
.- Returns:
- Whether or not
input
matches any of the givenpatterns
.
-
getDirPath
Get the path associated with the givenWatchKey
.- Parameters:
key
-WatchKey
whose correspondingPath
is being requested.- Returns:
- Either the correspond
Path
ornull
.
-
getListeners
Get theOnFileChangeListeners
associated with the givenpath
.- Parameters:
path
- Path whose listeners should be returned. Cannot benull
.- Returns:
- Either the
Set
of listeners associated withpath
ornull
.
-
getPatterns
Get theSet
of patterns associated with the givenOnFileChangeListener
.- Parameters:
listener
- Listener of interest.- Returns:
- Either the
Set
of patterns associated withlistener
ornull
.
-
getDir
Get thePath
associated with the givenOnFileChangeListener
.- Parameters:
listener
- Listener whose path is requested.- Returns:
- Either the
Path
associated withlistener
ornull
.
-
getWatchKey
Get the monitoring "registration" key associated with the givenPath
.- Parameters:
dir
-Path
whoseWatchKey
is requested.- Returns:
- Either the
WatchKey
corresponding todir
ornull
.
-
matchedListeners
- Parameters:
dir
- Directory containingfile
.file
- File that changed.- Returns:
Set
of listeners that should be notified thatfile
has changed.
-
notifyListeners
Method responsible for notifying listeners when a file matching their relevant pattern has changed. Note: "change" means one of:- file creation
- file removal
- file contents changing
- Parameters:
key
-mWatchService
"registration" key for one of thePaths
being watched. Cannot benull
.- See Also:
-
notifyListenersOfInvalidation
Method responsible for notifying listeners when the path they are watching has been deleted (or otherwise "invalidated" somehow).- Parameters:
key
- Key that has become invalid. Cannot benull
.
-
register
public void register(OnFileChangeListener listener, String dirPath, String... globPatterns) throws IOException Notifies the implementation of this interface thatdirPath
should be monitored for file system events. If the changed file matches any of theglobPatterns
,listener
should be notified.- Specified by:
register
in interfaceDirectoryWatchService
- Parameters:
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.- Throws:
IOException
- IfdirPath
is not a directory.
-
unregister
Notifies the implementation of this interface thatlistener
should cease to be notified of file system events.- Specified by:
unregister
in interfaceDirectoryWatchService
- Parameters:
listener
- Listener to unregister. Cannot benull
.
-
unregisterAll
Unregisters all listeners.- Specified by:
unregisterAll
in interfaceDirectoryWatchService
-
start
Start thisSimpleDirectoryWatchService
instance by spawning a new thread.- Specified by:
start
in interfaceDirectoryWatchService
- Specified by:
start
in interfaceService
- See Also:
-
stop
Stop thisSimpleDirectoryWatchService
thread.The killing happens lazily, giving the running thread an opportunity to finish the work at hand.
-
isRunning
Checks to see if the service is still running. -
run
-