Search in sources :

Example 6 with EventIterator

use of javax.jcr.observation.EventIterator in project sling by apache.

the class SuperimposingManagerImplTest method prepareNodeCreateEvent.

private EventIterator prepareNodeCreateEvent(Resource pResource) throws RepositoryException {
    String resourcePath = pResource.getPath();
    Event nodeEvent = mock(Event.class);
    when(nodeEvent.getType()).thenReturn(Event.NODE_ADDED);
    when(nodeEvent.getPath()).thenReturn(resourcePath);
    Event propertyEvent = mock(Event.class);
    when(propertyEvent.getType()).thenReturn(Event.PROPERTY_ADDED);
    when(propertyEvent.getPath()).thenReturn(resourcePath + "/" + SuperimposingResourceProvider.PROP_SUPERIMPOSE_SOURCE_PATH);
    EventIterator eventIterator = mock(EventIterator.class);
    when(eventIterator.hasNext()).thenReturn(true, true, false);
    when(eventIterator.nextEvent()).thenReturn(nodeEvent, propertyEvent);
    return eventIterator;
}
Also used : Event(javax.jcr.observation.Event) EventIterator(javax.jcr.observation.EventIterator)

Example 7 with EventIterator

use of javax.jcr.observation.EventIterator in project sling by apache.

the class SuperimposingManagerImplTest method prepareNodeRemoveEvent.

private EventIterator prepareNodeRemoveEvent(Resource pResource) throws RepositoryException {
    String resourcePath = pResource.getPath();
    Event nodeEvent = mock(Event.class);
    when(nodeEvent.getType()).thenReturn(Event.NODE_REMOVED);
    when(nodeEvent.getPath()).thenReturn(resourcePath);
    EventIterator eventIterator = mock(EventIterator.class);
    when(eventIterator.hasNext()).thenReturn(true, false);
    when(eventIterator.nextEvent()).thenReturn(nodeEvent);
    return eventIterator;
}
Also used : Event(javax.jcr.observation.Event) EventIterator(javax.jcr.observation.EventIterator)

Example 8 with EventIterator

use of javax.jcr.observation.EventIterator 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)

Example 9 with EventIterator

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

the class JackrabbitNodeTest method _testRenameEventHandling.

// Ignore("OAK-3658")
public void _testRenameEventHandling() throws RepositoryException, InterruptedException {
    Session s = getHelper().getSuperuserSession();
    ObservationManager mgr = s.getWorkspace().getObservationManager();
    final List<Event> events = newArrayList();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    try {
        mgr.addEventListener(new EventListener() {

            CountDownLatch latch = latch1;

            @Override
            public void onEvent(EventIterator eventIterator) {
                synchronized (events) {
                    while (eventIterator.hasNext()) {
                        events.add(eventIterator.nextEvent());
                    }
                    latch.countDown();
                    latch = latch2;
                }
            }
        }, Event.PERSIST | Event.NODE_ADDED | Event.NODE_MOVED | Event.NODE_REMOVED, testRootNode.getPath(), true, null, null, false);
        NodeIterator it = testRootNode.getNodes();
        Node n = it.nextNode();
        String name = n.getName();
        JackrabbitNode node = (JackrabbitNode) n;
        node.rename(name + 'X');
        superuser.save();
        StringBuilder diags = new StringBuilder();
        if (!latch1.await(60, SECONDS)) {
            diags.append("latch1 timed out ");
        }
        boolean foundMove = false;
        synchronized (events) {
            for (Event event : events) {
                if (diags.length() != 0) {
                    diags.append(", ");
                }
                diags.append("type " + event.getType() + " " + event.getDate() + "ms " + event.getPath());
                if (Event.NODE_MOVED == event.getType()) {
                    foundMove = true;
                    break;
                }
            }
            if (events.isEmpty()) {
                diags.append("none");
            }
        }
        if (!foundMove) {
            // force another event, wait some more
            testRootNode.addNode(name + "XYZ");
            superuser.save();
            StringBuffer addDiags = new StringBuffer();
            if (!latch2.await(60, SECONDS)) {
                addDiags.append("latch2 timed out ");
            }
            synchronized (events) {
                for (Event event : events) {
                    if (addDiags.length() != 0) {
                        addDiags.append(", ");
                    }
                    addDiags.append("type " + event.getType() + " " + event.getDate() + "ms " + event.getPath());
                }
            }
            if (addDiags.length() > 0) {
                diags.append("; next event after additional addNode/save operation: " + addDiags);
            }
        }
        if (!foundMove) {
            fail("Expected NODE_MOVED event upon renaming a node (received: " + diags + ")");
        }
    } finally {
        s.logout();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) ObservationManager(javax.jcr.observation.ObservationManager) CountDownLatch(java.util.concurrent.CountDownLatch) Event(javax.jcr.observation.Event) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) Session(javax.jcr.Session)

