1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
5 import java
.util
.Iterator
;
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
;
23 public class DefaultModulesManager
implements ExecutionModulesManager
{
24 private final static Log log
= LogFactory
25 .getLog(DefaultModulesManager
.class);
27 private List
<ExecutionModule
> executionModules
= new ArrayList
<ExecutionModule
>();
28 private List
<SlcExecutionNotifier
> slcExecutionNotifiers
= new ArrayList
<SlcExecutionNotifier
>();
30 protected ExecutionModule
getExecutionModule(String moduleName
,
32 for (ExecutionModule moduleT
: executionModules
) {
33 if (moduleT
.getName().equals(moduleName
)) {
34 if (moduleT
.getVersion().equals(version
)) {
42 public ExecutionModuleDescriptor
getExecutionModuleDescriptor(
43 String moduleName
, String version
) {
44 ExecutionModule module
= getExecutionModule(moduleName
, version
);
46 Assert
.notNull(module
);
48 return module
.getDescriptor();
51 public List
<ExecutionModule
> listExecutionModules() {
52 return executionModules
;
55 public void setExecutionModules(List
<ExecutionModule
> executionModules
) {
56 this.executionModules
= executionModules
;
59 protected Map
<String
, Object
> convertValues(
60 ExecutionFlowDescriptor executionFlowDescriptor
) {
61 // convert the values of flow.getFlowDescriptor()
62 Map
<String
, Object
> values
= executionFlowDescriptor
.getValues();
64 Map
<String
, Object
> convertedValues
= new HashMap
<String
, Object
>();
66 for (String key
: values
.keySet()) {
67 Object value
= values
.get(key
);
68 if (value
instanceof PrimitiveValue
) {
69 PrimitiveValue primitiveValue
= (PrimitiveValue
) value
;
71 // TODO: check that the class of the the primitiveValue.value
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());
80 return convertedValues
;
83 public void process(SlcExecution slcExecution
) {
84 log
.info("\n##\n## Process SLC Execution " + slcExecution
+ "\n##\n");
86 for (RealizedFlow flow
: slcExecution
.getRealizedFlows()) {
87 ExecutionModule module
= getExecutionModule(flow
.getModuleName(),
88 flow
.getModuleVersion());
90 ExecutionThread thread
= new ExecutionThread(flow
91 .getFlowDescriptor(), module
);
94 throw new SlcException("ExecutionModule "
95 + flow
.getModuleName() + ", version "
96 + flow
.getModuleVersion() + " not found.");
100 slcExecution
.setStatus(SlcExecution
.STATUS_RUNNING
);
101 dispatchUpdateStatus(slcExecution
, SlcExecution
.STATUS_SCHEDULED
,
102 SlcExecution
.STATUS_RUNNING
);
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
);
113 public void setSlcExecutionNotifiers(
114 List
<SlcExecutionNotifier
> slcExecutionNotifiers
) {
115 this.slcExecutionNotifiers
= slcExecutionNotifiers
;
118 private class ExecutionThread
extends Thread
{
119 private final ExecutionFlowDescriptor executionFlowDescriptor
;
120 private final ExecutionModule executionModule
;
122 public ExecutionThread(ExecutionFlowDescriptor executionFlowDescriptor
,
123 ExecutionModule executionModule
) {
124 super("SLC Execution #" /* + executionContext.getUuid() */);
125 this.executionFlowDescriptor
= executionFlowDescriptor
;
126 this.executionModule
= executionModule
;
130 ExecutionContext executionContext
= executionModule
131 .getExecutionContext();
133 .addVariables(convertValues(executionFlowDescriptor
));
135 executionModule
.execute(executionFlowDescriptor
);
136 } catch (Exception e
) {
137 // TODO: re-throw exception ?
138 log
.error("Execution " + executionContext
.getUuid()