LiveRelationshipEventHandler.java 6.04 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.PageModification
 *  com.day.cq.wcm.api.PageModification$ModificationType
 *  com.day.cq.wcm.api.WCMException
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.osgi.service.event.Event
 *  org.osgi.service.event.EventHandler
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl;

import com.day.cq.wcm.api.PageModification;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.impl.BlueprintEvent;
import com.day.cq.wcm.msm.impl.LiveCopyIndexImpl;
import com.day.cq.wcm.msm.impl.LiveCopyManagerImpl;
import com.day.cq.wcm.msm.impl.LiveCopyServiceImpl;
import com.day.cq.wcm.msm.impl.SourceTargetFilter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
@Properties(value={@Property(name="event.topics", propertyPrivate=1, value={"com/day/cq/wcm/msm/blueprint/pageEvent/synchronous"})})
public class LiveRelationshipEventHandler
implements EventHandler {
    private static final Logger log = LoggerFactory.getLogger(LiveRelationshipEventHandler.class);
    @Reference
    private LiveCopyServiceImpl liveCopyService = null;
    @Reference
    private ResourceResolverFactory resolverFactory = null;

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void handleEvent(Event event) {
        BlueprintEvent bpEvent = BlueprintEvent.fromEvent(event);
        Iterator<PageModification> iterator = bpEvent.getModifications(PageModification.ModificationType.MOVED);
        if (iterator.hasNext()) {
            try {
                ResourceResolver serviceResourceResolver = this.resolverFactory.getServiceResourceResolver(null);
                LiveCopyManagerImpl liveCopyManager = this.liveCopyService.createLiveCopyManager(serviceResourceResolver);
                TreeSet<String> done = new TreeSet<String>();
                try {
                    Set<String> changed = null;
                    while (iterator.hasNext()) {
                        PageModification modification = iterator.next();
                        if (!this.filterMove(modification)) continue;
                        String sourcePath = modification.getPath();
                        boolean parentDone = false;
                        Iterator<String> ancestors = done.headSet(sourcePath).iterator();
                        while (!parentDone && ancestors.hasNext()) {
                            parentDone = Text.isDescendantOrEqual((String)ancestors.next(), (String)sourcePath);
                        }
                        if (parentDone) continue;
                        changed = liveCopyManager.moveDependentLiveCopies(sourcePath, modification.getDestination());
                        done.add(sourcePath);
                    }
                    if (serviceResourceResolver.adaptTo(Session.class) != null) {
                        Session session = (Session)serviceResourceResolver.adaptTo(Session.class);
                        if (changed != null) {
                            session.save();
                            LiveCopyIndexImpl index = this.liveCopyService.getLiveCopyIndex();
                            for (String path : changed) {
                                index.update(path);
                            }
                        }
                    }
                }
                catch (WCMException we) {
                    log.error("Error while processing Event {}", (Throwable)we);
                }
                catch (RepositoryException e) {
                    log.error("Error while processing Event {}", (Throwable)e);
                }
                finally {
                    if (serviceResourceResolver != null) {
                        serviceResourceResolver.close();
                    }
                }
            }
            catch (LoginException e) {
                log.error("Failed to access Administrative Resolver {}", (Throwable)e);
            }
        }
    }

    private boolean filterMove(PageModification modification) {
        return modification.getType().equals((Object)PageModification.ModificationType.MOVED) && !modification.getPath().equals(modification.getDestination());
    }

    protected void bindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
        this.liveCopyService = liveCopyServiceImpl;
    }

    protected void unbindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
        if (this.liveCopyService == liveCopyServiceImpl) {
            this.liveCopyService = null;
        }
    }

    protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resolverFactory = resourceResolverFactory;
    }

    protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resolverFactory == resourceResolverFactory) {
            this.resolverFactory = null;
        }
    }
}