use of java.nio.file.WatchEvent in project cas by apereo.
the class ServiceRegistryConfigWatcher method handleEvent.
/**
* Handle event.
*
* @param key the key
*/
private void handleEvent(final WatchKey key) {
this.readLock.lock();
try {
//The filename is the context of the event.
key.pollEvents().stream().filter(event -> event.count() <= 1).forEach(event -> {
final WatchEvent.Kind kind = event.kind();
final WatchEvent<Path> ev = (WatchEvent<Path>) event;
final Path filename = ev.context();
final Path parent = (Path) key.watchable();
final Path fullPath = parent.resolve(filename);
final File file = fullPath.toFile();
LOGGER.trace("Detected event [{}] on file [{}]. Loading change...", kind, file);
if (kind.name().equals(ENTRY_CREATE.name()) && file.exists()) {
handleCreateEvent(file);
} else if (kind.name().equals(ENTRY_DELETE.name())) {
handleDeleteEvent();
} else if (kind.name().equals(ENTRY_MODIFY.name()) && file.exists()) {
handleModifyEvent(file);
}
});
} finally {
this.readLock.unlock();
}
}
use of java.nio.file.WatchEvent in project buck by facebook.
the class WatchmanWatcherTest method whenExistsIsFalseThenDeleteEventIsGenerated.
@Test
public void whenExistsIsFalseThenDeleteEventIsGenerated() throws IOException, InterruptedException {
ImmutableMap<String, Object> watchmanOutput = ImmutableMap.of("files", ImmutableList.of(ImmutableMap.<String, Object>of("name", "foo/bar/baz", "exists", false)));
Capture<WatchEvent<Path>> eventCapture = newCapture();
EventBus eventBus = createStrictMock(EventBus.class);
eventBus.post(capture(eventCapture));
replay(eventBus);
WatchmanWatcher watcher = createWatcher(eventBus, watchmanOutput);
watcher.postEvents(BuckEventBusFactory.newInstance(new FakeClock(0)), WatchmanWatcher.FreshInstanceAction.NONE);
verify(eventBus);
assertEquals("Should be delete event.", StandardWatchEventKinds.ENTRY_DELETE, eventCapture.getValue().kind());
}
use of java.nio.file.WatchEvent in project buck by facebook.
the class WatchmanWatcherIntegrationTest method globMatchesWholeName.
@Test
public void globMatchesWholeName() throws IOException, InterruptedException {
WatchmanWatcher watcher = createWatchmanWatcher(new PathOrGlobMatcher("*.txt"));
// Create a dot-file which should be ignored by the above glob.
Path path = tmp.getRoot().getFileSystem().getPath("foo/bar/hello.txt");
Files.createDirectories(tmp.getRoot().resolve(path).getParent());
Files.write(tmp.getRoot().resolve(path), new byte[0]);
// Verify we still get an event for the created path.
watcher.postEvents(new BuckEventBus(new FakeClock(0), new BuildId()), WatchmanWatcher.FreshInstanceAction.NONE);
ImmutableList<WatchEvent<?>> events = watchmanEventCollector.getEvents();
assertThat(events.size(), Matchers.equalTo(1));
WatchEvent<?> event = events.get(0);
Path eventPath = (Path) event.context();
assertThat(eventPath, Matchers.equalTo(path));
assertSame(event.kind(), StandardWatchEventKinds.ENTRY_CREATE);
}
use of java.nio.file.WatchEvent in project buck by facebook.
the class WatchmanWatcherTest method whenNameThenModifyEventIsGenerated.
@Test
public void whenNameThenModifyEventIsGenerated() throws IOException, InterruptedException {
ImmutableMap<String, Object> watchmanOutput = ImmutableMap.of("files", ImmutableList.of(ImmutableMap.<String, Object>of("name", "foo/bar/baz")));
Capture<WatchEvent<Path>> eventCapture = newCapture();
EventBus eventBus = createStrictMock(EventBus.class);
eventBus.post(capture(eventCapture));
replay(eventBus);
WatchmanWatcher watcher = createWatcher(eventBus, watchmanOutput);
watcher.postEvents(BuckEventBusFactory.newInstance(new FakeClock(0)), WatchmanWatcher.FreshInstanceAction.NONE);
verify(eventBus);
assertEquals("Should be modify event.", StandardWatchEventKinds.ENTRY_MODIFY, eventCapture.getValue().kind());
assertEquals("Path should match watchman output.", MorePaths.pathWithPlatformSeparators("foo/bar/baz"), eventCapture.getValue().context().toString());
}
use of java.nio.file.WatchEvent in project buck by facebook.
the class WatchmanWatcherTest method whenMultipleFilesThenMultipleEventsGenerated.
@Test
public void whenMultipleFilesThenMultipleEventsGenerated() throws IOException, InterruptedException {
ImmutableMap<String, Object> watchmanOutput = ImmutableMap.of("files", ImmutableList.of(ImmutableMap.<String, Object>of("name", "foo/bar/baz"), ImmutableMap.<String, Object>of("name", "foo/bar/boz")));
EventBus eventBus = createStrictMock(EventBus.class);
Capture<WatchEvent<Path>> firstEvent = newCapture();
Capture<WatchEvent<Path>> secondEvent = newCapture();
eventBus.post(capture(firstEvent));
eventBus.post(capture(secondEvent));
replay(eventBus);
WatchmanWatcher watcher = createWatcher(eventBus, watchmanOutput);
watcher.postEvents(BuckEventBusFactory.newInstance(new FakeClock(0)), WatchmanWatcher.FreshInstanceAction.NONE);
verify(eventBus);
assertEquals("Path should match watchman output.", MorePaths.pathWithPlatformSeparators("foo/bar/baz"), firstEvent.getValue().context().toString());
assertEquals("Path should match watchman output.", MorePaths.pathWithPlatformSeparators("foo/bar/boz"), secondEvent.getValue().context().toString());
}
Aggregations