use of javax.jcr.observation.EventIterator in project sling by apache.
the class SuperimposingManagerImplTest method prepareNodeChangeEvent.
private EventIterator prepareNodeChangeEvent(Resource pResource) throws RepositoryException {
String resourcePath = pResource.getPath();
Event propertyEvent = mock(Event.class);
when(propertyEvent.getType()).thenReturn(Event.PROPERTY_CHANGED);
when(propertyEvent.getPath()).thenReturn(resourcePath + "/" + SuperimposingResourceProvider.PROP_SUPERIMPOSE_SOURCE_PATH);
EventIterator eventIterator = mock(EventIterator.class);
when(eventIterator.hasNext()).thenReturn(true, false);
when(eventIterator.nextEvent()).thenReturn(propertyEvent);
return eventIterator;
}
use of javax.jcr.observation.EventIterator in project jackrabbit by apache.
the class SearchManager method onEvent.
// ---------------< EventListener interface >--------------------------------
public void onEvent(EventIterator events) {
log.debug("onEvent: indexing started");
long time = System.currentTimeMillis();
// nodes that need to be removed from the index.
final Set<NodeId> removedNodes = new HashSet<NodeId>();
// nodes that need to be added to the index.
final Map<NodeId, EventImpl> addedNodes = new HashMap<NodeId, EventImpl>();
// property events
List<EventImpl> propEvents = new ArrayList<EventImpl>();
while (events.hasNext()) {
EventImpl e = (EventImpl) events.nextEvent();
if (!isExcluded(e)) {
long type = e.getType();
if (type == Event.NODE_ADDED) {
addedNodes.put(e.getChildId(), e);
if (e.isShareableChildNode()) {
// simply re-index shareable nodes
removedNodes.add(e.getChildId());
}
} else if (type == Event.NODE_REMOVED) {
removedNodes.add(e.getChildId());
if (e.isShareableChildNode()) {
// check if there is a node remaining in the shared set
if (itemMgr.hasItemState(e.getChildId())) {
addedNodes.put(e.getChildId(), e);
}
}
} else {
propEvents.add(e);
}
}
}
// sort out property events
for (EventImpl e : propEvents) {
NodeId nodeId = e.getParentId();
if (e.getType() == Event.PROPERTY_ADDED) {
if (addedNodes.put(nodeId, e) == null) {
// only property added
// need to re-index
removedNodes.add(nodeId);
} else {
// the node where this prop belongs to is also new
}
} else if (e.getType() == Event.PROPERTY_CHANGED) {
// need to re-index
addedNodes.put(nodeId, e);
removedNodes.add(nodeId);
} else {
// property removed event is only generated when node still exists
addedNodes.put(nodeId, e);
removedNodes.add(nodeId);
}
}
Iterator<NodeState> addedStates = new Iterator<NodeState>() {
private final Iterator<NodeId> iter = addedNodes.keySet().iterator();
public void remove() {
throw new UnsupportedOperationException();
}
public boolean hasNext() {
return iter.hasNext();
}
public NodeState next() {
NodeState item = null;
NodeId id = (NodeId) iter.next();
try {
item = (NodeState) itemMgr.getItemState(id);
} catch (ItemStateException ise) {
// check whether this item state change originated from
// an external event
EventImpl e = addedNodes.get(id);
if (e == null || !e.isExternal()) {
log.error("Unable to index node " + id + ": does not exist");
} else {
log.info("Node no longer available " + id + ", skipped.");
}
}
return item;
}
};
Iterator<NodeId> removedIds = removedNodes.iterator();
if (removedNodes.size() > 0 || addedNodes.size() > 0) {
try {
handler.updateNodes(removedIds, addedStates);
} catch (RepositoryException e) {
log.error("Error indexing node.", e);
} catch (IOException e) {
log.error("Error indexing node.", e);
}
}
if (log.isDebugEnabled()) {
log.debug("onEvent: indexing finished in " + String.valueOf(System.currentTimeMillis() - time) + " ms.");
}
}
use of javax.jcr.observation.EventIterator 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.EventIterator in project jackrabbit by apache.
the class ClientEventPoll method run.
// ---------- Thread overwrite ---------------------------------------------
/**
* Checks for remote events and dispatches them to the locally registered
* event listeners. This is how this method works:
* <ol>
* <li>Continue with next step if {@link #terminate()} has not been called
* yet and the session is still alive.
* <li>Call the {@link RemoteObservationManager#getNextEvent(long)} method
* waiting for a specified time (5 seconds).
* <li>If no event was received in the specified time go back to step #1.
* <li>Extract the unique listener identifier from the remote event and
* find it in the list of locally registered event listeners. Go back to
* step #1 if no such listener exists.
* <li>Convert the remote event list to an <code>EventIterator</code> and
* call the <code>EventListener.onEvent()</code> method.
* <li>Go back to step #1.
* </ol>
*/
public void run() {
while (running && session.isLive()) {
try {
// ask for an event waiting at most POLL_TIMEOUT milliseconds
RemoteEventCollection remoteEvent = remote.getNextEvent(POLL_TIMEOUT);
// poll time out, check running and ask again
if (remoteEvent == null) {
continue;
}
// extract the listener id from the remote event and find
// the locally registered event listener
Long id = new Long(remoteEvent.getListenerId());
EventListener listener = (EventListener) listenerMap.get(id);
// silently ignored, running is checked and the server asked again
if (listener == null) {
continue;
}
// otherwise convert the remote events into an EventIterator
// and the listener is called
RemoteEventCollection.RemoteEvent[] remoteEvents = remoteEvent.getEvents();
EventIterator events = toEvents(remoteEvents);
try {
listener.onEvent(events);
} catch (Exception e) {
log.error("Unexpected failure of Listener " + listener, e);
}
} catch (RemoteException re) {
log.error("Problem handling event. Looking for next one.", re);
}
}
}
use of javax.jcr.observation.EventIterator in project jackrabbit by apache.
the class WarningOnSaveWithNotificationThreadTest method testWarning.
public void testWarning() throws Exception {
final List<Exception> exceptions = new ArrayList<Exception>();
EventResult result = new EventResult(log) {
@Override
public void onEvent(EventIterator events) {
try {
Session s = getHelper().getSuperuserSession();
try {
s.getNode(testRoot).addNode("bar");
s.save();
} finally {
s.logout();
}
} catch (RepositoryException e) {
exceptions.add(e);
}
super.onEvent(events);
}
};
addEventListener(result);
Tail tail = Tail.start(new File("target", "jcr.log"), MESSAGE);
try {
testRootNode.addNode("foo");
superuser.save();
removeEventListener(result);
result.getEvents(5000);
assertTrue("Warn message expected in log file.", tail.getLines().iterator().hasNext());
} finally {
tail.close();
}
if (!exceptions.isEmpty()) {
fail(exceptions.get(0).toString());
}
}
Aggregations