use of javax.jcr.observation.Event in project sling by apache.
the class PersistingJcrEventDistributionTriggerTest method testProcessEventWithPrivilegesAndNoNuggetsNode.
@Test
public void testProcessEventWithPrivilegesAndNoNuggetsNode() throws Exception {
String nuggetsPath = "/var/nuggets";
String serviceName = "serviceId";
Session session = mock(Session.class);
// first time it doesn't exist and should be created
when(session.nodeExists("/var/nuggets")).thenReturn(false);
// second time it should exist
when(session.nodeExists("/var/nuggets")).thenReturn(true);
Node rootNode = mock(Node.class);
Node varNode = mock(Node.class);
Node nuggetsNode = mock(Node.class);
when(varNode.addNode("nuggets", "sling:Folder")).thenReturn(nuggetsNode);
when(rootNode.addNode("var", "sling:Folder")).thenReturn(varNode);
when(session.getRootNode()).thenReturn(rootNode);
Workspace workspace = mock(Workspace.class);
ObservationManager observationManager = mock(ObservationManager.class);
when(workspace.getObservationManager()).thenReturn(observationManager);
when(session.getWorkspace()).thenReturn(workspace);
when(session.hasPermission(any(String.class), eq(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 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);
}
use of javax.jcr.observation.Event in project sling by apache.
the class JcrEventDistributionTriggerTest method testProcessEventWithPathProperty.
@Test
public void testProcessEventWithPathProperty() throws Exception {
SlingRepository repository = mock(SlingRepository.class);
Scheduler scheduler = mock(Scheduler.class);
ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
String path = "/some/path";
String serviceName = "serviceId";
JcrEventDistributionTrigger jcrEventdistributionTrigger = new JcrEventDistributionTrigger(repository, scheduler, resolverFactory, path, false, serviceName, null);
Event event = mock(Event.class);
when(event.getPath()).thenReturn("/some/path/generating/event");
DistributionRequest distributionRequest = jcrEventdistributionTrigger.processEvent(event);
assertNotNull(distributionRequest);
}
use of javax.jcr.observation.Event in project sling by apache.
the class PersistingJcrEventDistributionTriggerTest method testProcessEventWithoutPrivileges.
@Test
public void testProcessEventWithoutPrivileges() throws Exception {
String serviceName = "serviceId";
Session session = mock(Session.class);
when(session.nodeExists("/var/nuggets")).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";
String nuggetsPath = "/var/nuggets";
PersistedJcrEventDistributionTrigger persistingJcrEventdistributionTrigger = new PersistedJcrEventDistributionTrigger(repository, scheduler, resolverFactory, path, serviceName, nuggetsPath);
Event event = mock(Event.class);
DistributionRequest distributionRequest = persistingJcrEventdistributionTrigger.processEvent(event);
assertNull(distributionRequest);
}
use of javax.jcr.observation.Event in project sling by apache.
the class RootFolderListener method onEvent.
/**
* Schedule a scan.
*/
public void onEvent(final EventIterator it) {
// we only do the global scan for node changes
boolean globalScan = false;
// copy watched folders and remove all for other roots
final List<WatchedFolder> checkFolders = cfg.cloneWatchedFolders();
final Iterator<WatchedFolder> i = checkFolders.iterator();
while (i.hasNext()) {
final WatchedFolder wf = i.next();
if (!wf.getPathWithSlash().startsWith(this.pathWithSlash)) {
i.remove();
}
}
while (it.hasNext()) {
final Event e = it.nextEvent();
logger.debug("Got event {}", e);
if (e.getType() == Event.NODE_ADDED || e.getType() == Event.NODE_REMOVED) {
globalScan = true;
}
try {
final String path = e.getPath();
final Iterator<WatchedFolder> ii = checkFolders.iterator();
while (ii.hasNext()) {
final WatchedFolder folder = ii.next();
if (path.startsWith(folder.getPathWithSlash())) {
folder.markForScan();
ii.remove();
break;
}
}
} catch (final RepositoryException re) {
logger.warn("Error while getting path from event", re);
}
}
if (globalScan) {
timer.scheduleScan();
}
}
use of javax.jcr.observation.Event in project sling by apache.
the class SuperimposingManagerImpl method onEvent.
/**
* Handle resource events to add or remove superimposing registrations
*/
public void onEvent(EventIterator events) {
if (!isEnabled()) {
return;
}
try {
// collect all actions to be performed for this event
final Map<String, Boolean> actions = new HashMap<String, Boolean>();
boolean nodeAdded = false;
boolean nodeRemoved = false;
while (events.hasNext()) {
final Event event = events.nextEvent();
final String path = event.getPath();
final String name = ResourceUtil.getName(path);
if (event.getType() == Event.NODE_ADDED) {
nodeAdded = true;
} else if (event.getType() == Event.NODE_REMOVED && superimposingProviders.containsKey(path)) {
nodeRemoved = true;
actions.put(path, false);
} else if (StringUtils.equals(name, PROP_SUPERIMPOSE_SOURCE_PATH) || StringUtils.equals(name, PROP_SUPERIMPOSE_REGISTER_PARENT) || StringUtils.equals(name, PROP_SUPERIMPOSE_OVERLAYABLE)) {
final String nodePath = ResourceUtil.getParent(path);
actions.put(nodePath, true);
}
}
// transaction)
for (Map.Entry<String, Boolean> action : actions.entrySet()) {
if (action.getValue()) {
registerProvider(action.getKey());
} else {
unregisterProvider(action.getKey());
}
}
if (nodeAdded && nodeRemoved) {
// maybe a superimposing was moved, re-register all superimposings
// (existing ones will be skipped)
registerAllSuperimposings();
}
} catch (RepositoryException e) {
log.error("Unexpected repository exception during event processing.");
}
}
Aggregations