Search in sources :

Example 11 with Event

use of javax.jcr.observation.Event 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 12 with Event

use of javax.jcr.observation.Event 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 13 with Event

use of javax.jcr.observation.Event 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 14 with Event

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

the class ObservationTest method propertyValues.

@Test
public void propertyValues() throws RepositoryException, ExecutionException, InterruptedException {
    Node n = getNode(TEST_PATH);
    n.setProperty("toChange", new String[] { "one", "two" }, PropertyType.STRING);
    n.setProperty("toDelete", new String[] { "three", "four" }, PropertyType.STRING);
    getAdminSession().save();
    ExpectationListener listener = new ExpectationListener();
    observationManager.addEventListener(listener, ALL_EVENTS, "/", true, null, null, false);
    try {
        n.setProperty("added", new String[] { "five", "six" }, PropertyType.STRING);
        listener.expectValues(null, n.getProperty("added").getValues());
        Value[] before = n.getProperty("toChange").getValues();
        n.getProperty("toChange").setValue(new String[] { "1", "2" });
        listener.expectValues(before, n.getProperty("toChange").getValues());
        before = n.getProperty("toDelete").getValues();
        n.getProperty("toDelete").remove();
        listener.expectValues(before, null);
        getAdminSession().save();
        List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
        assertTrue("Missing events: " + missing, missing.isEmpty());
        List<Event> unexpected = listener.getUnexpected();
        assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    } finally {
        observationManager.removeEventListener(listener);
    }
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Value(javax.jcr.Value) Event(javax.jcr.observation.Event) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 15 with Event

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

the class ObservationTest method testAggregate2.

@Test
public void testAggregate2() throws Exception {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setAbsPath("/parent");
    filter.setIsDeep(true);
    filter.setEventTypes(ALL_EVENTS);
    filter = FilterFactory.wrap(filter).withNodeTypeAggregate(new String[] { "oak:Unstructured" }, // "file", "file/jcr:content",
    new String[] { "", "**" });
    // "file/jcr:content/**");
    oManager.addEventListener(listener, filter);
    Node parent = getAdminSession().getRootNode().addNode("parent", "nt:unstructured");
    // OAK-5096: in OR mode the following event also gets sent:
    listener.expect(parent.getPath() + "/jcr:primaryType", PROPERTY_ADDED);
    Node child = parent.addNode("child", "oak:Unstructured");
    listener.expectAdd(child);
    Node file = child.addNode("file", "nt:unstructured");
    listener.expectAdd(file);
    Node jcrContent = file.addNode("jcr:content", "nt:unstructured");
    listener.expect(jcrContent.getPath(), "/parent/child", NODE_ADDED);
    listener.expect(jcrContent.getPath() + "/jcr:primaryType", "/parent/child", PROPERTY_ADDED);
    Property jcrDataProperty = jcrContent.setProperty("jcr:data", "foo");
    listener.expect(jcrDataProperty.getPath(), "/parent/child", PROPERTY_ADDED);
    parent.getSession().save();
    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());
    file = getAdminSession().getRootNode().getNode("parent").getNode("child").getNode("file");
    jcrContent = file.getNode("jcr:content");
    Property newProperty = jcrContent.setProperty("newProperty", "foo");
    listener.expect(newProperty.getPath(), "/parent/child", PROPERTY_ADDED);
    Property lastModifiedBy = jcrContent.setProperty("jcr:lastModifiedBy", "bar");
    listener.expect(lastModifiedBy.getPath(), "/parent/child", PROPERTY_ADDED);
    jcrContent.getSession().save();
    Thread.sleep(2000);
    missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    assertTrue("Missing events: " + missing, missing.isEmpty());
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) Property(javax.jcr.Property) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Aggregations

Event (javax.jcr.observation.Event)138 Node (javax.jcr.Node)104 Test (org.junit.Test)56 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)46 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)41 RepositoryException (javax.jcr.RepositoryException)31 Session (javax.jcr.Session)21 JackrabbitEventFilter (org.apache.jackrabbit.api.observation.JackrabbitEventFilter)19 EventResult (org.apache.jackrabbit.test.api.observation.EventResult)17 EventIterator (javax.jcr.observation.EventIterator)16 ObservationManager (javax.jcr.observation.ObservationManager)14 Property (javax.jcr.Property)13 SlingRepository (org.apache.sling.jcr.api.SlingRepository)10 ArrayList (java.util.ArrayList)9 EventListener (javax.jcr.observation.EventListener)9 Scheduler (org.apache.sling.commons.scheduler.Scheduler)9 DistributionRequest (org.apache.sling.distribution.DistributionRequest)9 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)8 ExecutionException (java.util.concurrent.ExecutionException)7 PathNotFoundException (javax.jcr.PathNotFoundException)7