Search in sources :

Example 6 with JackrabbitEventFilter

use of org.apache.jackrabbit.api.observation.JackrabbitEventFilter in project jackrabbit-oak by apache.

the class ObservationTest method testAggregate2.

@Test
public void testAggregate2() throws Exception {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setAbsPath("/parent");
    filter.setIsDeep(true);
    filter.setEventTypes(ALL_EVENTS);
    filter = FilterFactory.wrap(filter).withNodeTypeAggregate(new String[] { "oak:Unstructured" }, // "file", "file/jcr:content",
    new String[] { "", "**" });
    // "file/jcr:content/**");
    oManager.addEventListener(listener, filter);
    Node parent = getAdminSession().getRootNode().addNode("parent", "nt:unstructured");
    // OAK-5096: in OR mode the following event also gets sent:
    listener.expect(parent.getPath() + "/jcr:primaryType", PROPERTY_ADDED);
    Node child = parent.addNode("child", "oak:Unstructured");
    listener.expectAdd(child);
    Node file = child.addNode("file", "nt:unstructured");
    listener.expectAdd(file);
    Node jcrContent = file.addNode("jcr:content", "nt:unstructured");
    listener.expect(jcrContent.getPath(), "/parent/child", NODE_ADDED);
    listener.expect(jcrContent.getPath() + "/jcr:primaryType", "/parent/child", PROPERTY_ADDED);
    Property jcrDataProperty = jcrContent.setProperty("jcr:data", "foo");
    listener.expect(jcrDataProperty.getPath(), "/parent/child", PROPERTY_ADDED);
    parent.getSession().save();
    List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    List<Event> unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    assertTrue("Missing events: " + missing, missing.isEmpty());
    file = getAdminSession().getRootNode().getNode("parent").getNode("child").getNode("file");
    jcrContent = file.getNode("jcr:content");
    Property newProperty = jcrContent.setProperty("newProperty", "foo");
    listener.expect(newProperty.getPath(), "/parent/child", PROPERTY_ADDED);
    Property lastModifiedBy = jcrContent.setProperty("jcr:lastModifiedBy", "bar");
    listener.expect(lastModifiedBy.getPath(), "/parent/child", PROPERTY_ADDED);
    jcrContent.getSession().save();
    Thread.sleep(2000);
    missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    assertTrue("Missing events: " + missing, missing.isEmpty());
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) Property(javax.jcr.Property) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 7 with JackrabbitEventFilter

use of org.apache.jackrabbit.api.observation.JackrabbitEventFilter in project jackrabbit-oak by apache.

the class ObservationTest method includeAncestorsRemove.

@Test
public void includeAncestorsRemove() throws Exception {
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setEventTypes(ALL_EVENTS);
    filter.setAbsPath(TEST_PATH + "/a/b/c/d");
    filter.setIsDeep(true);
    filter = FilterFactory.wrap(filter).withIncludeAncestorsRemove();
    FilterProvider filterProvider = doIncludeAncestorsRemove(filter);
    // with 'includeAncestorsRemove' flag the listener is registered at '/'
    assertMatches(filterProvider.getSubTrees(), "/");
    filter = new JackrabbitEventFilter();
    filter.setEventTypes(ALL_EVENTS);
    filter.setIsDeep(true);
    filter = FilterFactory.wrap(filter).withIncludeAncestorsRemove().withIncludeGlobPaths(TEST_PATH + "/a/b/c/**");
    filterProvider = doIncludeAncestorsRemove(filter);
    // with 'includeAncestorsRemove' flag the listener is registered at '/'
    assertMatches(filterProvider.getSubTrees(), "/");
}
Also used : JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) FilterProvider(org.apache.jackrabbit.oak.plugins.observation.filter.FilterProvider) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 8 with JackrabbitEventFilter

use of org.apache.jackrabbit.api.observation.JackrabbitEventFilter in project jackrabbit-oak by apache.

