Example usage for org.apache.commons.exec CommandLine addArguments

List of usage examples for org.apache.commons.exec CommandLine addArguments

Introduction

In this page you can find the example usage for org.apache.commons.exec CommandLine addArguments.

Prototype

public CommandLine addArguments(final String addArguments) 

Source Link

Document

Add multiple arguments.

Usage

From source file:de.yaio.services.webshot.server.controller.WebshotProvider.java

protected WebShotResultHandler runCommand(final String command, final String[] params, final long jobTimeout)
        throws IOException {
    int exitValue;
    boolean inBackground = false;
    ExecuteWatchdog watchdog = null;/*from   w w w .j  ava  2s  .c  om*/
    WebShotResultHandler resultHandler;

    // build up the command line to using a 'java.io.File'
    final CommandLine commandLine = new CommandLine(command);
    commandLine.addArguments(params);

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("start command " + command + " with params"
                + new ReflectionToStringBuilder(params, ToStringStyle.SHORT_PREFIX_STYLE).toString());
    }

    // create the executor and consider the exitValue '1' as success
    final Executor executor = new DefaultExecutor();
    executor.setExitValue(0);

    // create a watchdog if requested
    if (jobTimeout > 0) {
        watchdog = new ExecuteWatchdog(jobTimeout);
        executor.setWatchdog(watchdog);
    }

    if (inBackground) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("[WebShot] Executing non-blocking WebShot job  ...");
        }
        resultHandler = new WebShotResultHandler(watchdog);
        executor.execute(commandLine, resultHandler);
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("[WebShot] Executing blocking WebShot job  ...");
        }
        exitValue = executor.execute(commandLine);
        resultHandler = new WebShotResultHandler(exitValue);
    }

    return resultHandler;
}

From source file:com.blackducksoftware.tools.scmconnector.integrations.serena.SerenaConnector.java

@Override
public int sync() {
    CommandLine command = CommandLine.parse(executable);

    int exitStatus = 1;

    String[] arguments = { user, password, host, dbname, dsn, projectspec,
            (getFinalSourceDirectory()).replaceAll("/", "@/") };

    command.addArguments(arguments);

    try {/*from w  ww .j  a  v a2  s .c o m*/
        exitStatus = commandLineExecutor.executeCommand(log, command, new File(getFinalSourceDirectory()));
    } catch (Exception e) {
        log.error("Failure executing Serena Command", e);
        return -1;
    }

    log.info("Exit Status=" + exitStatus);

    return exitStatus;
}

From source file:modules.GeneralNativeCommandModule.java

protected KeyValueResult extractNative(String command, String options, Path path)
        throws NativeExecutionException {
    if (command == null || command.equals("")) {
        System.err.println("command null at GeneralNativeCommandModule.extractNative()");
        return null;
    }/* w  w w . j ava  2s.com*/
    CommandLine commandLine = new CommandLine(command);

    if (options != null && !options.equals("")) {
        String[] args = options.split(" ");
        commandLine.addArguments(args);
    }

    if (path != null) {
        commandLine.addArgument(path.toAbsolutePath().toString(), false);
    }
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    GeneralExecutableModuleConfig generalExecutableModuleConfig = getConfig();
    executor.setWatchdog(new ExecuteWatchdog(generalExecutableModuleConfig.timeout));
    if (getConfig().workingDirectory != null && getConfig().workingDirectory.exists()) {
        executor.setWorkingDirectory(getConfig().workingDirectory);
    }
    try {
        // System.out.println(commandLine);
        executor.execute(commandLine);
    } catch (ExecuteException xs) {
        NativeExecutionException n = new NativeExecutionException();
        n.initCause(xs);
        if (path != null) {
            n.path = path.toAbsolutePath().toString();
        }
        n.executionResult = outputStream.toString();
        n.exitCode = xs.getExitValue();
        throw n;
    } catch (IOException xs) {
        // System.out.println(commandLine);
        NativeExecutionException n = new NativeExecutionException();
        n.initCause(xs);
        if (path != null) {
            n.path = path.toAbsolutePath().toString();
        }
        n.executionResult = outputStream.toString();
        throw n;
    }
    KeyValueResult t = new KeyValueResult("GeneralNativeCommandResults");
    t.add("fullOutput", outputStream.toString().trim());
    return t;
}

From source file:modules.NativeProcessIterativeDaemonModule.java

