use of java.nio.file.WatchKey in project knime-core by knime.
the class SleepNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
if (m_selection == 0) {
// wait for
exec.setMessage("Waiting for " + (m_waittime / 1000) + " seconds");
waitFor(m_waittime);
} else if (m_selection == 1) {
// wait to
Calendar c = Calendar.getInstance();
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), m_toHours, m_toMin, m_toSec);
if (c.getTimeInMillis() - System.currentTimeMillis() <= 0) {
c.add(Calendar.DAY_OF_YEAR, 1);
}
exec.setMessage("Waiting until " + c.getTime());
final long sleepTime = c.getTimeInMillis() - System.currentTimeMillis();
waitFor(sleepTime);
} else if (m_selection == 2) {
WatchService w = FileSystems.getDefault().newWatchService();
Path p = FileUtil.resolveToPath(FileUtil.toURL(m_filePath));
if (p == null) {
throw new IllegalArgumentException("File location '" + m_filePath + "' is not a local file.");
}
exec.setMessage("Waiting for file '" + p + "'");
Path fileName = p.subpath(p.getNameCount() - 1, p.getNameCount());
Path parent = p.getParent();
Kind<Path> e = null;
if (m_fileStatus.equals("Creation")) {
e = StandardWatchEventKinds.ENTRY_CREATE;
} else if (m_fileStatus.equals("Modification")) {
e = StandardWatchEventKinds.ENTRY_MODIFY;
} else if (m_fileStatus.equals("Deletion")) {
e = StandardWatchEventKinds.ENTRY_DELETE;
} else {
throw new RuntimeException("Selected watchservice event is not available. Selected watchservice : " + m_fileStatus);
}
parent.register(w, e);
boolean keepLooking = true;
while (keepLooking) {
// watch file until the event appears
WatchKey key;
// wait for a key to be available
key = w.take();
for (WatchEvent<?> event : key.pollEvents()) {
if (fileName.equals(event.context())) {
keepLooking = false;
}
}
// reset key
boolean valid = key.reset();
if (!valid) {
break;
}
}
}
if (inData[0] == null) {
return new PortObject[] { FlowVariablePortObject.INSTANCE };
} else {
return inData;
}
}
use of java.nio.file.WatchKey in project fabric8 by jboss-fuse.
the class FileWatcher method processEvents.
private void processEvents() {
while (true) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException e) {
return;
}
Path dir;
synchronized (keysMonitor) {
dir = keys.get(key);
}
if (dir == null) {
LOGGER.warn("Could not find key for " + key);
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
WatchEvent<Path> ev = (WatchEvent<Path>) event;
// Context for directory entry event is the file name of entry
Path name = ev.context();
Path child = dir.resolve(name);
LOGGER.debug("Processing event {} on path {}", kind, child);
if (kind == OVERFLOW) {
// rescan();
continue;
}
try {
if (kind == ENTRY_CREATE) {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
// if directory is created, and watching recursively, then
// register it and its sub-directories
Files.walkFileTree(child, new FilteringFileVisitor());
} else if (Files.isRegularFile(child, NOFOLLOW_LINKS)) {
scan(child);
}
} else if (kind == ENTRY_MODIFY) {
if (Files.isRegularFile(child, NOFOLLOW_LINKS)) {
scan(child);
}
} else if (kind == ENTRY_DELETE) {
unscan(child);
}
} catch (IOException x) {
// ignore to keep sample readbale
x.printStackTrace();
}
}
// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
LOGGER.debug("Removing key " + key + " and dir " + dir + " from keys");
keys.remove(key);
// all directories are inaccessible
if (keys.isEmpty()) {
break;
}
}
}
}
use of java.nio.file.WatchKey in project fabric8 by jboss-fuse.
the class FileWatcher method watch.
private void watch(final Path path) throws IOException {
if (watcher != null) {
synchronized (keysMonitor) {
WatchKey key = path.register(watcher, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
keys.put(key, path);
}
} else {
LOGGER.warn("No watcher yet for path " + path);
}
}
use of java.nio.file.WatchKey in project smarthome by eclipse.
the class WatchQueueReader method run.
@Override
public void run() {
try {
for (; ; ) {
WatchKey key;
try {
key = watchService.take();
} catch (InterruptedException exc) {
logger.info("Caught InterruptedException: {}", exc.getLocalizedMessage());
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == OVERFLOW) {
logger.warn("Found an event of kind 'OVERFLOW': {}. File system changes might have been missed.", event);
continue;
}
Path resolvedPath = resolvePath(key, event);
if (resolvedPath != null) {
// Process the event only when a relative path to it is resolved
AbstractWatchService service;
synchronized (this) {
service = keyToService.get(key);
}
if (service != null) {
File f = resolvedPath.toFile();
if (kind == ENTRY_MODIFY && f.isDirectory()) {
logger.trace("Skipping modification event for directory: {}", f);
} else {
if (kind == ENTRY_MODIFY) {
processModificationEvent(key, event, resolvedPath, service);
} else {
service.processWatchEvent(event, kind, resolvedPath);
}
}
if (kind == ENTRY_CREATE && f.isDirectory() && service.watchSubDirectories() && service.getWatchEventKinds(resolvedPath) != null) {
registerDirectoryInternal(service, service.getWatchEventKinds(resolvedPath), resolvedPath);
} else if (kind == ENTRY_DELETE) {
synchronized (this) {
WatchKey toCancel = null;
for (WatchKey k : registeredKeys.keySet()) {
if (registeredKeys.get(k).equals(resolvedPath)) {
toCancel = k;
break;
}
}
if (toCancel != null) {
registeredKeys.remove(toCancel);
keyToService.remove(toCancel);
toCancel.cancel();
}
forgetChecksum(service, resolvedPath);
Map<Path, @Nullable ScheduledFuture<?>> keyFutures = futures.get(key);
if (keyFutures != null) {
ScheduledFuture<?> future = keyFutures.remove(resolvedPath);
if (future != null) {
future.cancel(true);
}
}
}
}
}
}
}
key.reset();
}
} catch (Exception exc) {
logger.debug("ClosedWatchServiceException caught! {}. \n{} Stopping ", exc.getLocalizedMessage(), Thread.currentThread().getName());
return;
}
}
use of java.nio.file.WatchKey in project smarthome by eclipse.
the class WatchQueueReader method registerDirectoryInternal.
private synchronized void registerDirectoryInternal(AbstractWatchService service, Kind<?>[] kinds, Path directory) {
if (watchService == null) {
try {
watchService = FileSystems.getDefault().newWatchService();
qr = new Thread(this, "Dir Watcher");
qr.start();
} catch (IOException e) {
logger.debug("The directory '{}' was not registered in the watch service", directory, e);
return;
}
}
WatchKey registrationKey = null;
try {
registrationKey = directory.register(this.watchService, kinds);
} catch (IOException e) {
logger.debug("The directory '{}' was not registered in the watch service: {}", directory, e.getMessage());
}
if (registrationKey != null) {
registeredKeys.put(registrationKey, directory);
keyToService.put(registrationKey, service);
} else {
logger.debug("The directory '{}' was not registered in the watch service", directory);
}
}
Aggregations