Package org.eclipse.jetty.util
Class PathWatcher
- java.lang.Object
-
- org.eclipse.jetty.util.component.AbstractLifeCycle
-
- org.eclipse.jetty.util.PathWatcher
-
- All Implemented Interfaces:
java.lang.Runnable
,LifeCycle
public class PathWatcher extends AbstractLifeCycle implements java.lang.Runnable
Watch a Path (and sub directories) for Path changes.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PathWatcher.Config
static class
PathWatcher.DirAction
static interface
PathWatcher.EventListListener
EventListListener Listener that reports accumulated events in one shotprivate static class
PathWatcher.ExactPathMatcher
static interface
PathWatcher.Listener
Listener for path change eventsstatic class
PathWatcher.PathMatcherSet
class
PathWatcher.PathWatchEvent
PathWatchEvent Represents a file event.static class
PathWatcher.PathWatchEventType
PathWatchEventType Type of an event-
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener
-
-
Field Summary
Fields Modifier and Type Field 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 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.WatchService
watchService
-
Constructor Summary
Constructors Constructor Description PathWatcher()
Construct new PathWatcher
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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)
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 listenervoid
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.-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stop
-
-
-
-
Field Detail
-
IS_WINDOWS
private static final boolean IS_WINDOWS
-
LOG
static final Logger LOG
-
WATCH_EVENT_KINDS
private static final java.nio.file.WatchEvent.Kind<?>[] WATCH_EVENT_KINDS
-
WATCH_DIR_KINDS
private static final java.nio.file.WatchEvent.Kind<?>[] WATCH_DIR_KINDS
-
watchService
private java.nio.file.WatchService watchService
-
configs
private final java.util.List<PathWatcher.Config> configs
-
keys
private final java.util.Map<java.nio.file.WatchKey,PathWatcher.Config> keys
-
listeners
private final java.util.List<java.util.EventListener> listeners
-
pending
private final java.util.Map<java.nio.file.Path,PathWatcher.PathWatchEvent> pending
-
events
private final java.util.List<PathWatcher.PathWatchEvent> events
-
updateQuietTimeDuration
private long updateQuietTimeDuration
Update Quiet Time - set to 1000 ms as default (a lower value in Windows is not supported)
-
updateQuietTimeUnit
private java.util.concurrent.TimeUnit updateQuietTimeUnit
-
thread
private java.lang.Thread thread
-
_notifyExistingOnStart
private boolean _notifyExistingOnStart
-
-
Method Detail
-
cast
protected static <T> java.nio.file.WatchEvent<T> cast(java.nio.file.WatchEvent<?> event)
-
getConfigs
public java.util.Collection<PathWatcher.Config> getConfigs()
-
watch
public void watch(java.nio.file.Path file)
Request watch on a the given path (either file or dir) using all Config defaults. In the case of a dir, the default is not to recurse into subdirs for watching.- Parameters:
file
- the path to watch
-
watch
public void watch(PathWatcher.Config config)
Request watch on a path with custom Config provided.- Parameters:
config
- the configuration to watch
-
addListener
public void addListener(java.util.EventListener listener)
Add a listener for changes the watcher notices.- Parameters:
listener
- change listener
-
appendConfigId
private void appendConfigId(java.lang.StringBuilder s)
Append some info on the paths that we are watching.
-
doStart
protected void doStart() throws java.lang.Exception
- Overrides:
doStart
in classAbstractLifeCycle
- Throws:
java.lang.Exception
- See Also:
AbstractLifeCycle.doStart()
-
doStop
protected void doStop() throws java.lang.Exception
- Overrides:
doStop
in classAbstractLifeCycle
- Throws:
java.lang.Exception
- See Also:
AbstractLifeCycle.doStop()
-
reset
public void reset()
Remove all current configs and listeners.
-
isNotifiable
protected boolean isNotifiable()
Check to see if the watcher is in a state where it should generate watch events to the listeners. Used to determine if watcher should generate events for existing files and dirs on startup.- Returns:
- true if the watcher should generate events to the listeners.
-
getListeners
public java.util.Iterator<java.util.EventListener> getListeners()
Get an iterator over the listeners.- Returns:
- iterator over the listeners.
-
getUpdateQuietTimeMillis
public long getUpdateQuietTimeMillis()
Change the quiet time.- Returns:
- the quiet time in millis
-
registerTree
private void registerTree(java.nio.file.Path dir, PathWatcher.Config config, boolean notify) throws java.io.IOException
- Throws:
java.io.IOException
-
registerDir
private void registerDir(java.nio.file.Path path, PathWatcher.Config config) throws java.io.IOException
- Throws:
java.io.IOException
-
register
protected void register(java.nio.file.Path path, PathWatcher.Config config) throws java.io.IOException
- Throws:
java.io.IOException
-
register
private void register(java.nio.file.Path path, PathWatcher.Config config, java.nio.file.WatchEvent.Kind<?>[] kinds) throws java.io.IOException
- Throws:
java.io.IOException
-
removeListener
public boolean removeListener(PathWatcher.Listener listener)
Delete a listener- Parameters:
listener
- the listener to remove- Returns:
- true if the listener existed and was removed
-
run
public void run()
Forever loop. Wait for the WatchService to report some filesystem events for the watched paths. When an event for a path first occurs, it is subjected to a quiet time. Subsequent events that arrive for the same path during this quiet time are accumulated and the timer reset. Only when the quiet time has expired are the accumulated events sent. MODIFY events are handled slightly differently - multiple MODIFY events arriving within a quiet time are coalesced into a single MODIFY event. Both the accumulation of events and coalescing of MODIFY events reduce the number and frequency of event reporting for "noisy" files (ie those that are undergoing rapid change).- Specified by:
run
in interfacejava.lang.Runnable
- See Also:
Runnable.run()
-
handleKey
private void handleKey(java.nio.file.WatchKey key)
-
handleWatchEvent
public 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.- Parameters:
path
- the path to add to the pending listevent
- the pending event
-
processPending
private long processPending()
-
notifyEvents
private void notifyEvents()
-
setNotifyExistingOnStart
public void setNotifyExistingOnStart(boolean notify)
Whether or not to issue notifications for directories and files that already exist when the watcher starts.- Parameters:
notify
- true if existing paths should be notified or not
-
isNotifyExistingOnStart
public boolean isNotifyExistingOnStart()
-
setUpdateQuietTime
public void setUpdateQuietTime(long duration, java.util.concurrent.TimeUnit unit)
Set the quiet time.- Parameters:
duration
- the quiet time durationunit
- the quite time unit
-
toString
public java.lang.String toString()
- Overrides:
toString
in classAbstractLifeCycle
-
-