LaunchAutoPromoteJob.java 8.8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.launches.api.Launch
 *  com.adobe.cq.launches.api.LaunchException
 *  com.adobe.cq.launches.api.LaunchManager
 *  com.adobe.cq.launches.api.LaunchPromotionParameters
 *  com.adobe.cq.launches.api.LaunchPromotionScope
 *  com.day.cq.replication.ReplicationActionType
 *  com.day.cq.replication.ReplicationException
 *  com.day.cq.replication.ReplicationStatus
 *  com.day.cq.replication.Replicator
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.reference.Reference
 *  com.day.cq.wcm.api.reference.ReferenceProvider
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  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.event.jobs.Job
 *  org.apache.sling.event.jobs.consumer.JobConsumer
 *  org.apache.sling.event.jobs.consumer.JobConsumer$JobResult
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.launches.impl;

import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchException;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchPromotionParameters;
import com.adobe.cq.launches.api.LaunchPromotionScope;
import com.adobe.cq.wcm.launches.impl.AutomaticLaunchPromotionParameters;
import com.adobe.cq.wcm.launches.utils.LaunchUtils;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationStatus;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.reference.Reference;
import com.day.cq.wcm.api.reference.ReferenceProvider;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.jcr.Node;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
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.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={JobConsumer.class})
@Property(name="job.topics", value={"com/adobe/cq/wcm/launches/autopromote"})
public class LaunchAutoPromoteJob
implements JobConsumer {
    private static final Logger log = LoggerFactory.getLogger(LaunchAutoPromoteJob.class);
    static final String JOB_TOPIC = "com/adobe/cq/wcm/launches/autopromote";
    static final String PN_PATH = "launchPath";
    static final String PN_LIVE_DATE = "liveDate";
    static final String PN_USER_ID = "userId";
    private static final String LAUNCH_PROMOTE_SERVICE_USER = "launch-promote-service";
    @org.apache.felix.scr.annotations.Reference
    private ResourceResolverFactory resolverFactory;
    @org.apache.felix.scr.annotations.Reference
    private Replicator replicator;
    @org.apache.felix.scr.annotations.Reference(referenceInterface=ReferenceProvider.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
    private final List<ReferenceProvider> referenceProviders = new CopyOnWriteArrayList<ReferenceProvider>();

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public JobConsumer.JobResult process(Job job) {
        String launchPath = (String)job.getProperty("launchPath", String.class);
        Calendar liveDate = (Calendar)job.getProperty("liveDate", Calendar.class);
        String userId = (String)job.getProperty("userId", String.class);
        log.info("Executing auto-promotion of launch [{}] at date: {}, triggered by {}", new Object[]{launchPath, liveDate.getTime(), userId});
        ResourceResolver resolver = null;
        try {
            Map<String, String> info = Collections.singletonMap("sling.service.subservice", "launch-promote-service");
            resolver = this.resolverFactory.getServiceResourceResolver(info);
            LaunchManager launchManager = (LaunchManager)resolver.adaptTo(LaunchManager.class);
            Launch launch = launchManager.getLaunch(launchPath);
            AutomaticLaunchPromotionParameters launchPromotionParameters = new AutomaticLaunchPromotionParameters(launch, LaunchPromotionScope.FULL);
            launchManager.promoteLaunch(launch, (LaunchPromotionParameters)launchPromotionParameters);
            try {
                Node contentNode = ((Node)launch.getResource().adaptTo(Node.class)).getNode("jcr:content");
                contentNode.setProperty("lastPromotedBy", userId);
            }
            catch (Exception e) {
                log.warn("Unable to set lastPromotedBy", (Throwable)e);
            }
            Resource productionRes = LaunchUtils.getTargetResource(launch.getRootResource(), null);
            this.publish((Session)resolver.adaptTo(Session.class), (Page)productionRes.adaptTo(Page.class), launch.isDeep());
            JobConsumer.JobResult jobResult = JobConsumer.JobResult.OK;
            return jobResult;
        }
        catch (LoginException e) {
            log.error("Unable to get a resource resolver while auto-promoting launch [{}]", (Object)launchPath, (Object)e);
        }
        catch (LaunchException e) {
            log.error("Unable to auto-promote launch [{}]", (Object)launchPath, (Object)e);
        }
        catch (ReplicationException e) {
            log.error("Unable to activate auto-promote launch [{}]", (Object)launchPath, (Object)e);
        }
        finally {
            if (resolver != null && resolver.isLive()) {
                resolver.close();
            }
        }
        return JobConsumer.JobResult.FAILED;
    }

    private void publish(Session session, Page page, boolean isDeep) throws ReplicationException {
        this.replicator.replicate(session, ReplicationActionType.ACTIVATE, page.getPath());
        for (ReferenceProvider referenceProvider : this.referenceProviders) {
            for (Reference reference : referenceProvider.findReferences((Resource)page.adaptTo(Resource.class))) {
                Resource resource = reference.getResource();
                boolean published = false;
                boolean outdated = false;
                ReplicationStatus replicationStatus = (ReplicationStatus)resource.adaptTo(ReplicationStatus.class);
                if (replicationStatus != null) {
                    boolean bl = published = replicationStatus.isDelivered() || replicationStatus.isActivated();
                    if (published) {
                        long lastPublished = replicationStatus.getLastPublished().getTimeInMillis();
                        boolean bl2 = outdated = lastPublished < reference.getLastModified();
                    }
                }
                if (published && !outdated) continue;
                this.replicator.replicate(session, ReplicationActionType.ACTIVATE, reference.getResource().getPath());
            }
        }
        if (isDeep) {
            Iterator children = page.listChildren();
            while (children.hasNext()) {
                this.publish(session, (Page)children.next(), true);
            }
        }
    }

    protected void bindReferenceProviders(ReferenceProvider referenceProvider) {
        this.referenceProviders.add(referenceProvider);
    }

    protected void unbindReferenceProviders(ReferenceProvider referenceProvider) {
        this.referenceProviders.remove((Object)referenceProvider);
    }

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

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

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

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