Search in sources :

Example 1 with EventBundle

use of org.apache.jackrabbit.spi.EventBundle in project jackrabbit by apache.

the class WorkspaceManager method onEventReceived.

// --------------------------------------------------------------------------
/**
 * Called when local or external events occurred. This method is called after
 * changes have been applied to the repository.
 *
 * @param eventBundles the event bundles generated by the repository service
 *                     as the effect of an local or external change.
 * @param lstnrs Array of internal event listeners
 * @throws InterruptedException if this thread is interrupted while waiting
 *                              for the {@link #updateSync}.
 */
private void onEventReceived(EventBundle[] eventBundles, InternalEventListener[] lstnrs) throws InterruptedException {
    if (log.isDebugEnabled()) {
        log.debug("received {} event bundles.", eventBundles.length);
        for (EventBundle eventBundle : eventBundles) {
            log.debug("IsLocal:  {}", eventBundle.isLocal());
            for (Iterator<Event> it = eventBundle.getEvents(); it.hasNext(); ) {
                Event e = it.next();
                String type;
                switch(e.getType()) {
                    case Event.NODE_ADDED:
                        type = "NodeAdded";
                        break;
                    case Event.NODE_REMOVED:
                        type = "NodeRemoved";
                        break;
                    case Event.PROPERTY_ADDED:
                        type = "PropertyAdded";
                        break;
                    case Event.PROPERTY_CHANGED:
                        type = "PropertyChanged";
                        break;
                    case Event.PROPERTY_REMOVED:
                        type = "PropertyRemoved";
                        break;
                    case Event.NODE_MOVED:
                        type = "NodeMoved";
                        break;
                    case Event.PERSIST:
                        type = "Persist";
                        break;
                    default:
                        type = "Unknown";
                }
                log.debug("  {}; {}", e.getPath(), type);
            }
        }
    }
    // do not deliver events while an operation executes
    updateSync.acquire();
    try {
        // notify listener
        for (EventBundle eventBundle : eventBundles) {
            for (InternalEventListener lstnr : lstnrs) {
                try {
                    lstnr.onEvent(eventBundle);
                } catch (Exception e) {
                    log.warn("Exception in event polling thread: " + e);
                    log.debug("Dump:", e);
                }
            }
        }
    } finally {
        updateSync.release();
    }
}
Also used : InternalEventListener(org.apache.jackrabbit.jcr2spi.observation.InternalEventListener) Event(org.apache.jackrabbit.spi.Event) EventBundle(org.apache.jackrabbit.spi.EventBundle) ItemExistsException(javax.jcr.ItemExistsException) NoSuchWorkspaceException(javax.jcr.NoSuchWorkspaceException) ItemNotFoundException(javax.jcr.ItemNotFoundException) AccessDeniedException(javax.jcr.AccessDeniedException) PathNotFoundException(javax.jcr.PathNotFoundException) LockException(javax.jcr.lock.LockException) NoSuchNodeTypeException(javax.jcr.nodetype.NoSuchNodeTypeException) InvalidQueryException(javax.jcr.query.InvalidQueryException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) VersionException(javax.jcr.version.VersionException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) RepositoryException(javax.jcr.RepositoryException) MergeException(javax.jcr.MergeException) NamespaceException(javax.jcr.NamespaceException) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException)

Example 2 with EventBundle

use of org.apache.jackrabbit.spi.EventBundle in project jackrabbit by apache.

the class EventSubscription method createEventBundle.

// --------------------------------< internal >------------------------------
private void createEventBundle(javax.jcr.observation.EventIterator events, boolean isLocal) {
    // do not create events when disposed
    if (disposed) {
        return;
    }
    List<Event> spiEvents = new ArrayList<Event>();
    while (events.hasNext()) {
        try {
            Event spiEvent = eventFactory.fromJCREvent(events.nextEvent());
            spiEvents.add(spiEvent);
        } catch (Exception ex) {
            log.warn("Unable to create SPI Event: " + ex);
        }
    }
    EventBundle bundle = new EventBundleImpl(spiEvents, isLocal);
    synchronized (eventBundles) {
        eventBundles.add(bundle);
        eventBundles.notify();
    }
}
Also used : EventBundleImpl(org.apache.jackrabbit.spi.commons.EventBundleImpl) ArrayList(java.util.ArrayList) Event(org.apache.jackrabbit.spi.Event) EventBundle(org.apache.jackrabbit.spi.EventBundle) RepositoryException(javax.jcr.RepositoryException)

Example 3 with EventBundle

use of org.apache.jackrabbit.spi.EventBundle in project jackrabbit by apache.

the class EventJournalImpl method refill.

// ----------------------------< internal >----------------------------------
private void refill() {
    try {
        EventBundle bundle = wspMgr.getEvents(filter, lastTimestamp);
        for (Event e : bundle) {
            buffer.add(e);
            lastTimestamp = e.getDate();
        }
    } catch (RepositoryException e) {
        log.warn("Exception while refilling event journal buffer", e);
    }
}
Also used : Event(org.apache.jackrabbit.spi.Event) EventBundle(org.apache.jackrabbit.spi.EventBundle) RepositoryException(javax.jcr.RepositoryException)

