use of java.nio.file.WatchKey in project JavaForFun by gumartinm.
the class InotifyTestMain method main.
public static void main(final String[] args) throws IOException, ExecutionException, InterruptedException {
final ExecutorService exec = Executors.newSingleThreadExecutor();
final Path filePath = FileSystems.getDefault().getPath(directory);
final Inotify task = new Inotify(filePath);
while (true) {
final Future<WatchKey> futureTask = exec.submit(task);
final WatchKey token = futureTask.get();
for (final WatchEvent<?> event : token.pollEvents()) {
sortEvent(event);
}
if (!token.reset()) {
break;
}
}
}
use of java.nio.file.WatchKey in project be5 by DevelopmentOnTheEdge.
the class WatchDir method processEvents.
/**
* Process all events for keys queued to the watcher
*/
private void processEvents() {
while (!stopped) {
// wait for key to be signalled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
if (stopped)
return;
Path dir = keys.get(key);
if (dir == null) {
// WatchKey not recognized
continue;
}
final List<WatchEvent<?>> events = key.pollEvents();
for (WatchEvent<?> event : events) {
if (stopped) {
return;
}
WatchEvent.Kind<?> kind = event.kind();
// TBD - provide example of how OVERFLOW event is handled
if (kind == OVERFLOW) {
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);
// handle
if (kind == ENTRY_MODIFY) {
// skip timestamp modification
if (Files.isRegularFile(child)) {
Long previouslyModified = lastModifiedByPath.get(child);
long lastModified = child.toFile().lastModified();
if (previouslyModified != null && (lastModified - previouslyModified) > 100) {
lastModifiedByPath.put(child, lastModified);
continue;
}
lastModifiedByPath.put(child, lastModified);
}
onModify.accept(child);
}
// register it and its sub-directories
if (recursive && (kind == ENTRY_CREATE)) {
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
// TODO: register only interesting new directories
registerAll(child);
}
} catch (IOException x) {
// ignore to keep sample readable
}
}
}
// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
keys.remove(key);
// all directories are inaccessible
if (keys.isEmpty()) {
break;
}
}
}
}
use of java.nio.file.WatchKey in project MassiveCore by MassiveCraft.
the class PusherCollFlatfile method run.
// -------------------------------------------- //
// OVERRIDE: THREAD
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public void run() {
// MassiveCore.get().log("Starting Pusher for " + coll.getBasename());
while (true) {
try {
WatchKey key = this.watcher.take();
Path dir = this.keys.get(key);
if (dir == null) {
// System.err.println("WatchKey not recognized!!");
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
handleEvent((WatchEvent<Path>) event, dir);
}
boolean valid = key.reset();
if (!valid)
this.keys.remove(key);
} catch (InterruptedException e) {
// We've been interrupted. Lets bail.
return;
} catch (Exception e) {
System.out.println("Pusher error for" + this.coll.getDebugName());
e.printStackTrace();
} finally {
handledIds.clear();
}
}
}
use of java.nio.file.WatchKey in project MassiveCore by MassiveCraft.
the class PusherCollFlatfile method register.
// -------------------------------------------- //
// REGISTER
// -------------------------------------------- //
public void register(Path path) throws IOException {
if (Files.notExists(path))
throw new IllegalArgumentException(path.toString() + " does not exist.");
WatchKey key = path.register(watcher, EVENT_TYPES);
// System.out.format("register: %s\n", path);
keys.put(key, path);
}
use of java.nio.file.WatchKey in project wildfly-swarm by wildfly-swarm.
the class Main method processEvents.
@SuppressWarnings("unchecked")
private static void processEvents(File watchedDir, Path file) {
for (; ; ) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
Kind<?> kind = event.kind();
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path name = ev.context();
Path child = watchedDir.toPath().resolve(name);
if (kind == ENTRY_DELETE && child.equals(file)) {
return;
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
}
Aggregations