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();
}
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) {
}
}
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();
}
}
}
});
}
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();
}
}
}
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);
}
}
Aggregations