]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/DefaultModulesManager.java
fdeb864a5b6b19be2b8c2346655ad692e524be5e
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / DefaultModulesManager.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.slc.SlcException;
12 import org.argeo.slc.execution.ExecutionContext;
13 import org.argeo.slc.execution.ExecutionFlowDescriptor;
14 import org.argeo.slc.execution.ExecutionModule;
15 import org.argeo.slc.execution.ExecutionModuleDescriptor;
16 import org.argeo.slc.execution.ExecutionModulesManager;
17 import org.argeo.slc.process.RealizedFlow;
18 import org.argeo.slc.process.SlcExecution;
19 import org.argeo.slc.process.SlcExecutionNotifier;
20 import org.dbunit.operation.UpdateOperation;
21 import org.springframework.util.Assert;
22
23 public class DefaultModulesManager implements ExecutionModulesManager {
24 private final static Log log = LogFactory
25 .getLog(DefaultModulesManager.class);
26
27 private List<ExecutionModule> executionModules = new ArrayList<ExecutionModule>();
28 private List<SlcExecutionNotifier> slcExecutionNotifiers = new ArrayList<SlcExecutionNotifier>();
29
30 protected ExecutionModule getExecutionModule(String moduleName,
31 String version) {
32 for (ExecutionModule moduleT : executionModules) {
33 if (moduleT.getName().equals(moduleName)) {
34 if (moduleT.getVersion().equals(version)) {
35 return moduleT;
36 }
37 }
38 }
39 return null;
40 }
41
42 public ExecutionModuleDescriptor getExecutionModuleDescriptor(
43 String moduleName, String version) {
44 ExecutionModule module = getExecutionModule(moduleName, version);
45
46 Assert.notNull(module);
47
48 return module.getDescriptor();
49 }
50
51 public List<ExecutionModule> listExecutionModules() {
52 return executionModules;
53 }
54
55 public void setExecutionModules(List<ExecutionModule> executionModules) {
56 this.executionModules = executionModules;
57 }
58
59 protected Map<String, Object> convertValues(
60 ExecutionFlowDescriptor executionFlowDescriptor) {
61 // convert the values of flow.getFlowDescriptor()
62 Map<String, Object> values = executionFlowDescriptor.getValues();
63
64 Map<String, Object> convertedValues = new HashMap<String, Object>();
65
66 for (String key : values.keySet()) {
67 Object value = values.get(key);
68 if (value instanceof PrimitiveValue) {
69 PrimitiveValue primitiveValue = (PrimitiveValue) value;
70
71 // TODO: check that the class of the the primitiveValue.value
72 // matches
73 // the primitiveValue.type
74 convertedValues.put(key, primitiveValue.getValue());
75 } else if (value instanceof RefValue) {
76 RefValue refValue = (RefValue) value;
77 convertedValues.put(key, refValue.getLabel());
78 }
79 }
80 return convertedValues;
81 }
82
83 public void process(SlcExecution slcExecution) {
84 log.info("\n##\n## Process SLC Execution " + slcExecution + "\n##\n");
85
86 for (RealizedFlow flow : slcExecution.getRealizedFlows()) {
87 ExecutionModule module = getExecutionModule(flow.getModuleName(),
88 flow.getModuleVersion());
89 if (module != null) {
90 ExecutionThread thread = new ExecutionThread(flow
91 .getFlowDescriptor(), module);
92 thread.start();
93 } else {
94 throw new SlcException("ExecutionModule "
95 + flow.getModuleName() + ", version "
96 + flow.getModuleVersion() + " not found.");
97 }
98 }
99
100 slcExecution.setStatus(SlcExecution.STATUS_RUNNING);
101 dispatchUpdateStatus(slcExecution, SlcExecution.STATUS_SCHEDULED,
102 SlcExecution.STATUS_RUNNING);
103 }
104
105 protected void dispatchUpdateStatus(SlcExecution slcExecution,
106 String oldStatus, String newStatus) {
107 for (Iterator<SlcExecutionNotifier> it = slcExecutionNotifiers
108 .iterator(); it.hasNext();) {
109 it.next().updateStatus(slcExecution, oldStatus, newStatus);
110 }
111 }
112
113 public void setSlcExecutionNotifiers(
114 List<SlcExecutionNotifier> slcExecutionNotifiers) {
115 this.slcExecutionNotifiers = slcExecutionNotifiers;
116 }
117
118 private class ExecutionThread extends Thread {
119 private final ExecutionFlowDescriptor executionFlowDescriptor;
120 private final ExecutionModule executionModule;
121
122 public ExecutionThread(ExecutionFlowDescriptor executionFlowDescriptor,
123 ExecutionModule executionModule) {
124 super("SLC Execution #" /* + executionContext.getUuid() */);
125 this.executionFlowDescriptor = executionFlowDescriptor;
126 this.executionModule = executionModule;
127 }
128
129 public void run() {
130 ExecutionContext executionContext = executionModule
131 .getExecutionContext();
132 executionContext
133 .addVariables(convertValues(executionFlowDescriptor));
134 try {
135 executionModule.execute(executionFlowDescriptor);
136 } catch (Exception e) {
137 // TODO: re-throw exception ?
138 log.error("Execution " + executionContext.getUuid()
139 + " failed.", e);
140 }
141 }
142 }
143 }