use of javax.jcr.observation.EventListener in project jackrabbit-oak by apache.
the class JackrabbitNodeTest method _testRenameEventHandling.
//Ignore("OAK-3658")
public void _testRenameEventHandling() throws RepositoryException, InterruptedException {
Session s = getHelper().getSuperuserSession();
ObservationManager mgr = s.getWorkspace().getObservationManager();
final List<Event> events = newArrayList();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
try {
mgr.addEventListener(new EventListener() {
CountDownLatch latch = latch1;
@Override
public void onEvent(EventIterator eventIterator) {
synchronized (events) {
while (eventIterator.hasNext()) {
events.add(eventIterator.nextEvent());
}
latch.countDown();
latch = latch2;
}
}
}, 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 + 'X');
superuser.save();
StringBuilder diags = new StringBuilder();
if (!latch1.await(60, SECONDS)) {
diags.append("latch1 timed out ");
}
boolean foundMove = false;
synchronized (events) {
for (Event event : events) {
if (diags.length() != 0) {
diags.append(", ");
}
diags.append("type " + event.getType() + " " + event.getDate() + "ms " + event.getPath());
if (Event.NODE_MOVED == event.getType()) {
foundMove = true;
break;
}
}
if (events.isEmpty()) {
diags.append("none");
}
}
if (!foundMove) {
// force another event, wait some more
testRootNode.addNode(name + "XYZ");
superuser.save();
StringBuffer addDiags = new StringBuffer();
if (!latch2.await(60, SECONDS)) {
addDiags.append("latch2 timed out ");
}
synchronized (events) {
for (Event event : events) {
if (addDiags.length() != 0) {
addDiags.append(", ");
}
addDiags.append("type " + event.getType() + " " + event.getDate() + "ms " + event.getPath());
}
}
if (addDiags.length() > 0) {
diags.append("; next event after additional addNode/save operation: " + addDiags);
}
}
if (!foundMove) {
fail("Expected NODE_MOVED event upon renaming a node (received: " + diags + ")");
}
} finally {
s.logout();
}
}
use of javax.jcr.observation.EventListener 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.EventListener 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.EventListener 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();
}
}
use of javax.jcr.observation.EventListener in project sling by apache.
the class SuperimposingManagerImpl method activate.
// ---------- SCR Integration
@Activate
protected synchronized void activate(final ComponentContext ctx) throws LoginException, RepositoryException {
// check enabled state
@SuppressWarnings("unchecked") final Dictionary<String, Object> props = ctx.getProperties();
this.enabled = PropertiesUtil.toBoolean(props.get(ENABLED_PROPERTY), ENABLED_DEFAULT);
log.debug("Config: " + "Enabled={} ", enabled);
if (!isEnabled()) {
return;
}
// get "find all" queries
this.findAllQueries = PropertiesUtil.toStringArray(props.get(FINDALLQUERIES_PROPERTY), new String[] { FINDALLQUERIES_DEFAULT });
this.obervationPaths = PropertiesUtil.toStringArray(props.get(OBSERVATION_PATHS_PROPERTY), new String[] { OBSERVATION_PATHS_DEFAULT });
if (null == resolver) {
bundleContext = ctx.getBundleContext();
resolver = resolverFactory.getAdministrativeResourceResolver(null);
// Watch for events on the root to register/deregister superimposings at runtime
// For each observed path create an event listener object which redirects the event to the main class
final Session session = resolver.adaptTo(Session.class);
if (session != null) {
this.observationEventListeners = new EventListener[this.obervationPaths.length];
for (int i = 0; i < this.obervationPaths.length; i++) {
this.observationEventListeners[i] = new EventListener() {
public void onEvent(EventIterator events) {
SuperimposingManagerImpl.this.onEvent(events);
}
};
session.getWorkspace().getObservationManager().addEventListener(this.observationEventListeners[i], Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, // absolute path
this.obervationPaths[i], // isDeep
true, // uuids
null, // node types
null, // noLocal
true);
}
}
// register all superimposing definitions that already exist
initialization = Executors.newSingleThreadExecutor().submit(new Runnable() {
public void run() {
try {
registerAllSuperimposings();
} catch (Throwable ex) {
log.warn("Error registering existing superimposing resources on service startup.", ex);
}
}
});
}
}
Aggregations