use of java.nio.file.WatchKey in project SSM by Intel-bigdata.
the class InterpreterOutputChangeWatcher method clear.
public void clear() {
synchronized (watchKeys) {
for (WatchKey key : watchKeys.keySet()) {
key.cancel();
}
watchKeys.clear();
watchFiles.clear();
}
}
use of java.nio.file.WatchKey in project SSM by Intel-bigdata.
the class InterpreterOutputChangeWatcher method run.
public void run() {
while (!stop) {
WatchKey key = null;
try {
key = watcher.poll(1, TimeUnit.SECONDS);
} catch (InterruptedException | ClosedWatchServiceException e) {
break;
}
if (key == null) {
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == OVERFLOW) {
continue;
}
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path filename = ev.context();
// search for filename
synchronized (watchKeys) {
for (File f : watchFiles) {
if (f.getName().compareTo(filename.toString()) == 0) {
File changedFile;
if (filename.isAbsolute()) {
changedFile = new File(filename.toString());
} else {
changedFile = new File(watchKeys.get(key), filename.toString());
}
logger.info("File change detected " + changedFile.getAbsolutePath());
if (listener != null) {
listener.fileChanged(changedFile);
}
}
}
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
}
use of java.nio.file.WatchKey in project syncany by syncany.
the class DefaultRecursiveWatcher method registerWatch.
private synchronized void registerWatch(Path dir) {
if (!watchPathKeyMap.containsKey(dir)) {
logger.log(Level.INFO, "- Registering " + dir);
try {
WatchKey watchKey = dir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY, OVERFLOW);
watchPathKeyMap.put(dir, watchKey);
} catch (IOException e) {
logger.log(Level.FINE, "IO Failed", e);
}
}
}
use of java.nio.file.WatchKey in project Payara by payara.
the class DirWatcher method syncWatches.
private synchronized void syncWatches() {
while (true) {
WatchKey key = watchService.poll();
if (key == null) {
return;
}
Path root = (Path) key.watchable();
WatchProcessor processor = processors.get(root);
if (processor == null || processor.isCancelled()) {
key.cancel();
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
boolean keepRegistered = true;
if (kind == OVERFLOW) {
LOGGER.warning(() -> "File system watcher overflowed for " + root);
keepRegistered = processor.overflowed();
} else {
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path filename = ev.context();
if (kind == ENTRY_CREATE) {
keepRegistered = processor.created(filename);
}
if (kind == ENTRY_DELETE) {
keepRegistered = processor.deleted(filename);
}
}
if (!keepRegistered) {
key.cancel();
}
}
boolean valid = key.reset();
if (!valid) {
processor.cancelled();
}
}
}
use of java.nio.file.WatchKey in project Payara by payara.
the class PayaraFileWatcher method run.
// Private methods
/**
* The event loop method
*/
private void run() {
registerQueuedPaths();
try {
// Block, waiting for an event on a watched file
WatchKey key = watcher.take();
// Loop through the events
for (WatchEvent<?> event : key.pollEvents()) {
// Find the absolute path of the modified file
final Path modifiedPath = ((Path) key.watchable()).resolve((Path) event.context());
// Loop through the watched paths to find the action associated with it
Iterator<Entry<Path, Runnable>> watchIterator = LISTENER_MAP.entrySet().iterator();
while (watchIterator.hasNext()) {
Entry<Path, Runnable> watchEntry = watchIterator.next();
// If this entry corresponds to the modified file
if (modifiedPath.endsWith(watchEntry.getKey())) {
// Ignore overflow events
if (event.kind() == StandardWatchEventKinds.OVERFLOW) {
continue;
}
File modifiedFile = modifiedPath.toFile();
// If it's been deleted, remove the file watcher
if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE && !modifiedFile.exists()) {
LOGGER.info(format("Watched file %s was deleted; removing the file watcher", modifiedPath));
watchIterator.remove();
} else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY && modifiedFile.length() > 0) {
// Run the associated action
LOGGER.fine(format("Watched file %s modified, running the listener", modifiedPath));
watchEntry.getValue().run();
}
}
}
}
key.reset();
} catch (InterruptedException ex) {
LOGGER.log(WARNING, "The file watcher thread was interrupted", ex);
}
}
Aggregations