From 31a8055cac622a55afd2668e77f00aba1d031f91 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Sat, 22 May 2010 19:15:58 +0000 Subject: [PATCH] Improve execution core git-svn-id: https://svn.argeo.org/slc/trunk@3588 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../core/execution/DefaultExecutionFlow.java | 2 +- .../core/execution/ExecutionResources.java | 17 ++++++ .../execution/FileExecutionResources.java | 27 +++++++--- .../slc/core/execution/tasks/SystemCall.java | 15 ++++-- .../xml/FlowBeanDefinitionParser.java | 54 +++++++++++++++---- 5 files changed, 92 insertions(+), 23 deletions(-) diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java index c16bc3265..0c6864f44 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java @@ -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) diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionResources.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionResources.java index 6886a0bd1..22642ceb7 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionResources.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionResources.java @@ -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 diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java index e1cd26b7a..aa8c9cc7b 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java @@ -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; } } diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java index 9d8ebdcf8..67130da9a 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java @@ -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(); command.add(arg); return this; } /** Append the argument (for chaining) */ public SystemCall arg(String arg, String value) { + if (command == null) + command = new ArrayList(); command.add(arg); command.add(value); return this; diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/xml/FlowBeanDefinitionParser.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/xml/FlowBeanDefinitionParser.java index 8594e956e..56ead5146 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/xml/FlowBeanDefinitionParser.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/xml/FlowBeanDefinitionParser.java @@ -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 execElems = new ArrayList(); List argsElems = new ArrayList(); 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 getBeanClass(Element element) { - return DefaultExecutionFlow.class; + protected Class getBeanClass(Element element) { + String clss = element.getAttribute("class"); + if (StringUtils.hasText(clss)) + // TODO: check that it actually works + try { + return (Class) getClass() + .getClassLoader().loadClass(clss); + } catch (ClassNotFoundException e) { + try { + return (Class) 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; } -- 2.39.2