public void startWatching() {
for (;;) {
WatchKey watchKey;
try {
watchKey = watchService.take();
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
return;
}
Path path = mapOfWatchKeysToPaths.get(watchKey);
if (path == null) {
System.err.println("WatchKey not recognized!!");
continue;
}
for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {
WatchEvent.Kind watchEventKind = watchEvent.kind();
// TBD - provide example of how OVERFLOW watchEvent is handled
if (watchEventKind == OVERFLOW) {
continue;
}
// Context for directory entry watchEvent is the file name of entry
WatchEvent<Path> ev = (WatchEvent<Path>) watchEvent;
Path name = ev.context();
Path child = path.resolve(name);
// print out watchEvent
//System.out.format("%s: %s\n", watchEvent.kind().name(), child);
if (watchEventKind == ENTRY_MODIFY) {
// we are not interested in events from parent directories...
if (! child.toFile().isDirectory()) {
if (!checkIfMatchesPattern(exludePatterns, child.toFile().getAbsolutePath())) {
System.out.println(
"Found file modification - triggering reload: "
+ child.toFile().getAbsolutePath());
restartAfterSomeTimeAndChanges.triggerRestart();
}
}
}
// ninjaJettyInsideSeparateJvm.restartNinjaJetty();
// if directory is created, then
// register it and its sub-directories recursively
if (watchEventKind == ENTRY_CREATE) {
restartAfterSomeTimeAndChanges.triggerRestart();
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException ioException) {
ioException.printStackTrace();
// ignore to keep sample readable
}
}
}
// reset watchKey and remove from set if directory no longer accessible
boolean valid = watchKey.reset();
if (!valid) {
mapOfWatchKeysToPaths.remove(watchKey);
// all directories are inaccessible
if (mapOfWatchKeysToPaths.isEmpty()) {