1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.slc.core.execution.generator;
17
18 import java.util.HashMap;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.argeo.slc.SlcException;
23 import org.springframework.aop.scope.ScopedProxyUtils;
24 import org.springframework.beans.BeansException;
25 import org.springframework.beans.MutablePropertyValues;
26 import org.springframework.beans.factory.config.BeanDefinitionHolder;
27 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
28 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
29 import org.springframework.beans.factory.config.RuntimeBeanReference;
30 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
31 import org.springframework.beans.factory.support.GenericBeanDefinition;
32 import org.springframework.core.Ordered;
33
34
35
36
37
38
39
40
41
42
43 public class ExecutionFlowGenerator implements BeanFactoryPostProcessor,
44 Ordered {
45
46 private final Log log = LogFactory.getLog(getClass());
47
48
49
50
51
52
53 protected ExecutionFlowGeneratorSource source;
54
55
56
57
58
59 protected RunnableFactory runnableFactory;
60
61
62
63
64
65
66
67 private String executionContextBeanName = "executionContext";
68
69
70
71
72
73
74
75 private String contextValuesBeanName = "executionFlowGenerator.contextValues";
76
77
78
79
80
81 private String flowBeanNamesPrefix = "";
82
83 private int order = Ordered.HIGHEST_PRECEDENCE;
84
85 public void postProcessBeanFactory(
86 ConfigurableListableBeanFactory beanFactory) throws BeansException {
87
88
89 if (!(beanFactory instanceof BeanDefinitionRegistry)) {
90 throw new SlcException("Can only work on "
91 + BeanDefinitionRegistry.class);
92 }
93
94
95 createAndRegisterContextValuesBean((BeanDefinitionRegistry) beanFactory);
96
97
98 for(RunnableDataNode node : source.getRunnableDataNodes()) {
99 runnableFactory.createAndRegisterRunnable(node, (BeanDefinitionRegistry) beanFactory);
100 }
101
102
103 for (RunnableCallFlowDescriptor descriptor : source
104 .getRunnableCallFlowDescriptors()) {
105 createAndRegisterFlowFor(descriptor, (BeanDefinitionRegistry) beanFactory);
106 }
107 }
108
109
110
111
112
113
114
115
116 private void createAndRegisterFlowFor(RunnableCallFlowDescriptor flowDescriptor, BeanDefinitionRegistry registry) {
117
118 GenericBeanDefinition flowBean = new GenericBeanDefinition();
119 flowBean.setBeanClass(RunnableCallFlow.class);
120
121 String beanName = flowBeanNamesPrefix + flowDescriptor.getBeanName();
122
123 MutablePropertyValues mpv = new MutablePropertyValues();
124 mpv.addPropertyValue("runnableCalls", flowDescriptor.getRunnableCalls());
125 mpv.addPropertyValue("sharedContextValuesMap", new RuntimeBeanReference(contextValuesBeanName));
126
127 mpv.addPropertyValue("name", beanName);
128 mpv.addPropertyValue("path", flowDescriptor.getPath());
129
130 mpv.addPropertyValue("executionContext", new RuntimeBeanReference(executionContextBeanName));
131
132 flowBean.setPropertyValues(mpv);
133
134
135 if(log.isDebugEnabled()) {
136 log.debug("Registering bean definition for RunnableCallFlow " + beanName);
137 }
138 registry.registerBeanDefinition(beanName, flowBean);
139 }
140
141
142
143
144
145
146 private void createAndRegisterContextValuesBean(BeanDefinitionRegistry registry) {
147 GenericBeanDefinition contextValuesBean = new GenericBeanDefinition();
148 contextValuesBean.setBeanClass(HashMap.class);
149
150 BeanDefinitionHolder bdh = ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(contextValuesBean, contextValuesBeanName), registry, true);
151 registry.registerBeanDefinition(contextValuesBeanName, bdh.getBeanDefinition());
152 }
153
154 public int getOrder() {
155 return order;
156 }
157
158 public void setOrder(int order) {
159 this.order = order;
160 }
161
162 public void setSource(ExecutionFlowGeneratorSource source) {
163 this.source = source;
164 }
165
166 public void setRunnableFactory(RunnableFactory runnableFactory) {
167 this.runnableFactory = runnableFactory;
168 }
169
170 public void setExecutionContextBeanName(String executionContextBeanName) {
171 this.executionContextBeanName = executionContextBeanName;
172 }
173
174 public void setContextValuesBeanName(String contextValuesBeanName) {
175 this.contextValuesBeanName = contextValuesBeanName;
176 }
177
178 public void setFlowBeanNamesPrefix(String flowBeanNamesPrefix) {
179 this.flowBeanNamesPrefix = flowBeanNamesPrefix;
180 }
181 }