X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fexecution%2Ftasks%2FSystemCall.java;h=2e5b14116a6a7675e69f72cacfa7a198765c84d5;hb=82da8aeebe37e93f26158678590f94341f8ea325;hp=eb1d8fd41463128cb1fb4e22c7a4e845ab478057;hpb=5a2ccf5f49cbda95891436c1ce8e9bdd419c0b41;p=gpl%2Fargeo-slc.git 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 eb1d8fd41..2e5b14116 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 @@ -1,6 +1,7 @@ package org.argeo.slc.core.execution.tasks; import java.io.File; +import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; @@ -27,6 +28,7 @@ import org.apache.commons.logging.Log; 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; @@ -50,6 +52,7 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable { private Resource stdOutFile = null; private Resource stdErrFile = null; private Resource stdInFile = null; + private Boolean redirectStdOut = false; private Map> osCommands = new HashMap>(); private Map osCmds = new HashMap(); @@ -67,6 +70,8 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable { private TestResult testResult; + private ExecutionResources executionResources; + /** Empty constructor */ public SystemCall() { @@ -102,23 +107,21 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable { /** Executes the system call. */ public void run() { // Manage streams - final Writer stdOutWriter; - final Writer stdErrWriter; - final InputStream stdInStream; - if (stdOutFile != null) { - stdOutWriter = createWriter(stdOutFile); - } else { - stdOutWriter = null; - } + Writer stdOutWriter = null; + OutputStream stdOutputStream = null; + Writer stdErrWriter = null; + InputStream stdInStream = null; + if (stdOutFile != null) + if (redirectStdOut) + stdOutputStream = createOutputStream(stdOutFile); + else + stdOutWriter = createWriter(stdOutFile, true); if (stdErrFile != null) { - stdErrWriter = createWriter(stdErrFile); + stdErrWriter = createWriter(stdErrFile, true); } else { - if (stdOutFile != null) { - stdErrWriter = createWriter(stdOutFile); - } else { - stdErrWriter = null; - } + if (stdOutFile != null && !redirectStdOut) + stdErrWriter = createWriter(stdOutFile, true); } if (stdInFile != null) @@ -128,8 +131,6 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable { throw new SlcException("Cannot open a stream for " + stdInFile, e2); } - else - stdInStream = null; if (log.isTraceEnabled()) { log.debug("os.name=" + System.getProperty("os.name")); @@ -149,7 +150,7 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable { if (redirectStreams) { // Redirect standard streams executor.setStreamHandler(createExecuteStreamHandler(stdOutWriter, - stdErrWriter, stdInStream)); + stdOutputStream, stdErrWriter, stdInStream)); } else { // Dummy stream handler (otherwise pump is used) executor.setStreamHandler(new DummyexecuteStreamHandler()); @@ -201,6 +202,15 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable { } + public String asCommand() { + return createCommandLine().toString(); + } + + @Override + public String toString() { + return asCommand(); + } + /** * Build a command line based on the properties. Can be overridden by * specific command wrappers. @@ -271,21 +281,24 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable { * logging mechanism. */ protected ExecuteStreamHandler createExecuteStreamHandler( - final Writer stdOutWriter, final Writer stdErrWriter, - final InputStream stdInStream) { + final Writer stdOutWriter, final OutputStream stdOutputStream, + final Writer stdErrWriter, final InputStream stdInStream) { // Log writers PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( - new LogOutputStream() { + stdOutputStream != null ? stdOutputStream + : new LogOutputStream() { + protected void processLine(String line, int level) { + if (line != null && !line.trim().equals("")) + log(stdOutLogLevel, line); + if (stdOutWriter != null) + appendLineToFile(stdOutWriter, line); + } + }, new LogOutputStream() { protected void processLine(String line, int level) { - log(stdOutLogLevel, line); - if (stdOutWriter != null) - appendLineToFile(stdOutWriter, line); - } - }, new LogOutputStream() { - protected void processLine(String line, int level) { - log(stdErrLogLevel, line); + if (line != null && !line.trim().equals("")) + log(stdErrLogLevel, line); if (stdErrWriter != null) appendLineToFile(stdErrWriter, line); } @@ -299,20 +312,19 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable { return new ExecuteResultHandler() { public void onProcessComplete(int exitValue) { + String msg = "System call '" + commandLine + + "' properly completed."; if (log.isDebugEnabled()) - log - .debug("Process " + commandLine - + " properly completed."); + log.debug(msg); if (testResult != null) { forwardPath(testResult, null); testResult.addResultPart(new SimpleResultPart( - TestStatus.PASSED, "Process " + commandLine - + " properly completed.")); + TestStatus.PASSED, msg)); } } public void onProcessFailed(ExecuteException e) { - String msg = "Process " + commandLine + " failed."; + String msg = "System call '" + commandLine + "' failed."; if (testResult != null) { forwardPath(testResult, null); testResult.addResultPart(new SimpleResultPart( @@ -377,19 +389,42 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable { } } - /** Creates the writer for the log files. */ - protected Writer createWriter(Resource target) { + /** Creates the writer for the output/err files. */ + protected Writer createWriter(Resource target, Boolean append) { FileWriter writer = null; try { - File file = target.getFile(); - writer = new FileWriter(file, true); + + final File file; + if (executionResources != null) + file = new File(executionResources.getAsOsPath(target, true)); + else + file = target.getFile(); + writer = new FileWriter(file, append); } catch (IOException e) { - log.error("Cannot create log file " + target, e); + log.error("Cannot get file for " + target, e); IOUtils.closeQuietly(writer); } return writer; } + /** Creates an outputstream for the output/err files. */ + protected OutputStream createOutputStream(Resource target) { + FileOutputStream OutputStream = null; + try { + + final File file; + if (executionResources != null) + file = new File(executionResources.getAsOsPath(target, true)); + else + file = target.getFile(); + OutputStream = new FileOutputStream(file, false); + } catch (IOException e) { + log.error("Cannot get file for " + target, e); + IOUtils.closeQuietly(OutputStream); + } + return OutputStream; + } + /** Append the argument (for chaining) */ public SystemCall arg(String arg) { command.add(arg); @@ -484,6 +519,14 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable { this.generateScript = generateScript; } + public void setExecutionResources(ExecutionResources executionResources) { + this.executionResources = executionResources; + } + + public void setRedirectStdOut(Boolean redirectStdOut) { + this.redirectStdOut = redirectStdOut; + } + private class DummyexecuteStreamHandler implements ExecuteStreamHandler { public void setProcessErrorStream(InputStream is) throws IOException {