Search in sources :

Example 1 with JcrResourceListener

use of org.apache.sling.jcr.resource.internal.JcrResourceListener in project sling by apache.

the class JcrResourceProvider method unregisterListeners.

/**
     * Unregister all observation listeners.
     */
private void unregisterListeners() {
    logger.debug("Unregistering resource listeners...");
    for (final Closeable c : this.listeners.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
        }
    }
    this.listeners.clear();
    if (this.listenerConfig != null) {
        try {
            this.listenerConfig.close();
        } catch (final IOException e) {
        // ignore this as the method above does not throw it
        }
        this.listenerConfig = null;
    }
    logger.debug("Unregistered resource listeners");
}
Also used : Closeable(java.io.Closeable) JcrResourceListener(org.apache.sling.jcr.resource.internal.JcrResourceListener) IOException(java.io.IOException)

Example 2 with JcrResourceListener

use of org.apache.sling.jcr.resource.internal.JcrResourceListener 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 JcrResourceListener

use of org.apache.sling.jcr.resource.internal.JcrResourceListener 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

Closeable (java.io.Closeable)3 JcrResourceListener (org.apache.sling.jcr.resource.internal.JcrResourceListener)3 IOException (java.io.IOException)2 RepositoryException (javax.jcr.RepositoryException)2 SlingException (org.apache.sling.api.SlingException)2 ObserverConfiguration (org.apache.sling.spi.resource.provider.ObserverConfiguration)2 HashMap (java.util.HashMap)1 JcrListenerBaseConfig (org.apache.sling.jcr.resource.internal.JcrListenerBaseConfig)1