Search in sources :

Example 11 with ObservationManager

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");
    }
}
Also used : JackrabbitObservationManager(org.apache.jackrabbit.api.observation.JackrabbitObservationManager) ObservationManager(javax.jcr.observation.ObservationManager) JackrabbitObservationManager(org.apache.jackrabbit.api.observation.JackrabbitObservationManager) RepositoryException(javax.jcr.RepositoryException) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) OakEventFilter(org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter)

Example 12 with ObservationManager

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));
}
Also used : ObservationManager(javax.jcr.observation.ObservationManager) Test(org.junit.Test)

Example 13 with ObservationManager

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));
}
Also used : AccessControlHandling(org.apache.jackrabbit.vault.fs.io.AccessControlHandling) ObservationManager(javax.jcr.observation.ObservationManager) FileInputStream(java.io.FileInputStream) ImportMode(org.apache.jackrabbit.vault.fs.api.ImportMode) PackageManager(org.apache.jackrabbit.vault.packaging.PackageManager) Packaging(org.apache.jackrabbit.vault.packaging.Packaging) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) File(java.io.File) VaultPackage(org.apache.jackrabbit.vault.packaging.VaultPackage) Session(javax.jcr.Session) Workspace(javax.jcr.Workspace) Test(org.junit.Test)

Example 14 with ObservationManager

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);
}
Also used : AccessControlHandling(org.apache.jackrabbit.vault.fs.io.AccessControlHandling) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObservationManager(javax.jcr.observation.ObservationManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DistributionExportOptions(org.apache.sling.distribution.serialization.DistributionExportOptions) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ImportMode(org.apache.jackrabbit.vault.fs.api.ImportMode) PackageManager(org.apache.jackrabbit.vault.packaging.PackageManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DistributionExportOptions(org.apache.sling.distribution.serialization.DistributionExportOptions) ExportOptions(org.apache.jackrabbit.vault.packaging.ExportOptions) Packaging(org.apache.jackrabbit.vault.packaging.Packaging) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionExportFilter(org.apache.sling.distribution.serialization.DistributionExportFilter) Session(javax.jcr.Session) Workspace(javax.jcr.Workspace) Test(org.junit.Test)

Example 15 with ObservationManager

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);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionRequestHandler(org.apache.sling.distribution.trigger.DistributionRequestHandler) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) Session(javax.jcr.Session) Workspace(javax.jcr.Workspace) Test(org.junit.Test)

Aggregations

ObservationManager (javax.jcr.observation.ObservationManager)29 Session (javax.jcr.Session)16 Event (javax.jcr.observation.Event)13 Test (org.junit.Test)11 EventListener (javax.jcr.observation.EventListener)10 Node (javax.jcr.Node)8 RepositoryException (javax.jcr.RepositoryException)8 EventIterator (javax.jcr.observation.EventIterator)8 Workspace (javax.jcr.Workspace)6 EventResult (org.apache.jackrabbit.test.api.observation.EventResult)6 ArrayList (java.util.ArrayList)4 DistributionRequest (org.apache.sling.distribution.DistributionRequest)3 HashSet (java.util.HashSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Future (java.util.concurrent.Future)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NodeIterator (javax.jcr.NodeIterator)2 Lock (javax.jcr.lock.Lock)2 JackrabbitEvent (org.apache.jackrabbit.api.observation.JackrabbitEvent)2 ImportMode (org.apache.jackrabbit.vault.fs.api.ImportMode)2