Search in sources :

Example 1 with ObserverConfiguration

use of org.apache.sling.spi.resource.provider.ObserverConfiguration 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)

Example 2 with ObserverConfiguration

use of org.apache.sling.spi.resource.provider.ObserverConfiguration in project sling by apache.

the class JcrResourceProvider method updateListeners.

/**
     * Update observation listeners.
     */
private void updateListeners() {
    if (this.listenerConfig == null) {
        this.unregisterListeners();
        this.registerListeners();
    } else {
        logger.debug("Updating resource listeners...");
        final Map<ObserverConfiguration, Closeable> oldMap = new HashMap<>(this.listeners);
        this.listeners.clear();
        try {
            for (final ObserverConfiguration config : this.getProviderContext().getObservationReporter().getObserverConfigurations()) {
                // check if such a listener already exists
                Closeable listener = oldMap.remove(config);
                if (listener == null) {
                    logger.debug("Registering listener for {}", config.getPaths());
                    listener = new JcrResourceListener(this.listenerConfig, config);
                } else {
                    logger.debug("Updating listener for {}", config.getPaths());
                    ((JcrResourceListener) listener).update(config);
                }
                this.listeners.put(config, listener);
            }
        } catch (final RepositoryException e) {
            throw new SlingException("Can't create the JCR event listener.", e);
        }
        for (final Closeable c : oldMap.values()) {
            try {
                logger.debug("Removing listener for {}", ((JcrResourceListener) c).getConfig().getPaths());
                c.close();
            } catch (final IOException e) {
            // ignore this as the method above does not throw it
            }
        }
        logger.debug("Updated resource listeners");
    }
}
Also used : HashMap(java.util.HashMap) Closeable(java.io.Closeable) ObserverConfiguration(org.apache.sling.spi.resource.provider.ObserverConfiguration) SlingException(org.apache.sling.api.SlingException) RepositoryException(javax.jcr.RepositoryException) JcrResourceListener(org.apache.sling.jcr.resource.internal.JcrResourceListener) IOException(java.io.IOException)

Example 3 with ObserverConfiguration

use of org.apache.sling.spi.resource.provider.ObserverConfiguration in project sling by apache.

the class JcrResourceProvider method registerListeners.

/**
     * Register all observation listeners.
     */
private void registerListeners() {
    if (this.repository != null) {
        logger.debug("Registering resource listeners...");
        try {
            this.listenerConfig = new JcrListenerBaseConfig(this.getProviderContext().getObservationReporter(), this.repository);
            for (final ObserverConfiguration config : this.getProviderContext().getObservationReporter().getObserverConfigurations()) {
                logger.debug("Registering listener for {}", config.getPaths());
                final Closeable listener = new JcrResourceListener(this.listenerConfig, config);
                this.listeners.put(config, listener);
            }
        } catch (final RepositoryException e) {
            throw new SlingException("Can't create the JCR event listener.", e);
        }
        logger.debug("Registered resource listeners");
    }
}
Also used : JcrListenerBaseConfig(org.apache.sling.jcr.resource.internal.JcrListenerBaseConfig) Closeable(java.io.Closeable) ObserverConfiguration(org.apache.sling.spi.resource.provider.ObserverConfiguration) SlingException(org.apache.sling.api.SlingException) RepositoryException(javax.jcr.RepositoryException) JcrResourceListener(org.apache.sling.jcr.resource.internal.JcrResourceListener)

Aggregations

ObserverConfiguration (org.apache.sling.spi.resource.provider.ObserverConfiguration)3 Closeable (java.io.Closeable)2 RepositoryException (javax.jcr.RepositoryException)2 SlingException (org.apache.sling.api.SlingException)2 JcrResourceListener (org.apache.sling.jcr.resource.internal.JcrResourceListener)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Node (javax.jcr.Node)1 Session (javax.jcr.Session)1 ResourceChange (org.apache.sling.api.resource.observation.ResourceChange)1 ChangeType (org.apache.sling.api.resource.observation.ResourceChange.ChangeType)1 JcrListenerBaseConfig (org.apache.sling.jcr.resource.internal.JcrListenerBaseConfig)1 Test (org.junit.Test)1