Example 4 with EventBundle

use of org.apache.jackrabbit.spi.EventBundle in project jackrabbit by apache.

the class RepositoryServiceImpl method poll.

private EventBundle[] poll(String uri, String subscriptionId, long timeout, SessionInfoImpl sessionInfo) throws RepositoryException {
    HttpPoll request = null;
    try {
        request = new HttpPoll(uri, subscriptionId, timeout);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        EventDiscovery disc = request.getResponseBodyAsEventDiscovery(response);
        EventBundle[] events;
        if (disc.isEmpty()) {
            events = new EventBundle[0];
        } else {
            Element discEl = disc.toXml(DomUtil.createDocument());
            ElementIterator it = DomUtil.getChildren(discEl, ObservationConstants.N_EVENTBUNDLE);
            List<EventBundle> bundles = new ArrayList<EventBundle>();
            while (it.hasNext()) {
                Element bundleElement = it.nextElement();
                String value = DomUtil.getAttribute(bundleElement, ObservationConstants.XML_EVENT_LOCAL, null);
                // check if it matches a batch id recently submitted
                boolean isLocal = false;
                if (value != null) {
                    isLocal = Boolean.parseBoolean(value);
                }
                bundles.add(new EventBundleImpl(buildEventList(bundleElement, sessionInfo, uri), isLocal));
            }
            events = bundles.toArray(new EventBundle[bundles.size()]);
        }
        return events;
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (ParserConfigurationException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) EventBundle(org.apache.jackrabbit.spi.EventBundle) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) HttpPoll(org.apache.jackrabbit.webdav.client.methods.HttpPoll) EventDiscovery(org.apache.jackrabbit.webdav.observation.EventDiscovery) EventBundleImpl(org.apache.jackrabbit.spi.commons.EventBundleImpl) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 5 with EventBundle

use of org.apache.jackrabbit.spi.EventBundle in project jackrabbit by apache.

the class EventSubscription method getEventBundles.

/**
 * @return all the pending event bundles.
 */
EventBundle[] getEventBundles(long timeout) {
    EventBundle[] bundles;
    synchronized (eventBundles) {
        if (eventBundles.isEmpty()) {
            try {
                eventBundles.wait(timeout);
            } catch (InterruptedException e) {
            // continue
            }
        }
        bundles = eventBundles.toArray(new EventBundle[eventBundles.size()]);
        eventBundles.clear();
    }
    EventFilter[] eventFilters = filters.toArray(new EventFilter[filters.size()]);
    // apply filters to bundles
    for (int i = 0; i < bundles.length; i++) {
        List<Event> filteredEvents = new ArrayList<Event>();
        for (Iterator<Event> it = bundles[i].getEvents(); it.hasNext(); ) {
            Event e = it.next();
            // TODO: this is actually not correct. if filters are empty no event should go out
            if (eventFilters == null || eventFilters.length == 0) {
                filteredEvents.add(e);
            } else {
                for (EventFilter eventFilter : eventFilters) {
                    if (eventFilter.accept(e, bundles[i].isLocal())) {
                        filteredEvents.add(e);
                        break;
                    }
                }
            }
        }
        bundles[i] = new EventBundleImpl(filteredEvents, bundles[i].isLocal());
    }
    return bundles;
}
Also used : EventBundleImpl(org.apache.jackrabbit.spi.commons.EventBundleImpl) ArrayList(java.util.ArrayList) Event(org.apache.jackrabbit.spi.Event) EventBundle(org.apache.jackrabbit.spi.EventBundle) EventFilter(org.apache.jackrabbit.spi.EventFilter)

Aggregations

EventBundle (org.apache.jackrabbit.spi.EventBundle)5 RepositoryException (javax.jcr.RepositoryException)4 Event (org.apache.jackrabbit.spi.Event)4 ArrayList (java.util.ArrayList)3 EventBundleImpl (org.apache.jackrabbit.spi.commons.EventBundleImpl)3 IOException (java.io.IOException)1 AccessDeniedException (javax.jcr.AccessDeniedException)1 InvalidItemStateException (javax.jcr.InvalidItemStateException)1 ItemExistsException (javax.jcr.ItemExistsException)1 ItemNotFoundException (javax.jcr.ItemNotFoundException)1 MergeException (javax.jcr.MergeException)1 NamespaceException (javax.jcr.NamespaceException)1 NoSuchWorkspaceException (javax.jcr.NoSuchWorkspaceException)1 PathNotFoundException (javax.jcr.PathNotFoundException)1 ReferentialIntegrityException (javax.jcr.ReferentialIntegrityException)1 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)1 LockException (javax.jcr.lock.LockException)1 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)1 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)1 InvalidQueryException (javax.jcr.query.InvalidQueryException)1