Search in sources :

Example 16 with JackrabbitEventFilter

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

the class ObservationManagerImpl method addEventListener.

@Override
public void addEventListener(EventListener listener, int eventTypes, String absPath, boolean isDeep, String[] uuids, String[] nodeTypeName, boolean noLocal) throws RepositoryException {
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setEventTypes(eventTypes);
    if (absPath != null) {
        filter.setAbsPath(absPath);
    }
    filter.setIsDeep(isDeep);
    if (uuids != null) {
        filter.setIdentifiers(uuids);
    }
    if (nodeTypeName != null) {
        filter.setNodeTypes(nodeTypeName);
    }
    filter.setNoLocal(noLocal);
    filter.setNoExternal(listener instanceof ExcludeExternal);
    addEventListener(listener, filter);
}
Also used : ExcludeExternal(org.apache.jackrabbit.oak.plugins.observation.ExcludeExternal) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter)

Example 17 with JackrabbitEventFilter

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

the class ObservationTest method testAggregate1.

@Test
public void testAggregate1() 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" }, new String[] { "", "jcr:content", "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", "nt:unstructured");
    // OAK-5096: in OR mode the following event also gets sent:
    listener.expectAdd(child);
    Node file = child.addNode("file", "oak:Unstructured");
    listener.expectAdd(file);
    Node jcrContent = file.addNode("jcr:content", "nt:unstructured");
    listener.expect(jcrContent.getPath(), "/parent/child/file", NODE_ADDED);
    listener.expect(jcrContent.getPath() + "/jcr:primaryType", "/parent/child/file", PROPERTY_ADDED);
    Property jcrDataProperty = jcrContent.setProperty("jcr:data", "foo");
    listener.expect(jcrDataProperty.getPath(), "/parent/child/file", 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/file", PROPERTY_ADDED);
    Property lastModifiedBy = jcrContent.setProperty("jcr:lastModifiedBy", "bar");
    listener.expect(lastModifiedBy.getPath(), "/parent/child/file", 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 18 with JackrabbitEventFilter

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

the class ObservationTest method doTestFileWithGlobs.

private void doTestFileWithGlobs(String globPath, String... expectedSubTrees) throws RepositoryException, ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, AccessDeniedException, ReferentialIntegrityException, InvalidItemStateException, InterruptedException, ExecutionException {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setEventTypes(ALL_EVENTS);
    filter = FilterFactory.wrap(filter).withIncludeGlobPaths(globPath);
    oManager.addEventListener(listener, filter);
    ChangeProcessor cp = oManager.getChangeProcessor(listener);
    assertNotNull(cp);
    FilterProvider filterProvider = cp.getFilterProvider();
    assertNotNull(filterProvider);
    assertArrayEquals(expectedSubTrees, Iterables.toArray(filterProvider.getSubTrees(), String.class));
    Node parent = getAdminSession().getRootNode().addNode("parent", "nt:unstructured");
    Node bar = parent.addNode("bar", "nt:unstructured");
    Node zetDotJsp = bar.addNode("zet.jsp", "nt:unstructured");
    listener.expect(zetDotJsp.getPath() + "/jcr:primaryType", PROPERTY_ADDED);
    parent.getSession().save();
    Thread.sleep(1000);
    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());
    Session session = getAdminSession();
    session.getRootNode().getNode("parent").remove();
    session.save();
    oManager.removeEventListener(listener);
}
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) Session(javax.jcr.Session)

Example 19 with JackrabbitEventFilter

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

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

the class ObservationTest method includeRemovedSubtree_BeforeValue.

@Test
public void includeRemovedSubtree_BeforeValue() throws RepositoryException, ExecutionException, InterruptedException {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    Node testNode = getNode(TEST_PATH);
    Node a = testNode.addNode("a");
    a.setProperty("propA", "24");
    a.addNode("c").setProperty("propB", "42");
    testNode.getSession().save();
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setEventTypes(ALL_EVENTS);
    filter.setAbsPath("/");
    filter.setIsDeep(true);
    filter = FilterFactory.wrap(filter).withIncludeSubtreeOnRemove();
    oManager.addEventListener(listener, filter);
    Node c = testNode.getNode("a").getNode("c");
    listener.expectRemove(c);
    listener.expectBeforeValue(c.getProperty("propB").getPath(), PROPERTY_REMOVED, "42");
    a = testNode.getNode("a");
    listener.expectBeforeValue(a.getProperty("propA").getPath(), PROPERTY_REMOVED, "24");
    listener.expectRemove(a).remove();
    testNode.getSession().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 : 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)

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