2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.core
.execution
;
19 import java
.io
.Serializable
;
20 import java
.util
.ArrayList
;
21 import java
.util
.HashMap
;
22 import java
.util
.List
;
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
;
38 /** Spring based implementation of execution specifications. */
39 public class DefaultExecutionSpec
implements ExecutionSpec
, BeanNameAware
,
40 ApplicationContextAware
, InitializingBean
, Serializable
{
41 private static final long serialVersionUID
= 5159882223926926539L;
42 private final static Log log
= LogFactory
43 .getLog(DefaultExecutionSpec
.class);
44 private transient ApplicationContext applicationContext
;
46 private String description
;
47 private Map
<String
, ExecutionSpecAttribute
> attributes
= new HashMap
<String
, ExecutionSpecAttribute
>();
49 private String name
= INTERNAL_NAME
;
51 public Map
<String
, ExecutionSpecAttribute
> getAttributes() {
55 public void setAttributes(Map
<String
, ExecutionSpecAttribute
> attributes
) {
56 this.attributes
= attributes
;
59 public void setBeanName(String name
) {
64 * The Spring bean name (only relevant for specs declared has high-level
67 public String
getName() {
71 public boolean equals(Object obj
) {
72 return ((ExecutionSpec
) obj
).getName().equals(name
);
76 * The Spring bean description (only relevant for specs declared has
79 public String
getDescription() {
83 private ConfigurableListableBeanFactory
getBeanFactory() {
84 return ((ConfigurableApplicationContext
) applicationContext
)
88 public void setApplicationContext(ApplicationContext applicationContext
) {
89 this.applicationContext
= applicationContext
;
92 public void afterPropertiesSet() throws Exception
{
93 if (description
== null) {
95 description
= getBeanFactory().getBeanDefinition(name
)
97 } catch (NoSuchBeanDefinitionException e
) {
102 for (String key
: attributes
.keySet()) {
103 ExecutionSpecAttribute attr
= attributes
.get(key
);
104 if (attr
instanceof RefSpecAttribute
) {
105 RefSpecAttribute rsa
= (RefSpecAttribute
) attr
;
106 if (rsa
.getChoices() == null) {
107 List
<RefValueChoice
> choices
= buildRefValueChoices(rsa
);
108 rsa
.setChoices(choices
);
110 if (log
.isTraceEnabled())
111 log
.debug("Spec attr " + key
+ " has "
112 + rsa
.getChoices().size() + " choices");
118 * Generates a list of ref value choices based on the bean available in the
119 * application ocntext.
121 protected List
<RefValueChoice
> buildRefValueChoices(RefSpecAttribute rsa
) {
122 List
<RefValueChoice
> choices
= new ArrayList
<RefValueChoice
>();
123 if (applicationContext
== null) {
124 log
.warn("No application context declared,"
125 + " cannot scan ref value choices.");
129 for (String beanName
: getBeanFactory().getBeanNamesForType(
130 rsa
.getTargetClass(), true, false)) {
132 // Since Spring 3, systemProperties is implicitly defined but has no
134 // if (beanName.equals("systemProperties"))
135 // continue beanNames;
137 BeanDefinition bd
= getBeanFactory().getBeanDefinition(beanName
);
138 RefValueChoice choice
= new RefValueChoice();
139 choice
.setName(beanName
);
140 choice
.setDescription(bd
.getDescription());
141 if (log
.isTraceEnabled())
142 log
.debug("Found choice " + beanName
+ " for " + rsa
);