Search in sources :

Example 26 with ObservationManager

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

the class RepositoryServiceImpl method executeWithLocalEvents.

private Object executeWithLocalEvents(Callable call, SessionInfoImpl sInfo) throws RepositoryException {
    if (supportsObservation) {
        // register local event listener
        Collection<EventSubscription> subscr = sInfo.getSubscriptions();
        if (subscr.size() != 0) {
            ObservationManager obsMgr = sInfo.getSession().getWorkspace().getObservationManager();
            List<EventListener> listeners = new ArrayList<EventListener>(subscr.size());
            try {
                for (EventSubscription s : subscr) {
                    EventListener listener = s.getLocalEventListener();
                    listeners.add(listener);
                    obsMgr.addEventListener(listener, EventSubscription.ALL_EVENTS, "/", true, null, null, false);
                }
                return call.run();
            } finally {
                for (EventListener listener : listeners) {
                    try {
                        obsMgr.removeEventListener(listener);
                    } catch (RepositoryException e) {
                    // ignore and remove next
                    }
                }
            }
        }
    }
    // if we get here simply run as is
    return call.run();
}
Also used : ArrayList(java.util.ArrayList) ObservationManager(javax.jcr.observation.ObservationManager) RepositoryException(javax.jcr.RepositoryException) EventListener(javax.jcr.observation.EventListener)

Example 27 with ObservationManager

use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.

the class ObservationTest method testEventRemovedNode.

/**
     * @see <a href="https://issues.apache.org/jira/browse/OAK-4196">OAK-4196</a>
     */
@Test
public void testEventRemovedNode() throws Exception {
    // withdraw the READ privilege on childNPath
    deny(path, readPrivileges);
    assertFalse(testSession.nodeExists(childNPath));
    // testUser registers a event listener for changes under testRoot
    ObservationManager obsMgr = testSession.getWorkspace().getObservationManager();
    EventResult listener = new EventResult(this.log);
    try {
        obsMgr.addEventListener(listener, Event.NODE_REMOVED, testRoot, true, null, null, true);
        // superuser removes the node with childNPath order to provoke events being generated
        superuser.getItem(childNPath).remove();
        superuser.save();
        // since the testUser does not have read-permission on the removed
        // childNPath, no corresponding event must be generated.
        Event[] evts = listener.getEvents(DEFAULT_WAIT_TIMEOUT);
        for (Event evt : evts) {
            if (evt.getType() == Event.NODE_REMOVED && evt.getPath().equals(childNPath)) {
                fail("TestUser does not have READ permission on " + childNPath);
            }
        }
    } finally {
        obsMgr.removeEventListener(listener);
    }
}
Also used : EventResult(org.apache.jackrabbit.test.api.observation.EventResult) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) Test(org.junit.Test)

Example 28 with ObservationManager

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

the class JcrResourceListenerScalabilityTest method setUp.

@SuppressWarnings("deprecation")
@Before
public void setUp() throws RepositoryException, InvalidSyntaxException {
    ObservationManager observationManager = mock(ObservationManager.class);
    Workspace workspace = mock(Workspace.class);
    when(workspace.getObservationManager()).thenReturn(observationManager);
    Session session = mock(Session.class);
    when(session.getWorkspace()).thenReturn(workspace);
    SlingRepository repository = mock(SlingRepository.class);
    when(repository.loginAdministrative(null)).thenReturn(session);
    final ProviderContext ctx = new SimpleProviderContext();
    this.config = new JcrListenerBaseConfig(ctx.getObservationReporter(), RepositoryUtil.getRepository());
    jcrResourceListener = new JcrResourceListener(this.config, ctx.getObservationReporter().getObserverConfigurations().get(0));
    Event event = mock(MockEvent.class);
    events = mock(EventIterator.class);
    when(events.hasNext()).thenReturn(true);
    when(event.getPath()).thenCallRealMethod();
    when(event.getType()).thenReturn(Event.NODE_ADDED);
    when(events.nextEvent()).thenReturn(event);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) ProviderContext(org.apache.sling.spi.resource.provider.ProviderContext) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) EventIterator(javax.jcr.observation.EventIterator) Workspace(javax.jcr.Workspace) Session(javax.jcr.Session) Before(org.junit.Before)

Example 29 with ObservationManager

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

the class PersistingJcrEventDistributionTriggerTest method testProcessEventWithPrivileges.

@Test
public void testProcessEventWithPrivileges() throws Exception {
    String nuggetsPath = "/var/nuggets";
    String serviceName = "serviceId";
    Session session = mock(Session.class);
    when(session.nodeExists("/var/nuggets")).thenReturn(true);
    Workspace workspace = mock(Workspace.class);
    ObservationManager observationManager = mock(ObservationManager.class);
    when(workspace.getObservationManager()).thenReturn(observationManager);
    when(session.getWorkspace()).thenReturn(workspace);
    when(session.hasPermission(nuggetsPath, 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 nuggetsNode = mock(Node.class);
    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)

Aggregations

ObservationManager (javax.jcr.observation.ObservationManager)29 Session (javax.jcr.Session)16 Event (javax.jcr.observation.Event)13 Test (org.junit.Test)11 EventListener (javax.jcr.observation.EventListener)10 Node (javax.jcr.Node)8 RepositoryException (javax.jcr.RepositoryException)8 EventIterator (javax.jcr.observation.EventIterator)8 Workspace (javax.jcr.Workspace)6 EventResult (org.apache.jackrabbit.test.api.observation.EventResult)6 ArrayList (java.util.ArrayList)4 DistributionRequest (org.apache.sling.distribution.DistributionRequest)3 HashSet (java.util.HashSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Future (java.util.concurrent.Future)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NodeIterator (javax.jcr.NodeIterator)2 Lock (javax.jcr.lock.Lock)2 JackrabbitEvent (org.apache.jackrabbit.api.observation.JackrabbitEvent)2 ImportMode (org.apache.jackrabbit.vault.fs.api.ImportMode)2