Improve execution core
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 22 May 2010 19:15:58 +0000 (19:15 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 22 May 2010 19:15:58 +0000 (19:15 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@3588 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionResources.java
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/xml/FlowBeanDefinitionParser.java

index c16bc3265917577f01d898c096da85107455a694..0c6864f440fccf7aab7fbd7b346c1eaf5a32c3b0 100644 (file)
@@ -92,7 +92,7 @@ public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
        public void run() {
                try {
                        for (Runnable executable : executables) {
-                               doExecuteRunnable(executable);
+                               this.doExecuteRunnable(executable);
                        }
                } catch (RuntimeException e) {
                        if (failOnError)
index 6886a0bd129cbcb04ee40a06e215ebbbb11ed9e3..22642ceb70085545a8d1d2fcf1cde227a664f2bc 100644 (file)
@@ -1,11 +1,28 @@
 package org.argeo.slc.core.execution;
 
+import java.io.File;
+
 import org.springframework.core.io.Resource;
 
 public interface ExecutionResources {
+       /** The base directory where this execution can write */
+       public File getWritableBaseDir();
+
        /** Allocates a local file in the writable area and return it as a resource. */
        public Resource getWritableResource(String relativePath);
 
+       /**
+        * Allocates a local file in the writable area and return it as a fully
+        * qualified OS path.
+        */
+       public String getWritableOsPath(String relativePath);
+
+       /**
+        * Allocates a local file in the writable area and return it as a
+        * {@link File}.
+        */
+       public File getWritableOsFile(String relativePath);
+
        /**
         * Returns the resource as a file path. If the resource is not writable it
         * is copied as a file in the writable area and the path to this local file
index e1cd26b7a821367cbd068419f26747ed90330080..aa8c9cc7bb94d980e55a34194b842ec35380aa97 100644 (file)
@@ -79,6 +79,18 @@ public class FileExecutionResources implements ExecutionResources {
                return resource;
        }
 
+       public String getWritableOsPath(String relativePath) {
+               try {
+                       return getFile(relativePath).getCanonicalPath();
+               } catch (IOException e) {
+                       throw new SlcException("Cannot find canonical path", e);
+               }
+       }
+
+       public File getWritableOsFile(String relativePath) {
+               return getFile(relativePath);
+       }
+
        public String getAsOsPath(Resource resource, Boolean overwrite) {
                File file = fileFromResource(resource);
                if (file != null)
@@ -133,8 +145,13 @@ public class FileExecutionResources implements ExecutionResources {
 
        }
 
-       public File getFile(String relativePath) {
+       protected File getFile(String relativePath) {
+               File writableBaseDir = getWritableBaseDir();
+               return new File(writableBaseDir.getPath() + File.separator
+                               + relativePath.replace('/', File.separatorChar));
+       }
 
+       public File getWritableBaseDir() {
                if (withExecutionSubdirectory) {
                        Assert.notNull(executionContext, "execution context is null");
                        String path = baseDir.getPath()
@@ -144,13 +161,9 @@ public class FileExecutionResources implements ExecutionResources {
                                                                        executionContext
                                                                                        .getVariable(ExecutionContext.VAR_EXECUTION_CONTEXT_CREATION_DATE))
                                        + executionContext.getUuid();
-                       File executionDir = new File(path);
-
-                       return new File(executionDir.getPath() + File.separator
-                                       + relativePath.replace('/', File.separatorChar));
+                       return new File(path);
                } else {
-                       return new File(baseDir.getPath() + File.separator
-                                       + relativePath.replace('/', File.separatorChar));
+                       return baseDir;
                }
        }
 
index 9d8ebdcf8785920a459be223eed70596ea693e5c..67130da9ad49cd76e51e7ed079402bed5137dc29 100644 (file)
@@ -30,14 +30,13 @@ import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.UnsupportedException;
 import org.argeo.slc.core.execution.ExecutionResources;
-import org.argeo.slc.core.structure.tree.TreeSRelatedHelper;
 import org.argeo.slc.core.test.SimpleResultPart;
 import org.argeo.slc.test.TestResult;
 import org.argeo.slc.test.TestStatus;
 import org.springframework.core.io.Resource;
 
 /** Execute an OS specific system call. */
-public class SystemCall extends TreeSRelatedHelper implements Runnable {
+public class SystemCall implements Runnable {
        public final static String LOG_STDOUT = "System.out";
 
        private final Log log = LogFactory.getLog(getClass());
@@ -341,7 +340,7 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable {
                                if (log.isTraceEnabled())
                                        log.trace(msg);
                                if (testResult != null) {
-                                       forwardPath(testResult, null);
+                                       forwardPath(testResult);
                                        testResult.addResultPart(new SimpleResultPart(
                                                        TestStatus.PASSED, msg));
                                }
@@ -350,7 +349,7 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable {
                        public void onProcessFailed(ExecuteException e) {
                                String msg = "System call '" + commandLine + "' failed.";
                                if (testResult != null) {
-                                       forwardPath(testResult, null);
+                                       forwardPath(testResult);
                                        testResult.addResultPart(new SimpleResultPart(
                                                        TestStatus.ERROR, msg, e));
                                } else {
@@ -363,6 +362,10 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable {
                };
        }
 
+       protected void forwardPath(TestResult testResult) {
+               // TODO: allocate a TreeSPath
+       }
+
        /**
         * Shortcut method getting the execDir to use
         */
@@ -463,12 +466,16 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable {
 
        /** Append the argument (for chaining) */
        public SystemCall arg(String arg) {
+               if (command == null)
+                       command = new ArrayList<Object>();
                command.add(arg);
                return this;
        }
 
        /** Append the argument (for chaining) */
        public SystemCall arg(String arg, String value) {
+               if (command == null)
+                       command = new ArrayList<Object>();
                command.add(arg);
                command.add(value);
                return this;
index 8594e956e4b375c4185dd3628d17d2ddee228d11..56ead5146ed008f943ce459b23018605b045311f 100644 (file)
@@ -5,7 +5,10 @@ import java.util.List;
 
 import org.argeo.slc.SlcException;
 import org.argeo.slc.core.execution.DefaultExecutionFlow;
+import org.argeo.slc.execution.ExecutionFlow;
+import org.springframework.beans.factory.BeanDefinitionStoreException;
 import org.springframework.beans.factory.config.RuntimeBeanReference;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
 import org.springframework.beans.factory.support.ManagedList;
 import org.springframework.beans.factory.support.ManagedMap;
@@ -42,10 +45,9 @@ public class FlowBeanDefinitionParser extends
                if (StringUtils.hasText(parent))
                        builder.setParentName(parent);
 
-               
-               builder.getBeanDefinition().setDescription(DomUtils.getChildElementValueByTagName(element, 
-                               "description"));
-               
+               builder.getBeanDefinition().setDescription(
+                               DomUtils.getChildElementValueByTagName(element, "description"));
+
                List<Element> execElems = new ArrayList<Element>();
                List<Element> argsElems = new ArrayList<Element>();
                NodeList nodeList = element.getChildNodes();
@@ -54,7 +56,7 @@ public class FlowBeanDefinitionParser extends
                        if (node instanceof Element) {
                                if (DomUtils.nodeNameEquals(node, "arg"))
                                        argsElems.add((Element) node);
-                               else if(!DomUtils.nodeNameEquals(node, "description"))
+                               else if (!DomUtils.nodeNameEquals(node, "description"))
                                        execElems.add((Element) node);
                        }
                }
@@ -77,18 +79,36 @@ public class FlowBeanDefinitionParser extends
                // Executables
                if (execElems.size() != 0) {
                        ManagedList executables = new ManagedList(execElems.size());
-                       for(Element child : execElems) {
-                               // child validity check is performed in xsd  
+                       for (Element child : execElems) {
+                               // child validity check is performed in xsd
                                executables.add(NamespaceUtils.parseBeanOrReference(child,
-                                               parserContext, builder.getBeanDefinition()));                           
+                                               parserContext, builder.getBeanDefinition()));
                        }
-                       builder.addPropertyValue("executables", executables);
+                       if (executables.size() > 0)
+                               builder.addPropertyValue("executables", executables);
                }
        }
 
+       @SuppressWarnings("unchecked")
        @Override
-       protected Class<DefaultExecutionFlow> getBeanClass(Element element) {
-               return DefaultExecutionFlow.class;
+       protected Class<? extends ExecutionFlow> getBeanClass(Element element) {
+               String clss = element.getAttribute("class");
+               if (StringUtils.hasText(clss))
+                       // TODO: check that it actually works
+                       try {
+                               return (Class<? extends ExecutionFlow>) getClass()
+                                               .getClassLoader().loadClass(clss);
+                       } catch (ClassNotFoundException e) {
+                               try {
+                                       return (Class<? extends ExecutionFlow>) Thread
+                                                       .currentThread().getContextClassLoader().loadClass(
+                                                                       clss);
+                               } catch (ClassNotFoundException e1) {
+                                       throw new SlcException("Cannot load class " + clss, e);
+                               }
+                       }
+               else
+                       return DefaultExecutionFlow.class;
        }
 
        // parse nested bean definition
@@ -98,6 +118,18 @@ public class FlowBeanDefinitionParser extends
        // builder.getBeanDefinition());
        // }
 
+       @Override
+       protected String resolveId(Element element,
+                       AbstractBeanDefinition definition, ParserContext parserContext)
+                       throws BeanDefinitionStoreException {
+               String name = element.getAttribute("name");
+               if (StringUtils.hasText(name)) {
+                       return name;
+               } else {
+                       return super.resolveId(element, definition, parserContext);
+               }
+       }
+
        protected boolean shouldGenerateIdAsFallback() {
                return true;
        }