Search in sources :

Example 6 with ObservationManager

use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.

the class ObservationTest method testEventRemovedNodeWhenDenyEntryIsRemoved.

/**
     * @see <a href="https://issues.apache.org/jira/browse/OAK-4196">OAK-4196</a>
     */
@Test
public void testEventRemovedNodeWhenDenyEntryIsRemoved() throws Exception {
    // withdraw the READ privilege on childNPath
    deny(childNPath, readPrivileges);
    assertFalse(testSession.nodeExists(childNPath));
    assertTrue(testSession.nodeExists(childNPath2));
    // testUser registers a event listener for changes under testRoot
    ObservationManager obsMgr = testSession.getWorkspace().getObservationManager();
    EventResult listener = new EventResult(this.log);
    try {
        obsMgr.addEventListener(listener, Event.NODE_REMOVED, testRoot, true, null, null, true);
        // superuser removes the node with childNPath & childNPath2 in
        // order to provoke events being generated
        superuser.getItem(childNPath).remove();
        superuser.getItem(childNPath2).remove();
        superuser.save();
        // since the events are generated _after_ persisting all the changes
        // and the removal also removes the permission entries denying access
        // testUser will be notified about the removal because the remaining
        // permission setup after the removal grants read access.
        Event[] evts = listener.getEvents(DEFAULT_WAIT_TIMEOUT);
        List<String> eventPaths = new ArrayList<String>();
        for (Event evt : evts) {
            if (evt.getType() == Event.NODE_REMOVED) {
                eventPaths.add(evt.getPath());
            }
        }
        assertTrue(eventPaths.contains(childNPath));
        assertTrue(eventPaths.contains(childNPath2));
    } finally {
        obsMgr.removeEventListener(listener);
    }
}
Also used : EventResult(org.apache.jackrabbit.test.api.observation.EventResult) ArrayList(java.util.ArrayList) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) Test(org.junit.Test)

Example 7 with ObservationManager

use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.

the class ObservationTest method testEventGeneration.

@Test
public void testEventGeneration() throws Exception {
    // withdraw the READ privilege
    deny(path, readPrivileges);
    // testUser registers a event listener for 'path
    ObservationManager obsMgr = testSession.getWorkspace().getObservationManager();
    EventResult listener = new EventResult(this.log);
    try {
        obsMgr.addEventListener(listener, Event.NODE_REMOVED, testRoot, true, null, null, true);
        // superuser removes the node with childNPath & siblingPath in
        // order to provoke events being generated
        superuser.getItem(childNPath).remove();
        superuser.getItem(siblingPath).remove();
        superuser.save();
        // since the testUser does not have read-permission on the removed
        // childNPath, no corresponding event must be generated.
        Event[] evts = listener.getEvents(DEFAULT_WAIT_TIMEOUT);
        for (Event evt : evts) {
            if (evt.getType() == Event.NODE_REMOVED && evt.getPath().equals(childNPath)) {
                fail("TestUser does not have READ permission below " + path + " -> events below must not show up.");
            }
        }
    } finally {
        obsMgr.removeEventListener(listener);
    }
}
Also used : EventResult(org.apache.jackrabbit.test.api.observation.EventResult) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) Test(org.junit.Test)

Example 8 with ObservationManager

use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.

the class ObservationTest method externalEvents.

@Test
public void externalEvents() throws Throwable {
    final Set<String> externallyAdded = synchronizedSet(new LinkedHashSet<String>());
    final List<Throwable> exceptions = Lists.newArrayList();
    ObservationManager obsMgr = s1.getWorkspace().getObservationManager();
    final AtomicLong localEvents = new AtomicLong();
    final AtomicLong externalEvents = new AtomicLong();
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(EventIterator events) {
            try {
                Set<String> paths = Sets.newHashSet();
                while (events.hasNext()) {
                    Event event = events.nextEvent();
                    String external = "";
                    AtomicLong counter = localEvents;
                    if (event instanceof JackrabbitEvent) {
                        if (((JackrabbitEvent) event).isExternal()) {
                            external = " (external)";
                            counter = externalEvents;
                            paths.add(event.getPath());
                        }
                    }
                    System.out.println(event.getPath() + external);
                    counter.incrementAndGet();
                }
                while (!paths.isEmpty()) {
                    Iterator<String> it = externallyAdded.iterator();
                    String p = it.next();
                    assertTrue("missing event for " + p, paths.remove(p));
                    it.remove();
                }
            } catch (Throwable e) {
                exceptions.add(e);
            }
        }
    };
    obsMgr.addEventListener(listener, Event.NODE_ADDED, "/", true, null, null, false);
    Future f1 = executor.submit(new Worker(s1, exceptions, new HashSet<String>()));
    Future f2 = executor.submit(new Worker(s2, exceptions, externallyAdded));
    f1.get();
    f2.get();
    Thread.sleep(10 * 1000);
    System.out.println("local events: " + localEvents.get());
    System.out.println("external events: " + externalEvents.get());
    for (Throwable t : exceptions) {
        throw t;
    }
}
Also used : ObservationManager(javax.jcr.observation.ObservationManager) AtomicLong(java.util.concurrent.atomic.AtomicLong) JackrabbitEvent(org.apache.jackrabbit.api.observation.JackrabbitEvent) JackrabbitEvent(org.apache.jackrabbit.api.observation.JackrabbitEvent) Event(javax.jcr.observation.Event) Future(java.util.concurrent.Future) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 9 with ObservationManager

