Search in sources :

Example 1 with Event

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

the class HierarchyEventListener method pushEvents.

/**
     * Retrieve the workspace state(s) affected by the given event and refresh
     * them accordingly.
     *
     * @param events the events to process.
     */
private void pushEvents(Collection<Event> events) {
    if (events.isEmpty()) {
        log.debug("Empty event bundle");
        return;
    }
    // TODO: handle new 283 event types and clean add/remove that is also present as move-event.
    // collect set of removed node ids
    Set<ItemId> removedEvents = new HashSet<ItemId>();
    // separately collect the add events
    Set<Event> addEvents = new HashSet<Event>();
    for (Iterator<Event> it = events.iterator(); it.hasNext(); ) {
        Event event = it.next();
        int type = event.getType();
        if (type == Event.NODE_REMOVED) {
            // remember removed nodes separately for proper handling later on.
            removedEvents.add(event.getItemId());
        } else if (type == Event.NODE_ADDED || type == Event.PROPERTY_ADDED) {
            addEvents.add(event);
            it.remove();
        }
    }
    /* Process ADD-events.
           In case of persisting transients modifications, the event-set may
           still contain events that are not covered by the changeLog such as
           new version-history or other autocreated properties and nodes.

           Add events need to be processed hierarchically, since its not possible
           to add a new child reference to a state that is not yet present in
           the state manager.
           The 'progress' flag is used to make sure, that during each loop at
           least one event has been processed and removed from the iterator.
           If this is not the case, there are not parent states present in the
           state manager that need to be updated and the remaining events may
           be ignored.
         */
    boolean progress = true;
    while (!addEvents.isEmpty() && progress) {
        progress = false;
        for (Iterator<Event> it = addEvents.iterator(); it.hasNext(); ) {
            Event ev = it.next();
            NodeId parentId = ev.getParentId();
            HierarchyEntry parent = null;
            if (parentId != null) {
                parent = hierarchyMgr.lookup(parentId);
                if (parent == null && ev.getPath() != null && parentId.getUniqueID() != null) {
                    // the parent by path.
                    try {
                        Path parentPath = ev.getPath().getAncestor(1);
                        parent = hierarchyMgr.lookup(parentPath);
                    } catch (RepositoryException e) {
                        // should not occur
                        log.debug(e.getMessage());
                    }
                }
            }
            if (parent != null && parent.denotesNode()) {
                ((NodeEntry) parent).refresh(ev);
                it.remove();
                progress = true;
            }
        }
    }
    /* process all other events (removal, property changed) */
    for (Event event : events) {
        int type = event.getType();
        NodeId parentId = event.getParentId();
        NodeEntry parent = (parentId != null) ? (NodeEntry) hierarchyMgr.lookup(parentId) : null;
        switch(type) {
            case Event.NODE_REMOVED:
            case Event.PROPERTY_REMOVED:
                //   only remove the parent an skip this event.
                if (parent != null && !removedEvents.contains(parentId)) {
                    parent.refresh(event);
                }
                break;
            case Event.PROPERTY_CHANGED:
                // not exist either -> no need to inform propEntry
                if (parent != null) {
                    parent.refresh(event);
                }
                break;
            case Event.NODE_MOVED:
                // TODO: implementation missing
                throw new UnsupportedOperationException("Implementation missing");
            //break;
            case Event.PERSIST:
                // TODO: implementation missing
                throw new UnsupportedOperationException("Implementation missing");
            //break;
            default:
                // should never occur
                throw new IllegalArgumentException("Invalid event type: " + event.getType());
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) RepositoryException(javax.jcr.RepositoryException) ItemId(org.apache.jackrabbit.spi.ItemId) NodeId(org.apache.jackrabbit.spi.NodeId) Event(org.apache.jackrabbit.spi.Event) HashSet(java.util.HashSet)

Example 2 with Event

use of org.apache.jackrabbit.spi.Event 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 3 with Event

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

the class RepositoryServiceImpl method getEvents.

/**
     * {@inheritDoc}
     */
public EventBundle getEvents(SessionInfo sessionInfo, EventFilter filter, long after) throws RepositoryException, UnsupportedRepositoryOperationException {
    SessionInfoImpl sInfo = getSessionInfoImpl(sessionInfo);
    EventJournal journal = sInfo.getSession().getWorkspace().getObservationManager().getEventJournal();
    if (journal == null) {
        throw new UnsupportedRepositoryOperationException();
    }
    EventFactory factory = new EventFactory(sInfo.getSession(), sInfo.getNamePathResolver(), idFactory, qValueFactory);
    journal.skipTo(after);
    List<Event> events = new ArrayList<Event>();
    int batchSize = 1024;
    boolean distinctDates = true;
    long lastDate = Long.MIN_VALUE;
    while (journal.hasNext() && (batchSize > 0 || !distinctDates)) {
        Event e = factory.fromJCREvent(journal.nextEvent());
        if (filter.accept(e, false)) {
            distinctDates = lastDate != e.getDate();
            lastDate = e.getDate();
            events.add(e);
            batchSize--;
        }
    }
    return new EventBundleImpl(events, false);
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) EventBundleImpl(org.apache.jackrabbit.spi.commons.EventBundleImpl) ArrayList(java.util.ArrayList) Event(org.apache.jackrabbit.spi.Event) EventJournal(javax.jcr.observation.EventJournal)

Example 4 with Event

use of org.apache.jackrabbit.spi.Event 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 5 with Event

use of org.apache.jackrabbit.spi.Event 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)

Aggregations

Event (org.apache.jackrabbit.spi.Event)8 RepositoryException (javax.jcr.RepositoryException)6 ArrayList (java.util.ArrayList)5 EventBundle (org.apache.jackrabbit.spi.EventBundle)4 EventBundleImpl (org.apache.jackrabbit.spi.commons.EventBundleImpl)4 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)3 AccessDeniedException (javax.jcr.AccessDeniedException)2 InvalidItemStateException (javax.jcr.InvalidItemStateException)2 ItemNotFoundException (javax.jcr.ItemNotFoundException)2 NamespaceException (javax.jcr.NamespaceException)2 PathNotFoundException (javax.jcr.PathNotFoundException)2 LockException (javax.jcr.lock.LockException)2 ItemId (org.apache.jackrabbit.spi.ItemId)2 NodeId (org.apache.jackrabbit.spi.NodeId)2 Path (org.apache.jackrabbit.spi.Path)2 ElementIterator (org.apache.jackrabbit.webdav.xml.ElementIterator)2 Element (org.w3c.dom.Element)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1