]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/generator/ExecutionFlowGenerator.java
Add resource as stdin
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / generator / ExecutionFlowGenerator.java
1 package org.argeo.slc.core.execution.generator;
2
3 import java.util.HashMap;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.argeo.slc.SlcException;
8 import org.springframework.aop.scope.ScopedProxyUtils;
9 import org.springframework.beans.BeansException;
10 import org.springframework.beans.MutablePropertyValues;
11 import org.springframework.beans.factory.config.BeanDefinitionHolder;
12 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
13 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
14 import org.springframework.beans.factory.config.RuntimeBeanReference;
15 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
16 import org.springframework.beans.factory.support.GenericBeanDefinition;
17 import org.springframework.core.Ordered;
18 import org.springframework.core.PriorityOrdered;
19
20 /**
21 * Generates <code>ExecutionFlows</code> and <code>Runnables</code> as
22 * beans in the Spring Application Context.
23 * Called by the Application Context as a <code>BeanFactoryPostProcessor</code>.
24 * Two kinds of beans are generated:
25 * <code>RunnableCallFlow</code>, calling a list of <code>Runnables</code> from the
26 * Application Context after configuring the <code>ExecutionContext</code>,
27 * and outputs of a <code>RunnableFactory</code>.
28 */
29 public class ExecutionFlowGenerator implements BeanFactoryPostProcessor,
30 PriorityOrdered {
31
32 private final Log log = LogFactory.getLog(getClass());
33
34 /**
35 * Source providing a list of <code>RunnableCallFlowDescriptor</code>
36 * used to create <code>RunnableCallFlow</code> and a list of
37 * <code>RunnableDataNode</code> used to create any kind of flow via a factory
38 */
39 protected ExecutionFlowGeneratorSource source;
40
41 /**
42 * Factory used to create Runnables in the Application context from
43 * the <code>RunnableDataNode</code> provided from the source.
44 */
45 protected RunnableFactory runnableFactory;
46
47 /**
48 * Bean name of the <code>ExecutionContext</code>.
49 * Used to provide the created <code>RunnableCallFlow</code> beans
50 * with a <code>RuntimeBeanReference</code> to
51 * the <code>ExecutionContext</code>
52 */
53 private String executionContextBeanName = "executionContext";
54
55 /**
56 * Bean name of the context values Map.
57 * A bean of class HashMap is created with this name, and a
58 * <code>RuntimeBeanReference</code> is provided to the created
59 * <code>RunnableCallFlow</code> beans.
60 */
61 private String contextValuesBeanName = "executionFlowGenerator.contextValues";
62
63 /**
64 * Prefix added to the bean names defined in each
65 * <code>RunnableCallFlowDescriptor</code>
66 */
67 private String flowBeanNamesPrefix = "";
68
69 public void postProcessBeanFactory(
70 ConfigurableListableBeanFactory beanFactory) throws BeansException {
71
72 // assert that the beanFactory is a BeanDefinitionRegistry
73 if (!(beanFactory instanceof BeanDefinitionRegistry)) {
74 throw new SlcException("Can only work on "
75 + BeanDefinitionRegistry.class);
76 }
77
78 // add bean for the Context Values Map
79 createAndRegisterContextValuesBean((BeanDefinitionRegistry) beanFactory);
80
81 // add beans for each RunnableDataNode
82 for(RunnableDataNode node : source.getRunnableDataNodes()) {
83 runnableFactory.createAndRegisterRunnable(node, (BeanDefinitionRegistry) beanFactory);
84 }
85
86 // add beans for each RunnableCallFlowDescriptor of the source to the application context
87 for (RunnableCallFlowDescriptor descriptor : source
88 .getRunnableCallFlowDescriptors()) {
89 createAndRegisterFlowFor(descriptor, (BeanDefinitionRegistry) beanFactory);
90 }
91 }
92
93 /**
94 * Creates a <code>RunnableCallFlow</code> bean
95 * for a <code>RunnableCallFlowDescriptor</code> and registers
96 * it in the <code>BeanDefinitionRegistry</code>
97 * @param flowDescriptor
98 * @param registry
99 */
100 private void createAndRegisterFlowFor(RunnableCallFlowDescriptor flowDescriptor, BeanDefinitionRegistry registry) {
101 // create the flow bean
102 GenericBeanDefinition flowBean = new GenericBeanDefinition();
103 flowBean.setBeanClass(RunnableCallFlow.class);
104
105 String beanName = flowBeanNamesPrefix + flowDescriptor.getBeanName();
106
107 MutablePropertyValues mpv = new MutablePropertyValues();
108 mpv.addPropertyValue("runnableCalls", flowDescriptor.getRunnableCalls());
109 mpv.addPropertyValue("sharedContextValuesMap", new RuntimeBeanReference(contextValuesBeanName));
110
111 mpv.addPropertyValue("name", beanName);
112 mpv.addPropertyValue("path", flowDescriptor.getPath());
113
114 mpv.addPropertyValue("executionContext", new RuntimeBeanReference(executionContextBeanName));
115
116 flowBean.setPropertyValues(mpv);
117
118 // register it
119 if(log.isDebugEnabled()) {
120 log.debug("Registering bean definition for RunnableCallFlow " + beanName);
121 }
122 registry.registerBeanDefinition(beanName, flowBean);
123 }
124
125 /**
126 * Creates the Context Values bean and register it in the
127 * <code>BeanDefinitionRegistry</code>
128 * @param registry
129 */
130 private void createAndRegisterContextValuesBean(BeanDefinitionRegistry registry) {
131 GenericBeanDefinition contextValuesBean = new GenericBeanDefinition();
132 contextValuesBean.setBeanClass(HashMap.class);
133
134 BeanDefinitionHolder bdh = ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(contextValuesBean, contextValuesBeanName), registry, true);
135 registry.registerBeanDefinition(contextValuesBeanName, bdh.getBeanDefinition());
136 }
137
138 public int getOrder() {
139 return Ordered.HIGHEST_PRECEDENCE;
140 }
141
142 public void setSource(ExecutionFlowGeneratorSource source) {
143 this.source = source;
144 }
145
146 public void setRunnableFactory(RunnableFactory runnableFactory) {
147 this.runnableFactory = runnableFactory;
148 }
149
150 public void setExecutionContextBeanName(String executionContextBeanName) {
151 this.executionContextBeanName = executionContextBeanName;
152 }
153
154 public void setContextValuesBeanName(String contextValuesBeanName) {
155 this.contextValuesBeanName = contextValuesBeanName;
156 }
157
158 public void setFlowBeanNamesPrefix(String flowBeanNamesPrefix) {
159 this.flowBeanNamesPrefix = flowBeanNamesPrefix;
160 }
161 }