DTMJobsInitializer.java 4.72 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.workflow.WorkflowService
 *  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.commons.scheduler.Scheduler
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dtm.impl;

import com.adobe.cq.dtm.impl.constants.DTMServerType;
import com.adobe.cq.dtm.impl.util.DTMConfigurationUtil;
import com.day.cq.workflow.WorkflowService;
import java.util.Map;
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.commons.scheduler.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1)
public class DTMJobsInitializer {
    @Reference
    ResourceResolverFactory resourceResolverFactory = null;
    @Reference
    private Scheduler scheduler = null;
    @Reference
    private WorkflowService workflowService = null;
    private static final Logger LOG = LoggerFactory.getLogger(DTMJobsInitializer.class);

    @Activate
    public void activate(Map<String, Object> properties) {
        LOG.info("Turning ON DTM Workflow triggering jobs.");
        this.turnDTMJobsOnOrOff(true);
    }

    @Deactivate
    public void deactivate(Map<String, Object> properties) {
        LOG.info("Turning OFF DTM Workflow triggering jobs.");
        this.turnDTMJobsOnOrOff(false);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void turnDTMJobsOnOrOff(boolean turnOnDTMJobs) {
        ResourceResolver serviceResourceResolver = null;
        try {
            serviceResourceResolver = this.resourceResolverFactory.getServiceResourceResolver(null);
            Resource dtmConfigsRoot = serviceResourceResolver.getResource("/etc/cloudservices/dynamictagmanagement");
            if (dtmConfigsRoot != null) {
                for (Resource dtmConfig : dtmConfigsRoot.getChildren()) {
                    Resource dtmConfigContent = dtmConfig.getChild("jcr:content");
                    if (dtmConfigContent == null) continue;
                    if (!turnOnDTMJobs) {
                        String dtmConfigPath = dtmConfig.getPath();
                        this.scheduler.unschedule(DTMConfigurationUtil.getDTMJobName(dtmConfigPath, DTMServerType.STAGING));
                        this.scheduler.unschedule(DTMConfigurationUtil.getDTMJobName(dtmConfigPath, DTMServerType.PRODUCTION));
                        continue;
                    }
                    DTMConfigurationUtil.scheduleOrUnscheduleDTMJob(this.resourceResolverFactory, this.workflowService, this.scheduler, dtmConfigContent, DTMServerType.STAGING);
                    DTMConfigurationUtil.scheduleOrUnscheduleDTMJob(this.resourceResolverFactory, this.workflowService, this.scheduler, dtmConfigContent, DTMServerType.PRODUCTION);
                }
            }
        }
        catch (LoginException e) {
            LOG.error("Could not obtain a resource resolver.", (Throwable)e);
        }
        finally {
            if (serviceResourceResolver != null) {
                serviceResourceResolver.close();
            }
        }
    }

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

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

    protected void bindScheduler(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    protected void unbindScheduler(Scheduler scheduler) {
        if (this.scheduler == scheduler) {
            this.scheduler = null;
        }
    }

    protected void bindWorkflowService(WorkflowService workflowService) {
        this.workflowService = workflowService;
    }

    protected void unbindWorkflowService(WorkflowService workflowService) {
        if (this.workflowService == workflowService) {
            this.workflowService = null;
        }
    }
}