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 org
.argeo
.slc
.execution
.ExecutionModule
;
22 public abstract class AbstractSpringExecutionModule
implements ExecutionModule
25 protected ApplicationContext applicationContext;
27 protected ExecutionContext executionContext;
29 protected ExecutionFlowDescriptorConverter descriptorConverter = new DefaultDescriptorConverter();
31 public ExecutionModuleDescriptor getDescriptor() {
32 ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
33 md.setName(getName());
34 md.setVersion(getVersion());
36 Map<String, ExecutionFlow> executionFlows = listFlows();
37 for (String name : executionFlows.keySet()) {
38 ExecutionFlow executionFlow = executionFlows.get(name);
40 Assert.notNull(executionFlow.getName());
41 Assert.state(name.equals(executionFlow.getName()));
43 ExecutionSpec executionSpec = executionFlow.getExecutionSpec();
44 Assert.notNull(executionSpec);
45 Assert.notNull(executionSpec.getName());
47 Map<String, Object> values = new TreeMap<String, Object>();
48 for (String key : executionSpec.getAttributes().keySet()) {
49 ExecutionSpecAttribute attribute = executionSpec
50 .getAttributes().get(key);
52 if (executionFlow.isSetAsParameter(key)) {
53 Object value = executionFlow.getParameter(key);
54 if (attribute instanceof PrimitiveSpecAttribute) {
55 PrimitiveValue primitiveValue = new PrimitiveValue();
57 .setType(((PrimitiveSpecAttribute) attribute)
59 primitiveValue.setValue(value);
60 values.put(key, primitiveValue);
61 } else if (attribute instanceof RefSpecAttribute) {
62 RefValue refValue = new RefValue();
63 if (value instanceof ScopedObject) {
64 refValue.setLabel("RUNTIME "
65 + value.getClass().getName());
67 refValue.setLabel("STATIC "
68 + value.getClass().getName());
70 values.put(key, refValue);
71 } else if (attribute instanceof ResourceSpecAttribute) {
72 PrimitiveValue primitiveValue = new PrimitiveValue();
74 .setType(((ResourceSpecAttribute) attribute)
76 primitiveValue.setValue(value);
77 values.put(key, primitiveValue);
79 throw new SlcException("Unkown spec attribute type "
80 + attribute.getClass());
86 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(name,
87 values, executionSpec);
88 if (executionFlow.getPath() != null)
89 efd.setPath(executionFlow.getPath());
91 // Add execution spec if necessary
92 if (!md.getExecutionSpecs().contains(executionSpec))
93 md.getExecutionSpecs().add(executionSpec);
96 md.getExecutionFlows().add(efd);
102 protected Map<String, ExecutionFlow> listFlows() {
103 GenericBeanFactoryAccessor accessor = new GenericBeanFactoryAccessor(
105 Map<String, ExecutionFlow> executionFlows = accessor
106 .getBeansOfType(ExecutionFlow.class);
107 return executionFlows;
110 public void execute(ExecutionFlowDescriptor executionFlowDescriptor) {
111 if (descriptorConverter != null)
112 executionContext.addVariables(descriptorConverter
113 .convertValues(executionFlowDescriptor));
114 ExecutionFlow flow = (ExecutionFlow) applicationContext.getBean(
115 executionFlowDescriptor.getName(), ExecutionFlow.class);
119 public void setApplicationContext(ApplicationContext applicationContext)
120 throws BeansException {
121 this.applicationContext = applicationContext;
124 public void setExecutionContext(ExecutionContext executionContext) {
125 this.executionContext = executionContext;
128 public void setDescriptorConverter(
129 ExecutionFlowDescriptorConverter descriptorConverter) {
130 this.descriptorConverter = descriptorConverter;