Example 10 with EventIterator

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

the class ObservationTest method doIncludeAncestorsRemove.

private FilterProvider doIncludeAncestorsRemove(JackrabbitEventFilter filter) throws Exception {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    Node testNode = getNode(TEST_PATH);
    testNode.addNode("a").addNode("b").addNode("c").addNode("d").setProperty("e", 42);
    testNode.getSession().save();
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    oManager.addEventListener(listener, filter);
    Node d = testNode.getNode("a").getNode("b").getNode("c").getNode("d");
    Property e = d.getProperty("e");
    listener.expectRemove(e);
    // listener.expectRemove(d.getProperty("jcr:primaryType"));
    // d.remove();
    listener.expectRemove(d).remove();
    testNode.getSession().save();
    Thread.sleep(1000);
    List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    List<Event> unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    assertTrue("Missing events: " + missing, missing.isEmpty());
    oManager.addEventListener(new EventListener() {

        @Override
        public void onEvent(EventIterator events) {
            while (events.hasNext()) {
                System.out.println("/a-listener GOT: " + events.next());
            }
        }
    }, NODE_REMOVED, TEST_PATH + "/a", false, null, null, false);
    System.out.println("REGISTERED");
    testNode = getNode(TEST_PATH);
    Node b = testNode.getNode("a").getNode("b");
    listener.expect(b.getPath(), NODE_REMOVED);
    listener.optional(new Expectation("/a/b/c is optionally sent depending on filter") {

        @Override
        public boolean onEvent(Event event) throws Exception {
            if (event.getPath().equals(TEST_PATH + "/a/b/c") && event.getType() == NODE_REMOVED) {
                return true;
            }
            return false;
        }
    });
    b.remove();
    // but not the jcr:primaryType
    testNode.getSession().save();
    Thread.sleep(1000);
    missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    unexpected = listener.getUnexpected();
    assertTrue("Missing events: " + missing, missing.isEmpty());
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    Node a = testNode.getNode("a");
    listener.expect(a.getPath(), NODE_REMOVED);
    a.remove();
    // but not the jcr:primaryType
    testNode.getSession().save();
    missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    assertTrue("Missing events: " + missing, missing.isEmpty());
    ChangeProcessor cp = oManager.getChangeProcessor(listener);
    assertNotNull(cp);
    FilterProvider filterProvider = cp.getFilterProvider();
    assertNotNull(filterProvider);
    return filterProvider;
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) ItemExistsException(javax.jcr.ItemExistsException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) TimeoutException(java.util.concurrent.TimeoutException) VersionException(javax.jcr.version.VersionException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) AccessDeniedException(javax.jcr.AccessDeniedException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) LockException(javax.jcr.lock.LockException) NoSuchNodeTypeException(javax.jcr.nodetype.NoSuchNodeTypeException) ExecutionException(java.util.concurrent.ExecutionException) FilterProvider(org.apache.jackrabbit.oak.plugins.observation.filter.FilterProvider) Event(javax.jcr.observation.Event) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) Property(javax.jcr.Property)

Aggregations

EventIterator (javax.jcr.observation.EventIterator)26 Event (javax.jcr.observation.Event)14 EventListener (javax.jcr.observation.EventListener)12 RepositoryException (javax.jcr.RepositoryException)9 Session (javax.jcr.Session)9 Node (javax.jcr.Node)7 ObservationManager (javax.jcr.observation.ObservationManager)7 ArrayList (java.util.ArrayList)4 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)3 JackrabbitEvent (org.apache.jackrabbit.api.observation.JackrabbitEvent)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 PathNotFoundException (javax.jcr.PathNotFoundException)2 Lock (javax.jcr.lock.Lock)2 FilterProvider (org.apache.jackrabbit.oak.plugins.observation.filter.FilterProvider)2 File (java.io.File)1 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 HashMap (java.util.HashMap)1