]> 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
Introduce system calls
[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 if (executionFlow.getPath() != null)
86 efd.setPath(executionFlow.getPath());
87
88 // Add execution spec if necessary
89 if (!md.getExecutionSpecs().contains(executionSpec))
90 md.getExecutionSpecs().add(executionSpec);
91
92 // Add execution flow
93 md.getExecutionFlows().add(efd);
94 }
95
96 return md;
97 }
98
99 public void execute(ExecutionFlowDescriptor executionFlowDescriptor) {
100 if (descriptorConverter != null)
101 executionContext.addVariables(descriptorConverter
102 .convertValues(executionFlowDescriptor));
103 ExecutionFlow flow = (ExecutionFlow) applicationContext.getBean(
104 executionFlowDescriptor.getName(), ExecutionFlow.class);
105 flow.execute();
106 }
107
108 public void setApplicationContext(ApplicationContext applicationContext)
109 throws BeansException {
110 this.applicationContext = applicationContext;
111 }
112
113 public void setExecutionContext(ExecutionContext executionContext) {
114 this.executionContext = executionContext;
115 }
116
117 public void setDescriptorConverter(
118 ExecutionFlowDescriptorConverter descriptorConverter) {
119 this.descriptorConverter = descriptorConverter;
120 }
121
122 }