Search in sources :

Example 21 with EventIterator

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

the class SuperimposingManagerImplTest method prepareNodeChangeEvent.

private EventIterator prepareNodeChangeEvent(Resource pResource) throws RepositoryException {
    String resourcePath = pResource.getPath();
    Event propertyEvent = mock(Event.class);
    when(propertyEvent.getType()).thenReturn(Event.PROPERTY_CHANGED);
    when(propertyEvent.getPath()).thenReturn(resourcePath + "/" + SuperimposingResourceProvider.PROP_SUPERIMPOSE_SOURCE_PATH);
    EventIterator eventIterator = mock(EventIterator.class);
    when(eventIterator.hasNext()).thenReturn(true, false);
    when(eventIterator.nextEvent()).thenReturn(propertyEvent);
    return eventIterator;
}
Also used : Event(javax.jcr.observation.Event) EventIterator(javax.jcr.observation.EventIterator)

Example 22 with EventIterator

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

the class SearchManager method onEvent.

// ---------------< EventListener interface >--------------------------------
public void onEvent(EventIterator events) {
    log.debug("onEvent: indexing started");
    long time = System.currentTimeMillis();
    // nodes that need to be removed from the index.
    final Set<NodeId> removedNodes = new HashSet<NodeId>();
    // nodes that need to be added to the index.
    final Map<NodeId, EventImpl> addedNodes = new HashMap<NodeId, EventImpl>();
    // property events
    List<EventImpl> propEvents = new ArrayList<EventImpl>();
    while (events.hasNext()) {
        EventImpl e = (EventImpl) events.nextEvent();
        if (!isExcluded(e)) {
            long type = e.getType();
            if (type == Event.NODE_ADDED) {
                addedNodes.put(e.getChildId(), e);
                if (e.isShareableChildNode()) {
                    // simply re-index shareable nodes
                    removedNodes.add(e.getChildId());
                }
            } else if (type == Event.NODE_REMOVED) {
                removedNodes.add(e.getChildId());
                if (e.isShareableChildNode()) {
                    // check if there is a node remaining in the shared set
                    if (itemMgr.hasItemState(e.getChildId())) {
                        addedNodes.put(e.getChildId(), e);
                    }
                }
            } else {
                propEvents.add(e);
            }
        }
    }
    // sort out property events
    for (EventImpl e : propEvents) {
        NodeId nodeId = e.getParentId();
        if (e.getType() == Event.PROPERTY_ADDED) {
            if (addedNodes.put(nodeId, e) == null) {
                // only property added
                // need to re-index
                removedNodes.add(nodeId);
            } else {
            // the node where this prop belongs to is also new
            }
        } else if (e.getType() == Event.PROPERTY_CHANGED) {
            // need to re-index
            addedNodes.put(nodeId, e);
            removedNodes.add(nodeId);
        } else {
            // property removed event is only generated when node still exists
            addedNodes.put(nodeId, e);
            removedNodes.add(nodeId);
        }
    }
    Iterator<NodeState> addedStates = new Iterator<NodeState>() {

        private final Iterator<NodeId> iter = addedNodes.keySet().iterator();

        public void remove() {
            throw new UnsupportedOperationException();
        }

        public boolean hasNext() {
            return iter.hasNext();
        }

        public NodeState next() {
            NodeState item = null;
            NodeId id = (NodeId) iter.next();
            try {
                item = (NodeState) itemMgr.getItemState(id);
            } catch (ItemStateException ise) {
                // check whether this item state change originated from
                // an external event
                EventImpl e = addedNodes.get(id);
                if (e == null || !e.isExternal()) {
                    log.error("Unable to index node " + id + ": does not exist");
                } else {
                    log.info("Node no longer available " + id + ", skipped.");
                }
            }
            return item;
        }
    };
    Iterator<NodeId> removedIds = removedNodes.iterator();
    if (removedNodes.size() > 0 || addedNodes.size() > 0) {
        try {
            handler.updateNodes(removedIds, addedStates);
        } catch (RepositoryException e) {
            log.error("Error indexing node.", e);
        } catch (IOException e) {
            log.error("Error indexing node.", e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("onEvent: indexing finished in " + String.valueOf(System.currentTimeMillis() - time) + " ms.");
    }
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) EventImpl(org.apache.jackrabbit.core.observation.EventImpl) NodeId(org.apache.jackrabbit.core.id.NodeId) EventIterator(javax.jcr.observation.EventIterator) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 23 with EventIterator

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

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

the class ClientEventPoll method run.

// ---------- Thread overwrite ---------------------------------------------
/**
 * Checks for remote events and dispatches them to the locally registered
 * event listeners. This is how this method works:
 * <ol>
 * <li>Continue with next step if {@link #terminate()} has not been called
 * yet and the session is still alive.
 * <li>Call the {@link RemoteObservationManager#getNextEvent(long)} method
 * waiting for a specified time (5 seconds).
 * <li>If no event was received in the specified time go back to step #1.
 * <li>Extract the unique listener identifier from the remote event and
 * find it in the list of locally registered event listeners. Go back to
 * step #1 if no such listener exists.
 * <li>Convert the remote event list to an <code>EventIterator</code> and
 * call the <code>EventListener.onEvent()</code> method.
 * <li>Go back to step #1.
 * </ol>
 */
public void run() {
    while (running && session.isLive()) {
        try {
            // ask for an event waiting at most POLL_TIMEOUT milliseconds
            RemoteEventCollection remoteEvent = remote.getNextEvent(POLL_TIMEOUT);
            // poll time out, check running and ask again
            if (remoteEvent == null) {
                continue;
            }
            // extract the listener id from the remote event and find
            // the locally registered event listener
            Long id = new Long(remoteEvent.getListenerId());
            EventListener listener = (EventListener) listenerMap.get(id);
            // silently ignored, running is checked and the server asked again
            if (listener == null) {
                continue;
            }
            // otherwise convert the remote events into an EventIterator
            // and the listener is called
            RemoteEventCollection.RemoteEvent[] remoteEvents = remoteEvent.getEvents();
            EventIterator events = toEvents(remoteEvents);
            try {
                listener.onEvent(events);
            } catch (Exception e) {
                log.error("Unexpected failure of Listener " + listener, e);
            }
        } catch (RemoteException re) {
            log.error("Problem handling event. Looking for next one.", re);
        }
    }
}
Also used : RemoteEventCollection(org.apache.jackrabbit.rmi.remote.RemoteEventCollection) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) ArrayEventIterator(org.apache.jackrabbit.rmi.iterator.ArrayEventIterator) RemoteException(java.rmi.RemoteException) RemoteRuntimeException(org.apache.jackrabbit.rmi.client.RemoteRuntimeException) RemoteException(java.rmi.RemoteException) RepositoryException(javax.jcr.RepositoryException) RemoteRepositoryException(org.apache.jackrabbit.rmi.client.RemoteRepositoryException)

Example 25 with EventIterator

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

the class WarningOnSaveWithNotificationThreadTest method testWarning.

public void testWarning() throws Exception {
    final List<Exception> exceptions = new ArrayList<Exception>();
    EventResult result = new EventResult(log) {

        @Override
        public void onEvent(EventIterator events) {
            try {
                Session s = getHelper().getSuperuserSession();
                try {
                    s.getNode(testRoot).addNode("bar");
                    s.save();
                } finally {
                    s.logout();
                }
            } catch (RepositoryException e) {
                exceptions.add(e);
            }
            super.onEvent(events);
        }
    };
    addEventListener(result);
    Tail tail = Tail.start(new File("target", "jcr.log"), MESSAGE);
    try {
        testRootNode.addNode("foo");
        superuser.save();
        removeEventListener(result);
        result.getEvents(5000);
        assertTrue("Warn message expected in log file.", tail.getLines().iterator().hasNext());
    } finally {
        tail.close();
    }
    if (!exceptions.isEmpty()) {
        fail(exceptions.get(0).toString());
    }
}
Also used : EventResult(org.apache.jackrabbit.test.api.observation.EventResult) Tail(org.apache.jackrabbit.core.Tail) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) EventIterator(javax.jcr.observation.EventIterator) File(java.io.File) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

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