Search in sources :

Example 1 with PathWatchEvent

use of org.eclipse.jetty.util.PathWatcher.PathWatchEvent in project jetty.project by eclipse.

the class JettyRunWarExplodedMojo method configureScanner.

/**
     * @see AbstractJettyMojo#configureScanner()
     */
public void configureScanner() throws MojoExecutionException {
    scanner.watch(project.getFile().toPath());
    File webInfDir = new File(war, "WEB-INF");
    File webXml = new File(webInfDir, "web.xml");
    if (webXml.exists())
        scanner.watch(webXml.toPath());
    File jettyWebXmlFile = findJettyWebXmlFile(webInfDir);
    if (jettyWebXmlFile != null)
        scanner.watch(jettyWebXmlFile.toPath());
    File jettyEnvXmlFile = new File(webInfDir, "jetty-env.xml");
    if (jettyEnvXmlFile.exists())
        scanner.watch(jettyEnvXmlFile.toPath());
    File classes = new File(webInfDir, "classes");
    if (classes.exists()) {
        PathWatcher.Config classesConfig = new PathWatcher.Config(classes.toPath());
        classesConfig.setRecurseDepth(PathWatcher.Config.UNLIMITED_DEPTH);
        scanner.watch(classesConfig);
    }
    File lib = new File(webInfDir, "lib");
    if (lib.exists()) {
        PathWatcher.Config libConfig = new PathWatcher.Config(lib.toPath());
        libConfig.setRecurseDepth(PathWatcher.Config.UNLIMITED_DEPTH);
        scanner.watch(libConfig);
    }
    scanner.addListener(new PathWatcher.EventListListener() {

        @Override
        public void onPathWatchEvents(List<PathWatchEvent> events) {
            try {
                boolean reconfigure = false;
                for (PathWatchEvent e : events) {
                    if (e.getPath().equals(project.getFile().toPath())) {
                        reconfigure = true;
                        break;
                    }
                }
                restartWebApp(reconfigure);
            } catch (Exception e) {
                getLog().error("Error reconfiguring/restarting webapp after change in watched files", e);
            }
        }
    });
}
Also used : PathWatcher(org.eclipse.jetty.util.PathWatcher) PathWatchEvent(org.eclipse.jetty.util.PathWatcher.PathWatchEvent) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 2 with PathWatchEvent

use of org.eclipse.jetty.util.PathWatcher.PathWatchEvent in project jetty.project by eclipse.

the class PathWatcherDemo method run.

public void run(List<Path> paths) throws Exception {
    PathWatcher watcher = new PathWatcher();
    //watcher.addListener(new PathWatcherDemo());
    watcher.addListener(new PathWatcher.EventListListener() {

        @Override
        public void onPathWatchEvents(List<PathWatchEvent> events) {
            if (events == null)
                LOG.warn("Null events received");
            if (events.isEmpty())
                LOG.warn("Empty events received");
            LOG.info("Bulk notification received");
            for (PathWatchEvent e : events) onPathWatchEvent(e);
        }
    });
    watcher.setNotifyExistingOnStart(false);
    List<String> excludes = new ArrayList<>();
    // ignore backup files
    excludes.add("glob:*.bak");
    // ignore scratch files
    excludes.add("regex:^.*/\\~[^/]*$");
    for (Path path : paths) {
        if (Files.isDirectory(path)) {
            PathWatcher.Config config = new PathWatcher.Config(path);
            config.addExcludeHidden();
            config.addExcludes(excludes);
            config.setRecurseDepth(4);
            watcher.watch(config);
        } else {
            watcher.watch(path);
        }
    }
    watcher.start();
    Thread.currentThread().join();
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) PathWatchEvent(org.eclipse.jetty.util.PathWatcher.PathWatchEvent)

Aggregations

PathWatchEvent (org.eclipse.jetty.util.PathWatcher.PathWatchEvent)2 File (java.io.File)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 PathWatcher (org.eclipse.jetty.util.PathWatcher)1