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;
}
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);
}
}
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);
}
}
}
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;
}
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);
}
}
Aggregations