Search in sources :

Example 6 with LogFacade

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();
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) LogFacade(org.apache.meecrowave.logging.tomcat.LogFacade) WatchEvent(java.nio.file.WatchEvent) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

LogFacade (org.apache.meecrowave.logging.tomcat.LogFacade)6 IOException (java.io.IOException)5 Path (java.nio.file.Path)4 File (java.io.File)3 InputStream (java.io.InputStream)3 Field (java.lang.reflect.Field)3 Collection (java.util.Collection)3 Objects (java.util.Objects)3 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 FileWriter (java.io.FileWriter)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 ManagementFactory (java.lang.management.ManagementFactory)2 MalformedURLException (java.net.MalformedURLException)2 ServerSocket (java.net.ServerSocket)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 StandardCharsets (java.nio.charset.StandardCharsets)2