Top Description Inners Fields Constructors Methods
io.vertx.core.impl.launcher.commands

public Class Watcher

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Imports
io.vertx.core.Handler, io.vertx.core.impl.logging.Logger, .LoggerFactory, java.io.File, java.nio.file.WatchService, java.util.*, java.util.stream.Collectors

A file alteration monitor based on a home made file system scan and watching files matching a set of includes patterns. These patterns are Ant patterns (can use **, * or ?). This class takes 2 Handler as parameter and orchestrate the redeployment method when a matching file is modified (created, updated or deleted). Users have the possibility to execute a shell command during the redeployment. On a file change, the undeploy Handler is called, followed by the execution of the user command. Then the deploy Handler is invoked.

The watcher watches all files from the current directory and sub-directories.

Author
Clement Escoffier

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private volatile boolean
private final String
private final File
private final Handler<Handler<Void>>
private final Map<File, Map<File, Watcher.FileInfo>>
private final Set<File>
private final long
private final List<String>
private long
lastChange

This field is always access from the scan thread.

private static final Logger
private final List<File>
private final long
private final Handler<Handler<Void>>

Constructor Summary

AccessConstructor and Description
public
Watcher(File
the root directory
root
,
List<String>
the list of include patterns, should not be null or empty
includes
,
Handler<Handler<Void>>
the function called when deployment is required
deploy
,
Handler<Handler<Void>>
the function called when un-deployment is required
undeploy
,
String
an optional command executed after the un-deployment and before the deployment
onRedeployCommand
,
long
the amount of time in milliseconds to wait between two redeploy even if there are changes
gracePeriod
,
long
the time in millisecond between 2 file system scans
scanPeriod
)

Creates a new Watcher.

Method Summary

Modifier and TypeMethod and Description
private void
private void
private boolean

Returns:

true if a change occurred requiring the redeployment.
changesHaveOccurred
()

Checks whether or not a change has occurred in one of the watched file that match one of the given include pattern .

public void
close()

Stops watching.

private void
pack-priv static List<File>
extractRoots(File root, List<String> includes)

protected boolean

Returns:

true if the file matches at least one pattern, false otherwise.
match
(File
the file
file
)

Checks whether the given file matches one of the includes patterns.

public void
run()

Implements java.lang.Runnable.run.

The watching thread runnable method.

private List<String>
private void
trigger()

Redeployment process.

public Watcher

Returns:

the current watcher.
watch
()

Starts watching.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

closedback to summary
private volatile boolean closed
cmdback to summary
private final String cmd
cwdback to summary
private final File cwd
deployback to summary
private final Handler<Handler<Void>> deploy
fileMapback to summary
private final Map<File, Map<File, Watcher.FileInfo>> fileMap
filesToWatchback to summary
private final Set<File> filesToWatch
gracePeriodback to summary
private final long gracePeriod
includesback to summary
private final List<String> includes
lastChangeback to summary
private long lastChange

This field is always access from the scan thread. No need to be volatile.

LOGGERback to summary
private static final Logger LOGGER
rootsback to summary
private final List<File> roots
scanPeriodback to summary
private final long scanPeriod
undeployback to summary
private final Handler<Handler<Void>> undeploy

Constructor Detail

Watcherback to summary
public Watcher(File root, List<String> includes, Handler<Handler<Void>> deploy, Handler<Handler<Void>> undeploy, String onRedeployCommand, long gracePeriod, long scanPeriod)

Creates a new Watcher.

Parameters
root:File

the root directory

includes:List<String>

the list of include patterns, should not be null or empty

deploy:Handler<Handler<Void>>

the function called when deployment is required

undeploy:Handler<Handler<Void>>

the function called when un-deployment is required

onRedeployCommand:String

an optional command executed after the un-deployment and before the deployment

gracePeriod:long

the amount of time in milliseconds to wait between two redeploy even if there are changes

scanPeriod:long

the time in millisecond between 2 file system scans

Method Detail

addFilesToWatchedListback to summary
private void addFilesToWatchedList(List<File> roots)
addFileToWatchedListback to summary
private void addFileToWatchedList(File file)
changesHaveOccurredback to summary
private boolean changesHaveOccurred()

Checks whether or not a change has occurred in one of the watched file that match one of the given include pattern . Are detected: new files, modified file and deleted files. File modification is detected using File#lastModified(), so the behavior depends on the file system precision.

Returns:boolean

true if a change occurred requiring the redeployment.

closeback to summary
public void close()

Stops watching. This method stops the underlying WatchService.

executeUserCommandback to summary
private void executeUserCommand(Handler<Void> onCompletion)
extractRootsback to summary
pack-priv static List<File> extractRoots(File root, List<String> includes)
matchback to summary
protected boolean match(File file)

Checks whether the given file matches one of the includes patterns.

Parameters
file:File

the file

Returns:boolean

true if the file matches at least one pattern, false otherwise.

runback to summary
public void run()

Implements java.lang.Runnable.run.

The watching thread runnable method.

Annotations
@Override
sanitizeIncludePatternsback to summary
private List<String> sanitizeIncludePatterns(List<String> includes)
triggerback to summary
private void trigger()

Redeployment process.

watchback to summary
public Watcher watch()

Starts watching. The watch processing is made in another thread started in this method.

Returns:Watcher

the current watcher.

io.vertx.core.impl.launcher.commands back to summary

private final Class Watcher.FileInfo

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv long
pack-priv long

Constructor Summary

AccessConstructor and Description
private
FileInfo(long lastModified, long length)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

lastModifiedback to summary
pack-priv long lastModified
lengthback to summary
pack-priv long length

Constructor Detail

FileInfoback to summary
private FileInfo(long lastModified, long length)