Search in sources :

Example 1 with ResourceChangeListener

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

the class ResourceChangeListenerWhiteboard method activate.

public void activate(final BundleContext bundleContext, final ResourceProviderTracker resourceProviderTracker, final String[] searchPath) {
    this.searchPath = searchPath;
    this.resourceProviderTracker = resourceProviderTracker;
    this.resourceProviderTracker.setObservationReporterGenerator(this);
    this.tracker = new ServiceTracker<>(bundleContext, ResourceChangeListener.class, new ServiceTrackerCustomizer<ResourceChangeListener, ServiceReference<ResourceChangeListener>>() {

        @Override
        public void removedService(final ServiceReference<ResourceChangeListener> reference, final ServiceReference<ResourceChangeListener> service) {
            final ResourceChangeListenerInfo info = listeners.remove(reference);
            if (info != null) {
                updateProviderTracker();
            }
        }

        @Override
        public void modifiedService(final ServiceReference<ResourceChangeListener> reference, final ServiceReference<ResourceChangeListener> service) {
            removedService(reference, service);
            addingService(reference);
        }

        @Override
        public ServiceReference<ResourceChangeListener> addingService(final ServiceReference<ResourceChangeListener> reference) {
            final ResourceChangeListenerInfo info = new ResourceChangeListenerInfo(reference, searchPath);
            if (info.isValid()) {
                final ResourceChangeListener listener = bundleContext.getService(reference);
                if (listener != null) {
                    info.setListener(listener);
                    listeners.put(reference, info);
                    updateProviderTracker();
                }
            } else {
                logger.warn("Ignoring invalid resource change listenr {}", reference);
            }
            return reference;
        }
    });
    this.tracker.open();
}
Also used : ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) ResourceChangeListener(org.apache.sling.api.resource.observation.ResourceChangeListener) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with ResourceChangeListener

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

the class BasicObservationReporter method reportChanges.

@Override
public void reportChanges(final ObserverConfiguration config, final Iterable<ResourceChange> changes, final boolean distribute) {
    if (config != null && config instanceof BasicObserverConfiguration) {
        final BasicObserverConfiguration observerConfig = (BasicObserverConfiguration) config;
        ResourceChangeListenerInfo previousInfo = null;
        List<ResourceChange> filteredChanges = null;
        for (final ResourceChangeListenerInfo info : observerConfig.getListeners()) {
            if (previousInfo == null || !equals(previousInfo, info)) {
                filteredChanges = filterChanges(changes, info);
                previousInfo = info;
            }
            if (!filteredChanges.isEmpty()) {
                final ResourceChangeListener listener = info.getListener();
                if (listener != null) {
                    listener.onChange(filteredChanges);
                }
            }
        }
        // TODO implement distribute
        if (distribute) {
            logger.error("Distrubte flag is send for observation events, however distribute is currently not implemented!");
        }
    }
}
Also used : ResourceChangeListener(org.apache.sling.api.resource.observation.ResourceChangeListener) ResourceChange(org.apache.sling.api.resource.observation.ResourceChange)

Aggregations

ResourceChangeListener (org.apache.sling.api.resource.observation.ResourceChangeListener)2 ResourceChange (org.apache.sling.api.resource.observation.ResourceChange)1 ServiceReference (org.osgi.framework.ServiceReference)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1