Search in sources :

Example 16 with ResourceChange

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

the class ProcessorManagerImpl method onChange.

@Override
public void onChange(final List<ResourceChange> changes) {
    for (final ResourceChange change : changes) {
        // check if the event handles something in the search paths
        String path = change.getPath();
        int foundPos = -1;
        for (final String sPath : this.searchPath) {
            if (path.startsWith(sPath)) {
                foundPos = sPath.length();
                break;
            }
        }
        boolean handled = false;
        if (foundPos != -1) {
            // now check if this is a rewriter config
            // relative path after the search path
            final int firstSlash = path.indexOf('/', foundPos);
            final int pattern = path.indexOf(CONFIG_PATH, foundPos);
            // only if firstSlash and pattern are at the same position, this might be a rewriter config
            if (firstSlash == pattern && firstSlash != -1) {
                // the node should be a child of CONFIG_PATH
                if (path.length() > pattern + CONFIG_PATH.length() && path.charAt(pattern + CONFIG_PATH.length()) == '/') {
                    // if a child resource is changed, make sure we have the correct path
                    final int slashPos = path.indexOf('/', pattern + CONFIG_PATH.length() + 1);
                    if (slashPos != -1) {
                        path = path.substring(0, slashPos);
                    }
                    // we should do the update async as we don't want to block the event delivery
                    final String configPath = path;
                    final Thread t = new Thread() {

                        @Override
                        public void run() {
                            if (change.getType() == ChangeType.REMOVED) {
                                removeProcessor(configPath);
                            } else {
                                updateProcessor(configPath);
                            }
                        }
                    };
                    t.start();
                    handled = true;
                }
            }
        }
        if (!handled && change.getType() == ChangeType.REMOVED) {
            final Thread t = new Thread() {

                @Override
                public void run() {
                    checkRemoval(change.getPath());
                }
            };
            t.start();
        }
    }
}
Also used : ResourceChange(org.apache.sling.api.resource.observation.ResourceChange)

Example 17 with ResourceChange

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

the class BasicObservationReporter method filterChanges.

/**
     * Filter the change list based on the resource change listener, only type and external needs to be checkd.
     * @param changes The list of changes
     * @param config The resource change listener info
     * @return The filtered list.
     */
private List<ResourceChange> filterChanges(final Iterable<ResourceChange> changes, final ResourceChangeListenerInfo config) {
    final ResourceChangeListImpl filtered = new ResourceChangeListImpl(this.searchPath);
    for (final ResourceChange c : changes) {
        if (matches(c, config)) {
            filtered.add(c);
        }
    }
    filtered.lock();
    return filtered;
}
Also used : ResourceChange(org.apache.sling.api.resource.observation.ResourceChange)

Example 18 with ResourceChange

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

the class ResourceProviderTracker method postResourceProviderChange.

/**
     * Post a change event for a resource provider change
     * @param type The change type
     * @param info The resource provider
     */
private void postResourceProviderChange(final ProviderEvent event) {
    final ObservationReporter or = this.providerReporter;
    if (or != null) {
        final ResourceChange change = new ResourceChange(event.isAdd ? ChangeType.PROVIDER_ADDED : ChangeType.PROVIDER_REMOVED, event.path, false);
        or.reportChanges(Collections.singletonList(change), false);
    }
}
Also used : ObservationReporter(org.apache.sling.spi.resource.provider.ObservationReporter) ResourceChange(org.apache.sling.api.resource.observation.ResourceChange)

Example 19 with ResourceChange

use of org.apache.sling.api.resource.observation.ResourceChange 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)

Example 20 with ResourceChange

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

the class SlingServletResolver method onChange.

@Override
public void onChange(final List<ResourceChange> changes) {
    boolean flushCache = false;
    for (final ResourceChange change : changes) {
        // if the path of the event is a sub path of a search path
        // we flush the whole cache
        final String path = change.getPath();
        int index = 0;
        while (!flushCache && index < searchPaths.length) {
            if (path.startsWith(this.searchPaths[index])) {
                flushCache = true;
            }
            index++;
        }
        if (flushCache) {
            flushCache();
            // we can stop looping
            break;
        }
    }
}
Also used : ResourceChange(org.apache.sling.api.resource.observation.ResourceChange)

Aggregations

ResourceChange (org.apache.sling.api.resource.observation.ResourceChange)38 Test (org.junit.Test)20 File (java.io.File)17 HashSet (java.util.HashSet)4 ArrayList (java.util.ArrayList)3 Event (org.osgi.service.event.Event)2 ImmutableList (com.google.common.collect.ImmutableList)1 SoftReference (java.lang.ref.SoftReference)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 Event (javax.jcr.observation.Event)1 JackrabbitEvent (org.apache.jackrabbit.api.observation.JackrabbitEvent)1