Package org.eclipse.jetty.util
Class PathWatcher
java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.PathWatcher
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 ClassesModifier and TypeClassDescriptionstatic class
static enum
static interface
EventListListener Listener that reports accumulated events in one shotprivate static class
static interface
Listener for path change eventsstatic class
class
PathWatchEvent Represents a file event.static enum
PathWatchEventType Type of an eventNested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private final List
<PathWatcher.Config> private final List
<PathWatcher.PathWatchEvent> private static final boolean
private final Map
<WatchKey, PathWatcher.Config> private final List
<EventListener> (package private) static final Logger
private final Map
<Path, PathWatcher.PathWatchEvent> private Thread
private long
Update Quiet Time - set to 1000 ms as default (a lower value in Windows is not supported)private TimeUnit
private static final WatchEvent.Kind<?>[]
private static final WatchEvent.Kind<?>[]
private WatchService
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(EventListener listener) Add a listener for changes the watcher notices.private void
Append some info on the paths that we are watching.protected static <T> WatchEvent
<T> cast
(WatchEvent<?> event) protected void
doStart()
protected void
doStop()
Get an iterator over the listeners.long
Change the quiet time.private void
void
handleWatchEvent
(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
Check to see if the watcher is in a state where it should generate watch events to the listeners.boolean
private void
private long
protected void
register
(Path path, PathWatcher.Config config) private void
register
(Path path, PathWatcher.Config config, WatchEvent.Kind<?>[] kinds) private void
registerDir
(Path path, PathWatcher.Config config) private void
registerTree
(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, TimeUnit unit) Set the quiet time.toString()
void
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 Details
-
IS_WINDOWS
private static final boolean IS_WINDOWS -
LOG
-
WATCH_EVENT_KINDS
-
WATCH_DIR_KINDS
-
watchService
-
configs
-
keys
-
listeners
-
pending
-
events
-
updateQuietTimeDuration
private long updateQuietTimeDurationUpdate Quiet Time - set to 1000 ms as default (a lower value in Windows is not supported) -
updateQuietTimeUnit
-
thread
-
_notifyExistingOnStart
private boolean _notifyExistingOnStart
-
-
Constructor Details
-
PathWatcher
public PathWatcher()Construct new PathWatcher
-
-
Method Details
-
cast
-
getConfigs
-
watch
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
Request watch on a path with custom Config provided.- Parameters:
config
- the configuration to watch
-
addListener
Add a listener for changes the watcher notices.- Parameters:
listener
- change listener
-
appendConfigId
Append some info on the paths that we are watching. -
doStart
- Overrides:
doStart
in classAbstractLifeCycle
- Throws:
Exception
- See Also:
-
doStop
- Overrides:
doStop
in classAbstractLifeCycle
- Throws:
Exception
- See Also:
-
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
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
- Throws:
IOException
-
registerDir
- Throws:
IOException
-
register
- Throws:
IOException
-
register
private void register(Path path, PathWatcher.Config config, WatchEvent.Kind<?>[] kinds) throws IOException - Throws:
IOException
-
removeListener
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). -
handleKey
-
handleWatchEvent
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
Set the quiet time.- Parameters:
duration
- the quiet time durationunit
- the quite time unit
-
toString
- Overrides:
toString
in classAbstractLifeCycle
-