View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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  package org.argeo.slc.core.execution;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.argeo.slc.SlcException;
24  import org.springframework.beans.BeansException;
25  import org.springframework.beans.MutablePropertyValues;
26  import org.springframework.beans.factory.config.BeanDefinition;
27  import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
28  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
29  import org.springframework.beans.factory.support.BeanDefinitionRegistry;
30  import org.springframework.beans.factory.support.GenericBeanDefinition;
31  import org.springframework.core.Ordered;
32  import org.springframework.core.PriorityOrdered;
33  
34  public abstract class AbstractExecutionFlowGenerator implements
35  		BeanFactoryPostProcessor, PriorityOrdered {
36  	private final Log log = LogFactory.getLog(getClass());
37  
38  	protected abstract Map<String, BeanDefinition> createExecutionFlowDefinitions(
39  			ConfigurableListableBeanFactory beanFactory);
40  
41  	public void postProcessBeanFactory(
42  			ConfigurableListableBeanFactory beanFactory) throws BeansException {
43  		if (!(beanFactory instanceof BeanDefinitionRegistry)) {
44  			throw new SlcException("Can only work on "
45  					+ BeanDefinitionRegistry.class);
46  		}
47  
48  		Map<String, BeanDefinition> definitions = createExecutionFlowDefinitions(beanFactory);
49  
50  		for (String beanName : definitions.keySet()) {
51  			if (log.isTraceEnabled())
52  				log.debug("Registering execution flow " + beanName);
53  			((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(
54  					beanName, definitions.get(beanName));
55  		}
56  	}
57  
58  	protected GenericBeanDefinition createDefaultFlowDefinition(
59  			List<Runnable> executables) {
60  		GenericBeanDefinition bd = new GenericBeanDefinition();
61  		bd.setBeanClass(DefaultExecutionFlow.class);
62  
63  		MutablePropertyValues mpv = new MutablePropertyValues();
64  		mpv.addPropertyValue("executables", executables);
65  
66  		bd.setPropertyValues(mpv);
67  		return bd;
68  	}
69  
70  	public int getOrder() {
71  		return Ordered.HIGHEST_PRECEDENCE;
72  	}
73  
74  }