1 package org
.argeo
.slc
.core
.execution
;
4 import java
.util
.TreeMap
;
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
;
23 public abstract class AbstractSpringExecutionModule
implements ExecutionModule
26 protected ApplicationContext applicationContext;
28 protected ExecutionContext executionContext;
30 protected ExecutionFlowDescriptorConverter descriptorConverter = new DefaultDescriptorConverter();
32 public ExecutionModuleDescriptor getDescriptor() {
33 ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
34 md.setName(getName());
35 md.setVersion(getVersion());
37 Map<String, ExecutionFlow> executionFlows = listFlows();
38 for (String name : executionFlows.keySet()) {
39 ExecutionFlow executionFlow = executionFlows.get(name);
41 Assert.notNull(executionFlow.getName());
42 Assert.state(name.equals(executionFlow.getName()));
44 ExecutionSpec executionSpec = executionFlow.getExecutionSpec();
45 Assert.notNull(executionSpec);
46 Assert.notNull(executionSpec.getName());
48 Map<String, Object> values = new TreeMap<String, Object>();
49 for (String key : executionSpec.getAttributes().keySet()) {
50 ExecutionSpecAttribute attribute = executionSpec
51 .getAttributes().get(key);
53 if (executionFlow.isSetAsParameter(key)) {
54 Object value = executionFlow.getParameter(key);
55 if (attribute instanceof PrimitiveSpecAttribute) {
56 PrimitiveValue primitiveValue = new PrimitiveValue();
58 .setType(((PrimitiveSpecAttribute) attribute)
60 primitiveValue.setValue(value);
61 values.put(key, primitiveValue);
62 } else if (attribute instanceof RefSpecAttribute) {
63 RefValue refValue = new RefValue();
64 if (value instanceof ScopedObject) {
65 refValue.setLabel("RUNTIME "
66 + value.getClass().getName());
68 refValue.setLabel("STATIC "
69 + value.getClass().getName());
71 values.put(key, refValue);
72 } else if (attribute instanceof ResourceSpecAttribute) {
73 PrimitiveValue primitiveValue = new PrimitiveValue();
75 .setType(((ResourceSpecAttribute) attribute)
77 primitiveValue.setValue(value);
78 values.put(key, primitiveValue);
80 throw new SlcException("Unkown spec attribute type "
81 + attribute.getClass());
87 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(name,
88 values, executionSpec);
89 if (executionFlow.getPath() != null)
90 efd.setPath(executionFlow.getPath());
92 // Add execution spec if necessary
93 if (!md.getExecutionSpecs().contains(executionSpec))
94 md.getExecutionSpecs().add(executionSpec);
97 md.getExecutionFlows().add(efd);
103 protected Map<String, ExecutionFlow> listFlows() {
104 GenericBeanFactoryAccessor accessor = new GenericBeanFactoryAccessor(
106 Map<String, ExecutionFlow> executionFlows = accessor
107 .getBeansOfType(ExecutionFlow.class);
108 return executionFlows;
111 public void execute(ExecutionFlowDescriptor executionFlowDescriptor) {
112 if (descriptorConverter != null)
113 executionContext.addVariables(descriptorConverter
114 .convertValues(executionFlowDescriptor));
115 ExecutionFlow flow = (ExecutionFlow) applicationContext.getBean(
116 executionFlowDescriptor.getName(), ExecutionFlow.class);
120 public void setApplicationContext(ApplicationContext applicationContext)
121 throws BeansException {
122 this.applicationContext = applicationContext;
125 public void setExecutionContext(ExecutionContext executionContext) {
126 this.executionContext = executionContext;
129 public void setDescriptorConverter(
130 ExecutionFlowDescriptorConverter descriptorConverter) {
131 this.descriptorConverter = descriptorConverter;