Search in sources :

Example 16 with EventIterator

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

the class EventIteratorTest method testSkip.

/**
 * Tests the method skip()
 */
public void testSkip() throws RepositoryException {
    EventResult listener = new EventResult(log);
    addEventListener(listener, Event.NODE_ADDED);
    testRootNode.addNode(nodeName1, testNodeType);
    testRootNode.addNode(nodeName2, testNodeType);
    testRootNode.addNode(nodeName3, testNodeType);
    testRootNode.getSession().save();
    EventIterator events = listener.getEventIterator(DEFAULT_WAIT_TIMEOUT);
    removeEventListener(listener);
    assertNotNull("No events delivered within " + DEFAULT_WAIT_TIMEOUT + "ms.", events);
    // skip zero elements
    events.skip(0);
    assertEquals("getPosition() for first element must return 0.", 0, events.getPosition());
    // skip one element
    events.skip(2);
    assertEquals("Wrong value for getPosition()", 2, events.getPosition());
    // skip past end
    try {
        events.skip(2);
        fail("EventIterator must throw NoSuchElementException when skipping past the end");
    } catch (NoSuchElementException e) {
    // success
    }
}
Also used : EventIterator(javax.jcr.observation.EventIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 17 with EventIterator

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

the class EventIteratorTest method testGetSize.

/**
 * Tests if getSize() returns the correct number of events. If getSize()
 * returns -1 a {@link org.apache.jackrabbit.test.NotExecutableException}
 * is thrown.
 */
public void testGetSize() throws RepositoryException, NotExecutableException {
    EventResult listener = new EventResult(log);
    addEventListener(listener, Event.NODE_ADDED);
    testRootNode.addNode(nodeName1, testNodeType);
    testRootNode.getSession().save();
    EventIterator events = listener.getEventIterator(DEFAULT_WAIT_TIMEOUT);
    removeEventListener(listener);
    assertNotNull("No events delivered within " + DEFAULT_WAIT_TIMEOUT + "ms.", events);
    long size = events.getSize();
    if (size == -1) {
        throw new NotExecutableException("EventIterator.getSize() returns unavailable size.");
    }
    assertEquals("Wrong number of events", 1, size);
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) EventIterator(javax.jcr.observation.EventIterator)

Example 18 with EventIterator

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

the class ObservationTest method testJCR_2293.

/**
 * Check whether an item with the path of an add node event exists.
 * Regression test for JCR-2293.
 * @throws RepositoryException
 * @throws InterruptedException
 */
public void testJCR_2293() throws RepositoryException, InterruptedException {
    final String parentPath = testNode.getPath();
    final String folderName = "folder_" + System.currentTimeMillis();
    final Session session = getHelper().getReadWriteSession();
    final Session session2 = getHelper().getReadOnlySession();
    // Don't remove. See JCR-2293.
    session2.getItem(parentPath);
    WaitableEventListener eventListener = new WaitableEventListener() {

        private RepositoryException failure;

        private boolean done;

        public synchronized void onEvent(final EventIterator events) {
            try {
                while (events.hasNext()) {
                    Event event = events.nextEvent();
                    Item item2 = session2.getItem(event.getPath());
                    assertEquals(parentPath + "/" + folderName, item2.getPath());
                }
            } catch (RepositoryException e) {
                failure = e;
            } finally {
                done = true;
                notifyAll();
            }
        }

        public synchronized void waitForEvent(int timeout) throws InterruptedException, RepositoryException {
            if (!done) {
                wait(timeout);
            }
            if (!done) {
                fail("Event listener not called");
            }
            if (failure != null) {
                throw failure;
            }
        }
    };
    session2.getWorkspace().getObservationManager().addEventListener(eventListener, Event.NODE_ADDED, parentPath, true, null, null, false);
    Node parent = (Node) session.getItem(parentPath);
    Node toDelete = parent.addNode(folderName, "nt:folder");
    parent.save();
    try {
        eventListener.waitForEvent(60000);
    } finally {
        toDelete.remove();
        parent.save();
        assertFalse(parent.hasNode(folderName));
    }
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) RepositoryException(javax.jcr.RepositoryException) EventIterator(javax.jcr.observation.EventIterator) Session(javax.jcr.Session)

Example 19 with EventIterator

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

the class ChangeProcessor method contentChanged.

@Override
public void contentChanged(@Nonnull NodeState before, @Nonnull NodeState after, @Nonnull CommitInfo info) {
    // OAK-5160 before is now guaranteed to be non-null
    checkNotNull(before);
    checkNotNull(after);
    checkNotNull(info);
    try {
        long start = PERF_LOGGER.start();
        FilterProvider provider = filterProvider.get();
        // FIXME don't rely on toString for session id
        if (provider.includeCommit(contentSession.toString(), info)) {
            EventFilter filter = provider.getFilter(before, after);
            EventIterator events = new EventQueue(namePathMapper, info, before, after, provider.getSubTrees(), Filters.all(filter, VISIBLE_FILTER), provider.getEventAggregator());
            long time = System.nanoTime();
            boolean hasEvents = events.hasNext();
            tracker.recordProducerTime(System.nanoTime() - time, TimeUnit.NANOSECONDS);
            if (hasEvents && runningMonitor.enterIf(running)) {
                if (commitRateLimiter != null) {
                    commitRateLimiter.beforeNonBlocking();
                }
                try {
                    CountingIterator countingEvents = new CountingIterator(events);
                    eventListener.onEvent(countingEvents);
                    countingEvents.updateCounters(eventCount, eventDuration);
                } finally {
                    if (commitRateLimiter != null) {
                        commitRateLimiter.afterNonBlocking();
                    }
                    runningMonitor.leave();
                }
            }
        }
        PERF_LOGGER.end(start, 100, "Generated events (before: {}, after: {})", before, after);
    } catch (Exception e) {
        LOG.warn("Error while dispatching observation events for " + tracker, e);
    }
}
Also used : EventIterator(javax.jcr.observation.EventIterator) EventFilter(org.apache.jackrabbit.oak.plugins.observation.filter.EventFilter) FilterProvider(org.apache.jackrabbit.oak.plugins.observation.filter.FilterProvider)

Example 20 with EventIterator

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

the class ObservationTest method doIncludeAncestorsRemove_WithGlobs.

void doIncludeAncestorsRemove_WithGlobs(OakEventFilter oef) throws Exception {
    Node testNode = getNode(TEST_PATH);
    testNode.addNode("a").addNode("b").addNode("c").addNode("d.jsp").setProperty("e", 42);
    testNode.getSession().save();
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    final AtomicBoolean done = new AtomicBoolean(false);
    final AtomicBoolean unexpected = new AtomicBoolean(false);
    final AtomicBoolean failure = new AtomicBoolean(false);
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(EventIterator events) {
            while (events.hasNext()) {
                Event event = events.nextEvent();
                System.out.println("got: " + event);
                String path = "";
                try {
                    path = event.getPath();
                } catch (RepositoryException e) {
                    e.printStackTrace();
                    failure.set(true);
                }
                if (path.equals(TEST_PATH + "/a/b") && event.getType() == NODE_REMOVED) {
                    done.set(true);
                } else if (path.equals(TEST_PATH + "/a/b/c/d.jsp") && event.getType() == NODE_REMOVED) {
                    done.set(true);
                } else if (path.equals(TEST_PATH + "/a/b/c/d.jsp/jcr:primaryType") && event.getType() == PROPERTY_REMOVED) {
                    done.set(true);
                } else {
                    System.out.println("Unexpected event: " + event);
                    unexpected.set(true);
                }
            }
        }
    };
    oManager.addEventListener(listener, oef);
    Node b = testNode.getNode("a").getNode("b");
    b.remove();
    testNode.getSession().save();
    Thread.sleep(1000);
    assertTrue("didnt get either event", done.get());
    assertFalse("did get unexpected events", unexpected.get());
    assertFalse("got an exception", failure.get());
    oManager.removeEventListener(listener);
    testNode.getNode("a").remove();
    testNode.getSession().save();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) RepositoryException(javax.jcr.RepositoryException) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator)

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