ProcessAssembler.java 7.54 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.workflow.WorkflowException
 *  com.day.cq.workflow.WorkflowSession
 *  com.day.cq.workflow.exec.JavaProcess
 *  com.day.cq.workflow.exec.JavaProcessExt
 *  com.day.cq.workflow.exec.WorkItem
 *  com.day.cq.workflow.exec.WorkflowProcess
 *  com.day.cq.workflow.metadata.MetaDataMap
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.InvalidSyntaxException
 *  org.osgi.framework.ServiceReference
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.workflow.impl.process;

import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.JavaProcess;
import com.day.cq.workflow.exec.JavaProcessExt;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.impl.metadata.CQMetaDataMap;
import com.day.cq.workflow.metadata.MetaDataMap;
import java.util.ArrayList;
import java.util.List;
import org.apache.felix.scr.annotations.Activate;
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.Service;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={WorkflowProcess.class})
@Properties(value={@Property(name="service.description", value={"Workflow Process Assembler"}), @Property(name="process.label", value={"Process Assembler"})})
public class ProcessAssembler
implements WorkflowProcess {
    protected final Logger log = LoggerFactory.getLogger(ProcessAssembler.class);
    private ComponentContext context;

    public void execute(WorkItem item, WorkflowSession session, MetaDataMap metaData) throws WorkflowException {
        try {
            String[] args = this.buildArguments(metaData);
            List<ProcessConfiguration> processes = this.getProcesses(args);
            long millies = System.currentTimeMillis();
            this.log.info(item.getId() + ":Starting Process...");
            for (ProcessConfiguration process : processes) {
                Object proc = this.getProcess(process.getName());
                if (proc != null) {
                    this.log.info(item.getId() + ": Executing " + proc.toString());
                    if (proc instanceof WorkflowProcess) {
                        CQMetaDataMap metaDataMap = new CQMetaDataMap();
                        metaDataMap.put((Object)Arguments.PROCESS_ARGS.name(), (Object)process.getProcessArgs());
                        ((WorkflowProcess)proc).execute(item, session, (MetaDataMap)metaDataMap);
                        continue;
                    }
                    if (proc instanceof JavaProcessExt) {
                        ((JavaProcessExt)proc).execute(item, session, process.getArgs());
                        continue;
                    }
                    ((JavaProcess)proc).execute(item, session);
                    continue;
                }
                this.log.warn("No process found: " + process.toString());
            }
            this.log.info(item.getId() + ":Total Processing time is:" + (System.currentTimeMillis() - millies) + "ms");
        }
        catch (Exception e) {
            throw new WorkflowException((Throwable)e);
        }
    }

    public void execute(WorkItem item, WorkflowSession session, String[] args) throws Exception {
    }

    protected Object getProcess(String name) {
        try {
            WorkflowProcess process;
            int i;
            ServiceReference[] refs = this.context.getBundleContext().getAllServiceReferences(WorkflowProcess.class.getName(), null);
            if (refs != null) {
                for (i = 0; i < refs.length; ++i) {
                    process = (WorkflowProcess)this.context.getBundleContext().getService(refs[i]);
                    if (process == null || !process.getClass().getName().equals(name)) continue;
                    return process;
                }
            }
            if ((refs = this.context.getBundleContext().getAllServiceReferences(JavaProcess.class.getName(), null)) != null) {
                for (i = 0; i < refs.length; ++i) {
                    process = (JavaProcess)this.context.getBundleContext().getService(refs[i]);
                    if (process == null || !process.getClass().getName().equals(name)) continue;
                    return process;
                }
            }
            if ((refs = this.context.getBundleContext().getAllServiceReferences(JavaProcessExt.class.getName(), null)) != null) {
                for (i = 0; i < refs.length; ++i) {
                    process = (JavaProcessExt)this.context.getBundleContext().getService(refs[i]);
                    if (process == null || !process.getClass().getName().equals(name)) continue;
                    return process;
                }
            }
        }
        catch (InvalidSyntaxException e) {
            // empty catch block
        }
        return null;
    }

    protected List<ProcessConfiguration> getProcesses(String[] args) {
        ArrayList<ProcessConfiguration> procs = new ArrayList<ProcessConfiguration>();
        if (args != null && args.length > 0) {
            for (String arg : args) {
                String[] splits = arg.split("::");
                String name = splits[0];
                String[] procArgs = splits.length > 1 ? splits[1].split(";") : new String[]{};
                procs.add(new ProcessConfiguration(name, procArgs));
            }
        }
        return procs;
    }

    @Activate
    protected void activate(ComponentContext context) {
        this.context = context;
    }

    public String[] buildArguments(MetaDataMap metaData) {
        String processArgs = (String)metaData.get(Arguments.PROCESS_ARGS.name(), String.class);
        if (processArgs != null && !processArgs.equals("")) {
            return processArgs.split(",");
        }
        String[] processes = (String[])metaData.get(Arguments.PROCESSES.name(), String[].class);
        return processes;
    }

    public class ProcessConfiguration {
        String name;
        String[] args;

        public ProcessConfiguration(String name, String[] args) {
            this.name = name;
            this.args = args;
        }

        public String getName() {
            return this.name;
        }

        public String[] getArgs() {
            return this.args;
        }

        public String getProcessArgs() {
            StringBuilder builder = new StringBuilder();
            if (this.args != null && this.args.length > 0) {
                for (int i = 0; i < this.args.length; ++i) {
                    builder.append(this.args[i]);
                    if (i >= this.args.length - 1) continue;
                    builder.append(",");
                }
            }
            return builder.toString();
        }

        public String toString() {
            return "name: " + this.name + " args " + this.args;
        }
    }

    public static enum Arguments {
        PROCESS_ARGS,
        PROCESSES;
        

        private Arguments() {
        }
    }

}