All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.linguarobot.aws.cdk.maven.process.DefaultProcessRunner Maven / Gradle / Ivy

Go to download

The AWS CDK Maven plugin produces and deploys CloudFormation templates based on the cloud infrastructure defined by means of CDK. The goal of the project is to improve the experience of Java developers while working with CDK by eliminating the need for installing Node.js and interacting with the CDK application by means of CDK Toolkit.

There is a newer version: 0.0.8
Show newest version
package io.linguarobot.aws.cdk.maven.process;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.exec.ShutdownHookProcessDestroyer;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

public class DefaultProcessRunner implements ProcessRunner {

    private final File defaultWorkingDirectory;
    private final Executor executor;

    public DefaultProcessRunner(File defaultWorkingDirectory) {
        this.defaultWorkingDirectory = defaultWorkingDirectory;
        this.executor = createExecutor();
    }

    private static Executor createExecutor() {
        DefaultExecutor executor = new DefaultExecutor();
        executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
        executor.setExitValue(0);
        return executor;
    }

    @Override
    public int run(List command, ProcessContext processContext) {
        CommandLine commandLine =  toCommandLine(command);

        File workingDirectory = processContext.getWorkingDirectory().orElse(defaultWorkingDirectory);
        executor.setWorkingDirectory(workingDirectory);
        OutputStream output = processContext.getOutput().orElse(System.out);
        executor.setStreamHandler(new PumpStreamHandler(output));

        Map environment = processContext.getEnvironment().orElse(null);
        try {
            return executor.execute(commandLine, environment);
        } catch (ExecuteException e) {
            throw new ProcessExecutionException(command, e.getExitValue(), e.getCause());
        } catch (IOException e) {
            throw new ProcessExecutionException(command, e);
        }
    }

    private CommandLine toCommandLine(List command) {
        if (command.isEmpty()) {
            throw new IllegalArgumentException("The executable is missing");
        }

        CommandLine commandLine = new CommandLine(command.get(0));
        IntStream.range(1, command.size())
                .forEach(i -> commandLine.addArgument(command.get(i)));
        return commandLine;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy