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