WorkflowPageDelete.java 8.8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.workflow.WorkflowException
 *  com.day.cq.workflow.WorkflowService
 *  com.day.cq.workflow.WorkflowSession
 *  com.day.cq.workflow.exec.Workflow
 *  com.day.cq.workflow.exec.WorkflowData
 *  com.day.cq.workflow.model.WorkflowModel
 *  javax.jcr.Credentials
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.SimpleCredentials
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  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.sling.commons.osgi.OsgiUtil
 *  org.apache.sling.commons.threads.ModifiableThreadPoolConfig
 *  org.apache.sling.commons.threads.ThreadPool
 *  org.apache.sling.commons.threads.ThreadPoolConfig
 *  org.apache.sling.commons.threads.ThreadPoolConfig$ThreadPriority
 *  org.apache.sling.commons.threads.ThreadPoolManager
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.osgi.service.component.ComponentContext
 *  org.osgi.service.event.Event
 *  org.osgi.service.event.EventHandler
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.workflow.impl;

import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowService;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.model.WorkflowModel;
import java.util.Map;
import javax.jcr.Credentials;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
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.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.sling.commons.osgi.OsgiUtil;
import org.apache.sling.commons.threads.ModifiableThreadPoolConfig;
import org.apache.sling.commons.threads.ThreadPool;
import org.apache.sling.commons.threads.ThreadPoolConfig;
import org.apache.sling.commons.threads.ThreadPoolManager;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={EventHandler.class})
@Properties(value={@Property(name="event.topics", value={"com/day/cq/wcm/workflow/req/for/delete"}), @Property(name="event.filter", value={"(!(event.application=*))"})})
public class WorkflowPageDelete
implements EventHandler {
    @Property(label="minThreadPoolSize", description="Minimum Thread pool size", intValue={5})
    private static final String MINIMUM_THREAD_POOL_SIZE = "minThreadPoolSize";
    private int minThreadPoolSize;
    @Property(label="maxThreadPoolSize", description="Maximum Thread pool size", intValue={10})
    private static final String MAXUIMUM_THREAD_POOL_SIZE = "maxThreadPoolSize";
    private int maxThreadPoolSize;
    @Reference
    private ThreadPoolManager threadPoolManager;
    private ThreadPool threadPool;
    public static final String REQUEST_FOR_DELETION_MODEL = "/etc/workflow/models/request_for_deletion/jcr:content/model";
    public static final String EVENT_TOPIC = "com/day/cq/wcm/workflow/req/for/delete";
    @Reference
    private SlingRepository repository;
    @Reference
    private WorkflowService workflowService;
    private static final String TYPE_JCR_PATH = "JCR_PATH";
    private static final Logger log = LoggerFactory.getLogger(WorkflowPageDelete.class);

    @Activate
    protected final void activate(Map<Object, Object> config) {
        this.minThreadPoolSize = OsgiUtil.toInteger((Object)config.get("minThreadPoolSize"), (int)5);
        this.maxThreadPoolSize = OsgiUtil.toInteger((Object)config.get("maxThreadPoolSize"), (int)10);
        ModifiableThreadPoolConfig threadPoolConfig = new ModifiableThreadPoolConfig();
        if (threadPoolConfig.getMinPoolSize() < this.minThreadPoolSize) {
            threadPoolConfig.setMinPoolSize(this.minThreadPoolSize);
        }
        if (threadPoolConfig.getMaxPoolSize() < this.maxThreadPoolSize) {
            threadPoolConfig.setMaxPoolSize(this.maxThreadPoolSize);
        }
        threadPoolConfig.setPriority(ThreadPoolConfig.ThreadPriority.NORM);
        this.threadPool = this.threadPoolManager.create((ThreadPoolConfig)threadPoolConfig);
        if (this.threadPool == null) {
            throw new IllegalStateException("Could not get a ThreadPool");
        }
    }

    @Deactivate
    protected void deactivate(ComponentContext context) {
        if (this.threadPool != null) {
            this.threadPoolManager.release(this.threadPool);
            this.threadPoolManager = null;
            this.threadPool = null;
        }
    }

    public void handleEvent(final Event event) {
        this.threadPool.execute(new Runnable(){

            @Override
            public void run() {
                WorkflowPageDelete.this.process(event);
            }
        });
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public boolean process(Event event) {
        block9 : {
            String topic = event.getTopic();
            WorkflowSession adminWfSession = null;
            WorkflowSession userWfSession = null;
            try {
                if (!"com/day/cq/wcm/workflow/req/for/delete".equals(topic)) break block9;
                String userId = (String)event.getProperty("userId");
                adminWfSession = this.getWorkflowSession();
                userWfSession = this.getWorkflowSession(userId, adminWfSession);
                String path = (String)event.getProperty("path");
                WorkflowModel model = adminWfSession.getModel("/etc/workflow/models/request_for_deletion/jcr:content/model");
                WorkflowData wfData = userWfSession.newWorkflowData("JCR_PATH", (Object)path);
                try {
                    userWfSession.startWorkflow(model, wfData);
                }
                catch (WorkflowException e) {
                    log.warn("Unable to start workflow '{}' as user '{}'. Starting as admin. (reason: {})", new Object[]{model.getId(), userId, String.valueOf(e.getCause())});
                    adminWfSession.startWorkflow(model, wfData);
                }
            }
            catch (WorkflowException we) {
                log.error("Cannot start request for deletion workflow /etc/workflow/models/request_for_deletion/jcr:content/model " + we.getMessage(), (Throwable)we);
            }
            finally {
                if (userWfSession != null) {
                    userWfSession.logout();
                }
                if (adminWfSession != null) {
                    adminWfSession.logout();
                }
            }
        }
        return true;
    }

    private WorkflowSession getWorkflowSession() {
        try {
            return this.workflowService.getWorkflowSession(this.repository.loginAdministrative(null));
        }
        catch (RepositoryException re) {
            log.error("Cannot login into repository", (Throwable)re);
            return null;
        }
    }

    private WorkflowSession getWorkflowSession(String userId, WorkflowSession adminSession) {
        try {
            Session userSession = adminSession.getSession().impersonate((Credentials)new SimpleCredentials(userId, new char[0]));
            return this.workflowService.getWorkflowSession(userSession);
        }
        catch (RepositoryException re) {
            log.error("Cannot login into repository", (Throwable)re);
            return null;
        }
    }

    protected void bindThreadPoolManager(ThreadPoolManager threadPoolManager) {
        this.threadPoolManager = threadPoolManager;
    }

    protected void unbindThreadPoolManager(ThreadPoolManager threadPoolManager) {
        if (this.threadPoolManager == threadPoolManager) {
            this.threadPoolManager = null;
        }
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

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

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

}