Search in sources :

Example 1 with OakEventFilter

use of org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter in project jackrabbit-oak by apache.

the class ObservationTest method includeAncestorsRemove_WithGlobs.

@Test
public void includeAncestorsRemove_WithGlobs() throws Exception {
    OakEventFilter oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/a/b/c/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/a/b/*/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/a/*/*/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/*/b/*/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/*/b/c/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/*/*/c/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/*/*/*/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/a/**/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/**/c/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths("/**/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/**/d.jsp");
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove();
    doIncludeAncestorsRemove_WithGlobs(oef);
}
Also used : JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) OakEventFilter(org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 2 with OakEventFilter

use of org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter 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 3 with OakEventFilter

use of org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter in project jackrabbit-oak by apache.

the class ObservationTest method doIncludeAncestorsRemove_Unrelated.

private void doIncludeAncestorsRemove_Unrelated(String[] absPaths, String[] createAndRemoveNodes, String[] expectedRemoveNodeEvents, String[] expectedRemovePropertyEvents) throws Exception {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    OakEventFilter filterWithAncestorsRemove = FilterFactory.wrap(new JackrabbitEventFilter());
    filterWithAncestorsRemove.setEventTypes(NODE_REMOVED | PROPERTY_REMOVED);
    assertTrue(absPaths.length >= 1);
    String[] additionalPaths = new String[absPaths.length];
    System.arraycopy(absPaths, 0, additionalPaths, 0, absPaths.length);
    if (!absPaths[0].contains("*")) {
        filterWithAncestorsRemove.setAbsPath(absPaths[0]);
        if (absPaths.length > 1) {
            additionalPaths = new String[absPaths.length - 1];
            System.arraycopy(absPaths, 1, additionalPaths, 0, absPaths.length - 1);
        }
    }
    filterWithAncestorsRemove.withIncludeGlobPaths(additionalPaths);
    filterWithAncestorsRemove.setIsDeep(true);
    filterWithAncestorsRemove = filterWithAncestorsRemove.withIncludeAncestorsRemove();
    ExpectationListener listenerWithAncestorsRemove = new ExpectationListener();
    oManager.addEventListener(listenerWithAncestorsRemove, filterWithAncestorsRemove);
    OakEventFilter filterWithoutAncestorsRemove = FilterFactory.wrap(new JackrabbitEventFilter());
    filterWithoutAncestorsRemove.setEventTypes(NODE_REMOVED);
    if (!absPaths[0].contains("*")) {
        filterWithoutAncestorsRemove.setAbsPath(absPaths[0]);
    }
    filterWithoutAncestorsRemove.withIncludeGlobPaths(additionalPaths);
    filterWithoutAncestorsRemove.setIsDeep(true);
    ExpectationListener listenerWithoutAncestorsRemove = new ExpectationListener();
    oManager.addEventListener(listenerWithoutAncestorsRemove, filterWithoutAncestorsRemove);
    Session session = getAdminSession();
    for (String path : createAndRemoveNodes) {
        Iterator<String> it = PathUtils.elements(path).iterator();
        Node node = session.getRootNode();
        while (it.hasNext()) {
            String elem = it.next();
            if (!node.hasNode(elem)) {
                node = node.addNode(elem);
            } else {
                node = node.getNode(elem);
            }
        }
    }
    session.save();
    for (String nodePath : expectedRemoveNodeEvents) {
        listenerWithAncestorsRemove.expect(nodePath, NODE_REMOVED);
    }
    for (String propertyPath : expectedRemovePropertyEvents) {
        listenerWithAncestorsRemove.expect(propertyPath, PROPERTY_REMOVED);
    }
    for (String path : createAndRemoveNodes) {
        Iterator<String> it = PathUtils.elements(path).iterator();
        Node node = session.getRootNode();
        while (it.hasNext()) {
            String elem = it.next();
            node = node.getNode(elem);
        }
        node.remove();
        session.save();
    }
    Thread.sleep(1000);
    List<Expectation> missing = listenerWithoutAncestorsRemove.getMissing(TIME_OUT, TimeUnit.SECONDS);
    List<Event> unexpected = listenerWithoutAncestorsRemove.getUnexpected();
    assertTrue("Unexpected events (listenerWithoutAncestorsRemove): " + unexpected, unexpected.isEmpty());
    assertTrue("Missing events (listenerWithoutAncestorsRemove): " + missing, missing.isEmpty());
    missing = listenerWithAncestorsRemove.getMissing(TIME_OUT, TimeUnit.SECONDS);
    unexpected = listenerWithAncestorsRemove.getUnexpected();
    assertTrue("Unexpected events (listenerWithAncestorsRemove): " + unexpected, unexpected.isEmpty());
    assertTrue("Missing events (listenerWithAncestorsRemove): " + missing, missing.isEmpty());
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) OakEventFilter(org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter) Event(javax.jcr.observation.Event) Session(javax.jcr.Session)

Example 4 with OakEventFilter

use of org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter in project jackrabbit-oak by apache.

the class ObservationTest method testAggregate6.

// OAK-5096 : a specific **/*.jsp test case
@Test
public void testAggregate6() throws Exception {
    OakEventFilter oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.setIsDeep(true);
    oef.withIncludeAncestorsRemove().withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "", "jcr:content" }).withIncludeGlobPaths("/**/*.jsp");
    doTestAggregate6(oef, new String[] { "/" }, new String[] { "/**", "/**/*.jsp", "/**/*.jsp/**" });
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.setIsDeep(false);
    oef.withIncludeAncestorsRemove().withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "", "jcr:content" }).withIncludeGlobPaths("/**/*.jsp");
    doTestAggregate6(oef, new String[] { "/" }, new String[] { "/**", "/**/*.jsp", "/**/*.jsp/**" });
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeAncestorsRemove().withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "", "jcr:content" }).withIncludeGlobPaths("**/*.jsp");
    doTestAggregate6(oef, new String[] { "/" }, new String[] { "/**", "**/*.jsp", "**/*.jsp/**" });
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    // without includeAncestorsRemove this time
    oef.withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "", "jcr:content" }).withIncludeGlobPaths("/**/*.jsp");
    doTestAggregate6(oef, new String[] { "" }, new String[] { "/**/*.jsp", "/**/*.jsp/**" });
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    // without includeAncestorsRemove this time
    oef.withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "", "jcr:content" }).withIncludeGlobPaths("**/*.jsp");
    doTestAggregate6(oef, new String[] { "" }, new String[] { "**/*.jsp", "**/*.jsp/**" });
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeAncestorsRemove().withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "", "jcr:content" }).withIncludeGlobPaths("/parent/**/*.jsp");
    doTestAggregate6(oef, new String[] { "/" }, new String[] { "/parent", "/parent/**", "/parent/**/*.jsp", "/parent/**/*.jsp/**" });
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeAncestorsRemove().withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "", "jcr:content" }).withIncludeGlobPaths("/parent/bar/**/*.jsp");
    doTestAggregate6(oef, new String[] { "/" }, new String[] { "/parent", "/parent/bar", "/parent/bar/**", "/parent/bar/**/*.jsp", "/parent/bar/**/*.jsp/**" });
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    // without includeAncestorsRemove this time
    oef.withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "", "jcr:content" }).withIncludeGlobPaths("/parent/**/*.jsp");
    doTestAggregate6(oef, new String[] { "/parent" }, new String[] { "/parent/**/*.jsp", "/parent/**/*.jsp/**" });
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    // without includeAncestorsRemove this time
    oef.withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "", "jcr:content" }).withIncludeGlobPaths("/parent/**/*.jsp", "/foo/bar/**");
    doTestAggregate6(oef, new String[] { "/parent", "/foo/bar" }, new String[] { "/foo/bar/**", "/parent/**/*.jsp", "/parent/**/*.jsp/**" });
    oef = FilterFactory.wrap(new JackrabbitEventFilter());
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeAncestorsRemove().withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "", "jcr:content" }).withIncludeGlobPaths("/parent/**/*.jsp", "/foo/bar/**");
    doTestAggregate6(oef, new String[] { "/" }, new String[] { "/parent", "/foo", "/foo/bar", "/foo/bar/**", "/parent/**", "/parent/**/*.jsp", "/parent/**/*.jsp/**" });
}
Also used : JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) OakEventFilter(org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 5 with OakEventFilter

use of org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter in project jackrabbit-oak by apache.

the class ObservationTest method includeRemovedSubtree_Globs.

@Test
public void includeRemovedSubtree_Globs() throws Exception {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    Node testNode = getNode(TEST_PATH);
    testNode.addNode("a").addNode("b").addNode("c").addNode("d.jsp");
    testNode.addNode("e").addNode("f").addNode("g.jsp");
    testNode.getSession().save();
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    OakEventFilter oef = FilterFactory.wrap(filter);
    oef.setEventTypes(ALL_EVENTS);
    oef.withIncludeGlobPaths(TEST_PATH + "/a/**/*.jsp");
    oef.withNodeTypeAggregate(new String[] { "nt:unstructured" }, new String[] { "" });
    oef.withIncludeSubtreeOnRemove();
    oManager.addEventListener(listener, oef);
    // the glob is for a jsp - so we should (only) get an event for that
    // but only for the properties of d.jsp that get removed, not of removal of d.jsp itself
    // as that would again be reported towards the parent of d.jsp which is /a/b/c
    Node dDotJsp = testNode.getNode("a").getNode("b").getNode("c").getNode("d.jsp");
    listener.expect(dDotJsp.getPath() + "/jcr:primaryType", dDotJsp.getPath(), PROPERTY_REMOVED);
    // but we're removing /a/b
    testNode.getNode("a").getNode("b").remove();
    // and for removal of /e nothing should be generated
    testNode.getNode("e").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) OakEventFilter(org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Aggregations

JackrabbitEventFilter (org.apache.jackrabbit.api.observation.JackrabbitEventFilter)5 OakEventFilter (org.apache.jackrabbit.oak.jcr.observation.filter.OakEventFilter)5 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)3 Test (org.junit.Test)3 Node (javax.jcr.Node)2 Event (javax.jcr.observation.Event)2 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)2 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 ObservationManager (javax.jcr.observation.ObservationManager)1 JackrabbitObservationManager (org.apache.jackrabbit.api.observation.JackrabbitObservationManager)1