Search in sources :

Example 1 with EventIterator

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

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

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

the class JcrConsumerTest method testJcrConsumer.

@Test
public void testJcrConsumer() throws Exception {
    // start consumer thread first
    JcrConsumerThread consumerThread = new JcrConsumerThread();
    consumerThread.start();
    // wait until the consumer thread has tried to receive event at least once
    while (consumerThread.getReceiveTrialTimes() < 1) {
        Thread.sleep(10L);
    }
    // now create a node under the specified event node path
    Session session = openSession();
    try {
        Node folderNode = session.getRootNode();
        for (String folderNodeName : absPath.split("\\/")) {
            if (!"".equals(folderNodeName)) {
                if (folderNode.hasNode(folderNodeName)) {
                    folderNode.getNode(folderNodeName).remove();
                }
                folderNode = folderNode.addNode(folderNodeName, "nt:unstructured");
            }
        }
        folderNode.addNode("node", "nt:unstructured");
        session.save();
    } finally {
        if (session != null && session.isLive()) {
            session.logout();
        }
    }
    // wait until the consumer thread captures an event
    consumerThread.join();
    Exchange exchange = consumerThread.getExchange();
    assertNotNull(exchange);
    Message message = exchange.getIn();
    assertNotNull(message);
    assertTrue(message instanceof JcrMessage);
    EventIterator eventIterator = ((JcrMessage) message).getEventIterator();
    assertNotNull(eventIterator);
    assertEquals(1, eventIterator.getSize());
    List<?> eventList = message.getBody(List.class);
    assertEquals(1, eventList.size());
    Event event = (Event) eventList.get(0);
    assertEquals(Event.NODE_ADDED, event.getType());
    assertNotNull(event.getPath());
    assertTrue(event.getPath().startsWith(absPath));
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) EventIterator(javax.jcr.observation.EventIterator) Session(javax.jcr.Session) Test(org.junit.Test)

Example 4 with EventIterator

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

the class SuperimposingManagerImpl method activate.

// ---------- SCR Integration
@Activate
protected synchronized void activate(final ComponentContext ctx) throws LoginException, RepositoryException {
    // check enabled state
    @SuppressWarnings("unchecked") final Dictionary<String, Object> props = ctx.getProperties();
    this.enabled = PropertiesUtil.toBoolean(props.get(ENABLED_PROPERTY), ENABLED_DEFAULT);
    log.debug("Config: " + "Enabled={} ", enabled);
    if (!isEnabled()) {
        return;
    }
    // get "find all" queries
    this.findAllQueries = PropertiesUtil.toStringArray(props.get(FINDALLQUERIES_PROPERTY), new String[] { FINDALLQUERIES_DEFAULT });
    this.obervationPaths = PropertiesUtil.toStringArray(props.get(OBSERVATION_PATHS_PROPERTY), new String[] { OBSERVATION_PATHS_DEFAULT });
    if (null == resolver) {
        bundleContext = ctx.getBundleContext();
        resolver = resolverFactory.getAdministrativeResourceResolver(null);
        // Watch for events on the root to register/deregister superimposings at runtime
        // For each observed path create an event listener object which redirects the event to the main class
        final Session session = resolver.adaptTo(Session.class);
        if (session != null) {
            this.observationEventListeners = new EventListener[this.obervationPaths.length];
            for (int i = 0; i < this.obervationPaths.length; i++) {
                this.observationEventListeners[i] = new EventListener() {

                    public void onEvent(EventIterator events) {
                        SuperimposingManagerImpl.this.onEvent(events);
                    }
                };
                session.getWorkspace().getObservationManager().addEventListener(this.observationEventListeners[i], Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, // absolute path
                this.obervationPaths[i], // isDeep
                true, // uuids
                null, // node types
                null, // noLocal
                true);
            }
        }
        // register all superimposing definitions that already exist
        initialization = Executors.newSingleThreadExecutor().submit(new Runnable() {

            public void run() {
                try {
                    registerAllSuperimposings();
                } catch (Throwable ex) {
                    log.warn("Error registering existing superimposing resources on service startup.", ex);
                }
            }
        });
    }
}
Also used : EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) Session(javax.jcr.Session) Activate(org.apache.felix.scr.annotations.Activate)

Example 5 with EventIterator

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

the class SuperimposingManagerImplTest method prepareNodeMoveEvent.

private EventIterator prepareNodeMoveEvent(Resource pResource, String pOldPath) throws RepositoryException {
    String resourcePath = pResource.getPath();
    Event nodeRemoveEvent = mock(Event.class);
    when(nodeRemoveEvent.getType()).thenReturn(Event.NODE_REMOVED);
    when(nodeRemoveEvent.getPath()).thenReturn(pOldPath);
    Event nodeCreateEvent = mock(Event.class);
    when(nodeCreateEvent.getType()).thenReturn(Event.NODE_ADDED);
    when(nodeCreateEvent.getPath()).thenReturn(resourcePath);
    EventIterator eventIterator = mock(EventIterator.class);
    when(eventIterator.hasNext()).thenReturn(true, true, false);
    when(eventIterator.nextEvent()).thenReturn(nodeRemoveEvent, nodeCreateEvent);
    return eventIterator;
}
Also used : Event(javax.jcr.observation.Event) 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