]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlowDescriptorConverter.java
Use commons 0.3.3
[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 == null)
78 throw new SlcException("No spec attribute defined for '"
79 + key + "'");
80
81 if (attribute.getIsFrozen())
82 continue values;
83
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
94 // - targetClass
95 // - name
96 // String executionSpecName = executionSpec.getName();
97 // ExecutionSpec localSpec = (ExecutionSpec)
98 // applicationContext
99 // .getBean(executionSpecName);
100 // RefSpecAttribute localAttr = (RefSpecAttribute)
101 // localSpec
102 // .getAttributes().get(key);
103 // Class<?> targetClass = localAttr.getTargetClass();
104 //
105 // String primitiveType = PrimitiveUtils
106 // .classAsType(targetClass);
107 String primitiveType = null;
108 if (primitiveType != null) {
109 // not active
110 String ref = refValue.getRef();
111 Object obj = PrimitiveUtils.convert(primitiveType,
112 ref);
113 convertedValues.put(key, obj);
114 } else {
115 String ref = refValue.getRef();
116 if (ref != null && !ref.equals(REF_VALUE_INTERNAL)) {
117 Object obj = null;
118 if (applicationContext.containsBean(ref)) {
119 obj = applicationContext.getBean(ref);
120 } else {
121 // FIXME: hack in order to pass primitive
122 obj = ref;
123 }
124 convertedValues.put(key, obj);
125 } else {
126 log.warn("Cannot interpret " + refValue);
127 }
128 }
129 } else if (PrimitiveUtils.typeAsClass(type) != null) {
130 String ref = refValue.getRef();
131 Object obj = PrimitiveUtils.convert(type, ref);
132 convertedValues.put(key, obj);
133 } else {
134 throw new UnsupportedException("Ref value type",
135 refValue.getType());
136 }
137 } else {
138 convertedValues.put(key, value);
139 }
140 }
141 }
142 return convertedValues;
143 }
144
145 public void addFlowsToDescriptor(ExecutionModuleDescriptor md,
146 Map<String, ExecutionFlow> executionFlows) {
147 SortedSet<ExecutionFlowDescriptor> set = new TreeSet<ExecutionFlowDescriptor>(
148 new ExecutionFlowDescriptorComparator());
149 for (String name : executionFlows.keySet()) {
150 ExecutionFlow executionFlow = executionFlows.get(name);
151
152 ExecutionFlowDescriptor efd = getExecutionFlowDescriptor(executionFlow);
153 ExecutionSpec executionSpec = efd.getExecutionSpec();
154
155 // Add execution spec if necessary
156 if (!md.getExecutionSpecs().contains(executionSpec))
157 md.getExecutionSpecs().add(executionSpec);
158
159 // Add execution flow
160 set.add(efd);
161 // md.getExecutionFlows().add(efd);
162 }
163 md.getExecutionFlows().addAll(set);
164 }
165
166 public ExecutionFlowDescriptor getExecutionFlowDescriptor(
167 ExecutionFlow executionFlow) {
168 Assert.notNull(executionFlow.getName());
169 String name = executionFlow.getName();
170
171 ExecutionSpec executionSpec = executionFlow.getExecutionSpec();
172 Assert.notNull(executionSpec);
173 Assert.notNull(executionSpec.getName());
174
175 Map<String, Object> values = new TreeMap<String, Object>();
176 for (String key : executionSpec.getAttributes().keySet()) {
177 ExecutionSpecAttribute attribute = executionSpec.getAttributes()
178 .get(key);
179
180 if (attribute instanceof PrimitiveSpecAttribute) {
181 if (executionFlow.isSetAsParameter(key)) {
182 Object value = executionFlow.getParameter(key);
183 PrimitiveValue primitiveValue = new PrimitiveValue();
184 primitiveValue.setType(((PrimitiveSpecAttribute) attribute)
185 .getType());
186 primitiveValue.setValue(value);
187 values.put(key, primitiveValue);
188 } else {
189 // no need to add a primitive value if it is not set,
190 // all necessary information is in the spec
191 }
192 } else if (attribute instanceof RefSpecAttribute) {
193 if (attribute.getIsFrozen()) {
194 values.put(key, new RefValue(REF_VALUE_INTERNAL));
195 } else
196 values.put(
197 key,
198 buildRefValue((RefSpecAttribute) attribute,
199 executionFlow, key));
200 } else {
201 throw new SlcException("Unkown spec attribute type "
202 + attribute.getClass());
203 }
204
205 }
206
207 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(name, values,
208 executionSpec);
209 if (executionFlow.getPath() != null)
210 efd.setPath(executionFlow.getPath());
211 else
212 efd.setPath("");
213
214 // Takes description from spring
215 BeanDefinition bd = getBeanFactory().getBeanDefinition(name);
216 efd.setDescription(bd.getDescription());
217 return efd;
218 }
219
220 @SuppressWarnings(value = { "unchecked" })
221 protected RefValue buildRefValue(RefSpecAttribute rsa,
222 ExecutionFlow executionFlow, String key) {
223 RefValue refValue = new RefValue();
224 // FIXME: UI should be able to deal with other types
225 refValue.setType(REF_VALUE_TYPE_BEAN_NAME);
226 Class targetClass = rsa.getTargetClass();
227 String primitiveType = PrimitiveUtils.classAsType(targetClass);
228 if (primitiveType != null) {
229 if (executionFlow.isSetAsParameter(key)) {
230 Object value = executionFlow.getParameter(key);
231 refValue.setRef(value.toString());
232 }
233 refValue.setType(primitiveType);
234 return refValue;
235 } else {
236
237 if (executionFlow.isSetAsParameter(key)) {
238 String ref = null;
239 Object value = executionFlow.getParameter(key);
240 if (applicationContext == null) {
241 log.warn("No application context declared, cannot scan ref value.");
242 ref = value.toString();
243 } else {
244
245 // look for a ref to the value
246 Map<String, Object> beans = getBeanFactory()
247 .getBeansOfType(targetClass, false, false);
248 // TODO: also check scoped beans
249 beans: for (String beanName : beans.keySet()) {
250 Object obj = beans.get(beanName);
251 if (value instanceof ScopedObject) {
252 // don't call methods of the target of the scope
253 if (obj instanceof ScopedObject)
254 if (value == obj) {
255 ref = beanName;
256 break beans;
257 }
258 } else {
259 if (obj.equals(value)) {
260 ref = beanName;
261 break beans;
262 }
263 }
264 }
265 }
266 if (ref == null) {
267 log.warn("Cannot define reference for ref spec attribute "
268 + key + " in " + executionFlow + " (" + rsa + ")."
269 + " If it is an inner bean consider put it frozen.");
270 ref = REF_VALUE_INTERNAL;
271 } else {
272 if (log.isTraceEnabled())
273 log.trace(ref
274 + " is the reference for ref spec attribute "
275 + key + " in " + executionFlow + " (" + rsa
276 + ")");
277 }
278 refValue.setRef(ref);
279 }
280 return refValue;
281 }
282 }
283
284 private ConfigurableListableBeanFactory getBeanFactory() {
285 return ((ConfigurableApplicationContext) applicationContext)
286 .getBeanFactory();
287 }
288
289 /** Must be use within the execution application context */
290 public void setApplicationContext(ApplicationContext applicationContext)
291 throws BeansException {
292 this.applicationContext = applicationContext;
293 }
294
295 private static class ExecutionFlowDescriptorComparator implements
296 Comparator<ExecutionFlowDescriptor> {
297 public int compare(ExecutionFlowDescriptor o1,
298 ExecutionFlowDescriptor o2) {
299 // TODO: write unit tests for this
300
301 String name1 = o1.getName();
302 String name2 = o2.getName();
303
304 String path1 = o1.getPath();
305 String path2 = o2.getPath();
306
307 // Check whether name include path
308 int lastIndex1 = name1.lastIndexOf('/');
309 // log.debug(name1+", "+lastIndex1);
310 if (!StringUtils.hasText(path1) && lastIndex1 >= 0) {
311 path1 = name1.substring(0, lastIndex1);
312 name1 = name1.substring(lastIndex1 + 1);
313 }
314
315 int lastIndex2 = name2.lastIndexOf('/');
316 if (!StringUtils.hasText(path2) && lastIndex2 >= 0) {
317 path2 = name2.substring(0, lastIndex2);
318 name2 = name2.substring(lastIndex2 + 1);
319 }
320
321 // Perform the actual comparison
322 if (StringUtils.hasText(path1) && StringUtils.hasText(path2)) {
323 if (path1.equals(path2))
324 return name1.compareTo(name2);
325 else if (path1.startsWith(path2))
326 return -1;
327 else if (path2.startsWith(path1))
328 return 1;
329 else
330 return path1.compareTo(path2);
331 } else if (!StringUtils.hasText(path1)
332 && StringUtils.hasText(path2)) {
333 return 1;
334 } else if (StringUtils.hasText(path1)
335 && !StringUtils.hasText(path2)) {
336 return -1;
337 } else if (!StringUtils.hasText(path1)
338 && !StringUtils.hasText(path2)) {
339 return name1.compareTo(name2);
340 } else {
341 return 0;
342 }
343 }
344
345 }
346 }