2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.core
.execution
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
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
;
35 /** Spring based implementation of execution specifications. */
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
;
43 public DefaultExecutionSpec() {
47 public void setBeanName(String name
) {
51 private ConfigurableListableBeanFactory
getBeanFactory() {
52 return ((ConfigurableApplicationContext
) applicationContext
).getBeanFactory();
55 public void setApplicationContext(ApplicationContext applicationContext
) {
56 this.applicationContext
= applicationContext
;
59 public void afterPropertiesSet() throws Exception
{
60 if (getDescription() == null) {
62 setDescription(getBeanFactory().getBeanDefinition(getName()).getDescription());
63 } catch (NoSuchBeanDefinitionException e
) {
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
);
76 if (log
.isTraceEnabled())
77 log
.debug("Spec attr " + key
+ " has " + rsa
.getChoices().size() + " choices");
83 * Generates a list of ref value choices based on the bean available in the
84 * application context.
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.");
93 beanNames
: for (String beanName
: getBeanFactory().getBeanNamesForType(rsa
.getTargetClass(), true, false)) {
95 // Since Spring 3, systemProperties is implicitly defined but has no
97 if (beanName
.equals("systemProperties"))
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
);