}
}
private void processEvents() {
while (true) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException e) {
return;
}
Path dir = keys.get(key);
if (dir == null) {
LOGGER.warn("Could not find key for " + key);
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
WatchEvent<Path> ev = (WatchEvent<Path>)event;
// Context for directory entry event is the file name of entry
Path name = ev.context();
Path child = dir.resolve(name);
LOGGER.debug("Processing event {} on path {}", kind, child);
if (kind == OVERFLOW) {
// rescan();
continue;
}
try {
if (kind == ENTRY_CREATE) {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
// if directory is created, and watching recursively, then
// register it and its sub-directories
Files.walkFileTree(child, new FilteringFileVisitor());
} else if (Files.isRegularFile(child, NOFOLLOW_LINKS)) {
scan(child);
}
} else if (kind == ENTRY_MODIFY) {
if (Files.isRegularFile(child, NOFOLLOW_LINKS)) {
scan(child);
}
} else if (kind == ENTRY_DELETE) {
unscan(child);
}
} catch (IOException x) {
// ignore to keep sample readbale
x.printStackTrace();
}
}
// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
LOGGER.debug("Removing key " + key + " and dir " + dir + " from keys");
keys.remove(key);
// all directories are inaccessible