]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java
Use execDir to choose config and data dir when not forked.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / DefaultExecutionFlow.java
index 91b4ae949c162612a8aa89f1db8d0a04c83ec8b9..16105e241ef0e4ea10a14270831d500bf9550a3d 100644 (file)
@@ -10,7 +10,6 @@ import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.core.structure.tree.TreeSPath;
 import org.argeo.slc.core.structure.tree.TreeSRegistry;
-import org.argeo.slc.execution.Executable;
 import org.argeo.slc.execution.ExecutionFlow;
 import org.argeo.slc.execution.ExecutionSpec;
 import org.argeo.slc.execution.ExecutionSpecAttribute;
@@ -18,44 +17,53 @@ import org.argeo.slc.structure.StructureAware;
 import org.argeo.slc.structure.StructureRegistry;
 import org.springframework.beans.factory.BeanNameAware;
 import org.springframework.beans.factory.InitializingBean;
+import org.springframework.context.ResourceLoaderAware;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
 import org.springframework.validation.MapBindingResult;
 
 public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
-               BeanNameAware, StructureAware<TreeSPath> {
-       
+               BeanNameAware, StructureAware<TreeSPath>, ResourceLoaderAware {
+
        private final static Log log = LogFactory
-       .getLog(DefaultExecutionFlow.class);    
-       
-       private ExecutionSpec executionSpec = new DefaultExecutionSpec();
+                       .getLog(DefaultExecutionFlow.class);
+
+       private final ExecutionSpec executionSpec;
        private String name = null;
        private Map<String, Object> parameters = new HashMap<String, Object>();
-       private List<Executable> executables = new ArrayList<Executable>();
+       private List<Runnable> executables = new ArrayList<Runnable>();
 
        private String path;
        private StructureRegistry<TreeSPath> registry = new TreeSRegistry();
-               
+
+       private ResourceLoader resourceLoader = null;
+
        public DefaultExecutionFlow() {
-       }       
-       
+               this.executionSpec = new DefaultExecutionSpec();
+       }
+
        public DefaultExecutionFlow(ExecutionSpec executionSpec) {
                this.executionSpec = executionSpec;
        }
-       
-       public DefaultExecutionFlow(ExecutionSpec executionSpec, Map<String, Object> parameters) {
+
+       public DefaultExecutionFlow(ExecutionSpec executionSpec,
+                       Map<String, Object> parameters) {
                // be sure to have an execution spec
-               this.executionSpec = (executionSpec == null) ? new DefaultExecutionSpec() : executionSpec;
-               
-               //only parameters contained in the executionSpec can be set
-               for(String parameter : parameters.keySet()) {
-                       if(!executionSpec.getAttributes().containsKey(parameter)) {
-                               throw new SlcException("Parameter " + parameter + " is not defined in the ExecutionSpec");
+               this.executionSpec = (executionSpec == null) ? new DefaultExecutionSpec()
+                               : executionSpec;
+
+               // only parameters contained in the executionSpec can be set
+               for (String parameter : parameters.keySet()) {
+                       if (!executionSpec.getAttributes().containsKey(parameter)) {
+                               throw new SlcException("Parameter " + parameter
+                                               + " is not defined in the ExecutionSpec");
                        }
                }
-               
+
                // set the parameters
-               this.parameters.putAll(parameters);             
-               
-               //check that all the required parameters are defined
+               this.parameters.putAll(parameters);
+
+               // check that all the required parameters are defined
                MapBindingResult errors = new MapBindingResult(parameters, "execution#"
                                + getName());
                for (String key : executionSpec.getAttributes().keySet()) {
@@ -80,20 +88,19 @@ public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
 
                if (errors.hasErrors())
                        throw new SlcException("Could not prepare execution flow: "
-                                       + errors.toString());           
-               
-               
+                                       + errors.toString());
+
        }
 
-       public void execute() {
-               for (Executable executable : executables) {
-                       executable.execute();
+       public void run() {
+               for (Runnable executable : executables) {
+                       executable.run();
                }
        }
 
        public void afterPropertiesSet() throws Exception {
                if (path != null) {
-                       for (Executable executable : executables) {
+                       for (Runnable executable : executables) {
                                if (executable instanceof StructureAware) {
                                        ((StructureAware<TreeSPath>) executable).notifyCurrentPath(
                                                        registry, new TreeSPath(path));
@@ -106,7 +113,7 @@ public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
                this.name = name;
        }
 
-       public void setExecutables(List<Executable> executables) {
+       public void setExecutables(List<Runnable> executables) {
                this.executables = executables;
        }
 
@@ -123,23 +130,33 @@ public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
        }
 
        public Object getParameter(String parameterName) {
-               if (parameters.containsKey(parameterName)) {
-                       return parameters.get(parameterName);
+               // Verify that there is a spec attribute
+               ExecutionSpecAttribute specAttr = null;
+               if (executionSpec.getAttributes().containsKey(parameterName)) {
+                       specAttr = executionSpec.getAttributes().get(parameterName);
                } else {
-                       if (executionSpec.getAttributes().containsKey(parameterName)) {
-                               ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
-                                               parameterName);
-                               if (esa.getValue() != null) {
-                                       return esa.getValue();
-                               }
+                       throw new SlcException("Key " + parameterName
+                                       + " is not defined in the specifications of " + toString());
+               }
+
+               if (parameters.containsKey(parameterName)) {
+                       Object paramValue = parameters.get(parameterName);
+                       if (specAttr instanceof ResourceSpecAttribute) {
+                               // deal with resources
+                               Resource resource = resourceLoader.getResource(paramValue
+                                               .toString());
+                               return ((ResourceSpecAttribute) specAttr)
+                                               .convertResource(resource);
                        } else {
-                               throw new SlcException("Key " + parameterName
-                                               + " is not defined in the specifications of "
-                                               + toString());
+                               return paramValue;
+                       }
+               } else {
+                       if (specAttr.getValue() != null) {
+                               return specAttr.getValue();
                        }
                }
-               throw new SlcException("Key " + parameterName + " is not set as parameter in "
-                               + toString());
+               throw new SlcException("Key " + parameterName
+                               + " is not set as parameter in " + toString());
        }
 
        public Boolean isSetAsParameter(String key) {
@@ -175,4 +192,8 @@ public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
                }
        }
 
+       public void setResourceLoader(ResourceLoader resourceLoader) {
+               this.resourceLoader = resourceLoader;
+       }
+
 }