Search in sources :

Example 26 with WatchKey

use of java.nio.file.WatchKey in project be5 by DevelopmentOnTheEdge.

the class WatchDir method register.

/**
 * Register the given directory with the WatchService
 */
private void register(Path dir) throws IOException {
    // System.out.println("Registering watcher on "+dir);
    WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
    keys.put(key, dir);
}
Also used : WatchKey(java.nio.file.WatchKey)

Example 27 with WatchKey

use of java.nio.file.WatchKey in project HotswapAgent by HotswapProjects.

the class AbstractNIO2Watcher method closeClassLoader.

/**
 * Remove all transformers registered with a classloader
 *
 * @param classLoader
 */
@Override
public void closeClassLoader(ClassLoader classLoader) {
    for (Iterator<Entry<WatchEventListener, ClassLoader>> entryIterator = classLoaderListeners.entrySet().iterator(); entryIterator.hasNext(); ) {
        Entry<WatchEventListener, ClassLoader> entry = entryIterator.next();
        if (entry.getValue().equals(classLoader)) {
            entryIterator.remove();
            try {
                for (Iterator<Entry<Path, List<WatchEventListener>>> listenersIterator = listeners.entrySet().iterator(); listenersIterator.hasNext(); ) {
                    Entry<Path, List<WatchEventListener>> pathListenerEntry = listenersIterator.next();
                    List<WatchEventListener> l = pathListenerEntry.getValue();
                    if (l != null) {
                        l.remove(entry.getKey());
                    }
                    if (l == null || l.isEmpty()) {
                        listenersIterator.remove();
                    }
                }
            } catch (Exception e) {
                LOGGER.error("Ooops", e);
            }
        }
    }
    // cleanup...
    if (classLoaderListeners.isEmpty()) {
        listeners.clear();
        for (WatchKey wk : keys.keySet()) {
            try {
                wk.cancel();
            } catch (Exception e) {
                LOGGER.error("Ooops", e);
            }
        }
        try {
            this.watcher.close();
        } catch (IOException e) {
            LOGGER.error("Ooops", e);
        }
        LOGGER.info("All classloaders closed, released watch service..");
        try {
            // Reset
            this.watcher = FileSystems.getDefault().newWatchService();
        } catch (IOException e) {
            LOGGER.error("Ooops", e);
        }
    }
    LOGGER.debug("All watch listeners removed for classLoader {}", classLoader);
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException) WatchEventListener(org.hotswap.agent.watch.WatchEventListener) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) List(java.util.List)

Example 28 with WatchKey

use of java.nio.file.WatchKey in project HotswapAgent by HotswapProjects.

the class AbstractNIO2Watcher method processEvents.

/**
 * Process all events for keys queued to the watcher
 *
 * @return true if should continue
 * @throws InterruptedException
 */
private boolean processEvents() throws InterruptedException {
    // wait for key to be signalled
    WatchKey key = watcher.poll(10, TimeUnit.MILLISECONDS);
    if (key == null) {
        return true;
    }
    PathPair dir = keys.get(key);
    if (dir == null) {
        LOGGER.warning("WatchKey '{}' not recognized", key);
        return true;
    }
    for (WatchEvent<?> event : key.pollEvents()) {
        WatchEvent.Kind<?> kind = event.kind();
        if (kind == OVERFLOW) {
            LOGGER.warning("WatchKey '{}' overflowed", key);
            continue;
        }
        // Context for directory entry event is the file name of entry
        WatchEvent<Path> ev = cast(event);
        Path name = ev.context();
        Path child = dir.resolve(name);
        LOGGER.debug("Watch event '{}' on '{}' --> {}", event.kind().name(), child, name);
        dispatcher.add(ev, child);
        // register it and its sub-directories
        if (kind == ENTRY_CREATE) {
            try {
                if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
                    registerAll(dir.getWatched(), child);
                }
            } catch (IOException x) {
                LOGGER.warning("Unable to register events for directory {}", x, child);
            }
        }
    }
    // reset key and remove from set if directory no longer accessible
    boolean valid = key.reset();
    if (!valid) {
        LOGGER.warning("Watcher on {} not valid, removing...", keys.get(key).getShortDescription());
        keys.remove(key);
        // all directories are inaccessible
        if (keys.isEmpty()) {
            return false;
        }
        if (classLoaderListeners.isEmpty()) {
            for (WatchKey k : keys.keySet()) {
                k.cancel();
            }
            return false;
        }
    }
    return true;
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) IOException(java.io.IOException)

Example 29 with WatchKey

use of java.nio.file.WatchKey in project HotswapAgent by HotswapProjects.

the class TreeWatcherNIO method register.

/**
 * Register the given directory with the WatchService.
 *
 * @param watched the watched path
 * @param target the target path (could be different than watched)
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void register(Path watched, Path target) throws IOException {
    for (PathPair p : keys.values()) {
        // This may NOT be correct for all cases (ensure resolve will work!)
        if (p.isWatching(target)) {
            LOGGER.debug("Path {} watched via {}", target, p.getWatched());
            return;
        }
    }
    if (FILE_TREE == null) {
        LOGGER.debug("WATCHING:ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY - high} {}", watched);
    } else {
        LOGGER.debug("WATCHING: ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY - fileTree,high {}", watched);
    }
    final WatchKey key = watched.register(watcher, KINDS, MODIFIERS);
    keys.put(key, new PathPair(target, watched));
}
Also used : WatchKey(java.nio.file.WatchKey)

Example 30 with WatchKey

use of java.nio.file.WatchKey in project HotswapAgent by HotswapProjects.

the class WatcherNIO2 method register.

/**
 * Register the given directory with the WatchService
 */
private void register(Path dir) throws IOException {
    // try to set high sensitivity
    final WatchKey key = HIGH == null ? dir.register(watcher, KINDS) : dir.register(watcher, KINDS, HIGH);
    keys.put(key, PathPair.get(dir));
}
Also used : WatchKey(java.nio.file.WatchKey)

Aggregations

WatchKey (java.nio.file.WatchKey)134 Path (java.nio.file.Path)88 WatchEvent (java.nio.file.WatchEvent)65 IOException (java.io.IOException)49 WatchService (java.nio.file.WatchService)30 File (java.io.File)27 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)18 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)7 Map (java.util.Map)7 FileSystems (java.nio.file.FileSystems)6 FileVisitResult (java.nio.file.FileVisitResult)6 HashSet (java.util.HashSet)6 List (java.util.List)6 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)5 InputStream (java.io.InputStream)4 AccessDeniedException (java.nio.file.AccessDeniedException)4 Kind (java.nio.file.WatchEvent.Kind)4 Logger (org.slf4j.Logger)4