getLog().info(
"Now listening for changes on path " + inputDirectory.getPath());
initServer();
DirWatcher dirWatcher = null;
try {
dirWatcher = new DirWatcher(inputDirectory);
final AtomicBoolean done = new AtomicBoolean(false);
final BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
(new Thread() {
@Override
public void run() {
try {
getLog()
.info("Running. Enter a blank line to finish. Anything else forces re-rendering.");
while (true) {
String line = reader.readLine();
if (isBlank(line)) {
break;
}
reRender();
}
} catch (Exception exc) {
getLog().info("Ooops", exc);
} finally {
done.set(true);
}
}
}).start();
dirWatcher.start();
do {
Long result = dirWatcher.processEvents();
if (null == result) {
// do nothing on purpose
} else if (result >= lastProcessed) {
getLog().info("Refreshing");
super.execute();
lastProcessed = Long.valueOf(System.currentTimeMillis());
}
} while (!done.get());
} catch (Exception exc) {
getLog().info("Oops", exc);
throw new MojoExecutionException("Oops", exc);
} finally {
getLog().info("Finishing");
if (null != dirWatcher)
dirWatcher.stop();
stopServer();
}
}