use of java.nio.file.WatchEvent 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.WatchEvent 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;
}
}
}
use of java.nio.file.WatchEvent in project ariADDna by StnetixDevTeam.
the class FileSystemWatchingService method processEvents.
/**
* Process all events for keys queued to the watcher
*/
public void processEvents() {
while (isAlive) {
// wait for key to be signalled
// multiple events packed into key
WatchKey key;
try {
key = watcher.poll(50, TimeUnit.MILLISECONDS);
} catch (InterruptedException x) {
LOGGER.trace("processEvent was interrupted and service stopped with message: {}", x.getMessage());
return;
}
// Get the path associated with the key
Path dir = keys.get(key);
if (dir == null) {
continue;
}
// get the events list
List<WatchEvent<?>> events = key.pollEvents();
// create pair with path and events
Pair<Path, List<WatchEvent<?>>> eventsSet = new Pair<>(dir, events);
// add pair to the shared with Consumer queue
queue.add(eventsSet);
// 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.WatchEvent in project nifi-minifi by apache.
the class FileChangeIngestorTest method createMockWatchKeyForPath.
/* Helper methods to establish mock environment */
private WatchKey createMockWatchKeyForPath(String configFilePath) {
final WatchKey mockWatchKey = Mockito.mock(WatchKey.class);
final List<WatchEvent<?>> mockWatchEvents = (List<WatchEvent<?>>) Mockito.mock(List.class);
when(mockWatchKey.pollEvents()).thenReturn(mockWatchEvents);
when(mockWatchKey.reset()).thenReturn(true);
final Iterator mockIterator = Mockito.mock(Iterator.class);
when(mockWatchEvents.iterator()).thenReturn(mockIterator);
final WatchEvent mockWatchEvent = Mockito.mock(WatchEvent.class);
when(mockIterator.hasNext()).thenReturn(true, false);
when(mockIterator.next()).thenReturn(mockWatchEvent);
// In this case, we receive a trigger event for the directory monitored, and it was the file monitored
when(mockWatchEvent.context()).thenReturn(Paths.get(configFilePath));
when(mockWatchEvent.kind()).thenReturn(ENTRY_MODIFY);
return mockWatchKey;
}
use of java.nio.file.WatchEvent in project nifi-minifi by apache.
the class FileChangeIngestor method targetChanged.
protected boolean targetChanged() {
boolean targetChanged = false;
final WatchKey watchKey = this.watchService.poll();
if (watchKey == null) {
return targetChanged;
}
for (WatchEvent<?> watchEvt : watchKey.pollEvents()) {
final WatchEvent.Kind<?> evtKind = watchEvt.kind();
final WatchEvent<Path> pathEvent = (WatchEvent<Path>) watchEvt;
final Path changedFile = pathEvent.context();
// determine target change by verifying if the changed file corresponds to the config file monitored for this path
targetChanged = (evtKind == ENTRY_MODIFY && changedFile.equals(configFilePath.getName(configFilePath.getNameCount() - 1)));
}
// After completing inspection, reset for detection of subsequent change events
boolean valid = watchKey.reset();
if (!valid) {
throw new IllegalStateException("Unable to reinitialize file system watcher.");
}
return targetChanged;
}
Aggregations