Skip to content

Commit

Permalink
eirslett#41 adds debug logging of executed command line and exit value
Browse files Browse the repository at this point in the history
  • Loading branch information
tjuerge committed Jan 22, 2016
1 parent 0caeed3 commit 84030b4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
Expand Up @@ -77,7 +77,7 @@ private boolean nodeIsAlreadyInstalled() {
NodeExecutorConfig executorConfig = new InstallNodeExecutorConfig(config);
File nodeFile = executorConfig.getNodePath();
if(nodeFile.exists()){
final String version = new NodeExecutor(executorConfig, Arrays.asList("--version")).executeAndGetResult();
final String version = new NodeExecutor(executorConfig, Arrays.asList("--version")).executeAndGetResult(logger);

if(version.equals(nodeVersion)){
logger.info("Node " + version + " is already installed.");
Expand Down
Expand Up @@ -12,15 +12,12 @@ public NodeExecutor(NodeExecutorConfig config, List<String> arguments){
final String node = config.getNodePath().getAbsolutePath();
List<String> localPaths = new ArrayList<String>();
localPaths.add(config.getNodePath().getParent());
this.executor = new ProcessExecutor(
config.getWorkingDirectory(),
localPaths,
Utils.prepend(node, arguments),
config.getPlatform());
this.executor = new ProcessExecutor(config.getWorkingDirectory(), localPaths, node, arguments,
config.getPlatform());
}

public String executeAndGetResult() throws ProcessExecutionException {
return executor.executeAndGetResult();
public String executeAndGetResult(final Logger logger) throws ProcessExecutionException {
return executor.executeAndGetResult(logger);
}

public int executeAndRedirectOutput(final Logger logger) throws ProcessExecutionException {
Expand Down
Expand Up @@ -5,7 +5,6 @@
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -36,22 +35,23 @@ final class ProcessExecutor {
private CommandLine commandLine;
private final Executor executor;

public ProcessExecutor(File workingDirectory, List<String> paths, List<String> command, Platform platform) {
this(workingDirectory, paths, command, platform, 0);
public ProcessExecutor(File workingDirectory, List<String> paths, String executable, List<String> arguments,
Platform platform) {
this(workingDirectory, paths, executable, arguments, platform, 0);
}

public ProcessExecutor(File workingDirectory, List<String> paths, List<String> command, Platform platform,
long timeoutInSeconds) {
public ProcessExecutor(File workingDirectory, List<String> paths, String executable, List<String> arguments,
Platform platform, long timeoutInSeconds) {
this.environment = createEnvironment(paths, platform);
this.commandLine = createCommandLine(command);
this.commandLine = createCommandLine(executable, arguments);
this.executor = createExecutor(workingDirectory, timeoutInSeconds);
}

public String executeAndGetResult() throws ProcessExecutionException {
public String executeAndGetResult(final Logger logger) throws ProcessExecutionException {
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();

int exitValue = execute(stdout, stderr);
int exitValue = execute(logger, stdout, stderr);
if (exitValue == 0) {
return stdout.toString().trim();
} else {
Expand All @@ -63,15 +63,20 @@ public int executeAndRedirectOutput(final Logger logger) throws ProcessExecution
OutputStream stdout = new LoggerOutputStream(logger, 0);
OutputStream stderr = new LoggerOutputStream(logger, 1);

return execute(stdout, stderr);
return execute(logger, stdout, stderr);
}

private int execute(OutputStream stdout, OutputStream stderr) throws ProcessExecutionException {
private int execute(final Logger logger, final OutputStream stdout, final OutputStream stderr)
throws ProcessExecutionException {
logger.debug("Executing command line {}", commandLine);
try {
ExecuteStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr);
executor.setStreamHandler(streamHandler);

return executor.execute(commandLine, environment);
int exitValue = executor.execute(commandLine, environment);
logger.debug("Exit value {}", exitValue);

return exitValue;
} catch (ExecuteException e) {
if (executor.getWatchdog() != null && executor.getWatchdog().killedProcess()) {
throw new ProcessExecutionException("Process killed after timeout");
Expand Down Expand Up @@ -107,12 +112,11 @@ private Map<String, String> createEnvironment(List<String> paths, Platform platf
return environment;
}

private CommandLine createCommandLine(List<String> command) {
Iterator<String> args = command.iterator();
CommandLine commmandLine = new CommandLine(args.next());
private CommandLine createCommandLine(String executable, List<String> arguments) {
CommandLine commmandLine = new CommandLine(executable);

while(args.hasNext()) {
commmandLine.addArgument(args.next());
for (String argument : arguments) {
commmandLine.addArgument(argument);
}

return commmandLine;
Expand Down

0 comments on commit 84030b4

Please sign in to comment.