Search in sources :

Example 21 with ObservationManager

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

the class JackrabbitNodeTest method testRenameEventHandling.

public void testRenameEventHandling() throws RepositoryException {
    Session s = getHelper().getSuperuserSession();
    ObservationManager mgr = s.getWorkspace().getObservationManager();
    EventResult result = new EventResult(log);
    try {
        mgr.addEventListener(result, 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.toUpperCase());
        superuser.save();
        boolean foundMove = false;
        for (Event event : result.getEvents(5000)) {
            if (Event.NODE_MOVED == event.getType()) {
                foundMove = true;
                break;
            }
        }
        if (!foundMove) {
            fail("Expected NODE_MOVED event upon renaming a node.");
        }
    } finally {
        mgr.removeEventListener(result);
        s.logout();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) EventResult(org.apache.jackrabbit.test.api.observation.EventResult) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) Session(javax.jcr.Session)

Example 22 with ObservationManager

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

the class SubscriptionManagerImpl method registerSubscription.

/**
     * Register the event listener defined by the given subscription to the
     * repository's observation manager.
     *
     * @param subscription
     * @param resource
     * @throws DavException
     */
private void registerSubscription(SubscriptionImpl subscription, ObservationResource resource) throws DavException {
    try {
        Session session = getRepositorySession(resource);
        ObservationManager oMgr = session.getWorkspace().getObservationManager();
        String itemPath = subscription.getLocator().getRepositoryPath();
        oMgr.addEventListener(subscription, subscription.getJcrEventTypes(), itemPath, subscription.isDeep(), subscription.getUuidFilters(), subscription.getNodetypeNameFilters(), subscription.isNoLocal());
    } catch (RepositoryException e) {
        log.error("Unable to register eventlistener: " + e.getMessage());
        throw new JcrDavException(e);
    }
}
Also used : JcrDavException(org.apache.jackrabbit.webdav.jcr.JcrDavException) ObservationManager(javax.jcr.observation.ObservationManager) RepositoryException(javax.jcr.RepositoryException) JcrDavSession(org.apache.jackrabbit.webdav.jcr.JcrDavSession) Session(javax.jcr.Session)

Example 23 with ObservationManager

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

the class ShareableNodeTest method testObservation.

//------------------------------------------------------ specification tests
/**
     * Verify that observation events are sent only once (6.13.15).
     */
public void testObservation() throws Exception {
    // setup parent nodes and first child
    Node a1 = testRootNode.addNode("a1");
    Node a2 = testRootNode.addNode("a2");
    Node b1 = a1.addNode("b1");
    testRootNode.save();
    // add mixin
    b1.addMixin("mix:shareable");
    b1.save();
    // clone
    Workspace workspace = b1.getSession().getWorkspace();
    workspace.clone(workspace.getName(), b1.getPath(), a2.getPath() + "/b2", false);
    // event listener that counts events received
    class EventCounter implements SynchronousEventListener {

        private int count;

        public void onEvent(EventIterator events) {
            while (events.hasNext()) {
                events.nextEvent();
                count++;
            }
        }

        public int getEventCount() {
            return count;
        }

        public void resetCount() {
            count = 0;
        }
    }
    EventCounter el = new EventCounter();
    ObservationManager om = superuser.getWorkspace().getObservationManager();
    // add node underneath shared set: verify it generates one event only
    om.addEventListener(el, Event.NODE_ADDED, testRootNode.getPath(), true, null, null, false);
    b1.addNode("c");
    b1.save();
    superuser.getWorkspace().getObservationManager().removeEventListener(el);
    assertEquals(1, el.getEventCount());
    // remove node underneath shared set: verify it generates one event only
    el.resetCount();
    om.addEventListener(el, Event.NODE_REMOVED, testRootNode.getPath(), true, null, null, false);
    b1.getNode("c").remove();
    b1.save();
    superuser.getWorkspace().getObservationManager().removeEventListener(el);
    assertEquals(1, el.getEventCount());
    // add property underneath shared set: verify it generates one event only
    el.resetCount();
    om.addEventListener(el, Event.PROPERTY_ADDED, testRootNode.getPath(), true, null, null, false);
    b1.setProperty("c", "1");
    b1.save();
    superuser.getWorkspace().getObservationManager().removeEventListener(el);
    assertEquals(1, el.getEventCount());
    // modify property underneath shared set: verify it generates one event only
    el.resetCount();
    om.addEventListener(el, Event.PROPERTY_CHANGED, testRootNode.getPath(), true, null, null, false);
    b1.setProperty("c", "2");
    b1.save();
    superuser.getWorkspace().getObservationManager().removeEventListener(el);
    assertEquals(1, el.getEventCount());
    // remove property underneath shared set: verify it generates one event only
    el.resetCount();
    om.addEventListener(el, Event.PROPERTY_REMOVED, testRootNode.getPath(), true, null, null, false);
    b1.getProperty("c").remove();
    b1.save();
    superuser.getWorkspace().getObservationManager().removeEventListener(el);
    assertEquals(1, el.getEventCount());
}
Also used : Node(javax.jcr.Node) ObservationManager(javax.jcr.observation.ObservationManager) EventIterator(javax.jcr.observation.EventIterator) Workspace(javax.jcr.Workspace) SynchronousEventListener(org.apache.jackrabbit.core.observation.SynchronousEventListener)

