Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
WatchService Provides File Name Instead of Relative Path (#1597)
Browse files Browse the repository at this point in the history
* The watch service is now providing the relative path to the file when a watch event is processed. The directory modifications can be now optionally watched. Added more tests to ensure that the watch service is adequately working (some tests are platform dependent). The list of handled test events is now synchronized as several threads are accessing it. FolderObserver and ConfigDispatcher are adapted to the changes in the AbstractWatchService.

Signed-off-by: Dimitar Ivanov <dstivanov@gmail.com>
  • Loading branch information
dstivanov authored and Svilen Valkanov committed Nov 10, 2016
1 parent df8eb6f commit 785e675
Show file tree
Hide file tree
Showing 5 changed files with 542 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -157,8 +158,9 @@ protected boolean watchSubDirectories() {
* (java.nio.file.Path)
*/
@Override
protected void registerDirectory(Path subDir) throws IOException {
subDir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
protected WatchKey registerDirectory(Path subDir) throws IOException {
WatchKey registrationKey = subDir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
return registrationKey;
}

/*
Expand All @@ -169,8 +171,9 @@ protected void registerDirectory(Path subDir) throws IOException {
* (java.nio.file.WatchService, java.nio.file.Path)
*/
@Override
protected AbstractWatchQueueReader buildWatchQueueReader(WatchService watchService, Path toWatch) {
return new WatchQueueReader(watchService, toWatch);
protected AbstractWatchQueueReader buildWatchQueueReader(WatchService watchService, Path toWatch,
Map<WatchKey, Path> registredWatchKeys) {
return new WatchQueueReader(watchService, toWatch, registredWatchKeys);
}

private String getDefaultServiceConfigFile() {
Expand Down Expand Up @@ -315,15 +318,15 @@ private String[] parseLine(final String filePath, final String line) {

private class WatchQueueReader extends AbstractWatchQueueReader {

public WatchQueueReader(WatchService watchService, Path dir) {
super(watchService, dir);
public WatchQueueReader(WatchService watchService, Path dir, Map<WatchKey, Path> registeredKeys) {
super(watchService, dir, registeredKeys);
}

@Override
protected void processWatchEvent(WatchEvent<?> event, Kind<?> kind, Path path) {
if (kind == ENTRY_CREATE || kind == ENTRY_MODIFY) {
try {
processConfigFile(new File(dir.toAbsolutePath() + File.separator + path.toString()));
processConfigFile(new File(baseWatchedDir.toAbsolutePath() + File.separator + path.toString()));
} catch (IOException e) {
logger.warn("Could not process config file '{}': {}", path, e);
}
Expand Down
Loading

0 comments on commit 785e675

Please sign in to comment.