use of javax.jcr.observation.ObservationManager in project jackrabbit by apache.
the class JackrabbitNodeTest method testRenameEventHandling.
public void testRenameEventHandling() throws RepositoryException {
Session s = getHelper().getSuperuserSession();
ObservationManager mgr = s.getWorkspace().getObservationManager();
EventResult result = new EventResult(log);
try {
mgr.addEventListener(result, Event.PERSIST | Event.NODE_ADDED | Event.NODE_MOVED | Event.NODE_REMOVED, testRootNode.getPath(), true, null, null, false);
NodeIterator it = testRootNode.getNodes();
Node n = it.nextNode();
String name = n.getName();
JackrabbitNode node = (JackrabbitNode) n;
node.rename(name.toUpperCase());
superuser.save();
boolean foundMove = false;
for (Event event : result.getEvents(5000)) {
if (Event.NODE_MOVED == event.getType()) {
foundMove = true;
break;
}
}
if (!foundMove) {
fail("Expected NODE_MOVED event upon renaming a node.");
}
} finally {
mgr.removeEventListener(result);
s.logout();
}
}
use of javax.jcr.observation.ObservationManager in project jackrabbit by apache.
the class SubscriptionManagerImpl method registerSubscription.
/**
* Register the event listener defined by the given subscription to the
* repository's observation manager.
*
* @param subscription
* @param resource
* @throws DavException
*/
private void registerSubscription(SubscriptionImpl subscription, ObservationResource resource) throws DavException {
try {
Session session = getRepositorySession(resource);
ObservationManager oMgr = session.getWorkspace().getObservationManager();
String itemPath = subscription.getLocator().getRepositoryPath();
oMgr.addEventListener(subscription, subscription.getJcrEventTypes(), itemPath, subscription.isDeep(), subscription.getUuidFilters(), subscription.getNodetypeNameFilters(), subscription.isNoLocal());
} catch (RepositoryException e) {
log.error("Unable to register eventlistener: " + e.getMessage());
throw new JcrDavException(e);
}
}
use of javax.jcr.observation.ObservationManager in project jackrabbit by apache.
the class ShareableNodeTest method testObservation.
//------------------------------------------------------ specification tests
/**
* Verify that observation events are sent only once (6.13.15).
*/
public void testObservation() throws Exception {
// setup parent nodes and first child
Node a1 = testRootNode.addNode("a1");
Node a2 = testRootNode.addNode("a2");
Node b1 = a1.addNode("b1");
testRootNode.save();
// add mixin
b1.addMixin("mix:shareable");
b1.save();
// clone
Workspace workspace = b1.getSession().getWorkspace();
workspace.clone(workspace.getName(), b1.getPath(), a2.getPath() + "/b2", false);
// event listener that counts events received
class EventCounter implements SynchronousEventListener {
private int count;
public void onEvent(EventIterator events) {
while (events.hasNext()) {
events.nextEvent();
count++;
}
}
public int getEventCount() {
return count;
}
public void resetCount() {
count = 0;
}
}
EventCounter el = new EventCounter();
ObservationManager om = superuser.getWorkspace().getObservationManager();
// add node underneath shared set: verify it generates one event only
om.addEventListener(el, Event.NODE_ADDED, testRootNode.getPath(), true, null, null, false);
b1.addNode("c");
b1.save();
superuser.getWorkspace().getObservationManager().removeEventListener(el);
assertEquals(1, el.getEventCount());
// remove node underneath shared set: verify it generates one event only
el.resetCount();
om.addEventListener(el, Event.NODE_REMOVED, testRootNode.getPath(), true, null, null, false);
b1.getNode("c").remove();
b1.save();
superuser.getWorkspace().getObservationManager().removeEventListener(el);
assertEquals(1, el.getEventCount());
// add property underneath shared set: verify it generates one event only
el.resetCount();
om.addEventListener(el, Event.PROPERTY_ADDED, testRootNode.getPath(), true, null, null, false);
b1.setProperty("c", "1");
b1.save();
superuser.getWorkspace().getObservationManager().removeEventListener(el);
assertEquals(1, el.getEventCount());
// modify property underneath shared set: verify it generates one event only
el.resetCount();
om.addEventListener(el, Event.PROPERTY_CHANGED, testRootNode.getPath(), true, null, null, false);
b1.setProperty("c", "2");
b1.save();
superuser.getWorkspace().getObservationManager().removeEventListener(el);
assertEquals(1, el.getEventCount());
// remove property underneath shared set: verify it generates one event only
el.resetCount();
om.addEventListener(el, Event.PROPERTY_REMOVED, testRootNode.getPath(), true, null, null, false);
b1.getProperty("c").remove();
b1.save();
superuser.getWorkspace().getObservationManager().removeEventListener(el);
assertEquals(1, el.getEventCount());
}
use of javax.jcr.observation.ObservationManager in project jackrabbit by apache.
the class AbstractWriteTest method testEventGeneration.
public void testEventGeneration() throws RepositoryException, NotExecutableException {
/*
precondition:
testuser must have READ-only permission on test-node and below
*/
checkReadOnly(path);
Session testSession = getTestSession();
// withdraw the READ privilege
Privilege[] dnPrivs = privilegesFromName(Privilege.JCR_READ);
withdrawPrivileges(path, dnPrivs, getRestrictions(superuser, path));
// testUser registers a event listener for 'path
ObservationManager obsMgr = testSession.getWorkspace().getObservationManager();
EventResult listener = new EventResult(((JUnitTest) this).log);
try {
obsMgr.addEventListener(listener, Event.NODE_REMOVED, path, true, new String[0], new String[0], true);
// superuser removes the node with childNPath in order to provoke
// events being generated
superuser.getItem(childNPath).remove();
superuser.save();
obsMgr.removeEventListener(listener);
// since the testUser does not have read-permission on the removed
// node, 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 observationThroughput.
public void observationThroughput(final Repository repository, @Nullable Whiteboard whiteboard) throws RepositoryException, InterruptedException, ExecutionException {
long t = 0;
final AtomicInteger eventCount = new AtomicInteger();
final AtomicInteger nodeCount = new AtomicInteger();
List<Session> sessions = Lists.newArrayList();
List<EventListener> listeners = Lists.newArrayList();
List<String> testPaths = Lists.newArrayList();
Session s = createSession(repository);
String path = "/path/to/observation/benchmark-" + AbstractTest.TEST_ID;
try {
Node testRoot = JcrUtils.getOrCreateByPath(path, null, s);
for (int i = 0; i < WRITER_COUNT; i++) {
testPaths.add(testRoot.addNode("session-" + i).getPath());
}
s.save();
} finally {
s.logout();
}
String pathFilter = PATH_FILTER == null ? path : PATH_FILTER;
System.out.println("Path filter for event listener: " + pathFilter);
ExecutorService service = Executors.newFixedThreadPool(WRITER_COUNT);
try {
for (int k = 0; k < LISTENER_COUNT; k++) {
sessions.add(createSession(repository));
listeners.add(new Listener(eventCount));
ObservationManager obsMgr = sessions.get(k).getWorkspace().getObservationManager();
obsMgr.addEventListener(listeners.get(k), EVENT_TYPES, pathFilter, true, null, null, false);
}
// also add a listener on the root node
addRootListener(repository, sessions, listeners);
List<Future<Object>> createNodes = Lists.newArrayList();
for (final String p : testPaths) {
createNodes.add(service.submit(new Callable<Object>() {
private final Session session = createSession(repository);
private int numNodes = 0;
@Override
public Object call() throws Exception {
try {
Node testRoot = session.getNode(p);
createChildren(testRoot, 100);
for (Node m : JcrUtils.getChildNodes(testRoot)) {
createChildren(m, 100 / WRITER_COUNT);
for (Node n : JcrUtils.getChildNodes(m)) {
createChildren(n, 5);
}
}
session.save();
} finally {
session.logout();
}
return null;
}
private void createChildren(Node node, int count) throws RepositoryException {
for (int c = 0; c < count; c++) {
node.addNode("n" + c);
nodeCount.incrementAndGet();
if (++numNodes % SAVE_INTERVAL == 0) {
node.getSession().save();
}
}
}
}));
}
System.out.println("ms #node nodes/s #event event/s event-ratio queue external");
while (!isDone(createNodes) || (eventCount.get() / LISTENER_COUNT < nodeCount.get() * EVENTS_PER_NODE)) {
long t0 = System.currentTimeMillis();
Thread.sleep(OUTPUT_RESOLUTION);
t += System.currentTimeMillis() - t0;
int nc = nodeCount.get();
int ec = eventCount.get() / LISTENER_COUNT;
int[] ql = getObservationQueueLength(whiteboard);
double nps = (double) nc / t * 1000;
double eps = (double) ec / t * 1000;
double epn = (double) ec / nc / EVENTS_PER_NODE;
System.out.format("%7d %7d %7.1f %7d %7.1f %7.2f %7d %7d%n", t, nc, nps, ec, eps, epn, ql[0], ql[1]);
}
get(createNodes);
} finally {
for (int k = 0; k < sessions.size(); k++) {
sessions.get(k).getWorkspace().getObservationManager().removeEventListener(listeners.get(k));
sessions.get(k).logout();
}
service.shutdown();
service.awaitTermination(1, TimeUnit.MINUTES);
}
}
Aggregations