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