public class PathWatcher extends AbstractLifeCycle implements java.lang.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.DirAction |
static interface |
PathWatcher.EventListListener
EventListListener
Listener that reports accumulated events in one shot
|
private static class |
PathWatcher.ExactPathMatcher |
static interface |
PathWatcher.Listener
Listener for path change events
|
static class |
PathWatcher.PathMatcherSet |
class |
PathWatcher.PathWatchEvent
PathWatchEvent
Represents a file event.
|
static class |
PathWatcher.PathWatchEventType
PathWatchEventType
Type of an event
|
AbstractLifeCycle.AbstractLifeCycleListener
Modifier and Type | Field and Description |
---|---|
private boolean |
_notifyExistingOnStart |
private java.util.List<PathWatcher.Config> |
configs |
private java.util.List<PathWatcher.PathWatchEvent> |
events |
private static boolean |
IS_WINDOWS |
private java.util.Map<java.nio.file.WatchKey,PathWatcher.Config> |
keys |
private java.util.List<java.util.EventListener> |
listeners |
(package private) static Logger |
LOG |
private boolean |
nativeWatchService |
private java.util.Map<java.nio.file.Path,PathWatcher.PathWatchEvent> |
pending |
private java.lang.Thread |
thread |
private long |
updateQuietTimeDuration
Update Quiet Time - set to 1000 ms as default (a lower value in Windows is not supported)
|
private java.util.concurrent.TimeUnit |
updateQuietTimeUnit |
private static java.nio.file.WatchEvent.Kind<?>[] |
WATCH_DIR_KINDS |
private static java.nio.file.WatchEvent.Kind<?>[] |
WATCH_EVENT_KINDS |
private java.nio.file.WatchEvent.Modifier[] |
watchModifiers |
private java.nio.file.WatchService |
watchService |
Constructor and Description |
---|
PathWatcher()
Construct new PathWatcher
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(java.util.EventListener listener)
Add a listener for changes the watcher notices.
|
private void |
appendConfigId(java.lang.StringBuilder s)
Append some info on the paths that we are watching.
|
protected static <T> java.nio.file.WatchEvent<T> |
cast(java.nio.file.WatchEvent<?> event) |
private void |
createWatchService()
Create a fresh WatchService and determine if it is a
native implementation or not.
|
protected void |
doStart() |
protected void |
doStop() |
java.util.Collection<PathWatcher.Config> |
getConfigs() |
java.util.Iterator<java.util.EventListener> |
getListeners()
Get an iterator over the listeners.
|
long |
getUpdateQuietTimeMillis()
Change the quiet time.
|
private void |
handleKey(java.nio.file.WatchKey key) |
void |
handleWatchEvent(java.nio.file.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 boolean |
isNotifiable()
Check to see if the watcher is in a state where it should generate
watch events to the listeners.
|
boolean |
isNotifyExistingOnStart() |
private void |
notifyEvents() |
private long |
processPending() |
protected void |
register(java.nio.file.Path path,
PathWatcher.Config config) |
private void |
register(java.nio.file.Path path,
PathWatcher.Config config,
java.nio.file.WatchEvent.Kind<?>[] kinds) |
private void |
registerDir(java.nio.file.Path path,
PathWatcher.Config config) |
private void |
registerTree(java.nio.file.Path dir,
PathWatcher.Config config,
boolean notify) |
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,
java.util.concurrent.TimeUnit unit)
Set the quiet time.
|
java.lang.String |
toString() |
void |
watch(java.nio.file.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
private static final boolean IS_WINDOWS
static final Logger LOG
private static final java.nio.file.WatchEvent.Kind<?>[] WATCH_EVENT_KINDS
private static final java.nio.file.WatchEvent.Kind<?>[] WATCH_DIR_KINDS
private java.nio.file.WatchService watchService
private java.nio.file.WatchEvent.Modifier[] watchModifiers
private boolean nativeWatchService
private final java.util.List<PathWatcher.Config> configs
private final java.util.Map<java.nio.file.WatchKey,PathWatcher.Config> keys
private final java.util.List<java.util.EventListener> listeners
private final java.util.Map<java.nio.file.Path,PathWatcher.PathWatchEvent> pending
private final java.util.List<PathWatcher.PathWatchEvent> events
private long updateQuietTimeDuration
private java.util.concurrent.TimeUnit updateQuietTimeUnit
private java.lang.Thread thread
private boolean _notifyExistingOnStart
protected static <T> java.nio.file.WatchEvent<T> cast(java.nio.file.WatchEvent<?> event)
public java.util.Collection<PathWatcher.Config> getConfigs()
public void watch(java.nio.file.Path file)
file
- the path to watchpublic void watch(PathWatcher.Config config)
config
- the configuration to watchpublic void addListener(java.util.EventListener listener)
listener
- change listenerprivate void appendConfigId(java.lang.StringBuilder s)
s
- protected void doStart() throws java.lang.Exception
doStart
in class AbstractLifeCycle
java.lang.Exception
AbstractLifeCycle.doStart()
protected void doStop() throws java.lang.Exception
doStop
in class AbstractLifeCycle
java.lang.Exception
AbstractLifeCycle.doStop()
public void reset()
private void createWatchService() throws java.io.IOException
java.io.IOException
protected boolean isNotifiable()
public java.util.Iterator<java.util.EventListener> getListeners()
public long getUpdateQuietTimeMillis()
private void registerTree(java.nio.file.Path dir, PathWatcher.Config config, boolean notify) throws java.io.IOException
java.io.IOException
private void registerDir(java.nio.file.Path path, PathWatcher.Config config) throws java.io.IOException
java.io.IOException
protected void register(java.nio.file.Path path, PathWatcher.Config config) throws java.io.IOException
java.io.IOException
private void register(java.nio.file.Path path, PathWatcher.Config config, java.nio.file.WatchEvent.Kind<?>[] kinds) throws java.io.IOException
java.io.IOException
public boolean removeListener(PathWatcher.Listener listener)
listener
- the listener to removepublic void run()
run
in interface java.lang.Runnable
Runnable.run()
private void handleKey(java.nio.file.WatchKey key)
public void handleWatchEvent(java.nio.file.Path path, PathWatcher.PathWatchEvent event)
path
- the path to add to the pending listevent
- the pending eventprivate long processPending()
private void notifyEvents()
public void setNotifyExistingOnStart(boolean notify)
notify
- true if existing paths should be notified or notpublic boolean isNotifyExistingOnStart()
public void setUpdateQuietTime(long duration, java.util.concurrent.TimeUnit unit)
duration
- the quiet time durationunit
- the quite time unitpublic java.lang.String toString()
toString
in class AbstractLifeCycle