]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/AbstractSpringExecutionModule.java
Improve launching of executions
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / AbstractSpringExecutionModule.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.Map;
4 import java.util.TreeMap;
5
6 import org.argeo.slc.SlcException;
7 import org.argeo.slc.execution.ExecutionContext;
8 import org.argeo.slc.execution.ExecutionFlow;
9 import org.argeo.slc.execution.ExecutionFlowDescriptor;
10 import org.argeo.slc.execution.ExecutionFlowDescriptorConverter;
11 import org.argeo.slc.execution.ExecutionModule;
12 import org.argeo.slc.execution.ExecutionModuleDescriptor;
13 import org.argeo.slc.execution.ExecutionSpec;
14 import org.argeo.slc.execution.ExecutionSpecAttribute;
15 import org.springframework.aop.scope.ScopedObject;
16 import org.springframework.beans.BeansException;
17 import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
18 import org.springframework.context.ApplicationContext;
19 import org.springframework.context.ApplicationContextAware;
20 import org.springframework.util.Assert;
21
22 public abstract class AbstractSpringExecutionModule implements ExecutionModule,
23 ApplicationContextAware {
24
25 private ApplicationContext applicationContext;
26
27 private ExecutionContext executionContext;
28
29 private ExecutionFlowDescriptorConverter descriptorConverter = new DefaultDescriptorConverter();
30
31 public ExecutionModuleDescriptor getDescriptor() {
32 ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
33 md.setName(getName());
34 md.setVersion(getVersion());
35
36 GenericBeanFactoryAccessor accessor = new GenericBeanFactoryAccessor(
37 applicationContext);
38 Map<String, ExecutionFlow> executionFlows = accessor
39 .getBeansOfType(ExecutionFlow.class);
40
41 for (String name : executionFlows.keySet()) {
42 ExecutionFlow executionFlow = executionFlows.get(name);
43
44 Assert.notNull(executionFlow.getName());
45 Assert.state(name.equals(executionFlow.getName()));
46
47 ExecutionSpec executionSpec = executionFlow.getExecutionSpec();
48 Assert.notNull(executionSpec);
49 Assert.notNull(executionSpec.getName());
50
51 Map<String, Object> values = new TreeMap<String, Object>();
52 for (String key : executionSpec.getAttributes().keySet()) {
53 ExecutionSpecAttribute attribute = executionSpec
54 .getAttributes().get(key);
55
56 if (executionFlow.isSetAsParameter(key)) {
57 Object value = executionFlow.getParameter(key);
58 if (attribute instanceof PrimitiveSpecAttribute) {
59 PrimitiveValue primitiveValue = new PrimitiveValue();
60 primitiveValue
61 .setType(((PrimitiveSpecAttribute) attribute)
62 .getType());
63 primitiveValue.setValue(value);
64 values.put(key, primitiveValue);
65 } else if (attribute instanceof RefSpecAttribute) {
66 RefValue refValue = new RefValue();
67 if (value instanceof ScopedObject) {
68 refValue.setLabel("RUNTIME "
69 + value.getClass().getName());
70 } else {
71 refValue.setLabel("STATIC "
72 + value.getClass().getName());
73 }
74 values.put(key, refValue);
75 } else {
76 throw new SlcException("Unkown spec attribute type "
77 + attribute.getClass());
78 }
79 }
80
81 }
82
83 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(name,
84 values, executionSpec);
85
86 // Add execution spec if necessary
87 if (!md.getExecutionSpecs().contains(executionSpec))
88 md.getExecutionSpecs().add(executionSpec);
89
90 // Add execution flow
91 md.getExecutionFlows().add(efd);
92 }
93
94 return md;
95 }
96
97 public void execute(ExecutionFlowDescriptor executionFlowDescriptor) {
98 if (descriptorConverter != null)
99 executionContext.addVariables(descriptorConverter
100 .convertValues(executionFlowDescriptor));
101 ExecutionFlow flow = (ExecutionFlow) applicationContext.getBean(
102 executionFlowDescriptor.getName(), ExecutionFlow.class);
103 flow.execute();
104 }
105
106 public void setApplicationContext(ApplicationContext applicationContext)
107 throws BeansException {
108 this.applicationContext = applicationContext;
109 }
110
111 public void setExecutionContext(ExecutionContext executionContext) {
112 this.executionContext = executionContext;
113 }
114
115 public void setDescriptorConverter(
116 ExecutionFlowDescriptorConverter descriptorConverter) {
117 this.descriptorConverter = descriptorConverter;
118 }
119
120 }