Search in sources :

Example 26 with EventListener

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

the class ScriptableTestsProvider method activate.

protected void activate(final Map<String, Object> props) throws Exception {
    pid = (String) props.get(Constants.SERVICE_PID);
    session = repository.loginAdministrative(repository.getDefaultWorkspace());
    Map<String, Object> auth = new HashMap<>();
    auth.put(JcrResourceConstants.AUTHENTICATION_INFO_SESSION, session);
    resolver = resolverFactory.getResourceResolver(auth);
    // Copy resource resolver paths and make sure they end with a /
    final String[] paths = resolver.getSearchPath();
    allowedRoots = new String[paths.length];
    System.arraycopy(paths, 0, allowedRoots, 0, paths.length);
    for (int i = 0; i < allowedRoots.length; i++) {
        if (!allowedRoots[i].endsWith("/")) {
            allowedRoots[i] += "/";
        }
    }
    // Listen to changes to sling:Test nodes under allowed roots
    final int eventTypes = Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED;
    final boolean isDeep = true;
    final boolean noLocal = true;
    final String[] nodeTypes = { SLING_TEST_NODETYPE };
    final String[] uuid = null;
    for (String path : allowedRoots) {
        final EventListener listener = new RootListener(path);
        listeners.add(listener);
        session.getWorkspace().getObservationManager().addEventListener(listener, eventTypes, path, isDeep, uuid, nodeTypes, noLocal);
        log.debug("Listening for JCR events under {}", path);
    }
    log.info("Activated, will look for test resources under {}", Arrays.asList(allowedRoots));
}
Also used : HashMap(java.util.HashMap) 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