use of java.nio.file.WatchKey in project smarthome by eclipse.
the class AbstractFileTransformationService method processFolderEvents.
/**
* Ensures that a modified or deleted cached files does not stay in the cache
*/
private void processFolderEvents() {
WatchKey key = watchService.poll();
if (key != null) {
for (WatchEvent<?> e : key.pollEvents()) {
if (e.kind() == OVERFLOW) {
continue;
}
// Context for directory entry event is the file name of entry
@SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) e;
Path path = ev.context();
logger.debug("Refreshing transformation file '{}'", path);
for (String fileEntry : cachedFiles.keySet()) {
if (fileEntry.endsWith(path.toString())) {
cachedFiles.remove(fileEntry);
}
}
}
key.reset();
}
}
use of java.nio.file.WatchKey in project spf4j by zolyfarkas.
the class AvroFileReader method sowAndSubscribe.
// CHECKSTYLE:OFF
@SuppressFBWarnings("NOS_NON_OWNED_SYNCHRONIZATION")
public <E extends Exception> void sowAndSubscribe(final SowAndSubscribeHandler<T, E> handler, final FSWatchEventSensitivity 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);
handler.sowEnd();
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 ariADDna by StnetixDevTeam.
the class FileSystemWatchingService method registerDirectory.
/**
* Register the given directory with the WatchService; This function will be called by FileVisitor
*
* @param dir dir for watch
* @throws IOException
*/
void registerDirectory(Path dir) throws IOException {
LOGGER.debug("Register directory with path {}", dir);
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
keys.put(key, dir);
}
use of java.nio.file.WatchKey in project spring-cloud-config by spring-cloud.
the class FileMonitorConfiguration method filesFromEvents.
private Set<File> filesFromEvents() {
Set<File> files = new LinkedHashSet<File>();
if (this.watcher == null) {
return files;
}
WatchKey key = this.watcher.poll();
while (key != null) {
for (WatchEvent<?> event : key.pollEvents()) {
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE || event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
Path item = (Path) event.context();
File file = new File(((Path) key.watchable()).toAbsolutePath() + File.separator + item.getFileName());
if (file.isDirectory()) {
files.addAll(walkDirectory(file.toPath()));
} else {
if (!file.getPath().contains(".git") && !PatternMatchUtils.simpleMatch(this.excludes, file.getName())) {
if (log.isDebugEnabled()) {
log.debug("Watch Event: " + event.kind() + ": " + file);
}
files.add(file);
}
}
} else if (event.kind() == StandardWatchEventKinds.OVERFLOW) {
if (log.isDebugEnabled()) {
log.debug("Watch Event: " + event.kind() + ": context: " + event.context());
}
if (event.context() != null && event.context() instanceof Path) {
files.addAll(walkDirectory((Path) event.context()));
} else {
for (Path path : this.directory) {
files.addAll(walkDirectory(path));
}
}
} else {
if (log.isDebugEnabled()) {
log.debug("Watch Event: " + event.kind() + ": context: " + event.context());
}
}
}
key.reset();
key = this.watcher.poll();
}
return files;
}
use of java.nio.file.WatchKey in project nifi-minifi by apache.
the class FileChangeIngestorTest method testTargetChangedWithModificationEvent_nonConfigFile.
@Test
public void testTargetChangedWithModificationEvent_nonConfigFile() throws Exception {
when(mockDifferentiator.isNew(Mockito.any(InputStream.class))).thenReturn(false);
final ConfigurationChangeNotifier testNotifier = Mockito.mock(ConfigurationChangeNotifier.class);
// In this case, we receive a trigger event for the directory monitored, but it was another file not being monitored
final WatchKey mockWatchKey = createMockWatchKeyForPath("footage_not_found.yml");
establishMockEnvironmentForChangeTests(testNotifier, mockWatchKey);
notifierSpy.targetChanged();
verify(testNotifier, Mockito.never()).notifyListeners(Mockito.any(ByteBuffer.class));
}
Aggregations