Search in sources :

Example 86 with WatchKey

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

Example 87 with WatchKey

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

Example 88 with WatchKey

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;
    }
}
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 89 with WatchKey

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

Example 90 with WatchKey

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

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