]> git.argeo.org Git - gpl/argeo-slc.git/blob - execution/DefaultExecutionSpec.java
Prepare next development cycle
[gpl/argeo-slc.git] / execution / DefaultExecutionSpec.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.argeo.slc.execution.ExecutionSpecAttribute;
9 import org.argeo.slc.execution.RefSpecAttribute;
10 import org.argeo.slc.execution.RefValueChoice;
11 import org.springframework.beans.factory.BeanNameAware;
12 import org.springframework.beans.factory.InitializingBean;
13 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
14 import org.springframework.beans.factory.config.BeanDefinition;
15 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
16 import org.springframework.context.ApplicationContext;
17 import org.springframework.context.ApplicationContextAware;
18 import org.springframework.context.ConfigurableApplicationContext;
19
20 /** Spring based implementation of execution specifications. */
21 @Deprecated
22 public class DefaultExecutionSpec extends org.argeo.slc.runtime.DefaultExecutionSpec
23 implements BeanNameAware, ApplicationContextAware, InitializingBean {
24 private static final long serialVersionUID = 5159882223926926539L;
25 private final static Log log = LogFactory.getLog(DefaultExecutionSpec.class);
26 private transient ApplicationContext applicationContext;
27
28 public DefaultExecutionSpec() {
29 super();
30 }
31
32 public void setBeanName(String name) {
33 setName(name);
34 }
35
36 private ConfigurableListableBeanFactory getBeanFactory() {
37 return ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
38 }
39
40 public void setApplicationContext(ApplicationContext applicationContext) {
41 this.applicationContext = applicationContext;
42 }
43
44 public void afterPropertiesSet() throws Exception {
45 if (getDescription() == null) {
46 try {
47 setDescription(getBeanFactory().getBeanDefinition(getName()).getDescription());
48 } catch (NoSuchBeanDefinitionException e) {
49 // silent
50 }
51 }
52
53 for (String key : getAttributes().keySet()) {
54 ExecutionSpecAttribute attr = getAttributes().get(key);
55 if (attr instanceof RefSpecAttribute) {
56 RefSpecAttribute rsa = (RefSpecAttribute) attr;
57 if (rsa.getChoices() == null) {
58 List<RefValueChoice> choices = buildRefValueChoices(rsa);
59 rsa.setChoices(choices);
60 }
61 if (log.isTraceEnabled())
62 log.debug("Spec attr " + key + " has " + rsa.getChoices().size() + " choices");
63 }
64 }
65 }
66
67 /**
68 * Generates a list of ref value choices based on the bean available in the
69 * application context.
70 */
71 protected List<RefValueChoice> buildRefValueChoices(RefSpecAttribute rsa) {
72 List<RefValueChoice> choices = new ArrayList<RefValueChoice>();
73 if (applicationContext == null) {
74 log.warn("No application context declared," + " cannot scan ref value choices.");
75 return choices;
76 }
77
78 beanNames: for (String beanName : getBeanFactory().getBeanNamesForType(rsa.getTargetClass(), true, false)) {
79
80 // Since Spring 3, systemProperties is implicitly defined but has no
81 // bean definition
82 if (beanName.equals("systemProperties"))
83 continue beanNames;
84
85 BeanDefinition bd = getBeanFactory().getBeanDefinition(beanName);
86 RefValueChoice choice = new RefValueChoice();
87 choice.setName(beanName);
88 choice.setDescription(bd.getDescription());
89 if (log.isTraceEnabled())
90 log.debug("Found choice " + beanName + " for " + rsa);
91
92 choices.add(choice);
93
94 }
95 return choices;
96 }
97
98 }