public void run() {
// loop forever, waiting on monitor for watchService, unless isTerminateThread
for (;;) {
// wait for watchService to become signaled
WatchKey key;
try {
key = watchService.take();
if (isTerminateThread()) {
return;
}
} catch (InterruptedException x) {
return;
}
Path dir = monitoredKeys.get(key);
if (dir == null) {
LOG.info("did not recognize the requested WatchKey!");
continue;
}
List<WatchEvent<?>> events = key.pollEvents();
// process all events on the key
for (WatchEvent<?> event : events) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == OVERFLOW) {
// TODO - perhaps notify a special event to clear all caches,
// if file changes overflow the monitor
LOG.info("WatchService for aura file changes has overflowed. Changes may have been missed.");
continue;
}
// once we have a directory event, we know the context is the file name of entry
WatchEvent<Path> pathWatchEvent = cast(event);
Path name = pathWatchEvent.context();
// ensure the path resolution (absolute, relative) matches between paths
Path child = dir.resolve(name);
// isDir is true is file exists and is directory
boolean isDir = Files.isDirectory(child, NOFOLLOW_LINKS);
// signal appropriate handlers
if (!isDir) {
try {
if (kind == ENTRY_CREATE) {
listener.fileCreated(new FileChangeEvent(child));
}
else if (kind == ENTRY_MODIFY) {
listener.fileChanged(new FileChangeEvent(child));
}
else if (kind == ENTRY_DELETE) {
listener.fileDeleted(new FileChangeEvent(child));
}
} catch (Exception ex) {
LOG.info("Unable to signal source change due to exception: " + ex.getMessage());
}
}
// recursively add any new directories created
else if (kind == ENTRY_CREATE) {
try {
registerAll(child);
} catch (IOException x) {
// if we can't monitor it for some reason, it is not an error
}
}
}
// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
monitoredKeys.remove(key);
monitoredDirs.remove(dir.toString());
// all directories are inaccessible