Package org.guvnor.asset.management.backend.command

Source Code of org.guvnor.asset.management.backend.command.CompositeCommand

package org.guvnor.asset.management.backend.command;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.kie.internal.executor.api.Command;
import org.kie.internal.executor.api.CommandContext;
import org.kie.internal.executor.api.ExecutionResults;


public class CompositeCommand extends AbstractCommand {

    @Override
    public ExecutionResults execute(CommandContext commandContext) throws Exception {
        String commands = (String) getParameter(commandContext, "Commands");
        ClassLoader cl = (ClassLoader) getParameter(commandContext, "ClassLoader");
        if (cl == null) {
          cl = this.getClass().getClassLoader();
        }
        String[] commandsList = commands.split(",");
        ExecutionResults results = new ExecutionResults();
        Map<String, Object> data = new HashMap<String, Object>();
        for (String cmd : commandsList) {
            Class<?> forName = Class.forName(cmd.trim(), true, cl);
            Command newInstance = (Command) forName.newInstance();
            ExecutionResults execute = newInstance.execute(commandContext);
            Set<String> keySet = execute.keySet();
            for (String key : keySet) {
                data.put(key, execute.getData(key));
                //I'm adding the results as part of the context for the next commands execution
                commandContext.getData().put(key, execute.getData(key));
            }
        }
        results.setData(data);
        return results;
    }

}
TOP

Related Classes of org.guvnor.asset.management.backend.command.CompositeCommand

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.