Class RecursiveWatcher
- java.lang.Object
-
- org.syncany.operations.watch.RecursiveWatcher
-
- Direct Known Subclasses:
DefaultRecursiveWatcher
,WindowsRecursiveWatcher
public abstract class RecursiveWatcher extends java.lang.Object
The recursive file watcher monitors a folder (and its sub-folders).When a file event occurs, a timer is started to wait for the file operations to settle. It is reset whenever a new event occurs. When the timer times out, an event is thrown through the
RecursiveWatcher.WatchListener
.This is an abstract class, using several template methods that are called in different lifecycle states:
beforeStart()
,beforePollEventLoop()
,pollEvents()
, andafterStop()
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
RecursiveWatcher.WatchListener
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<java.nio.file.Path>
ignorePaths
protected static java.util.logging.Logger
logger
protected java.nio.file.Path
root
-
Constructor Summary
Constructors Constructor Description RecursiveWatcher(java.nio.file.Path root, java.util.List<java.nio.file.Path> ignorePaths, int settleDelay, RecursiveWatcher.WatchListener listener)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract void
afterStop()
Called after thestop()
method.protected abstract void
beforePollEventLoop()
Called in the watch service polling thread, right before thepollEvents()
loop.protected abstract void
beforeStart()
Called before thestart()
method.static RecursiveWatcher
createRecursiveWatcher(java.nio.file.Path root, java.util.List<java.nio.file.Path> ignorePaths, int settleDelay, RecursiveWatcher.WatchListener listener)
Creates a recursive watcher for the given root path.protected abstract boolean
pollEvents()
Called in the watch service polling thread, inside of thepollEvents()
loop.void
start()
Starts the watcher service and registers watches in all of the sub-folders of the given root folder.void
stop()
Stops the watch thread by interrupting it and subsequently calls theafterStop()
template method (to be implemented by subclasses.protected abstract void
watchEventsOccurred()
Called in the watch service polling thread, whenever a file system event occurs.
-
-
-
Field Detail
-
logger
protected static final java.util.logging.Logger logger
-
root
protected java.nio.file.Path root
-
ignorePaths
protected java.util.List<java.nio.file.Path> ignorePaths
-
-
Constructor Detail
-
RecursiveWatcher
public RecursiveWatcher(java.nio.file.Path root, java.util.List<java.nio.file.Path> ignorePaths, int settleDelay, RecursiveWatcher.WatchListener listener)
-
-
Method Detail
-
createRecursiveWatcher
public static RecursiveWatcher createRecursiveWatcher(java.nio.file.Path root, java.util.List<java.nio.file.Path> ignorePaths, int settleDelay, RecursiveWatcher.WatchListener listener)
Creates a recursive watcher for the given root path. The returned watcher will ignore the ignore paths and fire an event through theRecursiveWatcher.WatchListener
as soon as the settle delay (in ms) has passed.The method returns a platform-specific recursive watcher:
WindowsRecursiveWatcher
for Windows andDefaultRecursiveWatcher
for other operating systems.
-
start
public void start() throws java.lang.Exception
Starts the watcher service and registers watches in all of the sub-folders of the given root folder.This method calls the
beforeStart()
method before everything else. Subclasses may execute their own commands there. Before the watch thread is started,beforePollEventLoop()
is called. And in the watch thread loop,pollEvents()
is called.Important: This method returns immediately, even though the watches might not be in place yet. For large file trees, it might take several seconds until all directories are being monitored. For normal cases (1-100 folders), this should not take longer than a few milliseconds.
- Throws:
java.lang.Exception
-
stop
public void stop()
Stops the watch thread by interrupting it and subsequently calls theafterStop()
template method (to be implemented by subclasses.
-
beforeStart
protected abstract void beforeStart() throws java.lang.Exception
Called before thestart()
method. This method is only called once.- Throws:
java.lang.Exception
-
beforePollEventLoop
protected abstract void beforePollEventLoop()
Called in the watch service polling thread, right before thepollEvents()
loop. This method is only called once.
-
pollEvents
protected abstract boolean pollEvents() throws java.lang.InterruptedException
Called in the watch service polling thread, inside of thepollEvents()
loop. This method is called multiple times.- Throws:
java.lang.InterruptedException
-
watchEventsOccurred
protected abstract void watchEventsOccurred()
Called in the watch service polling thread, whenever a file system event occurs. This may be used by subclasses to (re-)set watches on folders. This method is called multiple times.
-
-