protected String extractNative(String command, String options, Path path) throws NativeExecutionException {
    if (command == null || command.equals("")) {
        System.err.println("command null at GeneralNativeCommandModule.extractNative()");
        return null;
    }/*from  w w w  . j av a 2 s .  c o m*/
    CommandLine commandLine = new CommandLine(command);

    if (options != null && !options.equals("")) {
        String[] args = options.split(" ");
        commandLine.addArguments(args);
    }

    if (path != null) {
        commandLine.addArgument(path.toAbsolutePath().toString(), false);
    }
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    GeneralExecutableModuleConfig generalExecutableModuleConfig = getConfig();
    executor.setWatchdog(new ExecuteWatchdog(generalExecutableModuleConfig.timeout));
    if (getConfig().workingDirectory != null && getConfig().workingDirectory.exists()) {
        executor.setWorkingDirectory(getConfig().workingDirectory);
    }
    try {

        // System.out.println("Now execute " + commandLine);
        executor.execute(commandLine);
    } catch (ExecuteException xs) {
        NativeExecutionException n = new NativeExecutionException();
        n.initCause(xs);
        if (path != null) {
            n.path = path.toAbsolutePath().toString();
        }
        n.executionResult = outputStream.toString();
        n.exitCode = xs.getExitValue();
        throw n;
    } catch (IOException xs) {
        NativeExecutionException n = new NativeExecutionException();
        n.initCause(xs);
        if (path != null) {
            n.path = path.toAbsolutePath().toString();
        }
        n.executionResult = outputStream.toString();
        throw n;
    }
    return outputStream.toString().trim();
}

From source file:com.netflix.genie.core.services.impl.LocalJobKillServiceImpl.java

private void killJobOnUnix(final int pid) throws GenieException {
    try {// www .  j  av  a  2 s . c  o  m
        // Ensure this process check can't be timed out
        final Calendar tomorrow = Calendar.getInstance(JobConstants.UTC);
        tomorrow.add(Calendar.DAY_OF_YEAR, 1);
        final ProcessChecker processChecker = new UnixProcessChecker(pid, this.executor, tomorrow.getTime());
        processChecker.checkProcess();
    } catch (final ExecuteException ee) {
        // This means the job was done already
        log.debug("Process with pid {} is already done", pid);
        return;
    } catch (final IOException ioe) {
        throw new GenieServerException("Unable to check process status for pid " + pid, ioe);
    }

    // TODO: Do we need retries?
    // This means the job client process is still running
    try {
        final CommandLine killCommand;
        if (this.runAsUser) {
            killCommand = new CommandLine("sudo");
            killCommand.addArgument("kill");
        } else {
            killCommand = new CommandLine("kill");
        }
        killCommand.addArguments(Integer.toString(pid));
        this.executor.execute(killCommand);
    } catch (final IOException ioe) {
        throw new GenieServerException("Unable to kill process " + pid, ioe);
    }
}

From source file:modules.NativeProcessMonitoringDaemonModule.java

protected void startNativeMonitor(String command, String options, Path path) throws NativeExecutionException {
    if (command == null || command.equals("")) {
        System.err.println("command null at GeneralNativeCommandModule.extractNative()");
        return;//from   ww w . ja v  a  2  s .  co m
    }
    CommandLine commandLine = new CommandLine(command);

    if (options != null && !options.equals("")) {
        String[] args = options.split(" ");
        commandLine.addArguments(args);
    }

    if (path != null) {
        commandLine.addArgument(path.toAbsolutePath().toString(), false);
    }
    executor = new DefaultExecutor();
    esh = new MyExecuteStreamHandler();
    executor.setStreamHandler(esh);

    // GeneralExecutableModuleConfig generalExecutableModuleConfig =
    // getConfig();
    executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
    if (getConfig().workingDirectory != null && getConfig().workingDirectory.exists()) {
        executor.setWorkingDirectory(getConfig().workingDirectory);
    }

    try {
        // System.out.println("Now execute: " + commandLine);
        executor.execute(commandLine, this);
    } catch (ExecuteException xs) {
        NativeExecutionException n = new NativeExecutionException();
        n.initCause(xs);
        if (path != null) {
            n.path = path.toAbsolutePath().toString();
        }
        n.exitCode = xs.getExitValue();
        throw n;
    } catch (IOException xs) {
        NativeExecutionException n = new NativeExecutionException();
        n.initCause(xs);
        if (path != null) {
            n.path = path.toAbsolutePath().toString();
        }
        throw n;
    }
    return;
}

From source file:com.netflix.genie.web.services.impl.LocalJobKillServiceImpl.java

