use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.
the class ObservationTest method testEventRemovedNodeWhenDenyEntryIsRemoved.
/**
* @see <a href="https://issues.apache.org/jira/browse/OAK-4196">OAK-4196</a>
*/
@Test
public void testEventRemovedNodeWhenDenyEntryIsRemoved() throws Exception {
// withdraw the READ privilege on childNPath
deny(childNPath, readPrivileges);
assertFalse(testSession.nodeExists(childNPath));
assertTrue(testSession.nodeExists(childNPath2));
// 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 & childNPath2 in
// order to provoke events being generated
superuser.getItem(childNPath).remove();
superuser.getItem(childNPath2).remove();
superuser.save();
// since the events are generated _after_ persisting all the changes
// and the removal also removes the permission entries denying access
// testUser will be notified about the removal because the remaining
// permission setup after the removal grants read access.
Event[] evts = listener.getEvents(DEFAULT_WAIT_TIMEOUT);
List<String> eventPaths = new ArrayList<String>();
for (Event evt : evts) {
if (evt.getType() == Event.NODE_REMOVED) {
eventPaths.add(evt.getPath());
}
}
assertTrue(eventPaths.contains(childNPath));
assertTrue(eventPaths.contains(childNPath2));
} finally {
obsMgr.removeEventListener(listener);
}
}
use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.
the class ObservationTest method testEventGeneration.
@Test
public void testEventGeneration() throws Exception {
// withdraw the READ privilege
deny(path, readPrivileges);
// testUser registers a event listener for 'path
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 & siblingPath in
// order to provoke events being generated
superuser.getItem(childNPath).remove();
superuser.getItem(siblingPath).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 below " + path + " -> events below must not show up.");
}
}
} finally {
obsMgr.removeEventListener(listener);
}
}
use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.
the class ObservationTest method externalEvents.
@Test
public void externalEvents() throws Throwable {
final Set<String> externallyAdded = synchronizedSet(new LinkedHashSet<String>());
final List<Throwable> exceptions = Lists.newArrayList();
ObservationManager obsMgr = s1.getWorkspace().getObservationManager();
final AtomicLong localEvents = new AtomicLong();
final AtomicLong externalEvents = new AtomicLong();
EventListener listener = new EventListener() {
@Override
public void onEvent(EventIterator events) {
try {
Set<String> paths = Sets.newHashSet();
while (events.hasNext()) {
Event event = events.nextEvent();
String external = "";
AtomicLong counter = localEvents;
if (event instanceof JackrabbitEvent) {
if (((JackrabbitEvent) event).isExternal()) {
external = " (external)";
counter = externalEvents;
paths.add(event.getPath());
}
}
System.out.println(event.getPath() + external);
counter.incrementAndGet();
}
while (!paths.isEmpty()) {
Iterator<String> it = externallyAdded.iterator();
String p = it.next();
assertTrue("missing event for " + p, paths.remove(p));
it.remove();
}
} catch (Throwable e) {
exceptions.add(e);
}
}
};
obsMgr.addEventListener(listener, Event.NODE_ADDED, "/", true, null, null, false);
Future f1 = executor.submit(new Worker(s1, exceptions, new HashSet<String>()));
Future f2 = executor.submit(new Worker(s2, exceptions, externallyAdded));
f1.get();
f2.get();
Thread.sleep(10 * 1000);
System.out.println("local events: " + localEvents.get());
System.out.println("external events: " + externalEvents.get());
for (Throwable t : exceptions) {
throw t;
}
}
use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.
the class NonLocalObservationIT method addEventHandler.
private void addEventHandler(Session s, final String expectedNodeSuffix) throws Exception {
ObservationManager o = s.getWorkspace().getObservationManager();
o.addEventListener(new EventListener() {
@Override
public void onEvent(EventIterator events) {
while (events.hasNext()) {
Event e = events.nextEvent();
if (!(e instanceof JackrabbitEvent)) {
continue;
}
if (((JackrabbitEvent) e).isExternal()) {
continue;
}
String p;
try {
p = e.getPath();
// "+expectedNodeSuffix+", path: " + p);
if (!p.endsWith(expectedNodeSuffix)) {
log.info("EXCEPTION: expectedNodeSuffix: " + expectedNodeSuffix + ", path: " + p);
throw new Exception("expectedNodeSuffix: " + expectedNodeSuffix + ", non-local path: " + p);
}
} catch (Exception e1) {
exception.set(e1);
}
}
}
}, Event.NODE_ADDED, "/", true, null, null, false);
}
use of javax.jcr.observation.ObservationManager in project jackrabbit-oak by apache.
the class CompatibilityIssuesTest method noEventsForTouchedProperties.
/**
* OAK-948 - JR2 generates propertyChange event for touched properties while Oak does not
*/
@Test
public void noEventsForTouchedProperties() throws RepositoryException, InterruptedException {
final String testNodeName = "test_touched_node";
final String testNodePath = '/' + testNodeName;
// Create the test node
Session session = getAdminSession();
Node testNode = session.getRootNode().addNode(testNodeName);
testNode.setProperty("foo", "bar");
testNode.setProperty("foo2", "bar0");
session.save();
Session observingSession = createAdminSession();
ObservationManager observationManager = observingSession.getWorkspace().getObservationManager();
try {
final List<Event> events = new ArrayList<Event>();
final CountDownLatch latch = new CountDownLatch(1);
EventListener listener = new EventListener() {
@Override
public void onEvent(EventIterator eventIt) {
while (eventIt.hasNext()) {
events.add(eventIt.nextEvent());
}
if (!events.isEmpty()) {
latch.countDown();
}
}
};
observationManager.addEventListener(listener, PROPERTY_CHANGED, testNodePath, true, null, null, false);
//Now touch foo and modify foo2
session.getNode(testNodePath).setProperty("foo", "bar");
session.getNode(testNodePath).setProperty("foo2", "bar2");
session.save();
latch.await(60, TimeUnit.SECONDS);
//Only one event is recorded for foo2 modification
assertEquals(1, events.size());
} finally {
observingSession.logout();
}
}
Aggregations