2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.Comparator
;
19 import java
.util
.HashMap
;
21 import java
.util
.SortedSet
;
22 import java
.util
.TreeMap
;
23 import java
.util
.TreeSet
;
25 import org
.apache
.commons
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.argeo
.slc
.SlcException
;
28 import org
.argeo
.slc
.UnsupportedException
;
29 import org
.argeo
.slc
.execution
.ExecutionFlow
;
30 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptor
;
31 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptorConverter
;
32 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
33 import org
.argeo
.slc
.execution
.ExecutionSpec
;
34 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
35 import org
.springframework
.aop
.scope
.ScopedObject
;
36 import org
.springframework
.beans
.BeansException
;
37 import org
.springframework
.beans
.factory
.BeanFactory
;
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
.StringUtils
;
46 * Performs conversion in both direction between data exchanged with the agent
47 * and the data in the application context.
49 public class DefaultExecutionFlowDescriptorConverter
implements
50 ExecutionFlowDescriptorConverter
, ApplicationContextAware
{
51 public final static String REF_VALUE_TYPE_BEAN_NAME
= "beanName";
53 /** Workaround for https://www.spartadn.com/bugzilla/show_bug.cgi?id=206 */
54 private final static String REF_VALUE_INTERNAL
= "[internal]";
56 private final static Log log
= LogFactory
57 .getLog(DefaultExecutionFlowDescriptorConverter
.class);
59 private ApplicationContext applicationContext
;
61 @SuppressWarnings("unused")
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
== null)
78 throw new SlcException("No spec attribute defined for '"
81 if (attribute
.getIsConstant())
84 Object value
= values
.get(key
);
85 if (value
instanceof PrimitiveValue
) {
86 PrimitiveValue primitiveValue
= (PrimitiveValue
) value
;
87 // TODO: check class <=> type
88 convertedValues
.put(key
, primitiveValue
.getValue());
89 } else if (value
instanceof RefValue
) {
90 RefValue refValue
= (RefValue
) value
;
91 String type
= refValue
.getType();
92 if (REF_VALUE_TYPE_BEAN_NAME
.equals(type
)) {
93 // FIXME: UI should send all information about spec
96 // String executionSpecName = executionSpec.getName();
97 // ExecutionSpec localSpec = (ExecutionSpec)
99 // .getBean(executionSpecName);
100 // RefSpecAttribute localAttr = (RefSpecAttribute)
102 // .getAttributes().get(key);
103 // Class<?> targetClass = localAttr.getTargetClass();
105 // String primitiveType = PrimitiveUtils
106 // .classAsType(targetClass);
107 String primitiveType
= null;
108 if (primitiveType
!= null) {
110 String ref
= refValue
.getRef();
111 Object obj
= PrimitiveUtils
.convert(primitiveType
,
113 convertedValues
.put(key
, obj
);
115 String ref
= refValue
.getRef();
116 if (ref
!= null && !ref
.equals(REF_VALUE_INTERNAL
)) {
118 if (applicationContext
.containsBean(ref
)) {
119 obj
= applicationContext
.getBean(ref
);
121 // FIXME: hack in order to pass primitive
124 convertedValues
.put(key
, obj
);
126 log
.warn("Cannot interpret " + refValue
);
129 } else if (PrimitiveUtils
.typeAsClass(type
) != null) {
130 String ref
= refValue
.getRef();
131 Object obj
= PrimitiveUtils
.convert(type
, ref
);
132 convertedValues
.put(key
, obj
);
134 throw new UnsupportedException("Ref value type",
138 // default is to take the value as is
139 convertedValues
.put(key
, value
);
143 return convertedValues
;
146 public void addFlowsToDescriptor(ExecutionModuleDescriptor md
,
147 Map
<String
, ExecutionFlow
> executionFlows
) {
148 SortedSet
<ExecutionFlowDescriptor
> set
= new TreeSet
<ExecutionFlowDescriptor
>(
149 new ExecutionFlowDescriptorComparator());
150 for (String name
: executionFlows
.keySet()) {
151 ExecutionFlow executionFlow
= executionFlows
.get(name
);
153 ExecutionFlowDescriptor efd
= getExecutionFlowDescriptor(executionFlow
);
154 ExecutionSpec executionSpec
= efd
.getExecutionSpec();
156 // Add execution spec if necessary
157 if (!md
.getExecutionSpecs().contains(executionSpec
))
158 md
.getExecutionSpecs().add(executionSpec
);
160 // Add execution flow
162 // md.getExecutionFlows().add(efd);
164 md
.getExecutionFlows().addAll(set
);
167 @SuppressWarnings("deprecation")
168 public ExecutionFlowDescriptor
getExecutionFlowDescriptor(
169 ExecutionFlow executionFlow
) {
170 if (executionFlow
.getName() == null)
171 throw new SlcException("Flow name is null: " + executionFlow
);
172 String name
= executionFlow
.getName();
174 ExecutionSpec executionSpec
= executionFlow
.getExecutionSpec();
175 if (executionSpec
== null)
176 throw new SlcException("Execution spec is null: " + executionFlow
);
177 if (executionSpec
.getName() == null)
178 throw new SlcException("Execution spec name is null: "
181 Map
<String
, Object
> values
= new TreeMap
<String
, Object
>();
182 for (String key
: executionSpec
.getAttributes().keySet()) {
183 ExecutionSpecAttribute attribute
= executionSpec
.getAttributes()
186 if (attribute
instanceof PrimitiveSpecAttribute
) {
187 if (executionFlow
.isSetAsParameter(key
)) {
188 Object value
= executionFlow
.getParameter(key
);
189 PrimitiveValue primitiveValue
= new PrimitiveValue();
190 primitiveValue
.setType(((PrimitiveSpecAttribute
) attribute
)
192 primitiveValue
.setValue(value
);
193 values
.put(key
, primitiveValue
);
195 // no need to add a primitive value if it is not set,
196 // all necessary information is in the spec
198 } else if (attribute
instanceof RefSpecAttribute
) {
199 if (attribute
.getIsConstant()) {
200 values
.put(key
, new RefValue(REF_VALUE_INTERNAL
));
204 buildRefValue((RefSpecAttribute
) attribute
,
205 executionFlow
, key
));
207 throw new SlcException("Unkown spec attribute type "
208 + attribute
.getClass());
213 ExecutionFlowDescriptor efd
= new ExecutionFlowDescriptor(name
, values
,
215 if (executionFlow
.getPath() != null)
216 efd
.setPath(executionFlow
.getPath());
220 // Takes description from spring
221 BeanFactory bf
= getBeanFactory();
223 BeanDefinition bd
= getBeanFactory().getBeanDefinition(name
);
224 efd
.setDescription(bd
.getDescription());
229 @SuppressWarnings(value
= { "unchecked" })
230 protected RefValue
buildRefValue(RefSpecAttribute rsa
,
231 ExecutionFlow executionFlow
, String key
) {
232 RefValue refValue
= new RefValue();
233 // FIXME: UI should be able to deal with other types
234 refValue
.setType(REF_VALUE_TYPE_BEAN_NAME
);
235 Class
<?
> targetClass
= rsa
.getTargetClass();
236 String primitiveType
= PrimitiveUtils
.classAsType(targetClass
);
237 if (primitiveType
!= null) {
238 if (executionFlow
.isSetAsParameter(key
)) {
239 Object value
= executionFlow
.getParameter(key
);
240 refValue
.setRef(value
.toString());
242 refValue
.setType(primitiveType
);
246 if (executionFlow
.isSetAsParameter(key
)) {
248 Object value
= executionFlow
.getParameter(key
);
249 if (applicationContext
== null) {
250 log
.warn("No application context declared, cannot scan ref value.");
251 ref
= value
.toString();
254 // look for a ref to the value
255 Map
<String
, Object
> beans
= getBeanFactory()
256 .getBeansOfType(targetClass
, false, false);
257 // TODO: also check scoped beans
258 beans
: for (String beanName
: beans
.keySet()) {
259 Object obj
= beans
.get(beanName
);
260 if (value
instanceof ScopedObject
) {
261 // don't call methods of the target of the scope
262 if (obj
instanceof ScopedObject
)
268 if (obj
.equals(value
)) {
276 if (log
.isTraceEnabled())
277 log
.trace("Cannot define reference for ref spec attribute "
284 + " If it is an inner bean consider put it frozen.");
285 ref
= REF_VALUE_INTERNAL
;
287 if (log
.isTraceEnabled())
289 + " is the reference for ref spec attribute "
290 + key
+ " in " + executionFlow
+ " (" + rsa
293 refValue
.setRef(ref
);
299 /** @return can be null */
300 private ConfigurableListableBeanFactory
getBeanFactory() {
301 if (applicationContext
== null)
303 return ((ConfigurableApplicationContext
) applicationContext
)
307 /** Must be use within the execution application context */
308 public void setApplicationContext(ApplicationContext applicationContext
)
309 throws BeansException
{
310 this.applicationContext
= applicationContext
;
313 private static class ExecutionFlowDescriptorComparator
implements
314 Comparator
<ExecutionFlowDescriptor
> {
315 @SuppressWarnings("deprecation")
316 public int compare(ExecutionFlowDescriptor o1
,
317 ExecutionFlowDescriptor o2
) {
318 // TODO: write unit tests for this
320 String name1
= o1
.getName();
321 String name2
= o2
.getName();
323 String path1
= o1
.getPath();
324 String path2
= o2
.getPath();
326 // Check whether name include path
327 int lastIndex1
= name1
.lastIndexOf('/');
328 // log.debug(name1+", "+lastIndex1);
329 if (!StringUtils
.hasText(path1
) && lastIndex1
>= 0) {
330 path1
= name1
.substring(0, lastIndex1
);
331 name1
= name1
.substring(lastIndex1
+ 1);
334 int lastIndex2
= name2
.lastIndexOf('/');
335 if (!StringUtils
.hasText(path2
) && lastIndex2
>= 0) {
336 path2
= name2
.substring(0, lastIndex2
);
337 name2
= name2
.substring(lastIndex2
+ 1);
340 // Perform the actual comparison
341 if (StringUtils
.hasText(path1
) && StringUtils
.hasText(path2
)) {
342 if (path1
.equals(path2
))
343 return name1
.compareTo(name2
);
344 else if (path1
.startsWith(path2
))
346 else if (path2
.startsWith(path1
))
349 return path1
.compareTo(path2
);
350 } else if (!StringUtils
.hasText(path1
)
351 && StringUtils
.hasText(path2
)) {
353 } else if (StringUtils
.hasText(path1
)
354 && !StringUtils
.hasText(path2
)) {
356 } else if (!StringUtils
.hasText(path1
)
357 && !StringUtils
.hasText(path2
)) {
358 return name1
.compareTo(name2
);