]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlowDescriptorConverter.java
Fix issue related to serialization, old spring template files and logging
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / DefaultExecutionFlowDescriptorConverter.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.core.execution;
18
19 import java.util.Comparator;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.SortedSet;
23 import java.util.TreeMap;
24 import java.util.TreeSet;
25
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;
45
46 /**
47 * Performs conversion in both direction between data exchanged with the agent
48 * and the data in the application context.
49 */
50 public class DefaultExecutionFlowDescriptorConverter implements
51 ExecutionFlowDescriptorConverter, ApplicationContextAware {
52 public final static String REF_VALUE_TYPE_BEAN_NAME = "beanName";
53
54 /** Workaround for https://www.spartadn.com/bugzilla/show_bug.cgi?id=206 */
55 private final static String REF_VALUE_INTERNAL = "[internal]";
56
57 private final static Log log = LogFactory
58 .getLog(DefaultExecutionFlowDescriptorConverter.class);
59
60 private ApplicationContext applicationContext;
61
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
67 .getExecutionSpec();
68
69 if (executionSpec == null && log.isTraceEnabled())
70 log.warn("Execution spec is null for " + executionFlowDescriptor);
71
72 if (values != null && executionSpec != null) {
73 values: for (String key : values.keySet()) {
74 ExecutionSpecAttribute attribute = executionSpec
75 .getAttributes().get(key);
76
77 if (attribute.getIsFrozen())
78 continue values;
79
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
90 // - targetClass
91 // - name
92 // String executionSpecName = executionSpec.getName();
93 // ExecutionSpec localSpec = (ExecutionSpec)
94 // applicationContext
95 // .getBean(executionSpecName);
96 // RefSpecAttribute localAttr = (RefSpecAttribute)
97 // localSpec
98 // .getAttributes().get(key);
99 // Class<?> targetClass = localAttr.getTargetClass();
100 //
101 // String primitiveType = PrimitiveUtils
102 // .classAsType(targetClass);
103 String primitiveType = null;
104 if (primitiveType != null) {
105 // not active
106 String ref = refValue.getRef();
107 Object obj = PrimitiveUtils.convert(primitiveType,
108 ref);
109 convertedValues.put(key, obj);
110 } else {
111 String ref = refValue.getRef();
112 if (ref != null && !ref.equals(REF_VALUE_INTERNAL)) {
113 Object obj = null;
114 if (applicationContext.containsBean(ref)) {
115 obj = applicationContext.getBean(ref);
116 } else {
117 // FIXME: hack in order to pass primitive
118 obj = ref;
119 }
120 convertedValues.put(key, obj);
121 } else {
122 log.warn("Cannot interpret " + refValue);
123 }
124 }
125 } else if (PrimitiveUtils.typeAsClass(type) != null) {
126 String ref = refValue.getRef();
127 Object obj = PrimitiveUtils.convert(type, ref);
128 convertedValues.put(key, obj);
129 } else {
130 throw new UnsupportedException("Ref value type",
131 refValue.getType());
132 }
133 }
134 }
135 }
136 return convertedValues;
137 }
138
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);
145
146 Assert.notNull(executionFlow.getName());
147 Assert.state(name.equals(executionFlow.getName()));
148
149 ExecutionSpec executionSpec = executionFlow.getExecutionSpec();
150 Assert.notNull(executionSpec);
151 Assert.notNull(executionSpec.getName());
152
153 Map<String, Object> values = new TreeMap<String, Object>();
154 for (String key : executionSpec.getAttributes().keySet()) {
155 ExecutionSpecAttribute attribute = executionSpec
156 .getAttributes().get(key);
157
158 if (attribute instanceof PrimitiveSpecAttribute) {
159 if (executionFlow.isSetAsParameter(key)) {
160 Object value = executionFlow.getParameter(key);
161 PrimitiveValue primitiveValue = new PrimitiveValue();
162 primitiveValue
163 .setType(((PrimitiveSpecAttribute) attribute)
164 .getType());
165 primitiveValue.setValue(value);
166 values.put(key, primitiveValue);
167 } else {
168 // no need to add a primitive value if it is not set,
169 // all necessary information is in the spec
170 }
171 } else if (attribute instanceof RefSpecAttribute) {
172 if (attribute.getIsFrozen()) {
173 values.put(key, new RefValue(REF_VALUE_INTERNAL));
174 } else
175 values.put(key, buildRefValue(
176 (RefSpecAttribute) attribute, executionFlow,
177 key));
178 } else {
179 throw new SlcException("Unkown spec attribute type "
180 + attribute.getClass());
181 }
182
183 }
184
185 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(name,
186 values, executionSpec);
187 if (executionFlow.getPath() != null)
188 efd.setPath(executionFlow.getPath());
189 else
190 efd.setPath("");
191
192 // Takes description from spring
193 BeanDefinition bd = getBeanFactory().getBeanDefinition(name);
194 efd.setDescription(bd.getDescription());
195
196 // Add execution spec if necessary
197 if (!md.getExecutionSpecs().contains(executionSpec))
198 md.getExecutionSpecs().add(executionSpec);
199
200 // Add execution flow
201 set.add(efd);
202 // md.getExecutionFlows().add(efd);
203 }
204 md.getExecutionFlows().addAll(set);
205 }
206
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());
219 }
220 refValue.setType(primitiveType);
221 return refValue;
222 } else {
223
224 if (executionFlow.isSetAsParameter(key)) {
225 String ref = null;
226 Object value = executionFlow.getParameter(key);
227 if (applicationContext == null) {
228 log
229 .warn("No application context declared, cannot scan ref value.");
230 ref = value.toString();
231 } else {
232
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)
242 if (value == obj) {
243 ref = beanName;
244 break beans;
245 }
246 } else {
247 if (obj.equals(value)) {
248 ref = beanName;
249 break beans;
250 }
251 }
252 }
253 }
254 if (ref == null) {
255 log
256 .warn("Cannot define reference for ref spec attribute "
257 + key
258 + " in "
259 + executionFlow
260 + " ("
261 + rsa
262 + ")."
263 + " If it is an inner bean consider put it frozen.");
264 ref = REF_VALUE_INTERNAL;
265 } else {
266 if (log.isTraceEnabled())
267 log.trace(ref
268 + " is the reference for ref spec attribute "
269 + key + " in " + executionFlow + " (" + rsa
270 + ")");
271 }
272 refValue.setRef(ref);
273 }
274 return refValue;
275 }
276 }
277
278 private ConfigurableListableBeanFactory getBeanFactory() {
279 return ((ConfigurableApplicationContext) applicationContext)
280 .getBeanFactory();
281 }
282
283 /** Must be use within the execution application context */
284 public void setApplicationContext(ApplicationContext applicationContext)
285 throws BeansException {
286 this.applicationContext = applicationContext;
287 }
288
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
294
295 String name1 = o1.getName();
296 String name2 = o2.getName();
297
298 String path1 = o1.getPath();
299 String path2 = o2.getPath();
300
301 // Check whether name include path
302 int lastIndex1 = name1.lastIndexOf('/');
303 if (!StringUtils.hasText(path1) && lastIndex1 >= 0) {
304 name1 = name1.substring(lastIndex1 + 1);
305 path1 = name1.substring(0, lastIndex1);
306 }
307
308 int lastIndex2 = name2.lastIndexOf('/');
309 if (!StringUtils.hasText(path2) && lastIndex2 >= 0) {
310 name2 = name2.substring(lastIndex2 + 1);
311 path2 = name2.substring(0, lastIndex2);
312 }
313
314 // Perform the actual comparison
315 if (StringUtils.hasText(path1) && StringUtils.hasText(path2)) {
316 if (path1.equals(path2))
317 return name1.compareTo(name2);
318 else if (path1.startsWith(path2))
319 return -1;
320 else if (path2.startsWith(path1))
321 return 1;
322 else
323 return path1.compareTo(path2);
324 } else if (!StringUtils.hasText(path1)
325 && StringUtils.hasText(path2)) {
326 return 1;
327 } else if (StringUtils.hasText(path1)
328 && !StringUtils.hasText(path2)) {
329 return -1;
330 } else if (!StringUtils.hasText(path1)
331 && !StringUtils.hasText(path2)) {
332 return name1.compareTo(name2);
333 } else {
334 return 0;
335 }
336 }
337
338 }
339 }