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.LoggerLogging object.private final Map<Path, Set<OnFileChangeListener>> private final AtomicBooleanWhether or not thisDirectoryWatchServiceis running.private final Map<OnFileChangeListener, Set<PathMatcher>> Mapping ofOnFileChangeListenersto theSetof patterns being used to observe changes inPathsof interest.Mapping of monitoring "registration" keys to thePaththat it will be watching.private final WatchServiceWatchServiceused 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 giveneventto a specific type ofWatchEvent.private PathgetDir(OnFileChangeListener listener) Get thePathassociated with the givenOnFileChangeListener.private PathgetDirPath(WatchKey key) Get the path associated with the givenWatchKey.private Set<OnFileChangeListener> getListeners(Path path) Get theOnFileChangeListenersassociated with the givenpath.private Set<PathMatcher> getPatterns(OnFileChangeListener listener) Get theSetof patterns associated with the givenOnFileChangeListener.private WatchKeygetWatchKey(Path dir) Get the monitoring "registration" key associated with the givenPath.booleanChecks to see if the service is still running.private Set<OnFileChangeListener> matchedListeners(Path dir, Path file) private static PathMatchermatcherForGlobExpression(String globPattern) Returns aPathMatcherthat performs "glob" matches with the givenglobPatternagainst theStringrepresentation ofPathobjects.static booleanmatches(Path input, PathMatcher pattern) private static booleanmatchesAny(Path input, Set<PathMatcher> patterns) private voidnotifyListeners(WatchKey key) Method responsible for notifying listeners when a file matching their relevant pattern has changed.private voidMethod responsible for notifying listeners when the path they are watching has been deleted (or otherwise "invalidated" somehow).voidregister(OnFileChangeListener listener, String dirPath, String... globPatterns) Notifies the implementation of this interface thatdirPathshould be monitored for file system events.voidrun()voidstart()Start thisSimpleDirectoryWatchServiceinstance by spawning a new thread.voidstop()Stop thisSimpleDirectoryWatchServicethread.voidunregister(OnFileChangeListener listener) Notifies the implementation of this interface thatlistenershould cease to be notified of file system events.voidUnregisters all listeners.
-
Field Details
-
logger
Logging object. -
mWatchService
WatchServiceused to monitor changes in variousPaths. -
mIsRunning
Whether or not thisDirectoryWatchServiceis running. -
mWatchKeyToDirPathMap
Mapping of monitoring "registration" keys to thePaththat it will be watching. -
mDirPathToListenersMap
-
mListenerToFilePatternsMap
Mapping ofOnFileChangeListenersto theSetof patterns being used to observe changes inPathsof 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 giveneventto a specific type ofWatchEvent.- Type Parameters:
T- Type to whicheventwill be casted.- Parameters:
event- Event to cast.- Returns:
eventcasted toWatchEvent<T>.
-
matcherForGlobExpression
Returns aPathMatcherthat performs "glob" matches with the givenglobPatternagainst theStringrepresentation ofPathobjects.- Parameters:
globPattern- Pattern to match against.nullor emptyStringvalues 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
inputmatchespattern.
-
matchesAny
- Parameters:
input- Path to check.patterns-Setof patterns to attempt to matchinputagainst. Cannot benull.- Returns:
- Whether or not
inputmatches any of the givenpatterns.
-
getDirPath
Get the path associated with the givenWatchKey.- Parameters:
key-WatchKeywhose correspondingPathis being requested.- Returns:
- Either the correspond
Pathornull.
-
getListeners
Get theOnFileChangeListenersassociated with the givenpath.- Parameters:
path- Path whose listeners should be returned. Cannot benull.- Returns:
- Either the
Setof listeners associated withpathornull.
-
getPatterns
Get theSetof patterns associated with the givenOnFileChangeListener.- Parameters:
listener- Listener of interest.- Returns:
- Either the
Setof patterns associated withlistenerornull.
-
getDir
Get thePathassociated with the givenOnFileChangeListener.- Parameters:
listener- Listener whose path is requested.- Returns:
- Either the
Pathassociated withlistenerornull.
-
getWatchKey
Get the monitoring "registration" key associated with the givenPath.- Parameters:
dir-PathwhoseWatchKeyis requested.- Returns:
- Either the
WatchKeycorresponding todirornull.
-
matchedListeners
- Parameters:
dir- Directory containingfile.file- File that changed.- Returns:
Setof listeners that should be notified thatfilehas 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 thePathsbeing 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 thatdirPathshould be monitored for file system events. If the changed file matches any of theglobPatterns,listenershould be notified.- Specified by:
registerin 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- IfdirPathis not a directory.
-
unregister
Notifies the implementation of this interface thatlistenershould cease to be notified of file system events.- Specified by:
unregisterin interfaceDirectoryWatchService- Parameters:
listener- Listener to unregister. Cannot benull.
-
unregisterAll
Unregisters all listeners.- Specified by:
unregisterAllin interfaceDirectoryWatchService
-
start
Start thisSimpleDirectoryWatchServiceinstance by spawning a new thread.- Specified by:
startin interfaceDirectoryWatchService- Specified by:
startin interfaceService- See Also:
-
stop
Stop thisSimpleDirectoryWatchServicethread.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
-