]> 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
@update:81; Ability to copy the content of resources locally, thus enabling launchyin...
[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 if (attribute instanceof ResourceSpecAttribute) {
76 PrimitiveValue primitiveValue = new PrimitiveValue();
77 primitiveValue
78 .setType(((ResourceSpecAttribute) attribute)
79 .getType());
80 primitiveValue.setValue(value);
81 values.put(key, primitiveValue);
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 if (executionFlow.getPath() != null)
93 efd.setPath(executionFlow.getPath());
94
95 // Add execution spec if necessary
96 if (!md.getExecutionSpecs().contains(executionSpec))
97 md.getExecutionSpecs().add(executionSpec);
98
99 // Add execution flow
100 md.getExecutionFlows().add(efd);
101 }
102
103 return md;
104 }
105
106 public void execute(ExecutionFlowDescriptor executionFlowDescriptor) {
107 if (descriptorConverter != null)
108 executionContext.addVariables(descriptorConverter
109 .convertValues(executionFlowDescriptor));
110 ExecutionFlow flow = (ExecutionFlow) applicationContext.getBean(
111 executionFlowDescriptor.getName(), ExecutionFlow.class);
112 flow.run();
113 }
114
115 public void setApplicationContext(ApplicationContext applicationContext)
116 throws BeansException {
117 this.applicationContext = applicationContext;
118 }
119
120 public void setExecutionContext(ExecutionContext executionContext) {
121 this.executionContext = executionContext;
122 }
123
124 public void setDescriptorConverter(
125 ExecutionFlowDescriptorConverter descriptorConverter) {
126 this.descriptorConverter = descriptorConverter;
127 }
128
129 }