Example usage for org.apache.commons.exec Executor execute

List of usage examples for org.apache.commons.exec Executor execute

Introduction

In this page you can find the example usage for org.apache.commons.exec Executor execute.

Prototype

int execute(CommandLine command) throws ExecuteException, IOException;

Source Link

Document

Methods for starting synchronous execution.

Usage

From source file:com.magnet.tools.tests.WebLogicStepDefs.java

public static void ensureDeployApp(String url, String name, String location) throws Exception {
    Executor exec = new DefaultExecutor();
    CommandLine cl = new CommandLine(getScriptsDir() + File.separator + REDEPLOY_WLS_APP_SCRIPT);
    if (getArchetypeSettings() != null && getArchetypeSettings().length() != 0) {
        cl.addArgument(getArchetypeSettings());
    }/*w ww .  j a  va2s . c o m*/
    cl.addArgument(url);
    cl.addArgument(name);
    cl.addArgument(location);
    cl.setSubstitutionMap(getSubstitutionMap());
    int exitValue = exec.execute(cl);
    String msg = String.format("Server running at %s failed to deploy app %s at %s", url, name,
            expandVariables(location));
    ensureBuildSuccessful("redeploy.log");
    Assert.assertEquals(msg, 0, exitValue);
}

From source file:com.magnet.tools.tests.WebLogicStepDefs.java

public static void ensureStartServer(String location, String httpPingUrl) throws Exception {
    Executor exec = new DefaultExecutor();
    CommandLine cl = new CommandLine(getScriptsDir() + File.separator + START_WLS_SERVER_SCRIPT);
    if (getArchetypeSettings() != null && getArchetypeSettings().length() != 0) {
        cl.addArgument("--s");
        cl.addArgument(getArchetypeSettings());
    }/* ww w.  j a  v a 2 s.c  o  m*/
    cl.addArgument(String.format("-DdomainHome=%s", location));
    cl.addArgument(String.format("-DhttpPingUrl=%s", httpPingUrl));
    cl.setSubstitutionMap(getSubstitutionMap());
    int exitValue = exec.execute(cl);
    ensureBuildSuccessful("start-server.log");
    String msg = String.format("server failed to start at %s", expandVariables(location));
    Assert.assertEquals(msg, 0, exitValue);
}

From source file:de.torstenwalter.maven.plugins.ExpdpMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    CommandLine commandLine = new CommandLine(expdp);
    addCommonArguments(commandLine);//w  ww. ja  v  a2  s. c  o m

    getLog().debug("Executing command line: " + obfuscateCredentials(commandLine.toString(), getCredentials()));

    Executor exec = new DefaultExecutor();
    exec.setStreamHandler(new PumpStreamHandler(System.out, System.err));
    try {
        exec.execute(commandLine);
    } catch (ExecuteException e) {
        throw new MojoExecutionException("Command execution failed.", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Command execution failed.", e);
    }
}

From source file:com.muk.ext.process.ProcessExecutor.java

public int runCommandLine(CommandLine cmdLine, long maxWaitTimeInMillis) throws IOException {
    ExecuteWatchdog processWatchDog = new ExecuteWatchdog(maxWaitTimeInMillis);
    Executor executor = new DefaultExecutor();

    executor.setExitValue(0);//  w  w w. ja  va2  s.c  om
    executor.setWatchdog(processWatchDog);

    int result = 1;
    result = executor.execute(cmdLine);

    return result;
}

From source file:com.github.shyiko.hmp.StopMojo.java

private void kill(int pid) throws IOException {
    Executor executor = new DefaultExecutor();
    executor.setStreamHandler(new ExecutionStreamHandler(quiet));
    executor.execute(CommandLine.parse("kill -15 " + pid));
}

From source file:de.torstenwalter.maven.plugins.ImpdpMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    CommandLine commandLine = new CommandLine(impdp);
    addCommonArguments(commandLine);/* w w w  . j a  va2s.c  o  m*/

    if (StringUtils.isNotEmpty(remap_tablespace)) {
        commandLine.addArgument("REMAP_TABLESPACE=" + remap_tablespace);
    }

    if (StringUtils.isNotEmpty(remap_schema)) {
        commandLine.addArgument("REMAP_SCHEMA=" + remap_schema);
    }

    if (StringUtils.isNotEmpty(table_exists_action)) {
        commandLine.addArgument("TABLE_EXISTS_ACTION=" + table_exists_action);
    }

    getLog().info("Executing command line: " + obfuscateCredentials(commandLine.toString(), getCredentials()));

    Executor exec = new DefaultExecutor();
    exec.setStreamHandler(new PumpStreamHandler(System.out, System.err));
    try {
        exec.execute(commandLine);
    } catch (ExecuteException e) {
        throw new MojoExecutionException("Command execution failed.", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Command execution failed.", e);
    }
}

From source file:name.martingeisse.webide.features.verilog.compiler.VerilogBuilder.java