Example 24 with ObservationManager

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

the class AbstractWriteTest method testEventGeneration.

public void testEventGeneration() throws RepositoryException, NotExecutableException {
    /*
         precondition:
         testuser must have READ-only permission on test-node and below
        */
    checkReadOnly(path);
    Session testSession = getTestSession();
    // withdraw the READ privilege
    Privilege[] dnPrivs = privilegesFromName(Privilege.JCR_READ);
    withdrawPrivileges(path, dnPrivs, getRestrictions(superuser, path));
    // testUser registers a event listener for 'path
    ObservationManager obsMgr = testSession.getWorkspace().getObservationManager();
    EventResult listener = new EventResult(((JUnitTest) this).log);
    try {
        obsMgr.addEventListener(listener, Event.NODE_REMOVED, path, true, new String[0], new String[0], true);
        // superuser removes the node with childNPath in order to provoke
        // events being generated
        superuser.getItem(childNPath).remove();
        superuser.save();
        obsMgr.removeEventListener(listener);
        // since the testUser does not have read-permission on the removed
        // node, 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) Privilege(javax.jcr.security.Privilege) Session(javax.jcr.Session)

Example 25 with ObservationManager

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

the class ObservationTest method observationThroughput.

public void observationThroughput(final Repository repository, @Nullable Whiteboard whiteboard) throws RepositoryException, InterruptedException, ExecutionException {
    long t = 0;
    final AtomicInteger eventCount = new AtomicInteger();
    final AtomicInteger nodeCount = new AtomicInteger();
    List<Session> sessions = Lists.newArrayList();
    List<EventListener> listeners = Lists.newArrayList();
    List<String> testPaths = Lists.newArrayList();
    Session s = createSession(repository);
    String path = "/path/to/observation/benchmark-" + AbstractTest.TEST_ID;
    try {
        Node testRoot = JcrUtils.getOrCreateByPath(path, null, s);
        for (int i = 0; i < WRITER_COUNT; i++) {
            testPaths.add(testRoot.addNode("session-" + i).getPath());
        }
        s.save();
    } finally {
        s.logout();
    }
    String pathFilter = PATH_FILTER == null ? path : PATH_FILTER;
    System.out.println("Path filter for event listener: " + pathFilter);
    ExecutorService service = Executors.newFixedThreadPool(WRITER_COUNT);
    try {
        for (int k = 0; k < LISTENER_COUNT; k++) {
            sessions.add(createSession(repository));
            listeners.add(new Listener(eventCount));
            ObservationManager obsMgr = sessions.get(k).getWorkspace().getObservationManager();
            obsMgr.addEventListener(listeners.get(k), EVENT_TYPES, pathFilter, true, null, null, false);
        }
        // also add a listener on the root node
        addRootListener(repository, sessions, listeners);
        List<Future<Object>> createNodes = Lists.newArrayList();
        for (final String p : testPaths) {
            createNodes.add(service.submit(new Callable<Object>() {

                private final Session session = createSession(repository);

                private int numNodes = 0;

                @Override
                public Object call() throws Exception {
                    try {
                        Node testRoot = session.getNode(p);
                        createChildren(testRoot, 100);
                        for (Node m : JcrUtils.getChildNodes(testRoot)) {
                            createChildren(m, 100 / WRITER_COUNT);
                            for (Node n : JcrUtils.getChildNodes(m)) {
                                createChildren(n, 5);
                            }
                        }
                        session.save();
                    } finally {
                        session.logout();
                    }
                    return null;
                }

                private void createChildren(Node node, int count) throws RepositoryException {
                    for (int c = 0; c < count; c++) {
                        node.addNode("n" + c);
                        nodeCount.incrementAndGet();
                        if (++numNodes % SAVE_INTERVAL == 0) {
                            node.getSession().save();
                        }
                    }
                }
            }));
        }
        System.out.println("ms      #node   nodes/s #event  event/s event-ratio queue external");
        while (!isDone(createNodes) || (eventCount.get() / LISTENER_COUNT < nodeCount.get() * EVENTS_PER_NODE)) {
            long t0 = System.currentTimeMillis();
            Thread.sleep(OUTPUT_RESOLUTION);
            t += System.currentTimeMillis() - t0;
            int nc = nodeCount.get();
            int ec = eventCount.get() / LISTENER_COUNT;
            int[] ql = getObservationQueueLength(whiteboard);
            double nps = (double) nc / t * 1000;
            double eps = (double) ec / t * 1000;
            double epn = (double) ec / nc / EVENTS_PER_NODE;
            System.out.format("%7d %7d %7.1f %7d %7.1f %7.2f %7d %7d%n", t, nc, nps, ec, eps, epn, ql[0], ql[1]);
        }
        get(createNodes);
    } finally {
        for (int k = 0; k < sessions.size(); k++) {
            sessions.get(k).getWorkspace().getObservationManager().removeEventListener(listeners.get(k));
            sessions.get(k).logout();
        }
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : EventListener(javax.jcr.observation.EventListener) Node(javax.jcr.Node) ObservationManager(javax.jcr.observation.ObservationManager) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) EventListener(javax.jcr.observation.EventListener) Session(javax.jcr.Session)

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