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