private void compile(long workspaceId, final JsonAnalyzer descriptorAnalyzer, ResourcePath inputFilePath,
        ResourcePath outputFilePath) {/*w w w.  j  a v a2s . c  o  m*/
    try {

        // prepare
        ResourceHandle inputFile = new ResourceHandle(workspaceId, inputFilePath);
        ResourceHandle outputFile = new ResourceHandle(workspaceId, outputFilePath);

        // read the input file
        byte[] inputData = inputFile.readBinaryFile(false);
        if (inputData == null) {
            return;
        }

        // delete previous output file
        outputFile.delete();

        // build the command line
        CommandLine commandLine = new CommandLine(Configuration.getIverilogPath());
        commandLine.addArgument("-o");
        commandLine.addArgument(Configuration.getStdoutPath());
        commandLine.addArgument(Configuration.getStdinPath());

        // build I/O streams
        ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        OutputStream errorStream = System.err;
        ExecuteStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream, inputStream);

        // run Icarus
        Executor executor = new DefaultExecutor();
        executor.setStreamHandler(streamHandler);
        executor.execute(commandLine);

        // create the output file
        outputFile.writeFile(outputStream.toByteArray(), true, true);

    } catch (IOException e) {
        // TODO error message
        logger.error(e);
        return;
    }

}

From source file:com.taobao.ad.es.common.job.executor.ShellHttpJobExecutor.java

@Override
public JobResult execute(JobData jobData) throws IOException {
    JobResult jobResult = JobResult.succcessResult();
    CommandLine cmdLine = CommandLine.parse(jobData.getData().get(JobData.JOBDATA_DATA_JOBCOMMAND));
    Executor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(1200000);
    executor.setExitValue(0);/*from  ww w. j  a  v a 2  s . c o  m*/
    executor.setWatchdog(watchdog);
    int exitValue = -1;
    try {
        exitValue = executor.execute(cmdLine);
    } catch (ExecuteException e) {
        exitValue = e.getExitValue();
    }
    if (exitValue != 0) {
        jobResult = JobResult.errorResult(JobResult.RESULTCODE_OTHER_ERR,
                "Shell?");
        jobResult.setResultCode(exitValue);
        return jobResult;
    }
    jobResult.setResultCode(exitValue);
    return jobResult;
}

From source file:name.martingeisse.webide.features.verilog.simulator.VerilogSimulatorMenuDelegate.java

@Override
public void invoke(final Object context, final List<ResourceHandle> anchor, final Object parameter) {

    // check anchor
    logger.trace("VerilogSimulatorMenuDelegate invoked...");
    if (anchor.isEmpty()) {
        logger.trace("empty anchor (no file selected for simulation)");
        return;//from  www.ja v  a 2 s.  co m
    }
    final ResourceHandle inputFile = anchor.get(0);

    // make sure the anchor element is a file
    if (!inputFile.isFile()) {
        logger.trace("selected anchor is not a file");
        return;
    }
    logger.trace("selected file for simulation: " + inputFile.getPath());

    // catch exceptions to cleanly de-allocate the temporary folder
    TemporaryFolder temporaryFolder = null;
    try {

        // allocate a temporary folder for the output files
        temporaryFolder = new TemporaryFolder();
        logger.trace("allocation temporary folder: " + temporaryFolder.getInstanceFolder());

        // build the command line
        final CommandLine commandLine = new CommandLine(Configuration.getBashPath());
        commandLine.addArgument("--login");
        commandLine.addArgument("-c");
        commandLine.addArgument("vvp " + Configuration.getStdinPath(), false);
        logger.trace("command line: " + commandLine);

        // build I/O streams
        final ByteArrayInputStream inputStream = new ByteArrayInputStream(inputFile.readBinaryFile(true));
        final ByteArrayOutputStream outputStream = null;
        final OutputStream errorStream = System.err;
        final ExecuteStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream,
                inputStream);

        // run Icarus
        final Executor executor = new DefaultExecutor();
        executor.setStreamHandler(streamHandler);
        executor.setWorkingDirectory(temporaryFolder.getInstanceFolder());
        executor.execute(commandLine);
        logger.trace("VVP finished");

        // create the output files
        final ResourceHandle outputFolder = inputFile.getParent();
        logger.trace("creating output files in folder: " + outputFolder);
        for (final File temporaryFile : temporaryFolder.getInstanceFolder().listFiles()) {
            if (temporaryFile.isFile()) {
                final ResourceHandle outputFile = outputFolder.getChild(temporaryFile.getName());
                logger.trace("creating output file " + outputFile + " from " + temporaryFile);
                outputFile.writeFile(temporaryFile, true, true);
                logger.trace("output file created");
            } else {
                logger.trace("skipping (not a file): " + temporaryFile);
            }
        }
        logger.trace("output files created");

    } catch (final IOException e) {
        logger.error("exception during VVP simulation", e);
        return;
    } finally {
        if (temporaryFolder != null) {
            temporaryFolder.dispose();
        }
    }

}

From source file:ch.ivyteam.ivy.maven.engine.EngineControl.java

/**
 * Run a short living engine command where we expect a process failure as the engine invokes <code>System.exit(-1)</code>.
 * @param statusCmd /* w w  w.  j a v  a 2s . c  om*/
 * @return the output of the engine command.
 */
private String executeSynch(CommandLine statusCmd) {
    String engineOutput = null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
    Executor executor = createEngineExecutor();
    executor.setStreamHandler(streamHandler);
    executor.setExitValue(-1);
    try {
        executor.execute(statusCmd);
    } catch (IOException ex) { // expected!
    } finally {
        engineOutput = outputStream.toString();
        IOUtils.closeQuietly(outputStream);
    }
    return engineOutput;
}