use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.

the class NonLocalObservationIT method addEventHandler.

private void addEventHandler(Session s, final String expectedNodeSuffix) throws Exception {
    ObservationManager o = s.getWorkspace().getObservationManager();
    o.addEventListener(new EventListener() {

        @Override
        public void onEvent(EventIterator events) {
            while (events.hasNext()) {
                Event e = events.nextEvent();
                if (!(e instanceof JackrabbitEvent)) {
                    continue;
                }
                if (((JackrabbitEvent) e).isExternal()) {
                    continue;
                }
                String p;
                try {
                    p = e.getPath();
                    // "+expectedNodeSuffix+", path: " + p);
                    if (!p.endsWith(expectedNodeSuffix)) {
                        log.info("EXCEPTION: expectedNodeSuffix: " + expectedNodeSuffix + ", path: " + p);
                        throw new Exception("expectedNodeSuffix: " + expectedNodeSuffix + ", non-local path: " + p);
                    }
                } catch (Exception e1) {
                    exception.set(e1);
                }
            }
        }
    }, Event.NODE_ADDED, "/", true, null, null, false);
}
Also used : JackrabbitEvent(org.apache.jackrabbit.api.observation.JackrabbitEvent) JackrabbitEvent(org.apache.jackrabbit.api.observation.JackrabbitEvent) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) RepositoryException(javax.jcr.RepositoryException) AssumptionViolatedException(org.junit.AssumptionViolatedException)

Example 10 with ObservationManager

use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.

the class CompatibilityIssuesTest method noEventsForTouchedProperties.

/**
     * OAK-948 - JR2 generates propertyChange event for touched properties while Oak does not
     */
@Test
public void noEventsForTouchedProperties() throws RepositoryException, InterruptedException {
    final String testNodeName = "test_touched_node";
    final String testNodePath = '/' + testNodeName;
    // Create the test node
    Session session = getAdminSession();
    Node testNode = session.getRootNode().addNode(testNodeName);
    testNode.setProperty("foo", "bar");
    testNode.setProperty("foo2", "bar0");
    session.save();
    Session observingSession = createAdminSession();
    ObservationManager observationManager = observingSession.getWorkspace().getObservationManager();
    try {
        final List<Event> events = new ArrayList<Event>();
        final CountDownLatch latch = new CountDownLatch(1);
        EventListener listener = new EventListener() {

            @Override
            public void onEvent(EventIterator eventIt) {
                while (eventIt.hasNext()) {
                    events.add(eventIt.nextEvent());
                }
                if (!events.isEmpty()) {
                    latch.countDown();
                }
            }
        };
        observationManager.addEventListener(listener, PROPERTY_CHANGED, testNodePath, true, null, null, false);
        //Now touch foo and modify foo2
        session.getNode(testNodePath).setProperty("foo", "bar");
        session.getNode(testNodePath).setProperty("foo2", "bar2");
        session.save();
        latch.await(60, TimeUnit.SECONDS);
        //Only one event is recorded for foo2 modification
        assertEquals(1, events.size());
    } finally {
        observingSession.logout();
    }
}
Also used : Node(javax.jcr.Node) ArrayList(java.util.ArrayList) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) EventListener(javax.jcr.observation.EventListener) CountDownLatch(java.util.concurrent.CountDownLatch) EventIterator(javax.jcr.observation.EventIterator) Session(javax.jcr.Session) Test(org.junit.Test)

Aggregations

ObservationManager (javax.jcr.observation.ObservationManager)29 Session (javax.jcr.Session)16 Event (javax.jcr.observation.Event)13 Test (org.junit.Test)11 EventListener (javax.jcr.observation.EventListener)10 Node (javax.jcr.Node)8 RepositoryException (javax.jcr.RepositoryException)8 EventIterator (javax.jcr.observation.EventIterator)8 Workspace (javax.jcr.Workspace)6 EventResult (org.apache.jackrabbit.test.api.observation.EventResult)6 ArrayList (java.util.ArrayList)4 DistributionRequest (org.apache.sling.distribution.DistributionRequest)3 HashSet (java.util.HashSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Future (java.util.concurrent.Future)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NodeIterator (javax.jcr.NodeIterator)2 Lock (javax.jcr.lock.Lock)2 JackrabbitEvent (org.apache.jackrabbit.api.observation.JackrabbitEvent)2 ImportMode (org.apache.jackrabbit.vault.fs.api.ImportMode)2