Search in sources :

Example 46 with WatchKey

use of java.nio.file.WatchKey in project jimfs by google.

the class AbstractWatchServiceTest method testPostEvent.

@Test
public void testPostEvent() throws IOException {
    AbstractWatchService.Key key = watcher.register(new StubWatchable(), ImmutableSet.of(ENTRY_CREATE));
    AbstractWatchService.Event<Path> event = new AbstractWatchService.Event<>(ENTRY_CREATE, 1, null);
    key.post(event);
    key.signal();
    assertThat(watcher.queuedKeys()).containsExactly(key);
    WatchKey retrievedKey = watcher.poll();
    assertThat(retrievedKey).isEqualTo(key);
    List<WatchEvent<?>> events = retrievedKey.pollEvents();
    assertThat(events).hasSize(1);
    assertThat(events.get(0)).isEqualTo(event);
    // polling should have removed all events
    assertThat(retrievedKey.pollEvents()).isEmpty();
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) WatchEvent(java.nio.file.WatchEvent) Test(org.junit.Test)

Example 47 with WatchKey

use of java.nio.file.WatchKey in project j2objc by google.

the class MacOSXPathTest method test_register$WatchService$WatchEvent_Kind$WatchEvent_Modifier_NPE.

@Test
public void test_register$WatchService$WatchEvent_Kind$WatchEvent_Modifier_NPE() throws IOException {
    WatchService watchService = FileSystems.getDefault().newWatchService();
    WatchEvent.Kind<?>[] events = { ENTRY_CREATE };
    Path dirRoot = Paths.get(filesSetup.getTestDir(), "dir");
    Files.createDirectories(dirRoot);
    try {
        WatchKey key = dirRoot.register(null, events, ExtendedWatchEventModifier.FILE_TREE);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        WatchKey key = dirRoot.register(watchService, null, ExtendedWatchEventModifier.FILE_TREE);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : Path(java.nio.file.Path) Kind(java.nio.file.WatchEvent.Kind) WatchKey(java.nio.file.WatchKey) WatchService(java.nio.file.WatchService) Test(org.junit.Test)

Example 48 with WatchKey

use of java.nio.file.WatchKey in project dubbo by alibaba.

the class FileSystemDynamicConfiguration method processWatchEvents.

/**
 * Process the {@link WatchEvent WatchEvents} loop in async execution
 *
 * @param watchService {@link WatchService}
 */
private void processWatchEvents(WatchService watchService) {
    getWatchEventsLoopThreadPool().execute(() -> {
        // WatchEvents Loop
        while (true) {
            WatchKey watchKey = null;
            try {
                watchKey = watchService.take();
                if (watchKey.isValid()) {
                    for (WatchEvent event : watchKey.pollEvents()) {
                        WatchEvent.Kind kind = event.kind();
                        // configChangeType's key to match WatchEvent's Kind
                        ConfigChangeType configChangeType = CONFIG_CHANGE_TYPES_MAP.get(kind.name());
                        if (configChangeType != null) {
                            Path configDirectoryPath = (Path) watchKey.watchable();
                            Path currentPath = (Path) event.context();
                            Path configFilePath = configDirectoryPath.resolve(currentPath);
                            File configDirectory = configDirectoryPath.toFile();
                            executeMutually(configDirectory, () -> {
                                fireConfigChangeEvent(configDirectory, configFilePath.toFile(), configChangeType);
                                signalConfigDirectory(configDirectory);
                                return null;
                            });
                        }
                    }
                }
            } catch (Exception e) {
                return;
            } finally {
                if (watchKey != null) {
                    // reset
                    watchKey.reset();
                }
            }
        }
    });
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) ConfigChangeType(org.apache.dubbo.common.config.configcenter.ConfigChangeType) File(java.io.File) IOException(java.io.IOException)

Example 49 with WatchKey

use of java.nio.file.WatchKey in project blueocean-plugin by jenkinsci.

the class RecursivePathWatcher method start.

public void start(final PathEventHandler eventHandler) {
    running = true;
    while (running) {
        try {
            if (log.isLoggable(Level.FINE))
                log.fine("Ready... watching: " + dirs.keySet());
            WatchKey watchKey = watchService.take();
            Path dir = keys.get(watchKey);
            for (WatchEvent<?> event : watchKey.pollEvents()) {
                Object entry = event.context();
                if (log.isLoggable(Level.FINE))
                    log.fine(event.kind() + ": " + entry);
                if (entry instanceof Path) {
                    Path name = (Path) entry;
                    Path child = dir.resolve(name);
                    if (dirs.containsKey(child)) {
                        if (ENTRY_DELETE.equals(event.kind())) {
                            // for deleted subdirectories, clean them all up here
                            for (Iterator<Map.Entry<Path, WatchKey>> i = dirs.entrySet().iterator(); i.hasNext(); ) {
                                Map.Entry<Path, WatchKey> d = i.next();
                                if (d.getKey().startsWith(child)) {
                                    i.remove();
                                    d.getValue().cancel();
                                    keys.remove(d.getValue());
                                }
                            }
                        }
                    } else {
                        if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
                            if (ENTRY_CREATE.equals(event.kind())) {
                                register(child);
                            }
                        } else {
                            if (ENTRY_CREATE.equals(event.kind())) {
                                eventHandler.accept(Event.CREATE, child);
                            } else if (ENTRY_DELETE.equals(event.kind())) {
                                eventHandler.accept(Event.DELETE, child);
                            } else if (ENTRY_MODIFY.equals(event.kind())) {
                                eventHandler.accept(Event.MODIFY, child);
                            }
                        }
                    }
                }
            }
            // Check for this watchKey being invalid
            if (!watchKey.reset()) {
                watchKey.cancel();
                if (root.equals(dir)) {
                    watchService.close();
                    initWatchService();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) IOException(java.io.IOException)

Example 50 with WatchKey

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

the class InterpreterOutputChangeWatcher method watch.

public void watch(File file) throws IOException {
    String dirString;
    if (file.isFile()) {
        dirString = file.getParentFile().getAbsolutePath();
    } else {
        throw new IOException(file.getName() + " is not a file");
    }
    if (dirString == null) {
        dirString = "/";
    }
    Path dir = FileSystems.getDefault().getPath(dirString);
    logger.info("watch " + dir);
    WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
    synchronized (watchKeys) {
        watchKeys.put(key, new File(dirString));
        watchFiles.add(file);
    }
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException) 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