+ commandToUse = command;
+ String cmdToUse = null;
+ if (osCmds.containsKey(osName))
+ cmdToUse = osCmds.get(osName);
+ else
+ cmdToUse = cmd;
+
+ CommandLine commandLine = null;
+
+ // Which command definition to use
+ if (commandToUse == null && cmdToUse == null)
+ throw new SlcException("Please specify a command.");
+ else if (commandToUse != null && cmdToUse != null)
+ throw new SlcException(
+ "Specify the command either as a line or as a list.");
+ else if (cmdToUse != null) {
+ commandLine = CommandLine.parse(cmdToUse);
+ } else if (commandToUse != null) {
+ if (commandToUse.size() == 0)
+ throw new SlcException("Command line is empty.");
+
+ commandLine = new CommandLine(commandToUse.get(0).toString());
+
+ for (int i = 1; i < commandToUse.size(); i++) {
+ if (log.isTraceEnabled())
+ log.debug(commandToUse.get(i));
+ commandLine.addArgument(commandToUse.get(i).toString());
+ }
+ } else {
+ // all cases covered previously
+ throw new UnsupportedException();
+ }
+
+ if (generateScript != null) {
+ File scriptFile = new File(getExecDirToUse() + File.separator
+ + generateScript);
+ try {
+ FileUtils.writeStringToFile(scriptFile,
+ (osConsole != null ? osConsole + " " : "")
+ + commandLine.toString());
+ } catch (IOException e) {
+ throw new SlcException("Could not generate script "
+ + scriptFile, e);
+ }
+ commandLine = new CommandLine(scriptFile);
+ } else {
+ if (osConsole != null)
+ commandLine = CommandLine.parse(osConsole + " "
+ + commandLine.toString());
+ }
+
+ return commandLine;
+ }
+
+ /**
+ * Creates a {@link PumpStreamHandler} which redirects streams to the custom
+ * logging mechanism.
+ */
+ protected ExecuteStreamHandler createExecuteStreamHandler(
+ final Writer stdOutWriter, final OutputStream stdOutputStream,
+ final Writer stdErrWriter, final InputStream stdInStream) {
+
+ // Log writers
+
+ PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(
+ stdOutputStream != null ? stdOutputStream
+ : new LogOutputStream() {
+ protected void processLine(String line, int level) {
+ if (line != null && !line.trim().equals(""))
+ logStdOut(line);
+ if (stdOutWriter != null)
+ appendLineToFile(stdOutWriter, line);
+ }
+ }, new LogOutputStream() {
+ protected void processLine(String line, int level) {
+ if (line != null && !line.trim().equals(""))
+ logStdErr(line);
+ if (stdErrWriter != null)
+ appendLineToFile(stdErrWriter, line);
+ }
+ }, stdInStream) {
+
+ @Override
+ public void stop() {
+ // prevents the method to block when joining stdin
+ if (stdInSink != null)
+ IOUtils.closeQuietly(stdInSink);
+
+ super.stop();
+ }
+ };
+ return pumpStreamHandler;
+ }
+
+ /** Creates the default {@link ExecuteResultHandler}. */
+ protected ExecuteResultHandler createExecuteResultHandler(
+ final CommandLine commandLine) {
+ return new ExecuteResultHandler() {
+
+ public void onProcessComplete(int exitValue) {
+ String msg = "System call '" + commandLine
+ + "' properly completed.";
+ if (log.isTraceEnabled())
+ log.trace(msg);
+ if (testResult != null) {
+ forwardPath(testResult);
+ testResult.addResultPart(new SimpleResultPart(
+ TestStatus.PASSED, msg));
+ }
+ }
+
+ public void onProcessFailed(ExecuteException e) {
+ String msg = "System call '" + commandLine + "' failed.";
+ if (testResult != null) {
+ forwardPath(testResult);
+ testResult.addResultPart(new SimpleResultPart(
+ TestStatus.ERROR, msg, e));
+ } else {
+ if (exceptionOnFailed)
+ throw new SlcException(msg, e);
+ else
+ log.error(msg, e);
+ }
+ }
+ };
+ }
+
+ protected void forwardPath(TestResult testResult) {
+ // TODO: allocate a TreeSPath
+ }
+
+ /**
+ * Shortcut method getting the execDir to use
+ */
+ protected String getExecDirToUse() {
+ try {
+ if (execDir != null) {
+ return execDir;
+ }
+ return System.getProperty("user.dir");
+ } catch (Exception e) {
+ throw new SlcException("Cannot find exec dir", e);
+ }