public class PathWatcher extends AbstractLifeCycle implements Runnable
Suitable replacement for the old Scanner
implementation.
Allows for configured Excludes and Includes using FileSystem.getPathMatcher(String)
syntax.
Reports activity via registered PathWatcher.Listener
s
Modifier and Type | Class and Description |
---|---|
static class |
PathWatcher.Config |
static class |
PathWatcher.DepthLimitedFileVisitor |
static interface |
PathWatcher.EventListListener
EventListListener
Listener that reports accumulated events in one shot
|
static interface |
PathWatcher.Listener
Listener for path change events
|
static class |
PathWatcher.PathPendingEvents
PathPendingEvents
For a given path, a list of events that are awaiting the
quiet time.
|
static class |
PathWatcher.PathWatchEvent
PathWatchEvent
Represents a file event.
|
static class |
PathWatcher.PathWatchEventType
PathWatchEventType
Type of an event
|
AbstractLifeCycle.AbstractLifeCycleListener
Constructor and Description |
---|
PathWatcher()
Construct new PathWatcher
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(EventListener listener)
Add a listener for changes the watcher notices.
|
void |
addToPendingList(Path path,
PathWatcher.PathWatchEvent event)
Add an event reported by the WatchService to list of pending events
that will be sent after their quiet time has expired.
|
protected static <T> WatchEvent<T> |
cast(WatchEvent<?> event) |
protected void |
doStart() |
protected void |
doStop() |
Iterator<EventListener> |
getListeners()
Get an iterator over the listeners.
|
long |
getUpdateQuietTimeMillis()
Change the quiet time.
|
protected boolean |
isNotifiable()
Check to see if the watcher is in a state where it should generate
watch events to the listeners.
|
boolean |
isNotifyExistingOnStart() |
protected void |
notifyOnPathWatchEvents(List<PathWatcher.PathWatchEvent> events)
Generate events to the listeners.
|
protected void |
prepareConfig(PathWatcher.Config baseDir)
Register path in the config with the file watch service,
walking the tree if it happens to be a directory.
|
protected void |
register(Path dir,
PathWatcher.Config root)
Register a path (directory) with the WatchService.
|
boolean |
removeListener(PathWatcher.Listener listener)
Delete a listener
|
void |
reset()
Remove all current configs and listeners.
|
void |
run()
Forever loop.
|
void |
setNotifyExistingOnStart(boolean notify)
Whether or not to issue notifications for directories and files that
already exist when the watcher starts.
|
void |
setUpdateQuietTime(long duration,
TimeUnit unit)
Set the quiet time.
|
String |
toString() |
void |
watch(Path file)
Request watch on a the given path (either file or dir)
using all Config defaults.
|
void |
watch(PathWatcher.Config config)
Request watch on a path with custom Config
provided.
|
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stop
protected static <T> WatchEvent<T> cast(WatchEvent<?> event)
public void watch(Path file)
file
- the path to watchpublic void watch(PathWatcher.Config config)
config
- the configuration to watchprotected void prepareConfig(PathWatcher.Config baseDir) throws IOException
baseDir
- the base directory configuration to watchIOException
- if unable to walk the filesystem treepublic void addListener(EventListener listener)
listener
- change listenerprotected void doStart() throws Exception
doStart
in class AbstractLifeCycle
Exception
AbstractLifeCycle.doStart()
protected void doStop() throws Exception
doStop
in class AbstractLifeCycle
Exception
AbstractLifeCycle.doStop()
public void reset()
protected boolean isNotifiable()
public Iterator<EventListener> getListeners()
public long getUpdateQuietTimeMillis()
protected void notifyOnPathWatchEvents(List<PathWatcher.PathWatchEvent> events)
events
- the events capturedprotected void register(Path dir, PathWatcher.Config root) throws IOException
dir
- the directory to registerroot
- the configuration rootIOException
- if unable to register the path with the watch service.public boolean removeListener(PathWatcher.Listener listener)
listener
- the listener to removepublic void run()
run
in interface Runnable
Runnable.run()
public void addToPendingList(Path path, PathWatcher.PathWatchEvent event)
path
- the path to add to the pending listevent
- the pending eventpublic void setNotifyExistingOnStart(boolean notify)
notify
- true if existing paths should be notified or notpublic boolean isNotifyExistingOnStart()
public void setUpdateQuietTime(long duration, TimeUnit unit)
duration
- the quiet time durationunit
- the quite time unitCopyright © 1995-2016 Webtide. All Rights Reserved.