use of java.nio.file.WatchKey in project jetty.project by eclipse.
the class PathWatcher method register.
/**
* Register a path (directory) with the WatchService.
*
* @param dir the directory to register
* @param root the configuration root
* @throws IOException if unable to register the path with the watch service.
*/
protected void register(Path dir, Config root) throws IOException {
LOG.debug("Registering watch on {}", dir);
if (watchModifiers != null) {
// Java Watcher
WatchKey key = dir.register(watchService, WATCH_EVENT_KINDS, watchModifiers);
keys.put(key, root.asSubConfig(dir));
} else {
// Native Watcher
WatchKey key = dir.register(watchService, WATCH_EVENT_KINDS);
keys.put(key, root.asSubConfig(dir));
}
}
use of java.nio.file.WatchKey in project cas by apereo.
the class ServiceRegistryConfigWatcher method handleEvent.
/**
* Handle event.
*
* @param key the key
*/
private void handleEvent(final WatchKey key) {
this.readLock.lock();
try {
//The filename is the context of the event.
key.pollEvents().stream().filter(event -> event.count() <= 1).forEach(event -> {
final WatchEvent.Kind kind = event.kind();
final WatchEvent<Path> ev = (WatchEvent<Path>) event;
final Path filename = ev.context();
final Path parent = (Path) key.watchable();
final Path fullPath = parent.resolve(filename);
final File file = fullPath.toFile();
LOGGER.trace("Detected event [{}] on file [{}]. Loading change...", kind, file);
if (kind.name().equals(ENTRY_CREATE.name()) && file.exists()) {
handleCreateEvent(file);
} else if (kind.name().equals(ENTRY_DELETE.name())) {
handleDeleteEvent();
} else if (kind.name().equals(ENTRY_MODIFY.name()) && file.exists()) {
handleModifyEvent(file);
}
});
} finally {
this.readLock.unlock();
}
}
use of java.nio.file.WatchKey in project che by eclipse.
the class FileWatcherService method register.
/**
* Registers a directory for tracking of corresponding entry creation,
* modification or deletion events. Each call of this method increase
* by one registration counter that corresponds to each folder being
* watched. Any event related to such directory entry is passed further to
* the specific handler only if registration counter related to the
* directory is above zero, otherwise registration watch key is canceled
* and no further directory watching is being performed.
*
* @param dir
* directory
*/
public void register(Path dir) {
LOG.debug("Registering directory '{}'", dir);
if (keys.values().contains(dir)) {
int previous = registrations.get(dir);
LOG.debug("Directory is already being watched, increasing watch counter, previous value: {}", previous);
registrations.put(dir, previous + 1);
} else {
try {
LOG.debug("Starting watching directory '{}'", dir);
WatchKey watchKey = dir.register(service, eventKinds, eventModifiers);
keys.put(watchKey, dir);
registrations.put(dir, 1);
} catch (IOException e) {
LOG.error("Can't register dir {} in file watch service", dir, e);
}
}
}
use of java.nio.file.WatchKey in project asterixdb by apache.
the class FileSystemWatcher method register.
/**
* Register the given directory, and all its sub-directories, with the
* WatchService.
*/
private void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
keys.put(key, dir);
}
use of java.nio.file.WatchKey in project asterixdb by apache.
the class FileSystemWatcher method take.
// take is blocking
public File take() throws IOException {
File next = poll();
if (next != null) {
return next;
}
if (done || !isFeed) {
return null;
}
// No file was found, wait for the filesystem to push events
WatchKey key;
while (!it.hasNext()) {
try {
key = watcher.take();
} catch (InterruptedException x) {
if (LOGGER.isEnabledFor(Level.WARN)) {
LOGGER.warn("Feed Closed");
}
if (watcher == null) {
return null;
}
continue;
} catch (ClosedWatchServiceException e) {
if (LOGGER.isEnabledFor(Level.WARN)) {
LOGGER.warn("The watcher has exited");
}
if (watcher == null) {
return null;
}
continue;
}
handleEvents(key);
if (endOfEvents(key)) {
return null;
}
}
// files were found, re-create the iterator and move it one step
return it.next();
}
Aggregations