private void killJobOnUnix(final int pid) throws GenieException {
    try {/*from ww  w. j a va2  s.c o  m*/
        // Ensure this process check can't be timed out
        final Instant tomorrow = Instant.now().plus(1, ChronoUnit.DAYS);
        final ProcessChecker processChecker = new UnixProcessChecker(pid, this.executor, tomorrow);
        processChecker.checkProcess();
    } catch (final ExecuteException ee) {
        // This means the job was done already
        log.debug("Process with pid {} is already done", pid);
        return;
    } catch (final IOException ioe) {
        throw new GenieServerException("Unable to check process status for pid " + pid, ioe);
    }

    // TODO: Do we need retries?
    // This means the job client process is still running
    try {
        final CommandLine killCommand;
        if (this.runAsUser) {
            killCommand = new CommandLine("sudo");
            killCommand.addArgument("kill");
        } else {
            killCommand = new CommandLine("kill");
        }
        killCommand.addArguments(Integer.toString(pid));
        this.executor.execute(killCommand);
    } catch (final IOException ioe) {
        throw new GenieServerException("Unable to kill process " + pid, ioe);
    }
}

From source file:com.netflix.genie.web.services.impl.JobKillServiceV3.java

private void killJobOnUnix(final int pid) throws GenieException {
    try {/*from  w w w . j  av  a  2s .c o m*/
        // Ensure this process check can't be timed out
        final Instant tomorrow = Instant.now().plus(1, ChronoUnit.DAYS);
        final ProcessChecker processChecker = this.processCheckerFactory.get(pid, tomorrow);
        processChecker.checkProcess();
    } catch (final ExecuteException ee) {
        // This means the job was done already
        log.debug("Process with pid {} is already done", pid);
        return;
    } catch (final IOException ioe) {
        throw new GenieServerException("Unable to check process status for pid " + pid, ioe);
    }

    // TODO: Do we need retries?
    // This means the job client process is still running
    try {
        final CommandLine killCommand;
        if (this.runAsUser) {
            killCommand = new CommandLine("sudo");
            killCommand.addArgument("kill");
        } else {
            killCommand = new CommandLine("kill");
        }
        killCommand.addArguments(Integer.toString(pid));
        this.executor.execute(killCommand);
    } catch (final IOException ioe) {
        throw new GenieServerException("Unable to kill process " + pid, ioe);
    }
}

From source file:de.fischer.thotti.core.runner.NDRunner.java

private NDTestResult forkNDTestRunnerInSlaveMode(NonDistributedTestType test, TestInstanceType instance)
        throws IOException {
    NDTestResult result = new NDTestResult();
    GregorianCalendar startTime;// ww  w  .  j  a  v  a2 s. c  om
    long startTimeMS;
    long endTimeMS;
    int exitCode = -9999;

    result.setTestId(test.getId());
    result.setJVMArgs(instance.getJvmArguments());
    result.setJVMArgsID(instance.getJvmArgsID());
    result.setParamGrpID(instance.getName());

    CommandLine cmdLine = new CommandLine("java");

    if (false == StringUtils.isEmpty(instance.getJvmArguments())) {
        cmdLine.addArguments(instance.getJvmArguments());
    }

    cmdLine.addArgument("-cp").addArgument(System.getProperty("java.class.path"))
            .addArgument(NDRunner.class.getName()).addArgument("--slave").addArgument("--executionid")
            .addArgument(instance.getExecutionID().toString());

    System.gc();
    DefaultExecutor executor = new DefaultExecutor();

    startTime = (GregorianCalendar) Calendar.getInstance();
    startTimeMS = startTime.getTimeInMillis();

    try {
        exitCode = executor.execute(cmdLine);
        result.setSuccessfulTermination(true);
        result.setExitCode(exitCode);
    } catch (ExecuteException e) {
        result.setSuccessfulTermination(false);
        result.setExitCode(e.getExitValue());
    } finally {
        endTimeMS = System.currentTimeMillis();
    }

    result.setExecutionTime(endTimeMS - startTimeMS);
    result.setStartTime(startTime);

    System.out.println(result);

    return result;
}

From source file:info.pancancer.arch3.worker.WorkerRunnable.java

