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