Search in sources :

Example 51 with WatchKey

use of java.nio.file.WatchKey in project SSM by Intel-bigdata.

the class InterpreterOutputChangeWatcher method clear.

public void clear() {
    synchronized (watchKeys) {
        for (WatchKey key : watchKeys.keySet()) {
            key.cancel();
        }
        watchKeys.clear();
        watchFiles.clear();
    }
}
Also used : WatchKey(java.nio.file.WatchKey)

Example 52 with WatchKey

use of java.nio.file.WatchKey in project SSM by Intel-bigdata.

the class InterpreterOutputChangeWatcher method run.

public void run() {
    while (!stop) {
        WatchKey key = null;
        try {
            key = watcher.poll(1, TimeUnit.SECONDS);
        } catch (InterruptedException | ClosedWatchServiceException e) {
            break;
        }
        if (key == null) {
            continue;
        }
        for (WatchEvent<?> event : key.pollEvents()) {
            WatchEvent.Kind<?> kind = event.kind();
            if (kind == OVERFLOW) {
                continue;
            }
            WatchEvent<Path> ev = (WatchEvent<Path>) event;
            Path filename = ev.context();
            // search for filename
            synchronized (watchKeys) {
                for (File f : watchFiles) {
                    if (f.getName().compareTo(filename.toString()) == 0) {
                        File changedFile;
                        if (filename.isAbsolute()) {
                            changedFile = new File(filename.toString());
                        } else {
                            changedFile = new File(watchKeys.get(key), filename.toString());
                        }
                        logger.info("File change detected " + changedFile.getAbsolutePath());
                        if (listener != null) {
                            listener.fileChanged(changedFile);
                        }
                    }
                }
            }
        }
        boolean valid = key.reset();
        if (!valid) {
            break;
        }
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) WatchEvent(java.nio.file.WatchEvent) File(java.io.File)

Example 53 with WatchKey

use of java.nio.file.WatchKey in project syncany by syncany.

the class DefaultRecursiveWatcher method registerWatch.

private synchronized void registerWatch(Path dir) {
    if (!watchPathKeyMap.containsKey(dir)) {
        logger.log(Level.INFO, "- Registering " + dir);
        try {
            WatchKey watchKey = dir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY, OVERFLOW);
            watchPathKeyMap.put(dir, watchKey);
        } catch (IOException e) {
            logger.log(Level.FINE, "IO Failed", e);
        }
    }
}
Also used : WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException)

Example 54 with WatchKey

use of java.nio.file.WatchKey in project Payara by payara.

the class DirWatcher method syncWatches.

private synchronized void syncWatches() {
    while (true) {
        WatchKey key = watchService.poll();
        if (key == null) {
            return;
        }
        Path root = (Path) key.watchable();
        WatchProcessor processor = processors.get(root);
        if (processor == null || processor.isCancelled()) {
            key.cancel();
            continue;
        }
        for (WatchEvent<?> event : key.pollEvents()) {
            WatchEvent.Kind<?> kind = event.kind();
            boolean keepRegistered = true;
            if (kind == OVERFLOW) {
                LOGGER.warning(() -> "File system watcher overflowed for " + root);
                keepRegistered = processor.overflowed();
            } else {
                WatchEvent<Path> ev = (WatchEvent<Path>) event;
                Path filename = ev.context();
                if (kind == ENTRY_CREATE) {
                    keepRegistered = processor.created(filename);
                }
                if (kind == ENTRY_DELETE) {
                    keepRegistered = processor.deleted(filename);
                }
            }
            if (!keepRegistered) {
                key.cancel();
            }
        }
        boolean valid = key.reset();
        if (!valid) {
            processor.cancelled();
        }
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent)

Example 55 with WatchKey

use of java.nio.file.WatchKey in project Payara by payara.

the class PayaraFileWatcher method run.

// Private methods
/**
 * The event loop method
 */
private void run() {
    registerQueuedPaths();
    try {
        // Block, waiting for an event on a watched file
        WatchKey key = watcher.take();
        // Loop through the events
        for (WatchEvent<?> event : key.pollEvents()) {
            // Find the absolute path of the modified file
            final Path modifiedPath = ((Path) key.watchable()).resolve((Path) event.context());
            // Loop through the watched paths to find the action associated with it
            Iterator<Entry<Path, Runnable>> watchIterator = LISTENER_MAP.entrySet().iterator();
            while (watchIterator.hasNext()) {
                Entry<Path, Runnable> watchEntry = watchIterator.next();
                // If this entry corresponds to the modified file
                if (modifiedPath.endsWith(watchEntry.getKey())) {
                    // Ignore overflow events
                    if (event.kind() == StandardWatchEventKinds.OVERFLOW) {
                        continue;
                    }
                    File modifiedFile = modifiedPath.toFile();
                    // If it's been deleted, remove the file watcher
                    if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE && !modifiedFile.exists()) {
                        LOGGER.info(format("Watched file %s was deleted; removing the file watcher", modifiedPath));
                        watchIterator.remove();
                    } else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY && modifiedFile.length() > 0) {
                        // Run the associated action
                        LOGGER.fine(format("Watched file %s modified, running the listener", modifiedPath));
                        watchEntry.getValue().run();
                    }
                }
            }
        }
        key.reset();
    } catch (InterruptedException ex) {
        LOGGER.log(WARNING, "The file watcher thread was interrupted", ex);
    }
}
Also used : Path(java.nio.file.Path) Entry(java.util.Map.Entry) WatchKey(java.nio.file.WatchKey) File(java.io.File)

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