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
}
}
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);
}
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));
}
}
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);
}
}
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();
}
Aggregations