use of javax.jcr.observation.ObservationManager in project sling by apache.
the class JcrListenerBaseConfig method register.
/**
* Register a JCR event listener
* @param listener The listener
* @param config The configuration
* @throws RepositoryException If registration fails.
*/
public void register(final EventListener listener, final ObserverConfiguration config) throws RepositoryException {
final ObservationManager mgr = this.session.getWorkspace().getObservationManager();
if (mgr instanceof JackrabbitObservationManager) {
final OakEventFilter filter = FilterFactory.wrap(new JackrabbitEventFilter());
// paths
final Set<String> paths = config.getPaths().toStringSet();
int globCount = 0, pathCount = 0;
for (final String p : paths) {
if (p.startsWith(Path.GLOB_PREFIX)) {
globCount++;
} else {
pathCount++;
}
}
final String[] pathArray = pathCount > 0 ? new String[pathCount] : null;
final String[] globArray = globCount > 0 ? new String[globCount] : null;
pathCount = 0;
globCount = 0;
// create arrays and remove global prefix
for (final String p : paths) {
if (p.startsWith(Path.GLOB_PREFIX)) {
globArray[globCount] = p.substring(Path.GLOB_PREFIX.length());
globCount++;
} else {
pathArray[pathCount] = p;
pathCount++;
}
}
if (globArray != null) {
filter.withIncludeGlobPaths(globArray);
}
if (pathArray != null) {
filter.setAdditionalPaths(pathArray);
}
filter.setIsDeep(true);
// exclude paths
final Set<String> excludePaths = config.getExcludedPaths().toStringSet();
if (!excludePaths.isEmpty()) {
filter.setExcludedPaths(excludePaths.toArray(new String[excludePaths.size()]));
}
// external
filter.setNoExternal(!config.includeExternal());
// types
filter.setEventTypes(this.getTypes(config));
// nt:file handling
filter.withNodeTypeAggregate(new String[] { "nt:file" }, new String[] { "", "jcr:content" });
// anchestor removes
filter.withIncludeAncestorsRemove();
((JackrabbitObservationManager) mgr).addEventListener(listener, filter);
} else {
throw new RepositoryException("Observation manager is not a JackrabbitObservationManager");
}
}
use of javax.jcr.observation.ObservationManager in project sling by apache.
the class MockWorkspaceTest method testObservationManager.
@Test
public void testObservationManager() throws RepositoryException {
// just make sure observation manager methods can be called, although they do nothing
ObservationManager mgr = underTest.getObservationManager();
mgr.addEventListener(null, 0, null, false, null, null, false);
mgr.removeEventListener(null);
assertFalse(mgr.getRegisteredEventListeners().hasNext());
mgr.setUserData("abc");
assertNull(mgr.getEventJournal());
assertNull(mgr.getEventJournal(0, "/any", true, null, null));
}
use of javax.jcr.observation.ObservationManager in project sling by apache.
the class FileVaultContentSerializerTest method testImportFromStream.
@Test
public void testImportFromStream() throws Exception {
Packaging packaging = mock(Packaging.class);
ImportMode importMode = ImportMode.REPLACE;
AccessControlHandling aclHandling = AccessControlHandling.IGNORE;
String[] packageRoots = new String[] { "/" };
String[] nodeFilters = new String[0];
String[] propertyFilters = new String[0];
boolean useReferences = false;
int thershold = 1024;
FileVaultContentSerializer fileVaultContentSerializer = new FileVaultContentSerializer("vlt", packaging, importMode, aclHandling, packageRoots, nodeFilters, propertyFilters, useReferences, thershold);
ResourceResolver sessionResolver = mock(ResourceResolver.class);
Session session = mock(Session.class);
File file = new File(getClass().getResource("/vlt/dp.vlt").getFile());
PackageManager pm = mock(PackageManager.class);
VaultPackage vaultPackage = mock(VaultPackage.class);
when(pm.open(any(File.class))).thenReturn(vaultPackage);
when(packaging.getPackageManager()).thenReturn(pm);
Workspace workspace = mock(Workspace.class);
ObservationManager observationManager = mock(ObservationManager.class);
when(workspace.getObservationManager()).thenReturn(observationManager);
when(session.getWorkspace()).thenReturn(workspace);
when(sessionResolver.adaptTo(Session.class)).thenReturn(session);
fileVaultContentSerializer.importFromStream(sessionResolver, new FileInputStream(file));
}
use of javax.jcr.observation.ObservationManager in project sling by apache.
the class FileVaultContentSerializerTest method testExportToStream.
@Test
public void testExportToStream() throws Exception {
Packaging packaging = mock(Packaging.class);
ImportMode importMode = ImportMode.REPLACE;
AccessControlHandling aclHandling = AccessControlHandling.IGNORE;
String[] packageRoots = new String[] { "/etc/packages" };
String[] nodeFilters = new String[0];
String[] propertyFilters = new String[0];
boolean useReferences = false;
int threshold = 1024;
FileVaultContentSerializer fileVaultContentSerializer = new FileVaultContentSerializer("vlt", packaging, importMode, aclHandling, packageRoots, nodeFilters, propertyFilters, useReferences, threshold);
ResourceResolver sessionResolver = mock(ResourceResolver.class);
Session session = mock(Session.class);
PackageManager pm = mock(PackageManager.class);
when(packaging.getPackageManager()).thenReturn(pm);
OutputStream outputStream = new ByteArrayOutputStream();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
return null;
}
}).when(pm).assemble(same(session), any(ExportOptions.class), same(outputStream));
Workspace workspace = mock(Workspace.class);
ObservationManager observationManager = mock(ObservationManager.class);
when(workspace.getObservationManager()).thenReturn(observationManager);
when(session.getWorkspace()).thenReturn(workspace);
when(sessionResolver.adaptTo(Session.class)).thenReturn(session);
DistributionExportFilter filter = mock(DistributionExportFilter.class);
DistributionRequest request = mock(DistributionRequest.class);
when(request.getPaths()).thenReturn(new String[] { "/libs" });
when(request.getFilters("/libs")).thenReturn(new String[0]);
DistributionExportOptions exportOptions = new DistributionExportOptions(request, filter);
fileVaultContentSerializer.exportToStream(sessionResolver, exportOptions, outputStream);
}
use of javax.jcr.observation.ObservationManager 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);
}
Aggregations