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
;
24 import java
.util
.UUID
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.execution
.ExecutionSpec
;
29 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
30 import org
.springframework
.beans
.factory
.BeanNameAware
;
31 import org
.springframework
.beans
.factory
.InitializingBean
;
32 import org
.springframework
.beans
.factory
.NoSuchBeanDefinitionException
;
33 import org
.springframework
.beans
.factory
.config
.BeanDefinition
;
34 import org
.springframework
.beans
.factory
.config
.ConfigurableListableBeanFactory
;
35 import org
.springframework
.context
.ApplicationContext
;
36 import org
.springframework
.context
.ApplicationContextAware
;
37 import org
.springframework
.context
.ConfigurableApplicationContext
;
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
= getClass().getName() + "#" + UUID
.randomUUID();
51 public Map
<String
, ExecutionSpecAttribute
> getAttributes() {
55 public void setAttributes(Map
<String
, ExecutionSpecAttribute
> attributes
) {
56 this.attributes
= attributes
;
59 public void setBeanName(String name
) {
63 public String
getName() {
67 public boolean equals(Object obj
) {
68 return ((ExecutionSpec
) obj
).getName().equals(name
);
71 public String
getDescription() {
75 private ConfigurableListableBeanFactory
getBeanFactory() {
76 return ((ConfigurableApplicationContext
) applicationContext
)
80 public void setApplicationContext(ApplicationContext applicationContext
) {
81 this.applicationContext
= applicationContext
;
84 public void afterPropertiesSet() throws Exception
{
85 if (description
== null) {
87 description
= getBeanFactory().getBeanDefinition(name
)
89 } catch (NoSuchBeanDefinitionException e
) {
94 for (String key
: attributes
.keySet()) {
95 ExecutionSpecAttribute attr
= attributes
.get(key
);
96 if (attr
instanceof RefSpecAttribute
) {
97 RefSpecAttribute rsa
= (RefSpecAttribute
) attr
;
98 if (rsa
.getChoices() == null) {
99 List
<RefValueChoice
> choices
= buildRefValueChoices(rsa
);
100 rsa
.setChoices(choices
);
102 if (log
.isTraceEnabled())
103 log
.debug("Spec attr " + key
+ " has "
104 + rsa
.getChoices().size() + " choices");
110 * Generates a list of ref value choices based on the bean available in the
111 * application ocntext.
113 protected List
<RefValueChoice
> buildRefValueChoices(RefSpecAttribute rsa
) {
114 List
<RefValueChoice
> choices
= new ArrayList
<RefValueChoice
>();
115 if (applicationContext
== null) {
116 log
.warn("No application context declared,"
117 + " cannot scan ref value choices.");
121 for (String beanName
: getBeanFactory().getBeanNamesForType(
122 rsa
.getTargetClass(), true, false)) {
124 // Since Spring 3, systemProperties is implicitly defined but has no
126 // if (beanName.equals("systemProperties"))
127 // continue beanNames;
129 BeanDefinition bd
= getBeanFactory().getBeanDefinition(beanName
);
130 RefValueChoice choice
= new RefValueChoice();
131 choice
.setName(beanName
);
132 choice
.setDescription(bd
.getDescription());
133 if (log
.isTraceEnabled())
134 log
.debug("Found choice " + beanName
+ " for " + rsa
);