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 Assert
.notNull(executionFlow
.getName());
147 Assert
.state(name
.equals(executionFlow
.getName()));
149 ExecutionSpec executionSpec
= executionFlow
.getExecutionSpec();
150 Assert
.notNull(executionSpec
);
151 Assert
.notNull(executionSpec
.getName());
153 Map
<String
, Object
> values
= new TreeMap
<String
, Object
>();
154 for (String key
: executionSpec
.getAttributes().keySet()) {
155 ExecutionSpecAttribute attribute
= executionSpec
156 .getAttributes().get(key
);
158 if (attribute
instanceof PrimitiveSpecAttribute
) {
159 if (executionFlow
.isSetAsParameter(key
)) {
160 Object value
= executionFlow
.getParameter(key
);
161 PrimitiveValue primitiveValue
= new PrimitiveValue();
163 .setType(((PrimitiveSpecAttribute
) attribute
)
165 primitiveValue
.setValue(value
);
166 values
.put(key
, primitiveValue
);
168 // no need to add a primitive value if it is not set,
169 // all necessary information is in the spec
171 } else if (attribute
instanceof RefSpecAttribute
) {
172 if (attribute
.getIsFrozen()) {
173 values
.put(key
, new RefValue(REF_VALUE_INTERNAL
));
175 values
.put(key
, buildRefValue(
176 (RefSpecAttribute
) attribute
, executionFlow
,
179 throw new SlcException("Unkown spec attribute type "
180 + attribute
.getClass());
185 ExecutionFlowDescriptor efd
= new ExecutionFlowDescriptor(name
,
186 values
, executionSpec
);
187 if (executionFlow
.getPath() != null)
188 efd
.setPath(executionFlow
.getPath());
192 // Takes description from spring
193 BeanDefinition bd
= getBeanFactory().getBeanDefinition(name
);
194 efd
.setDescription(bd
.getDescription());
196 // Add execution spec if necessary
197 if (!md
.getExecutionSpecs().contains(executionSpec
))
198 md
.getExecutionSpecs().add(executionSpec
);
200 // Add execution flow
202 // md.getExecutionFlows().add(efd);
204 md
.getExecutionFlows().addAll(set
);
207 @SuppressWarnings(value
= { "unchecked" })
208 protected RefValue
buildRefValue(RefSpecAttribute rsa
,
209 ExecutionFlow executionFlow
, String key
) {
210 RefValue refValue
= new RefValue();
211 // FIXME: UI should be able to deal with other types
212 refValue
.setType(REF_VALUE_TYPE_BEAN_NAME
);
213 Class targetClass
= rsa
.getTargetClass();
214 String primitiveType
= PrimitiveUtils
.classAsType(targetClass
);
215 if (primitiveType
!= null) {
216 if (executionFlow
.isSetAsParameter(key
)) {
217 Object value
= executionFlow
.getParameter(key
);
218 refValue
.setRef(value
.toString());
220 refValue
.setType(primitiveType
);
224 if (executionFlow
.isSetAsParameter(key
)) {
226 Object value
= executionFlow
.getParameter(key
);
227 if (applicationContext
== null) {
229 .warn("No application context declared, cannot scan ref value.");
230 ref
= value
.toString();
233 // look for a ref to the value
234 Map
<String
, Object
> beans
= getBeanFactory()
235 .getBeansOfType(targetClass
, false, false);
236 // TODO: also check scoped beans
237 beans
: for (String beanName
: beans
.keySet()) {
238 Object obj
= beans
.get(beanName
);
239 if (value
instanceof ScopedObject
) {
240 // don't call methods of the target of the scope
241 if (obj
instanceof ScopedObject
)
247 if (obj
.equals(value
)) {
256 .warn("Cannot define reference for ref spec attribute "
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
);