use of java.nio.file.WatchKey in project fabric8 by jboss-fuse.
the class FileWatcher method rescan.
// Implementation methods
// -------------------------------------------------------------------------
private void rescan() throws IOException {
try {
synchronized (processing) {
while (processing.get() > 0) {
processing.wait();
}
}
for (WatchKey key : keys.keySet()) {
key.cancel();
}
keys.clear();
Files.walkFileTree(root, new FilteringFileVisitor());
synchronized (processing) {
while (processing.get() > 0) {
processing.wait();
}
}
} catch (InterruptedException e) {
throw (IOException) new InterruptedIOException().initCause(e);
}
}
use of java.nio.file.WatchKey in project Singularity by HubSpot.
the class WatchServiceHelper method watch.
public void watch() throws IOException, InterruptedException {
LOG.info("Watching directory {} for event(s) {}", watchDirectory, watchEvents);
WatchKey watchKey = watchDirectory.register(watchService, watchEvents.toArray(new WatchEvent.Kind[watchEvents.size()]));
while (!stopped) {
if (watchKey != null) {
processWatchKey(watchKey);
if (!watchKey.reset()) {
LOG.warn("WatchKey for {} no longer valid", watchDirectory);
break;
}
}
watchKey = watchService.poll(pollWaitCheckShutdownMillis, TimeUnit.MILLISECONDS);
}
}
use of java.nio.file.WatchKey in project spf4j by zolyfarkas.
the class TSDBReader method watch.
// CHECKSTYLE:OFF
@SuppressFBWarnings("NOS_NON_OWNED_SYNCHRONIZATION")
public <E extends Exception> void watch(final Handler<Either<TableDef, DataBlock>, E> handler, final EventSensitivity es) throws IOException, InterruptedException, E {
// CHECKSTYLE:ON
synchronized (this) {
if (watch) {
throw new IllegalStateException("File is already watched " + file);
}
watch = true;
}
SensitivityWatchEventModifier sensitivity;
switch(es) {
case LOW:
sensitivity = SensitivityWatchEventModifier.LOW;
break;
case MEDIUM:
sensitivity = SensitivityWatchEventModifier.MEDIUM;
break;
case HIGH:
sensitivity = SensitivityWatchEventModifier.HIGH;
break;
default:
throw new UnsupportedOperationException("Unsupported sensitivity " + es);
}
final Path path = file.getParentFile().toPath();
try (WatchService watchService = path.getFileSystem().newWatchService()) {
path.register(watchService, new WatchEvent.Kind[] { StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.OVERFLOW }, sensitivity);
readAll(handler);
do {
WatchKey key = watchService.poll(1000, TimeUnit.MILLISECONDS);
if (key == null) {
if (reReadSize()) {
readAll(handler);
}
continue;
}
if (!key.isValid()) {
key.cancel();
break;
}
if (!key.pollEvents().isEmpty()) {
if (reReadSize()) {
readAll(handler);
}
}
if (!key.reset()) {
key.cancel();
break;
}
} while (watch);
} finally {
watch = false;
}
}
use of java.nio.file.WatchKey in project fakereplace by fakereplace.
the class WatchServiceFileSystemWatcher method addWatchedDirectory.
private void addWatchedDirectory(PathData data, Path dir) throws IOException {
WatchKey key = dir.register(watchService, new WatchEvent.Kind[] { ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY }, SensitivityWatchEventModifier.HIGH);
pathDataByKey.put(key, data);
data.keys.add(key);
}
use of java.nio.file.WatchKey in project wicket by apache.
the class Nio2ModificationWatcher method checkCreated.
/**
* Checks for newly created files and folders.
* New folders are registered to be watched.
* New files are removed from the MarkupCache because there could be
* {@link org.apache.wicket.markup.Markup#NO_MARKUP} (Not Found) entries for them already.
* @param log
* a logger that can be used to log the events
*/
protected void checkCreated(Logger log) {
WatchKey watchKey = watchService.poll();
if (watchKey != null) {
List<WatchEvent<?>> events = watchKey.pollEvents();
for (WatchEvent<?> event : events) {
WatchEvent.Kind<?> eventKind = event.kind();
Path eventPath = (Path) event.context();
if (eventKind == ENTRY_CREATE) {
entryCreated(eventPath, log);
} else if (eventKind == ENTRY_DELETE) {
entryDeleted(eventPath, log);
} else if (eventKind == ENTRY_MODIFY) {
entryModified(eventPath, log);
}
}
watchKey.reset();
}
}
Aggregations