Search in sources :

Example 1 with ChangeType

use of org.apache.sling.api.resource.observation.ResourceChange.ChangeType in project sling by apache.

the class JcrListenerBaseConfig method getTypes.

/**
     * Get the event types based on the configuraiton
     * @param c The configuration
     * @return The event type mask
     */
private int getTypes(final ObserverConfiguration c) {
    int result = 0;
    for (ChangeType t : c.getChangeTypes()) {
        switch(t) {
            case ADDED:
                result = result | Event.NODE_ADDED;
                break;
            case REMOVED:
                result = result | Event.NODE_REMOVED;
                break;
            case CHANGED:
                result = result | Event.PROPERTY_ADDED;
                result = result | Event.PROPERTY_CHANGED;
                result = result | Event.PROPERTY_REMOVED;
                break;
            default:
                break;
        }
    }
    return result;
}
Also used : ChangeType(org.apache.sling.api.resource.observation.ResourceChange.ChangeType)

Example 2 with ChangeType

use of org.apache.sling.api.resource.observation.ResourceChange.ChangeType in project sling by apache.

the class JcrResourceListenerTest method testMultiplePaths.

@Test
public void testMultiplePaths() throws Exception {
    ObserverConfiguration observerConfig = new ObserverConfiguration() {

        @Override
        public boolean includeExternal() {
            return true;
        }

        @Override
        public PathSet getPaths() {
            return PathSet.fromStrings("/libs", "/apps");
        }

        @Override
        public PathSet getExcludedPaths() {
            return PathSet.fromPaths();
        }

        @Override
        public Set<ChangeType> getChangeTypes() {
            return EnumSet.allOf(ChangeType.class);
        }

        @Override
        public boolean matches(String path) {
            return this.getPaths().matches(path) != null;
        }

        @Override
        public Set<String> getPropertyNamesHint() {
            return null;
        }
    };
    this.config.unregister(this.listener);
    this.listener = null;
    final Session session = this.adminSession;
    if (!session.nodeExists("/libs")) {
        createNode(session, "/libs");
    }
    if (!session.nodeExists("/apps")) {
        createNode(session, "/apps");
    }
    session.getNode("/libs").addNode("foo" + System.currentTimeMillis());
    session.getNode("/apps").addNode("foo" + System.currentTimeMillis());
    session.save();
    Thread.sleep(200);
    this.events.clear();
    try (final JcrResourceListener l = new JcrResourceListener(this.config, observerConfig)) {
        final String rootName = "test_" + System.currentTimeMillis();
        for (final String path : new String[] { "/libs", "/", "/apps", "/content" }) {
            final Node parent;
            if (!session.nodeExists(path)) {
                parent = createNode(session, path);
            } else {
                parent = session.getNode(path);
            }
            final Node node = parent.addNode(rootName, "nt:unstructured");
            session.save();
            node.setProperty("foo", "bar");
            session.save();
            node.remove();
            session.save();
        }
        assertEquals("Received: " + events, 6, events.size());
        final Set<String> addPaths = new HashSet<String>();
        final Set<String> modifyPaths = new HashSet<String>();
        final Set<String> removePaths = new HashSet<String>();
        for (final ResourceChange event : events) {
            if (event.getType() == ChangeType.ADDED) {
                addPaths.add(event.getPath());
            } else if (event.getType() == ChangeType.CHANGED) {
                modifyPaths.add(event.getPath());
            } else if (event.getType() == ChangeType.REMOVED) {
                removePaths.add(event.getPath());
            } else {
                fail("Unexpected event: " + event);
            }
            assertNotNull(event.getUserId());
        }
        assertEquals("Received: " + addPaths, 2, addPaths.size());
        assertTrue("Added set should contain /libs/" + rootName, addPaths.contains("/libs/" + rootName));
        assertTrue("Added set should contain /apps/" + rootName, addPaths.contains("/apps/" + rootName));
        assertEquals("Received: " + modifyPaths, 2, modifyPaths.size());
        assertTrue("Modified set should contain /libs/" + rootName, modifyPaths.contains("/libs/" + rootName));
        assertTrue("Modified set should contain /apps/" + rootName, modifyPaths.contains("/apps/" + rootName));
        assertEquals("Received: " + removePaths, 2, removePaths.size());
        assertTrue("Removed set should contain /libs/" + rootName, removePaths.contains("/libs/" + rootName));
        assertTrue("Removed set should contain /apps/" + rootName, removePaths.contains("/apps/" + rootName));
    }
}
Also used : ChangeType(org.apache.sling.api.resource.observation.ResourceChange.ChangeType) Node(javax.jcr.Node) ObserverConfiguration(org.apache.sling.spi.resource.provider.ObserverConfiguration) ResourceChange(org.apache.sling.api.resource.observation.ResourceChange) Session(javax.jcr.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ChangeType (org.apache.sling.api.resource.observation.ResourceChange.ChangeType)2 HashSet (java.util.HashSet)1 Node (javax.jcr.Node)1 Session (javax.jcr.Session)1 ResourceChange (org.apache.sling.api.resource.observation.ResourceChange)1 ObserverConfiguration (org.apache.sling.spi.resource.provider.ObserverConfiguration)1 Test (org.junit.Test)1