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