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);
}
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);
}
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;
}
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));
}
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));
}
Aggregations