]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/DefaultModulesManager.java
Bug corrections in DefaultModulesManager and ExecutionContext
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / DefaultModulesManager.java
index b55a9c377f0b1040eac1a7097bfa57d342af1915..7da391a3796316697be997cedd10c944136e3b51 100644 (file)
 package org.argeo.slc.core.execution;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.execution.ExecutionFlow;
+import org.argeo.slc.execution.ExecutionFlowDescriptor;
 import org.argeo.slc.execution.ExecutionModule;
 import org.argeo.slc.execution.ExecutionModuleDescriptor;
 import org.argeo.slc.execution.ExecutionModulesManager;
+import org.argeo.slc.process.RealizedFlow;
+import org.argeo.slc.process.SlcExecution;
 import org.springframework.util.Assert;
 
 public class DefaultModulesManager implements ExecutionModulesManager {
-       private List<ExecutionModule> executionModules = new ArrayList<ExecutionModule>();
+       private final static Log log = LogFactory
+                       .getLog(DefaultModulesManager.class);
 
-       public ExecutionModuleDescriptor getExecutionModuleDescriptor(
-                       String moduleName, String version) {
-               ExecutionModule module = null;
+       private List<ExecutionModule> executionModules = new ArrayList<ExecutionModule>();
+       
+       protected ExecutionModule getExecutionModule(String moduleName, String version) {
                for (ExecutionModule moduleT : executionModules) {
                        if (moduleT.getName().equals(moduleName)) {
-                               // TODO: check version
-                               module = moduleT;
-                               break;
+                               if(moduleT.getVersion().equals(version)) {
+                                       return moduleT;
+                               }
                        }
                }
-
+               return null;
+       }
+       
+       public ExecutionModuleDescriptor getExecutionModuleDescriptor(
+                       String moduleName, String version) {
+               ExecutionModule module = getExecutionModule(moduleName, version);
+               
                Assert.notNull(module);
 
                return module.getDescriptor();
        }
-       
-       
 
        public List<ExecutionModule> listExecutionModules() {
                return executionModules;
        }
 
-
-
        public void setExecutionModules(List<ExecutionModule> executionModules) {
                this.executionModules = executionModules;
        }
 
+       public void process(SlcExecution slcExecution) {
+               log.info("##\n## Process SLC Execution " + slcExecution+"\n##");
+
+               for(RealizedFlow flow : slcExecution.getRealizedFlows()) {
+                       ExecutionModule module = getExecutionModule(flow.getModuleName(),
+                                       flow.getModuleVersion());
+                       if(module != null) {
+                               ExecutionContext executionContext = new ExecutionContext();
+                               
+                               // convert the values of flow.getFlowDescriptor()
+                               Map<String, Object> values = flow.getFlowDescriptor().getValues();
+                               
+                               Map<String, Object> convertedValues = new HashMap<String, Object>();
+                               
+                               for(String key : values.keySet()) {
+                                       Object value = values.get(key);
+                                       if(value instanceof PrimitiveValue) {
+                                               PrimitiveValue primitiveValue = (PrimitiveValue) value;
+
+                                               // TODO: check that the class of the the primitiveValue.value matches
+                                               // the primitiveValue.type
+                                               convertedValues.put(key, primitiveValue.getValue());
+                                       }
+                                       else if(value instanceof RefValue) {
+                                               RefValue refValue = (RefValue) value;
+                                               convertedValues.put(key, refValue.getLabel());
+                                       }
+                               }
+                               
+                               executionContext.addVariables(convertedValues);
+                               ExecutionThread thread = new ExecutionThread(executionContext, flow.getFlowDescriptor(),
+                                               module);
+                               thread.start();
+                       }
+                       else {
+                               throw new SlcException("ExecutionModule " + flow.getModuleName() + ", version " 
+                                               + flow.getModuleVersion() + " not found.");
+                       }
+               }
+       }
+
+       private class ExecutionThread extends Thread {
+               private final ExecutionFlowDescriptor executionFlowDescriptor;
+               private final ExecutionContext executionContext;
+               private final ExecutionModule executionModule;
+
+               public ExecutionThread(ExecutionContext executionContext,
+                               ExecutionFlowDescriptor executionFlowDescriptor,
+                               ExecutionModule executionModule) {
+                       super("SLC Execution #" + executionContext.getUuid());
+                       this.executionFlowDescriptor = executionFlowDescriptor;
+                       this.executionContext = executionContext;
+                       this.executionModule = executionModule;
+               }
+
+               public void run() {
+                       ExecutionContext.registerExecutionContext(executionContext);                            
+                       try {
+                               executionModule.execute(executionFlowDescriptor);
+                       } catch (Exception e) {
+                               //TODO: re-throw exception ?
+                               log.error("Execution " + executionContext.getUuid()
+                                               + " failed.", e);
+                       }
+               }
+       }       
+       
 }