Search in sources :

Example 16 with EventListener

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

the class ScriptableTestsProvider method deactivate.

protected void deactivate() throws RepositoryException {
    if (resolver != null) {
        resolver.close();
        resolver = null;
    }
    if (session != null) {
        for (EventListener listener : listeners) {
            session.getWorkspace().getObservationManager().removeEventListener(listener);
        }
        listeners.clear();
        session.logout();
    }
    session = null;
}
Also used : EventListener(javax.jcr.observation.EventListener)

Example 17 with EventListener

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

the class SessionImpl method removeRegisteredEventListeners.

/**
     * Utility method that removes all registered event listeners.
     */
private void removeRegisteredEventListeners() {
    try {
        ObservationManager manager = getWorkspace().getObservationManager();
        // Use a copy to avoid modifying the set of registered listeners
        // while iterating over it
        Collection<EventListener> listeners = IteratorUtils.toList(manager.getRegisteredEventListeners());
        for (EventListener listener : listeners) {
            try {
                manager.removeEventListener(listener);
            } catch (RepositoryException e) {
                log.warn("Error removing event listener: " + listener, e);
            }
        }
    } catch (RepositoryException e) {
        log.warn("Error removing event listeners", e);
    }
}
Also used : ObservationManager(javax.jcr.observation.ObservationManager) RepositoryException(javax.jcr.RepositoryException) EventListener(javax.jcr.observation.EventListener)

Example 18 with EventListener

use of javax.jcr.observation.EventListener 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 19 with EventListener

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

the class WorkspaceResourceImpl method update.

/**
     * While RFC 3253 does not define any version-related operations for the
     * workspace resource, this implementation uses {@link VersionControlledResource#update(UpdateInfo)}
     * to map {@link Workspace#restore(javax.jcr.version.Version[], boolean)} to
     * a WebDAV call.
     * <p>
     * Limitation: note that the <code>MultiStatus</code> returned by this method
     * will not list any nodes that have been removed due to an Uuid conflict.
     *
     * @param updateInfo
     * @return
     * @throws org.apache.jackrabbit.webdav.DavException
     * @see org.apache.jackrabbit.webdav.version.VersionControlledResource#update(org.apache.jackrabbit.webdav.version.UpdateInfo)
     */
@Override
public MultiStatus update(UpdateInfo updateInfo) throws DavException {
    if (updateInfo == null) {
        throw new DavException(DavServletResponse.SC_BAD_REQUEST, "Valid update request body required.");
    }
    if (!exists()) {
        throw new DavException(DavServletResponse.SC_NOT_FOUND);
    }
    Session session = getRepositorySession();
    MultiStatus ms = new MultiStatus();
    try {
        Element udElem = updateInfo.getUpdateElement();
        boolean removeExisting = DomUtil.hasChildElement(udElem, ItemResourceConstants.XML_REMOVEEXISTING, ItemResourceConstants.NAMESPACE);
        // register eventListener in order to be able to report the modified resources.
        EventListener el = new EListener(updateInfo.getPropertyNameSet(), ms);
        registerEventListener(el, session.getRootNode().getPath());
        String[] hrefs = updateInfo.getVersionHref();
        if (hrefs == null || hrefs.length < 1) {
            throw new DavException(DavServletResponse.SC_BAD_REQUEST, "Invalid update request body: at least a single version href must be specified.");
        }
        // perform the update/restore according to the update info
        Version[] versions = new Version[hrefs.length];
        for (int i = 0; i < hrefs.length; i++) {
            DavResourceLocator vLoc = getLocator().getFactory().createResourceLocator(getLocator().getPrefix(), hrefs[i]);
            String versionPath = vLoc.getRepositoryPath();
            Item item = getRepositorySession().getItem(versionPath);
            if (item instanceof Version) {
                versions[i] = (Version) item;
            } else {
                throw new DavException(DavServletResponse.SC_BAD_REQUEST, "Invalid update request body: href does not identify a version " + hrefs[i]);
            }
        }
        session.getWorkspace().restore(versions, removeExisting);
        // unregister the event listener again
        unregisterEventListener(el);
    } catch (RepositoryException e) {
        throw new JcrDavException(e);
    }
    return ms;
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) MultiStatus(org.apache.jackrabbit.webdav.MultiStatus) RepositoryException(javax.jcr.RepositoryException) Item(javax.jcr.Item) Version(javax.jcr.version.Version) EventListener(javax.jcr.observation.EventListener) Session(javax.jcr.Session) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Example 20 with EventListener

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

the class ObservationManagerImpl method dispose.

/**
     * Unregisters all EventListeners.
     */
public void dispose() {
    try {
        EventListenerIterator it = getRegisteredEventListeners();
        while (it.hasNext()) {
            EventListener l = it.nextEventListener();
            log.debug("removing EventListener: " + l);
            removeEventListener(l);
        }
    } catch (RepositoryException e) {
        log.error("Internal error: Unable to dispose ObservationManager.", e);
    }
}
Also used : EventListenerIterator(javax.jcr.observation.EventListenerIterator) RepositoryException(javax.jcr.RepositoryException) EventListener(javax.jcr.observation.EventListener)

Aggregations

EventListener (javax.jcr.observation.EventListener)26 EventIterator (javax.jcr.observation.EventIterator)12 RepositoryException (javax.jcr.RepositoryException)11 Session (javax.jcr.Session)10 ObservationManager (javax.jcr.observation.ObservationManager)10 Event (javax.jcr.observation.Event)8 Node (javax.jcr.Node)6 ArrayList (java.util.ArrayList)3 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)3 JackrabbitEvent (org.apache.jackrabbit.api.observation.JackrabbitEvent)3 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Future (java.util.concurrent.Future)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 PathNotFoundException (javax.jcr.PathNotFoundException)2 Lock (javax.jcr.lock.Lock)2 Version (javax.jcr.version.Version)2 Test (org.junit.Test)2 RemoteException (java.rmi.RemoteException)1 HashSet (java.util.HashSet)1