@Override
public void run() {

    int max = maxRuns;

    try {//  w  w  w  .j  a v  a2s .c o  m
        // the VM UUID
        log.info(" WORKER VM UUID provided as: '" + vmUuid + "'");
        // write to
        // TODO: Add some sort of "local debug" mode so that developers working on their local
        // workstation can declare the queue if it doesn't exist. Normally, the results queue is
        // created by the Coordinator.
        resultsChannel = Utilities.setupExchange(settings, this.resultsQueueName);

        while (max > 0 || this.endless) {
            log.debug(max + " remaining jobs will be executed");
            log.info(" WORKER IS PREPARING TO PULL JOB FROM QUEUE " + this.jobQueueName);

            if (!endless) {
                max--;
            }

            // jobChannel needs to be created inside the loop because it is closed inside the loop, and it is closed inside this loop to
            // prevent pre-fetching.
            Channel jobChannel = Utilities.setupQueue(settings, this.jobQueueName);
            if (jobChannel == null) {
                throw new NullPointerException("jobChannel is null for queue: " + this.jobQueueName
                        + ". Something bad must have happened while trying to set up the queue connections. Please ensure that your configuration is correct.");
            }
            QueueingConsumer consumer = new QueueingConsumer(jobChannel);
            jobChannel.basicConsume(this.jobQueueName, false, consumer);

            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            log.info(vmUuid + "  received " + delivery.getEnvelope().toString());
            if (delivery.getBody() != null) {
                String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
                if (message.trim().length() > 0) {

                    log.info(" [x] Received JOBS REQUEST '" + message + "' @ " + vmUuid);

                    Job job = new Job().fromJSON(message);

                    Status status = new Status(vmUuid, job.getUuid(), StatusState.RUNNING,
                            Utilities.JOB_MESSAGE_TYPE, "job is starting", this.networkAddress);
                    status.setStderr("");
                    status.setStdout("");
                    String statusJSON = status.toJSON();

                    log.info(" WORKER LAUNCHING JOB");

                    // greedy acknowledge, it will be easier to deal with lost jobs than zombie workers in hostile OpenStack
                    // environments
                    log.info(vmUuid + " acknowledges " + delivery.getEnvelope().toString());
                    jobChannel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                    // we need to close the channel IMMEDIATELY to complete the ACK.
                    jobChannel.close();
                    // Close the connection object as well, or the main thread may not exit because of still-open-and-in-use resources.
                    jobChannel.getConnection().close();

                    WorkflowResult workflowResult = new WorkflowResult();
                    if (testMode) {
                        workflowResult.setWorkflowStdout("everything is awesome");
                        workflowResult.setExitCode(0);
                    } else {
                        String seqwareEngine = settings.getString(Constants.WORKER_SEQWARE_ENGINE,
                                Constants.SEQWARE_WHITESTAR_ENGINE);
                        String seqwareSettingsFile = settings.getString(Constants.WORKER_SEQWARE_SETTINGS_FILE);
                        String dockerImage = settings.getString(Constants.WORKER_SEQWARE_DOCKER_IMAGE_NAME);
                        workflowResult = launchJob(statusJSON, job, seqwareEngine, seqwareSettingsFile,
                                dockerImage);
                    }

                    status = new Status(vmUuid, job.getUuid(),
                            workflowResult.getExitCode() == 0 ? StatusState.SUCCESS : StatusState.FAILED,
                            Utilities.JOB_MESSAGE_TYPE, "job is finished", networkAddress);
                    status.setStderr(workflowResult.getWorkflowStdErr());
                    status.setStdout(workflowResult.getWorkflowStdout());
                    statusJSON = status.toJSON();

                    log.info(" WORKER FINISHING JOB");

                    finishJob(statusJSON);
                } else {
                    log.info(NO_MESSAGE_FROM_QUEUE_MESSAGE);
                }
                // we need to close the channel *conditionally*
                if (jobChannel.isOpen()) {
                    jobChannel.close();
                }
                // Close the connection object as well, or the main thread may not exit because of still-open-and-in-use resources.
                if (jobChannel.getConnection().isOpen()) {
                    jobChannel.getConnection().close();
                }
            } else {
                log.info(NO_MESSAGE_FROM_QUEUE_MESSAGE);
            }

            if (endless) {
                log.info("attempting to reset workspace");
                DefaultExecutor executor = new DefaultExecutor();
                DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
                // attempt a cleanup
                CommandLine cli = new CommandLine("sudo");
                List<String> args = new ArrayList<>(Arrays.asList("rm", "-rf", "/datastore/*"));
                cli.addArguments(args.toArray(new String[args.size()]));
                executor.execute(cli, resultHandler);
                // Use the result handler for non-blocking call, so this way we should be able to get updates of
                // stdout and stderr while the command is running.
                resultHandler.waitFor();
                log.info("exit code for cleanup: " + resultHandler.getExitValue());
            }
        }
        log.info(" \n\n\nWORKER FOR VM UUID HAS FINISHED!!!: '" + vmUuid + "'\n\n");

        // turns out this is needed when multiple threads are reading from the same
        // queue otherwise you end up with multiple unacknowledged messages being undeliverable to other workers!!!
        if (resultsChannel != null && resultsChannel.isOpen()) {
            resultsChannel.close();
            resultsChannel.getConnection().close();
        }
        log.debug("result channel open: " + resultsChannel.isOpen());
        log.debug("result channel connection open: " + resultsChannel.getConnection().isOpen());
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }
}