Search in sources :

Example 96 with WatchKey

use of java.nio.file.WatchKey in project JavaForFun by gumartinm.

the class InotifyTestMain method main.

public static void main(final String[] args) throws IOException, ExecutionException, InterruptedException {
    final ExecutorService exec = Executors.newSingleThreadExecutor();
    final Path filePath = FileSystems.getDefault().getPath(directory);
    final Inotify task = new Inotify(filePath);
    while (true) {
        final Future<WatchKey> futureTask = exec.submit(task);
        final WatchKey token = futureTask.get();
        for (final WatchEvent<?> event : token.pollEvents()) {
            sortEvent(event);
        }
        if (!token.reset()) {
            break;
        }
    }
}
Also used : Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) WatchKey(java.nio.file.WatchKey)

Example 97 with WatchKey

use of java.nio.file.WatchKey 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 98 with WatchKey

use of java.nio.file.WatchKey in project MassiveCore by MassiveCraft.

the class PusherCollFlatfile method run.

// -------------------------------------------- //
// OVERRIDE: THREAD
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public void run() {
    // MassiveCore.get().log("Starting Pusher for " + coll.getBasename());
    while (true) {
        try {
            WatchKey key = this.watcher.take();
            Path dir = this.keys.get(key);
            if (dir == null) {
                // System.err.println("WatchKey not recognized!!");
                continue;
            }
            for (WatchEvent<?> event : key.pollEvents()) {
                handleEvent((WatchEvent<Path>) event, dir);
            }
            boolean valid = key.reset();
            if (!valid)
                this.keys.remove(key);
        } catch (InterruptedException e) {
            // We've been interrupted. Lets bail.
            return;
        } catch (Exception e) {
            System.out.println("Pusher error for" + this.coll.getDebugName());
            e.printStackTrace();
        } finally {
            handledIds.clear();
        }
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException)

Example 99 with WatchKey

use of java.nio.file.WatchKey in project MassiveCore by MassiveCraft.

the class PusherCollFlatfile method register.

// -------------------------------------------- //
// REGISTER
// -------------------------------------------- //
public void register(Path path) throws IOException {
    if (Files.notExists(path))
        throw new IllegalArgumentException(path.toString() + " does not exist.");
    WatchKey key = path.register(watcher, EVENT_TYPES);
    // System.out.format("register: %s\n", path);
    keys.put(key, path);
}
Also used : WatchKey(java.nio.file.WatchKey)

Example 100 with WatchKey

use of java.nio.file.WatchKey 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)

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