Search in sources :

Example 16 with WatchKey

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();
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent)

Example 17 with WatchKey

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;
    }
}
Also used : Path(java.nio.file.Path) SensitivityWatchEventModifier(com.sun.nio.file.SensitivityWatchEventModifier) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) WatchService(java.nio.file.WatchService) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 18 with WatchKey

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);
}
Also used : WatchKey(java.nio.file.WatchKey)

Example 19 with WatchKey

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) File(java.io.File)

Example 20 with WatchKey

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));
}
Also used : ConfigurationChangeNotifier(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeNotifier) InputStream(java.io.InputStream) WatchKey(java.nio.file.WatchKey) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

WatchKey (java.nio.file.WatchKey)134 Path (java.nio.file.Path)88 WatchEvent (java.nio.file.WatchEvent)65 IOException (java.io.IOException)49 WatchService (java.nio.file.WatchService)30 File (java.io.File)27 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)18 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)7 Map (java.util.Map)7 FileSystems (java.nio.file.FileSystems)6 FileVisitResult (java.nio.file.FileVisitResult)6 HashSet (java.util.HashSet)6 List (java.util.List)6 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)5 InputStream (java.io.InputStream)4 AccessDeniedException (java.nio.file.AccessDeniedException)4 Kind (java.nio.file.WatchEvent.Kind)4 Logger (org.slf4j.Logger)4