Search in sources :

Example 71 with WatchKey

use of java.nio.file.WatchKey in project jdk8u_jdk by JetBrains.

the class LotsOfCancels method poll.

/**
     * Polls the given WatchService in a tight loop. This keeps the event
     * queue drained, it also hogs a CPU core which seems necessary to
     * tickle the original bug.
     */
static void poll(WatchService watcher) {
    try {
        for (; ; ) {
            WatchKey key = watcher.take();
            if (key != null) {
                key.pollEvents();
                key.reset();
            }
        }
    } catch (ClosedWatchServiceException expected) {
    // nothing to do
    } catch (Exception e) {
        e.printStackTrace();
        failed = true;
    }
}
Also used : WatchKey(java.nio.file.WatchKey) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException)

Example 72 with WatchKey

use of java.nio.file.WatchKey in project jdk8u_jdk by JetBrains.

the class LotsOfCancels method handle.

/**
     * Stress the given WatchService, specifically the cancel method, in
     * the given directory. Closes the WatchService when done.
     */
static void handle(Path dir, WatchService watcher) {
    try {
        try {
            Path file = dir.resolve("anyfile");
            for (int i = 0; i < 2000; i++) {
                WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
                Files.createFile(file);
                Files.delete(file);
                key.cancel();
            }
        } finally {
            watcher.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        failed = true;
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException)

Example 73 with WatchKey

use of java.nio.file.WatchKey in project karaf by apache.

the class AutoEncryptionSupport method run.

@Override
public void run() {
    WatchService watchService = null;
    try {
        watchService = FileSystems.getDefault().newWatchService();
        Path dir = Paths.get(System.getProperty("karaf.etc"));
        dir.register(watchService, ENTRY_MODIFY);
        Path file = dir.resolve("users.properties");
        encryptedPassword(new Properties(file.toFile()));
        while (running) {
            try {
                WatchKey key = watchService.poll(1, TimeUnit.SECONDS);
                if (key == null) {
                    continue;
                }
                for (WatchEvent<?> event : key.pollEvents()) {
                    @SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event;
                    // Context for directory entry event is the file name of entry
                    Path name = dir.resolve(ev.context());
                    if (file.equals(name)) {
                        encryptedPassword(new Properties(file.toFile()));
                    }
                }
                key.reset();
            } catch (IOException e) {
                LOGGER.warn(e.getMessage(), e);
            } catch (InterruptedException e) {
            // Ignore as this happens on shutdown
            }
        }
    } catch (IOException e) {
        LOGGER.warn(e.getMessage(), e);
    } finally {
        StreamUtils.close(watchService);
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) IOException(java.io.IOException) Properties(org.apache.felix.utils.properties.Properties) WatchService(java.nio.file.WatchService)

Example 74 with WatchKey

use of java.nio.file.WatchKey in project asterixdb by apache.

the class FileSystemWatcher method poll.

// poll is not blocking
public synchronized File poll() throws IOException {
    if (it.hasNext()) {
        return it.next();
    }
    if (done || !isFeed) {
        return null;
    }
    files.clear();
    it = files.iterator();
    if (keys.isEmpty()) {
        close();
        return null;
    }
    // Read new Events (Polling first to add all available files)
    WatchKey key;
    key = watcher.poll();
    while (key != null) {
        handleEvents(key);
        if (endOfEvents(key)) {
            close();
            return null;
        }
        key = watcher.poll();
    }
    return null;
}
Also used : WatchKey(java.nio.file.WatchKey)

Example 75 with WatchKey

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

the class WebAppManager method lifeCycleStarted.

private void lifeCycleStarted(LifeCycle lc, Profile profile) {
    // register watcher to watch web/app directories everywhere
    if (this.watcher.get(profile) != null) {
        FileUtil.findFiles("web", ".").stream().filter((file) -> (file.isDirectory())).forEachOrdered((file) -> {
            try {
                Path path = file.toPath();
                WebAppManager.this.watchPaths.put(path.register(this.watcher.get(profile), StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY), path);
            } catch (IOException ex) {
                log.error("Unable to watch {} for changes.", file);
            }
            (new Thread() {

                @Override
                public void run() {
                    while (WebAppManager.this.watcher.get(profile) != null) {
                        WatchKey key;
                        try {
                            key = WebAppManager.this.watcher.get(profile).take();
                        } catch (InterruptedException ex) {
                            return;
                        }
                        key.pollEvents().stream().filter((event) -> (event.kind() != OVERFLOW)).forEachOrdered((event) -> {
                            WebAppManager.this.savePreferences(profile);
                        });
                        if (!key.reset()) {
                            WebAppManager.this.watcher.remove(profile);
                        }
                    }
                }
            }).start();
        });
    }
}
Also used : OVERFLOW(java.nio.file.StandardWatchEventKinds.OVERFLOW) LifeCycle(org.eclipse.jetty.util.component.LifeCycle) URL(java.net.URL) AbstractPreferencesManager(jmri.util.prefs.AbstractPreferencesManager) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) InitializationException(jmri.util.prefs.InitializationException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) AngularRoute(jmri.server.web.spi.AngularRoute) WatchKey(java.nio.file.WatchKey) HashSet(java.util.HashSet) WebServer(jmri.web.server.WebServer) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) ProfileUtils(jmri.profile.ProfileUtils) Locale(java.util.Locale) Map(java.util.Map) Profile(jmri.profile.Profile) Path(java.nio.file.Path) PropertyChangeEvent(java.beans.PropertyChangeEvent) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ServiceLoader(java.util.ServiceLoader) WebMenuItem(jmri.server.web.spi.WebMenuItem) File(java.io.File) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) WatchService(java.nio.file.WatchService) List(java.util.List) WebManifest(jmri.server.web.spi.WebManifest) WebServerPreferences(jmri.web.server.WebServerPreferences) FileUtil(jmri.util.FileUtil) StringJoiner(java.util.StringJoiner) FileSystems(java.nio.file.FileSystems) Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException)

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