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
.util
.Comparator
;
20 import java
.util
.HashMap
;
22 import java
.util
.SortedSet
;
23 import java
.util
.TreeMap
;
24 import java
.util
.TreeSet
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.argeo
.slc
.UnsupportedException
;
30 import org
.argeo
.slc
.execution
.ExecutionFlow
;
31 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptor
;
32 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptorConverter
;
33 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
34 import org
.argeo
.slc
.execution
.ExecutionSpec
;
35 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
36 import org
.springframework
.aop
.scope
.ScopedObject
;
37 import org
.springframework
.beans
.BeansException
;
38 import org
.springframework
.beans
.factory
.config
.BeanDefinition
;
39 import org
.springframework
.beans
.factory
.config
.ConfigurableListableBeanFactory
;
40 import org
.springframework
.context
.ApplicationContext
;
41 import org
.springframework
.context
.ApplicationContextAware
;
42 import org
.springframework
.context
.ConfigurableApplicationContext
;
43 import org
.springframework
.util
.Assert
;
44 import org
.springframework
.util
.StringUtils
;
47 * Performs conversion in both direction between data exchanged with the agent
48 * and the data in the application context.
50 public class DefaultExecutionFlowDescriptorConverter
implements
51 ExecutionFlowDescriptorConverter
, ApplicationContextAware
{
52 public final static String REF_VALUE_TYPE_BEAN_NAME
= "beanName";
54 /** Workaround for https://www.spartadn.com/bugzilla/show_bug.cgi?id=206 */
55 private final static String REF_VALUE_INTERNAL
= "[internal]";
57 private final static Log log
= LogFactory
58 .getLog(DefaultExecutionFlowDescriptorConverter
.class);
60 private ApplicationContext applicationContext
;
62 public Map
<String
, Object
> convertValues(
63 ExecutionFlowDescriptor executionFlowDescriptor
) {
64 Map
<String
, Object
> values
= executionFlowDescriptor
.getValues();
65 Map
<String
, Object
> convertedValues
= new HashMap
<String
, Object
>();
66 ExecutionSpec executionSpec
= executionFlowDescriptor
69 if (executionSpec
== null && log
.isTraceEnabled())
70 log
.warn("Execution spec is null for " + executionFlowDescriptor
);
72 if (values
!= null && executionSpec
!= null) {
73 values
: for (String key
: values
.keySet()) {
74 ExecutionSpecAttribute attribute
= executionSpec
75 .getAttributes().get(key
);
77 if (attribute
.getIsFrozen())
80 Object value
= values
.get(key
);
81 if (value
instanceof PrimitiveValue
) {
82 PrimitiveValue primitiveValue
= (PrimitiveValue
) value
;
83 // TODO: check class <=> type
84 convertedValues
.put(key
, primitiveValue
.getValue());
85 } else if (value
instanceof RefValue
) {
86 RefValue refValue
= (RefValue
) value
;
87 String type
= refValue
.getType();
88 if (REF_VALUE_TYPE_BEAN_NAME
.equals(type
)) {
89 // FIXME: UI should send all information about spec
92 // String executionSpecName = executionSpec.getName();
93 // ExecutionSpec localSpec = (ExecutionSpec)
95 // .getBean(executionSpecName);
96 // RefSpecAttribute localAttr = (RefSpecAttribute)
98 // .getAttributes().get(key);
99 // Class<?> targetClass = localAttr.getTargetClass();
101 // String primitiveType = PrimitiveUtils
102 // .classAsType(targetClass);
103 String primitiveType
= null;
104 if (primitiveType
!= null) {
106 String ref
= refValue
.getRef();
107 Object obj
= PrimitiveUtils
.convert(primitiveType
,
109 convertedValues
.put(key
, obj
);
111 String ref
= refValue
.getRef();
112 if (ref
!= null && !ref
.equals(REF_VALUE_INTERNAL
)) {
114 if (applicationContext
.containsBean(ref
)) {
115 obj
= applicationContext
.getBean(ref
);
117 // FIXME: hack in order to pass primitive
120 convertedValues
.put(key
, obj
);
122 log
.warn("Cannot interpret " + refValue
);
125 } else if (PrimitiveUtils
.typeAsClass(type
) != null) {
126 String ref
= refValue
.getRef();
127 Object obj
= PrimitiveUtils
.convert(type
, ref
);
128 convertedValues
.put(key
, obj
);
130 throw new UnsupportedException("Ref value type",
136 return convertedValues
;
139 public void addFlowsToDescriptor(ExecutionModuleDescriptor md
,
140 Map
<String
, ExecutionFlow
> executionFlows
) {
141 SortedSet
<ExecutionFlowDescriptor
> set
= new TreeSet
<ExecutionFlowDescriptor
>(
142 new ExecutionFlowDescriptorComparator());
143 for (String name
: executionFlows
.keySet()) {
144 ExecutionFlow executionFlow
= executionFlows
.get(name
);
146 ExecutionFlowDescriptor efd
= getExecutionFlowDescriptor(executionFlow
);
147 ExecutionSpec executionSpec
= efd
.getExecutionSpec();
149 // Add execution spec if necessary
150 if (!md
.getExecutionSpecs().contains(executionSpec
))
151 md
.getExecutionSpecs().add(executionSpec
);
153 // Add execution flow
155 // md.getExecutionFlows().add(efd);
157 md
.getExecutionFlows().addAll(set
);
160 public ExecutionFlowDescriptor
getExecutionFlowDescriptor(
161 ExecutionFlow executionFlow
) {
162 Assert
.notNull(executionFlow
.getName());
163 String name
= executionFlow
.getName();
165 ExecutionSpec executionSpec
= executionFlow
.getExecutionSpec();
166 Assert
.notNull(executionSpec
);
167 Assert
.notNull(executionSpec
.getName());
169 Map
<String
, Object
> values
= new TreeMap
<String
, Object
>();
170 for (String key
: executionSpec
.getAttributes().keySet()) {
171 ExecutionSpecAttribute attribute
= executionSpec
.getAttributes()
174 if (attribute
instanceof PrimitiveSpecAttribute
) {
175 if (executionFlow
.isSetAsParameter(key
)) {
176 Object value
= executionFlow
.getParameter(key
);
177 PrimitiveValue primitiveValue
= new PrimitiveValue();
178 primitiveValue
.setType(((PrimitiveSpecAttribute
) attribute
)
180 primitiveValue
.setValue(value
);
181 values
.put(key
, primitiveValue
);
183 // no need to add a primitive value if it is not set,
184 // all necessary information is in the spec
186 } else if (attribute
instanceof RefSpecAttribute
) {
187 if (attribute
.getIsFrozen()) {
188 values
.put(key
, new RefValue(REF_VALUE_INTERNAL
));
192 buildRefValue((RefSpecAttribute
) attribute
,
193 executionFlow
, key
));
195 throw new SlcException("Unkown spec attribute type "
196 + attribute
.getClass());
201 ExecutionFlowDescriptor efd
= new ExecutionFlowDescriptor(name
, values
,
203 if (executionFlow
.getPath() != null)
204 efd
.setPath(executionFlow
.getPath());
208 // Takes description from spring
209 BeanDefinition bd
= getBeanFactory().getBeanDefinition(name
);
210 efd
.setDescription(bd
.getDescription());
214 @SuppressWarnings(value
= { "unchecked" })
215 protected RefValue
buildRefValue(RefSpecAttribute rsa
,
216 ExecutionFlow executionFlow
, String key
) {
217 RefValue refValue
= new RefValue();
218 // FIXME: UI should be able to deal with other types
219 refValue
.setType(REF_VALUE_TYPE_BEAN_NAME
);
220 Class targetClass
= rsa
.getTargetClass();
221 String primitiveType
= PrimitiveUtils
.classAsType(targetClass
);
222 if (primitiveType
!= null) {
223 if (executionFlow
.isSetAsParameter(key
)) {
224 Object value
= executionFlow
.getParameter(key
);
225 refValue
.setRef(value
.toString());
227 refValue
.setType(primitiveType
);
231 if (executionFlow
.isSetAsParameter(key
)) {
233 Object value
= executionFlow
.getParameter(key
);
234 if (applicationContext
== null) {
235 log
.warn("No application context declared, cannot scan ref value.");
236 ref
= value
.toString();
239 // look for a ref to the value
240 Map
<String
, Object
> beans
= getBeanFactory()
241 .getBeansOfType(targetClass
, false, false);
242 // TODO: also check scoped beans
243 beans
: for (String beanName
: beans
.keySet()) {
244 Object obj
= beans
.get(beanName
);
245 if (value
instanceof ScopedObject
) {
246 // don't call methods of the target of the scope
247 if (obj
instanceof ScopedObject
)
253 if (obj
.equals(value
)) {
261 log
.warn("Cannot define reference for ref spec attribute "
262 + key
+ " in " + executionFlow
+ " (" + rsa
+ ")."
263 + " If it is an inner bean consider put it frozen.");
264 ref
= REF_VALUE_INTERNAL
;
266 if (log
.isTraceEnabled())
268 + " is the reference for ref spec attribute "
269 + key
+ " in " + executionFlow
+ " (" + rsa
272 refValue
.setRef(ref
);
278 private ConfigurableListableBeanFactory
getBeanFactory() {
279 return ((ConfigurableApplicationContext
) applicationContext
)
283 /** Must be use within the execution application context */
284 public void setApplicationContext(ApplicationContext applicationContext
)
285 throws BeansException
{
286 this.applicationContext
= applicationContext
;
289 private static class ExecutionFlowDescriptorComparator
implements
290 Comparator
<ExecutionFlowDescriptor
> {
291 public int compare(ExecutionFlowDescriptor o1
,
292 ExecutionFlowDescriptor o2
) {
293 // TODO: write unit tests for this
295 String name1
= o1
.getName();
296 String name2
= o2
.getName();
298 String path1
= o1
.getPath();
299 String path2
= o2
.getPath();
301 // Check whether name include path
302 int lastIndex1
= name1
.lastIndexOf('/');
303 // log.debug(name1+", "+lastIndex1);
304 if (!StringUtils
.hasText(path1
) && lastIndex1
>= 0) {
305 path1
= name1
.substring(0, lastIndex1
);
306 name1
= name1
.substring(lastIndex1
+ 1);
309 int lastIndex2
= name2
.lastIndexOf('/');
310 if (!StringUtils
.hasText(path2
) && lastIndex2
>= 0) {
311 path2
= name2
.substring(0, lastIndex2
);
312 name2
= name2
.substring(lastIndex2
+ 1);
315 // Perform the actual comparison
316 if (StringUtils
.hasText(path1
) && StringUtils
.hasText(path2
)) {
317 if (path1
.equals(path2
))
318 return name1
.compareTo(name2
);
319 else if (path1
.startsWith(path2
))
321 else if (path2
.startsWith(path1
))
324 return path1
.compareTo(path2
);
325 } else if (!StringUtils
.hasText(path1
)
326 && StringUtils
.hasText(path2
)) {
328 } else if (StringUtils
.hasText(path1
)
329 && !StringUtils
.hasText(path2
)) {
331 } else if (!StringUtils
.hasText(path1
)
332 && !StringUtils
.hasText(path2
)) {
333 return name1
.compareTo(name2
);