Search in sources :

Example 1 with WatchEvent

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();
    }
}
Also used : CasRegisteredServicesRefreshEvent(org.apereo.cas.support.events.service.CasRegisteredServicesRefreshEvent) Logger(org.slf4j.Logger) WatchEvent(java.nio.file.WatchEvent) LoggerFactory(org.slf4j.LoggerFactory) Throwables(com.google.common.base.Throwables) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) File(java.io.File) WatchKey(java.nio.file.WatchKey) IOUtils(org.apache.commons.io.IOUtils) WatchService(java.nio.file.WatchService) Lock(java.util.concurrent.locks.Lock) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) Closeable(java.io.Closeable) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Path(java.nio.file.Path) FileSystems(java.nio.file.FileSystems) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Path(java.nio.file.Path) WatchEvent(java.nio.file.WatchEvent) File(java.io.File)

Example 2 with WatchEvent

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());
}
Also used : FakeClock(com.facebook.buck.timing.FakeClock) EasyMock.anyObject(org.easymock.EasyMock.anyObject) WatchEvent(java.nio.file.WatchEvent) BuckEventBus(com.facebook.buck.event.BuckEventBus) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.Test)

Example 3 with WatchEvent

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);
}
Also used : Path(java.nio.file.Path) BuckEventBus(com.facebook.buck.event.BuckEventBus) BuildId(com.facebook.buck.model.BuildId) FakeClock(com.facebook.buck.timing.FakeClock) PathOrGlobMatcher(com.facebook.buck.io.PathOrGlobMatcher) WatchEvent(java.nio.file.WatchEvent) Test(org.junit.Test)

Example 4 with WatchEvent

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());
}
Also used : FakeClock(com.facebook.buck.timing.FakeClock) EasyMock.anyObject(org.easymock.EasyMock.anyObject) WatchEvent(java.nio.file.WatchEvent) BuckEventBus(com.facebook.buck.event.BuckEventBus) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.Test)

Example 5 with WatchEvent

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());
}
Also used : FakeClock(com.facebook.buck.timing.FakeClock) EasyMock.anyObject(org.easymock.EasyMock.anyObject) BuckEventBus(com.facebook.buck.event.BuckEventBus) EventBus(com.google.common.eventbus.EventBus) WatchEvent(java.nio.file.WatchEvent) Test(org.junit.Test)

Aggregations

WatchEvent (java.nio.file.WatchEvent)91 Path (java.nio.file.Path)68 WatchKey (java.nio.file.WatchKey)57 IOException (java.io.IOException)33 File (java.io.File)26 Test (org.junit.Test)24 WatchService (java.nio.file.WatchService)20 BuckEventBus (com.facebook.buck.event.BuckEventBus)13 FakeClock (com.facebook.buck.timing.FakeClock)13 EventBus (com.google.common.eventbus.EventBus)12 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)10 EasyMock.anyObject (org.easymock.EasyMock.anyObject)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)6 Subscribe (com.google.common.eventbus.Subscribe)5 FakeWatchmanClient (com.facebook.buck.io.FakeWatchmanClient)4 FileSystems (java.nio.file.FileSystems)4 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)4 HashSet (java.util.HashSet)4 List (java.util.List)4