use of org.apache.meecrowave.logging.tomcat.LogFacade in project meecrowave by apache.
the class ReloadOnChangeController method run.
@Override
public void run() {
if (watchService == null) {
return;
}
final CountDownLatch latch = new CountDownLatch(1);
bouncer = new Thread(() -> {
// simple bouncing impl
long last = redeployMarker;
latch.countDown();
boolean needsRedeploy = false;
long antepenultiem = -1;
while (running) {
if (redeployMarker > last) {
antepenultiem = last;
last = redeployMarker;
needsRedeploy = true;
} else if (needsRedeploy) {
antepenultiem = last;
}
try {
Thread.sleep(bouncing);
} catch (final InterruptedException e) {
Thread.interrupted();
break;
}
if (needsRedeploy && last == antepenultiem) {
new LogFacade(ReloadOnChangeController.class.getName()).info("Redeploying " + context.getName());
redeploy();
}
}
});
bouncer.setName("meecrowave-watcher-redeployer");
bouncer.start();
try {
latch.await(1, TimeUnit.MINUTES);
} catch (final InterruptedException e) {
Thread.interrupted();
return;
}
paths.forEach(p -> {
try {
Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
dir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
return FileVisitResult.CONTINUE;
}
});
} catch (final IOException e) {
new LogFacade(ReloadOnChangeController.class.getName()).warn(e.getMessage());
}
});
try {
while (running) {
final WatchKey watchKey = watchService.poll(bouncing, TimeUnit.MILLISECONDS);
if (watchKey == null) {
Thread.sleep(bouncing);
continue;
}
boolean foundNew = false;
for (final WatchEvent<?> event : watchKey.pollEvents()) {
final Path path = Path.class.cast(event.context());
final WatchEvent.Kind<?> kind = event.kind();
if (!isIgnored(kind, path)) {
foundNew = true;
final File file = path.toAbsolutePath().toFile();
if (file.isDirectory()) {
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
try {
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
} catch (final IOException e) {
new LogFacade(ReloadOnChangeController.class.getName()).warn(e.getMessage());
}
}
}
break;
}
}
if (foundNew) {
new LogFacade(ReloadOnChangeController.class.getName()).info("Marking to redeploy " + context.getName());
redeployMarker = System.nanoTime();
}
if (!watchKey.reset()) {
// deletion
watchKey.cancel();
}
}
} catch (final InterruptedException ie) {
Thread.interrupted();
}
}
Aggregations