Search in sources :

Example 26 with Event

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

the class PersistingJcrEventDistributionTriggerTest method testProcessEventWithPrivilegesAndNoNuggetsNode.

@Test
public void testProcessEventWithPrivilegesAndNoNuggetsNode() throws Exception {
    String nuggetsPath = "/var/nuggets";
    String serviceName = "serviceId";
    Session session = mock(Session.class);
    // first time it doesn't exist and should be created
    when(session.nodeExists("/var/nuggets")).thenReturn(false);
    // second time it should exist
    when(session.nodeExists("/var/nuggets")).thenReturn(true);
    Node rootNode = mock(Node.class);
    Node varNode = mock(Node.class);
    Node nuggetsNode = mock(Node.class);
    when(varNode.addNode("nuggets", "sling:Folder")).thenReturn(nuggetsNode);
    when(rootNode.addNode("var", "sling:Folder")).thenReturn(varNode);
    when(session.getRootNode()).thenReturn(rootNode);
    Workspace workspace = mock(Workspace.class);
    ObservationManager observationManager = mock(ObservationManager.class);
    when(workspace.getObservationManager()).thenReturn(observationManager);
    when(session.getWorkspace()).thenReturn(workspace);
    when(session.hasPermission(any(String.class), eq(Session.ACTION_ADD_NODE))).thenReturn(true);
    SlingRepository repository = mock(SlingRepository.class);
    Scheduler scheduler = mock(Scheduler.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    when(repository.loginService(serviceName, null)).thenReturn(session);
    String path = "/some/path";
    PersistedJcrEventDistributionTrigger persistingJcrEventdistributionTrigger = new PersistedJcrEventDistributionTrigger(repository, scheduler, resolverFactory, path, serviceName, nuggetsPath);
    DistributionRequestHandler handler = mock(DistributionRequestHandler.class);
    persistingJcrEventdistributionTrigger.register(handler);
    Node eventNode = mock(Node.class);
    when(nuggetsNode.addNode(any(String.class), any(String.class))).thenReturn(eventNode);
    when(session.getNode(nuggetsPath)).thenReturn(nuggetsNode);
    Event event = mock(Event.class);
    when(event.getPath()).thenReturn("/some/path/generating/event");
    DistributionRequest distributionRequest = persistingJcrEventdistributionTrigger.processEvent(event);
    assertNotNull(distributionRequest);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionRequestHandler(org.apache.sling.distribution.trigger.DistributionRequestHandler) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) Session(javax.jcr.Session) Workspace(javax.jcr.Workspace) Test(org.junit.Test)

Example 27 with Event

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

the class JcrEventDistributionTriggerTest method testProcessEventWithPathProperty.

@Test
public void testProcessEventWithPathProperty() throws Exception {
    SlingRepository repository = mock(SlingRepository.class);
    Scheduler scheduler = mock(Scheduler.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    String path = "/some/path";
    String serviceName = "serviceId";
    JcrEventDistributionTrigger jcrEventdistributionTrigger = new JcrEventDistributionTrigger(repository, scheduler, resolverFactory, path, false, serviceName, null);
    Event event = mock(Event.class);
    when(event.getPath()).thenReturn("/some/path/generating/event");
    DistributionRequest distributionRequest = jcrEventdistributionTrigger.processEvent(event);
    assertNotNull(distributionRequest);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Event(javax.jcr.observation.Event) Test(org.junit.Test)

Example 28 with Event

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

the class PersistingJcrEventDistributionTriggerTest method testProcessEventWithoutPrivileges.

@Test
public void testProcessEventWithoutPrivileges() throws Exception {
    String serviceName = "serviceId";
    Session session = mock(Session.class);
    when(session.nodeExists("/var/nuggets")).thenReturn(true);
    SlingRepository repository = mock(SlingRepository.class);
    Scheduler scheduler = mock(Scheduler.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    when(repository.loginService(serviceName, null)).thenReturn(session);
    String path = "/some/path";
    String nuggetsPath = "/var/nuggets";
    PersistedJcrEventDistributionTrigger persistingJcrEventdistributionTrigger = new PersistedJcrEventDistributionTrigger(repository, scheduler, resolverFactory, path, serviceName, nuggetsPath);
    Event event = mock(Event.class);
    DistributionRequest distributionRequest = persistingJcrEventdistributionTrigger.processEvent(event);
    assertNull(distributionRequest);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Event(javax.jcr.observation.Event) Session(javax.jcr.Session) Test(org.junit.Test)

Example 29 with Event

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

the class RootFolderListener method onEvent.

/**
     * Schedule a scan.
     */
public void onEvent(final EventIterator it) {
    // we only do the global scan for node changes
    boolean globalScan = false;
    // copy watched folders and remove all for other roots
    final List<WatchedFolder> checkFolders = cfg.cloneWatchedFolders();
    final Iterator<WatchedFolder> i = checkFolders.iterator();
    while (i.hasNext()) {
        final WatchedFolder wf = i.next();
        if (!wf.getPathWithSlash().startsWith(this.pathWithSlash)) {
            i.remove();
        }
    }
    while (it.hasNext()) {
        final Event e = it.nextEvent();
        logger.debug("Got event {}", e);
        if (e.getType() == Event.NODE_ADDED || e.getType() == Event.NODE_REMOVED) {
            globalScan = true;
        }
        try {
            final String path = e.getPath();
            final Iterator<WatchedFolder> ii = checkFolders.iterator();
            while (ii.hasNext()) {
                final WatchedFolder folder = ii.next();
                if (path.startsWith(folder.getPathWithSlash())) {
                    folder.markForScan();
                    ii.remove();
                    break;
                }
            }
        } catch (final RepositoryException re) {
            logger.warn("Error while getting path from event", re);
        }
    }
    if (globalScan) {
        timer.scheduleScan();
    }
}
Also used : Event(javax.jcr.observation.Event) RepositoryException(javax.jcr.RepositoryException)

Example 30 with Event

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

the class SuperimposingManagerImpl method onEvent.

/**
     * Handle resource events to add or remove superimposing registrations
     */
public void onEvent(EventIterator events) {
    if (!isEnabled()) {
        return;
    }
    try {
        // collect all actions to be performed for this event
        final Map<String, Boolean> actions = new HashMap<String, Boolean>();
        boolean nodeAdded = false;
        boolean nodeRemoved = false;
        while (events.hasNext()) {
            final Event event = events.nextEvent();
            final String path = event.getPath();
            final String name = ResourceUtil.getName(path);
            if (event.getType() == Event.NODE_ADDED) {
                nodeAdded = true;
            } else if (event.getType() == Event.NODE_REMOVED && superimposingProviders.containsKey(path)) {
                nodeRemoved = true;
                actions.put(path, false);
            } else if (StringUtils.equals(name, PROP_SUPERIMPOSE_SOURCE_PATH) || StringUtils.equals(name, PROP_SUPERIMPOSE_REGISTER_PARENT) || StringUtils.equals(name, PROP_SUPERIMPOSE_OVERLAYABLE)) {
                final String nodePath = ResourceUtil.getParent(path);
                actions.put(nodePath, true);
            }
        }
        // transaction)
        for (Map.Entry<String, Boolean> action : actions.entrySet()) {
            if (action.getValue()) {
                registerProvider(action.getKey());
            } else {
                unregisterProvider(action.getKey());
            }
        }
        if (nodeAdded && nodeRemoved) {
            // maybe a superimposing was moved, re-register all superimposings
            // (existing ones will be skipped)
            registerAllSuperimposings();
        }
    } catch (RepositoryException e) {
        log.error("Unexpected repository exception during event processing.");
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Event(javax.jcr.observation.Event) RepositoryException(javax.jcr.RepositoryException) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Event (javax.jcr.observation.Event)138 Node (javax.jcr.Node)104 Test (org.junit.Test)56 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)46 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)41 RepositoryException (javax.jcr.RepositoryException)31 Session (javax.jcr.Session)21 JackrabbitEventFilter (org.apache.jackrabbit.api.observation.JackrabbitEventFilter)19 EventResult (org.apache.jackrabbit.test.api.observation.EventResult)17 EventIterator (javax.jcr.observation.EventIterator)16 ObservationManager (javax.jcr.observation.ObservationManager)14 Property (javax.jcr.Property)13 SlingRepository (org.apache.sling.jcr.api.SlingRepository)10 ArrayList (java.util.ArrayList)9 EventListener (javax.jcr.observation.EventListener)9 Scheduler (org.apache.sling.commons.scheduler.Scheduler)9 DistributionRequest (org.apache.sling.distribution.DistributionRequest)9 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)8 ExecutionException (java.util.concurrent.ExecutionException)7 PathNotFoundException (javax.jcr.PathNotFoundException)7