]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/AbstractExecutionFlowGenerator.java
Document and improve execution model
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / AbstractExecutionFlowGenerator.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.core.execution;
18
19 import java.util.List;
20 import java.util.Map;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.slc.SlcException;
25 import org.springframework.beans.BeansException;
26 import org.springframework.beans.MutablePropertyValues;
27 import org.springframework.beans.factory.config.BeanDefinition;
28 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
29 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
30 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
31 import org.springframework.beans.factory.support.GenericBeanDefinition;
32 import org.springframework.core.Ordered;
33 import org.springframework.core.PriorityOrdered;
34
35 public abstract class AbstractExecutionFlowGenerator implements
36 BeanFactoryPostProcessor, PriorityOrdered {
37 private final Log log = LogFactory.getLog(getClass());
38
39 protected abstract Map<String, BeanDefinition> createExecutionFlowDefinitions(
40 ConfigurableListableBeanFactory beanFactory);
41
42 public void postProcessBeanFactory(
43 ConfigurableListableBeanFactory beanFactory) throws BeansException {
44 if (!(beanFactory instanceof BeanDefinitionRegistry)) {
45 throw new SlcException("Can only work on "
46 + BeanDefinitionRegistry.class);
47 }
48
49 Map<String, BeanDefinition> definitions = createExecutionFlowDefinitions(beanFactory);
50
51 for (String beanName : definitions.keySet()) {
52 if (log.isTraceEnabled())
53 log.debug("Registering execution flow " + beanName);
54 ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(
55 beanName, definitions.get(beanName));
56 }
57 }
58
59 protected GenericBeanDefinition createDefaultFlowDefinition(
60 List<Runnable> executables) {
61 GenericBeanDefinition bd = new GenericBeanDefinition();
62 bd.setBeanClass(DefaultExecutionFlow.class);
63
64 MutablePropertyValues mpv = new MutablePropertyValues();
65 mpv.addPropertyValue("executables", executables);
66
67 bd.setPropertyValues(mpv);
68 return bd;
69 }
70
71 public int getOrder() {
72 return Ordered.HIGHEST_PRECEDENCE;
73 }
74
75 }