CanvasPageActivationHandler.java 8.57 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.ReplicationActionType
 *  com.day.cq.replication.ReplicationException
 *  com.day.cq.replication.Replicator
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  javax.jcr.observation.Event
 *  javax.jcr.observation.EventIterator
 *  javax.jcr.observation.EventListener
 *  javax.jcr.observation.ObservationManager
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.api.resource.ValueMap
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.designimporter.impl;

import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.designimporter.impl.common.PathSchemeHelper;
import com.day.cq.wcm.designimporter.util.ImporterUtil;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class CanvasPageActivationHandler
implements EventListener {
    @Reference
    ResourceResolverFactory resourceResolverFactory;
    @Reference
    private Replicator replicator;
    @Reference
    private ResourceResolverFactory resolverFactory;
    protected final Logger log;
    public static final String SERVICE_USER = "canvaspage-activate-service";
    private ResourceResolver resolver;
    private static final String LAST_REPLICATION_ACTION = "cq:lastReplicationAction";

    public CanvasPageActivationHandler() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Lifted jumps to return sites
     */
    public void onEvent(EventIterator events) {
lbl1: // 11 sources:
        while (events.hasNext() != false) {
            evt = events.nextEvent();
            if (evt.getType() != 4 && evt.getType() != 16) continue;
            resolver = null;
            try {
                path = evt.getPath();
                if (!path.endsWith("cq:lastReplicationAction")) continue;
                path = path.substring(0, path.lastIndexOf("jcr:content"));
                resolver = this.getServiceResolver();
                pageManager = (PageManager)resolver.adaptTo(PageManager.class);
                page = pageManager.getPage(path);
                if (page == null) continue;
                props = page.getProperties();
                replicationAction = (String)props.get((Object)"cq:lastReplicationAction");
                replicationActionType = ReplicationActionType.fromName((String)replicationAction);
                switch (.$SwitchMap$com$day$cq$replication$ReplicationActionType[replicationActionType.ordinal()]) {
                    case 1: {
                        this.handlePageActivation(page);
                        ** break;
                    }
                    case 2: {
                        this.handlePageDeactivation(page);
                    }
                }
                continue;
            }
            catch (RepositoryException e) {
                this.log.error("Repository exception encountered", (Throwable)e);
                continue;
            }
            catch (ReplicationException e) {
                this.log.error("Replication exception encountered", (Throwable)e);
                continue;
            }
            catch (LoginException e) {
                this.log.error("Unable to get service resource resolver", (Throwable)e);
                continue;
            }
            finally {
                if (resolver == null) continue;
                resolver.close();
                continue;
            }
        }
    }

    private void handlePageActivation(Page page) throws RepositoryException, ReplicationException {
        this.deactivatePendingArtifacts(page);
    }

    private void handlePageDeactivation(Page page) throws RepositoryException, ReplicationException {
        List<Resource> importers = ImporterUtil.findImporters((Resource)page.adaptTo(Resource.class));
        for (Resource importer : importers) {
            Resource canvas = ImporterUtil.getCanvas(importer);
            String canvasCompPath = PathSchemeHelper.getCanvasComponentPath(canvas);
            String canvasDesPAth = PathSchemeHelper.getCanvasDesignPath(canvas);
            Session session = ((Node)page.adaptTo(Node.class)).getSession();
            this.replicator.replicate(session, ReplicationActionType.DEACTIVATE, canvasDesPAth);
            this.replicator.replicate(session, ReplicationActionType.DEACTIVATE, canvasCompPath);
        }
        this.deactivatePendingArtifacts(page);
    }

    private void deactivatePendingArtifacts(Page page) throws RepositoryException, ReplicationException {
        Session session = ((Node)page.adaptTo(Node.class)).getSession();
        String[] pendingPaths = (String[])page.getProperties().get((Object)"cq:canvasArtifactsPendingDeactivate");
        if (pendingPaths != null) {
            for (String path : pendingPaths) {
                this.replicator.replicate(session, ReplicationActionType.DEACTIVATE, path);
            }
        }
    }

    @Activate
    protected void activate(ComponentContext context) throws Exception {
        this.resolver = this.getServiceResolver();
        Session session = (Session)this.resolver.adaptTo(Session.class);
        session.getWorkspace().getObservationManager().addEventListener((EventListener)this, 20, "/content/campaigns", true, null, new String[]{"cq:PageContent"}, true);
    }

    @Deactivate
    protected void deactivate(ComponentContext context) throws Exception {
        Session session = (Session)this.resolver.adaptTo(Session.class);
        session.getWorkspace().getObservationManager().removeEventListener((EventListener)this);
        session.logout();
    }

    private ResourceResolver getServiceResolver() throws LoginException {
        Map<String, String> info = Collections.singletonMap("sling.service.subservice", "canvaspage-activate-service");
        return this.resolverFactory.getServiceResourceResolver(info);
    }

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

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

    protected void bindReplicator(Replicator replicator) {
        this.replicator = replicator;
    }

    protected void unbindReplicator(Replicator replicator) {
        if (this.replicator == replicator) {
            this.replicator = null;
        }
    }

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

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

}