use of java.nio.file.WatchEvent in project structr by structr.
the class DirectoryWatchService method run.
// ----- interface RunnableService -----
@Override
public void run() {
while (running) {
if (!Services.getInstance().isInitialized()) {
try {
Thread.sleep(100);
} catch (InterruptedException i) {
}
// loop until we are stopped
continue;
}
synchronized (watchedRoots) {
for (final FolderInfo info : watchedRoots.values()) {
if (info.shouldScan()) {
// update last scanned timestamp
info.setLastScanned(System.currentTimeMillis());
// start a new scan thread
new Thread(new ScanWorker(false, Paths.get(info.getRoot()), info.getRoot(), true)).start();
}
}
}
try {
final WatchKey key = watchService.poll(100, TimeUnit.MILLISECONDS);
if (key != null && Services.getInstance().isInitialized()) {
final Path root;
// synchronize access to map but keep critical section short
synchronized (watchKeyMap) {
root = watchKeyMap.get(key);
}
if (root != null) {
final SecurityContext securityContext = SecurityContext.getSuperUserInstance();
try (final Tx tx = StructrApp.getInstance(securityContext).tx(true, true, false)) {
for (final WatchEvent event : key.pollEvents()) {
final Kind kind = event.kind();
if (OVERFLOW.equals(kind)) {
continue;
}
if (!handleWatchEvent(true, root, (Path) key.watchable(), event)) {
System.out.println("cancelling watch key for " + key.watchable());
key.cancel();
}
}
tx.success();
} catch (Throwable t) {
t.printStackTrace();
} finally {
key.reset();
}
} else {
key.cancel();
}
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("DirectoryWatchService ended..");
}
use of java.nio.file.WatchEvent in project zeppelin by apache.
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.WatchEvent in project streamsx.topology by IBMStreams.
the class DirectoryWatcher method process.
@SuppressWarnings("unchecked")
@Override
protected void process() throws Exception {
Path dir = dirFile.toPath();
WatchService watcher = FileSystems.getDefault().newWatchService();
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
sortAndSubmit(Arrays.asList(dirFile.listFiles(this)));
for (; !Thread.interrupted(); ) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException e) {
// shutdown has been requested
return;
}
List<File> newFiles = new ArrayList<>();
boolean needFullScan = false;
for (WatchEvent<?> watchEvent : key.pollEvents()) {
if (ENTRY_CREATE == watchEvent.kind()) {
Path newPath = ((WatchEvent<Path>) watchEvent).context();
File newFile = toFile(newPath);
if (accept(newFile))
newFiles.add(newFile);
} else if (ENTRY_DELETE == watchEvent.kind()) {
Path deletedPath = ((WatchEvent<Path>) watchEvent).context();
File deletedFile = toFile(deletedPath);
seenFiles.remove(deletedFile.getName());
} else if (OVERFLOW == watchEvent.kind()) {
needFullScan = true;
}
}
key.reset();
if (needFullScan) {
Collections.addAll(newFiles, dirFile.listFiles(this));
}
sortAndSubmit(newFiles);
}
}
use of java.nio.file.WatchEvent in project java-swing-tips by aterai.
the class TablePopupMenu method processEvents.
// Watching a Directory for Changes (The Java™ Tutorials > Essential Classes > Basic I/O)
// https://docs.oracle.com/javase/tutorial/essential/io/notification.html
// Process all events for keys queued to the watcher
public void processEvents(Path dir, WatchService watcher) {
for (; ; ) {
// wait for key to be signaled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException ex) {
EventQueue.invokeLater(() -> append("Interrupted"));
Thread.currentThread().interrupt();
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
// are lost or discarded.
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
// @SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event;
// The filename is the context of the event.
Path filename = (Path) event.context();
Path child = dir.resolve(filename);
EventQueue.invokeLater(() -> {
append(String.format("%s: %s", kind, child));
updateTable(kind, child);
});
}
// Reset the key -- this step is critical if you want to
// receive further watch events. If the key is no longer valid,
// the directory is inaccessible so exit the loop.
boolean valid = key.reset();
if (!valid) {
break;
}
}
}
use of java.nio.file.WatchEvent in project karaf by apache.
the class AutoEncryptionSupport method run.
@Override
public void run() {
WatchService watchService = null;
try {
watchService = FileSystems.getDefault().newWatchService();
Path dir = null;
Path file = null;
if (usersFileName == null) {
dir = Paths.get(System.getProperty("karaf.etc"));
file = dir.resolve("users.properties");
} else {
file = new File(usersFileName).toPath();
dir = file.getParent();
}
dir.register(watchService, ENTRY_MODIFY);
encryptedPassword(new Properties(file.toFile()));
while (running) {
try {
WatchKey key = watchService.poll(1, TimeUnit.SECONDS);
if (key == null) {
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
@SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event;
// Context for directory entry event is the file name of entry
Path name = dir.resolve(ev.context());
if (file.equals(name)) {
encryptedPassword(new Properties(file.toFile()));
}
}
key.reset();
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
} catch (InterruptedException e) {
// Ignore as this happens on shutdown
}
}
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
} finally {
StreamUtils.close(watchService);
}
}
Aggregations