the class ObservationTest method testConsecutiveGlobPaths.

@Test
public void testConsecutiveGlobPaths() throws Exception {
    Node testNode = getNode(TEST_PATH);
    Node a1 = testNode.addNode("a1");
    a1.addNode("b1").addNode("c1");
    a1.addNode("b2").addNode("c2");
    testNode.addNode("a2").addNode("b").addNode("c");
    testNode.getSession().save();
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setEventTypes(ALL_EVENTS);
    filter = FilterFactory.wrap(filter).withIncludeGlobPaths(TEST_PATH + "/a2/**").withIncludeGlobPaths(TEST_PATH + "/a1/**");
    oManager.addEventListener(listener, filter);
    ChangeProcessor cp = oManager.getChangeProcessor(listener);
    assertNotNull(cp);
    FilterProvider filterProvider = cp.getFilterProvider();
    assertNotNull(filterProvider);
    assertMatches(filterProvider.getSubTrees(), TEST_PATH + "/a1", TEST_PATH + "/a2");
    listener.expectRemove(testNode.getNode("a1").getNode("b2")).remove();
    listener.expectRemove(testNode.getNode("a2").getNode("b")).remove();
    testNode.getSession().save();
    Thread.sleep(1000);
    List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    assertTrue("Missing events: " + missing, missing.isEmpty());
    List<Event> unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) FilterProvider(org.apache.jackrabbit.oak.plugins.observation.filter.FilterProvider) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 9 with JackrabbitEventFilter

use of org.apache.jackrabbit.api.observation.JackrabbitEventFilter in project jackrabbit-oak by apache.

the class ObservationTest method pathExclude.

@Test
public void pathExclude() throws ExecutionException, InterruptedException, RepositoryException {
    assumeTrue(observationManager instanceof JackrabbitObservationManager);
    JackrabbitObservationManager oManager = (JackrabbitObservationManager) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter().setAbsPath(TEST_PATH).setIsDeep(true).setExcludedPaths(TEST_PATH + "/c", TEST_PATH + "/d", "/x/y").setEventTypes(ALL_EVENTS);
    oManager.addEventListener(listener, filter);
    Node n = getNode(TEST_PATH);
    listener.expectAdd(listener.expectAdd(listener.expectAdd(n.addNode("a")).addNode("a1")).setProperty("p", "q"));
    listener.expectAdd(listener.expectAdd(n.addNode("b")).setProperty("p", "q"));
    n.addNode("c").addNode("c1").setProperty("p", "q");
    n.addNode("d").setProperty("p", "q");
    getAdminSession().save();
    List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    assertTrue("Missing events: " + missing, missing.isEmpty());
    List<Event> unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
}
Also used : JackrabbitObservationManager(org.apache.jackrabbit.api.observation.JackrabbitObservationManager) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 10 with JackrabbitEventFilter

use of org.apache.jackrabbit.api.observation.JackrabbitEventFilter 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)

Aggregations

JackrabbitEventFilter (org.apache.jackrabbit.api.observation.JackrabbitEventFilter)25 Node (javax.jcr.Node)19 Event (javax.jcr.observation.Event)19 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)19 Test (org.junit.Test)19 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)18 JackrabbitObservationManager (org.apache.jackrabbit.api.observation.JackrabbitObservationManager)7 FilterProvider (org.apache.jackrabbit.oak.plugins.observation.filter.FilterProvider)6 OakEventFilter (org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter)5 Property (javax.jcr.Property)3 Session (javax.jcr.Session)2 ArrayList (java.util.ArrayList)1 RepositoryException (javax.jcr.RepositoryException)1 NodeDefinition (javax.jcr.nodetype.NodeDefinition)1 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)1 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)1 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)1 ObservationManager (javax.jcr.observation.ObservationManager)1 ExcludeExternal (org.apache.jackrabbit.oak.plugins.observation.ExcludeExternal)1 EventResult (org.apache.jackrabbit.test.api.observation.EventResult)1