]> 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
Change the generated XML for the execution message
[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.ExecutionFlow;
9 import org.argeo.slc.execution.ExecutionFlowDescriptor;
10 import org.argeo.slc.execution.ExecutionModule;
11 import org.argeo.slc.execution.ExecutionModuleDescriptor;
12 import org.argeo.slc.execution.ExecutionSpec;
13 import org.argeo.slc.execution.ExecutionSpecAttribute;
14 import org.argeo.slc.process.SlcExecution;
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 private ApplicationContext applicationContext;
25
26 public ExecutionModuleDescriptor getDescriptor() {
27 ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
28 md.setName(getName());
29 md.setVersion(getVersion());
30
31 GenericBeanFactoryAccessor accessor = new GenericBeanFactoryAccessor(
32 applicationContext);
33 Map<String, ExecutionFlow> executionFlows = accessor
34 .getBeansOfType(ExecutionFlow.class);
35
36 for (String name : executionFlows.keySet()) {
37 ExecutionFlow executionFlow = executionFlows.get(name);
38
39 Assert.notNull(executionFlow.getName());
40 Assert.state(name.equals(executionFlow.getName()));
41
42 ExecutionSpec executionSpec = executionFlow.getExecutionSpec();
43 Assert.notNull(executionSpec);
44 Assert.notNull(executionSpec.getName());
45
46 Map<String, Object> values = new TreeMap<String, Object>();
47 for (String key : executionSpec.getAttributes().keySet()) {
48 ExecutionSpecAttribute attribute = executionSpec
49 .getAttributes().get(key);
50
51 if (executionFlow.isSetAsParameter(key)) {
52 Object value = executionFlow.getParameter(key);
53 if (attribute instanceof PrimitiveSpecAttribute) {
54 PrimitiveValue primitiveValue = new PrimitiveValue();
55 primitiveValue
56 .setType(((PrimitiveSpecAttribute) attribute)
57 .getType());
58 primitiveValue.setValue(value);
59 values.put(key, primitiveValue);
60 } else if (attribute instanceof RefSpecAttribute) {
61 RefValue refValue = new RefValue();
62 if (value instanceof ScopedObject) {
63 refValue.setLabel("RUNTIME "
64 + value.getClass().getName());
65 } else {
66 refValue.setLabel("STATIC "
67 + value.getClass().getName());
68 }
69 values.put(key, refValue);
70 } else {
71 throw new SlcException("Unkown spec attribute type "
72 + attribute.getClass());
73 }
74 }
75
76 }
77
78 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(name,
79 values, executionSpec);
80
81 // Add execution spec if necessary
82 if (!md.getExecutionSpecs().contains(executionSpec))
83 md.getExecutionSpecs().add(executionSpec);
84
85 // Add execution flow
86 md.getExecutionFlows().add(efd);
87 }
88
89 return md;
90 }
91
92 public void execute(SlcExecution slcExecution) {
93 applicationContext.publishEvent(new NewExecutionEvent(this,
94 slcExecution));
95 }
96
97 public void setApplicationContext(ApplicationContext applicationContext)
98 throws BeansException {
99 this.applicationContext = applicationContext;
100 }
101
102 }