]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionSpec.java
Modular distributions
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / DefaultExecutionSpec.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.UUID;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.slc.execution.ExecutionSpec;
12 import org.argeo.slc.execution.ExecutionSpecAttribute;
13 import org.springframework.beans.factory.BeanNameAware;
14 import org.springframework.beans.factory.InitializingBean;
15 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
16 import org.springframework.beans.factory.config.BeanDefinition;
17 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
18 import org.springframework.context.ApplicationContext;
19 import org.springframework.context.ApplicationContextAware;
20 import org.springframework.context.ConfigurableApplicationContext;
21
22 public class DefaultExecutionSpec implements ExecutionSpec, BeanNameAware,
23 ApplicationContextAware, InitializingBean {
24 private final static Log log = LogFactory
25 .getLog(DefaultExecutionSpec.class);
26 private ApplicationContext applicationContext;
27
28 private String description;
29 private Map<String, ExecutionSpecAttribute> attributes = new HashMap<String, ExecutionSpecAttribute>();
30
31 private String name = getClass().getName() + "#" + UUID.randomUUID();
32
33 public Map<String, ExecutionSpecAttribute> getAttributes() {
34 return attributes;
35 }
36
37 public void setAttributes(Map<String, ExecutionSpecAttribute> attributes) {
38 this.attributes = attributes;
39 }
40
41 public void setBeanName(String name) {
42 this.name = name;
43 }
44
45 public String getName() {
46 return name;
47 }
48
49 public boolean equals(Object obj) {
50 return ((ExecutionSpec) obj).getName().equals(name);
51 }
52
53 public String getDescription() {
54 return description;
55 }
56
57 private ConfigurableListableBeanFactory getBeanFactory() {
58 return ((ConfigurableApplicationContext) applicationContext)
59 .getBeanFactory();
60 }
61
62 public void setApplicationContext(ApplicationContext applicationContext) {
63 this.applicationContext = applicationContext;
64 }
65
66 public void afterPropertiesSet() throws Exception {
67 if (description == null) {
68 try {
69 description = getBeanFactory().getBeanDefinition(name)
70 .getDescription();
71 } catch (NoSuchBeanDefinitionException e) {
72 // silent
73 }
74 }
75
76 for (String key : attributes.keySet()) {
77 ExecutionSpecAttribute attr = attributes.get(key);
78 if (attr instanceof RefSpecAttribute) {
79 RefSpecAttribute rsa = (RefSpecAttribute) attr;
80 if (rsa.getChoices() == null) {
81 List<RefValueChoice> choices = buildRefValueChoices(rsa);
82 if (log.isTraceEnabled())
83 log.debug("Found " + choices.size() + " choices for "
84 + rsa + " in spec " + name);
85
86 rsa.setChoices(choices);
87 }
88 }
89 }
90 }
91
92 protected List<RefValueChoice> buildRefValueChoices(RefSpecAttribute rsa) {
93 List<RefValueChoice> choices = new ArrayList<RefValueChoice>();
94 if (applicationContext == null) {
95 log.warn("No application context declared,"
96 + " cannot scan ref value choices.");
97 return choices;
98 }
99
100 for (String beanName : getBeanFactory().getBeanNamesForType(
101 rsa.getTargetClass(), true, false)) {
102 BeanDefinition bd = getBeanFactory().getBeanDefinition(beanName);
103 RefValueChoice choice = new RefValueChoice();
104 choice.setName(beanName);
105 choice.setDescription(bd.getDescription());
106 if (log.isTraceEnabled())
107 log.debug("Found choice " + beanName + " for " + rsa);
108
109 choices.add(choice);
110
111 }
112 return choices;
113 }
114
115 }