Search in sources :

Example 81 with WatchEvent

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

Example 82 with WatchEvent

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

Example 83 with WatchEvent

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;
            }
        }
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) ArrayList(java.util.ArrayList) List(java.util.List) FileSystemWatchEvent(com.stnetix.ariaddna.localstoragemanager.event.FileSystemWatchEvent) WatchEvent(java.nio.file.WatchEvent) Pair(javafx.util.Pair)

Example 84 with WatchEvent

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

Example 85 with WatchEvent

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

Aggregations

WatchEvent (java.nio.file.WatchEvent)92 Path (java.nio.file.Path)68 WatchKey (java.nio.file.WatchKey)58 IOException (java.io.IOException)33 File (java.io.File)27 Test (org.junit.Test)24 WatchService (java.nio.file.WatchService)20 BuckEventBus (com.facebook.buck.event.BuckEventBus)13 FakeClock (com.facebook.buck.timing.FakeClock)13 EventBus (com.google.common.eventbus.EventBus)12 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)10 EasyMock.anyObject (org.easymock.EasyMock.anyObject)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)6 Subscribe (com.google.common.eventbus.Subscribe)5 FakeWatchmanClient (com.facebook.buck.io.FakeWatchmanClient)4 FileSystems (java.nio.file.FileSystems)4 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)4 HashSet (java.util.HashSet)4 List (java.util.List)4