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

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

Introduction

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

Prototype

public CommandLine addArgument(final String argument) 

Source Link

Document

Add a single argument.

Usage

From source file:eu.crisis_economics.abm.dashboard.cluster.script.BashScheduler.java

private void makeScriptsExecutable() {
    File scriptsDirectory = new File(scriptsDir + File.separator + schedulerType);
    String[] scripts = scriptsDirectory.list(new PatternFilenameFilter(".*\\.sh"));

    CommandLine commandLine = new CommandLine("chmod");
    commandLine.addArgument("755");
    for (String script : scripts) {
        commandLine.addArgument(scriptsDir + File.separator + schedulerType + File.separator + script, false);
    }//from  w  w  w  .j a v a2 s  . c o m

    DefaultExecutor executor = new DefaultExecutor();

    try {
        executor.execute(commandLine);
    } catch (ExecuteException e) {
        // ignore this; there will be an exception later, if this scheduler is used
    } catch (IOException e) {
        // ignore this; there will be an exception later, if this scheduler is used
    }
}

From source file:com.blackducksoftware.tools.scmconnector.integrations.subversion.SubversionConnector.java

private boolean repoExists(File targetDir) throws Exception {
    validateExecutableInstance(EXECUTABLE);

    CommandLine command = CommandLine.parse(EXECUTABLE);

    command.addArgument("status");

    CommandResults commandResults;/*from ww w  .j  av  a  2s .  co  m*/
    try {
        commandResults = commandLineExecutor.executeCommandForOutput(log, command, targetDir);
    } catch (Exception e) {
        log.error("Failure executing SVN Command", e);
        commandResults = null;
    }

    if (commandResults != null && commandResults.getStatus() == 0) {

        // warning message of form "svn: warning: W155007: 'C:\SVNFiles' is
        // not a working copy" only
        // printed when repository is not checked out to directory
        if (commandResults.getOutput().trim().contains("warning")) {
            log.info("repository does not exist in directory: " + targetDir.getAbsolutePath());
            return false;
        }

        log.info("repository exists in directory: " + targetDir.getAbsolutePath());

        return true;
    } else {
        log.info("repository does not exist in directory: " + targetDir.getAbsolutePath());
        return false;
    }
}

From source file:com.alibaba.jstorm.yarn.utils.JStormUtils.java

/**
 * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler If it is frontend, ByteArrayOutputStream.toString get the result
 * <p/>/*  w w  w .  j  ava  2s . c o m*/
 * This function don't care whether the command is successfully or not
 *
 * @param command
 * @param environment
 * @param workDir
 * @param resultHandler
 * @return
 * @throws IOException
 */
@Deprecated
public static ByteArrayOutputStream launchProcess(String command, final Map environment, final String workDir,
        ExecuteResultHandler resultHandler) throws IOException {

    String[] cmdlist = command.split(" ");

    CommandLine cmd = new CommandLine(cmdlist[0]);
    for (String cmdItem : cmdlist) {
        if (StringUtils.isBlank(cmdItem) == false) {
            cmd.addArgument(cmdItem);
        }
    }

    DefaultExecutor executor = new DefaultExecutor();

    executor.setExitValue(0);
    if (StringUtils.isBlank(workDir) == false) {
        executor.setWorkingDirectory(new File(workDir));
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
    if (streamHandler != null) {
        executor.setStreamHandler(streamHandler);
    }

    try {
        if (resultHandler == null) {
            executor.execute(cmd, environment);
        } else {
            executor.execute(cmd, environment, resultHandler);
        }
    } catch (ExecuteException e) {

        // @@@@
        // failed to run command
    }

    return out;

}

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

public void execute() throws MojoExecutionException, MojoFailureException {
    CommandLine commandLine = new CommandLine(impdp);
    addCommonArguments(commandLine);/*from  www  . ja  v  a2 s  .  com*/

    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:eu.creatingfuture.propeller.blocklyprop.propeller.PropellerLoad.java

protected boolean loadIntoRam(String executable, File ramFile, String comPort) {
    try {//from w  ww.jav  a  2  s  .c om
        Map map = new HashMap();
        map.put("ramFile", ramFile);

        CommandLine cmdLine = new CommandLine(executable);
        cmdLine.addArgument("-r");
        if (comPort != null) {
            cmdLine.addArgument("-p").addArgument(comPort);
        }
        cmdLine.addArgument("${ramFile}");

        cmdLine.setSubstitutionMap(map);
        DefaultExecutor executor = new DefaultExecutor();
        //executor.setExitValues(new int[]{451, 301});

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);

        try {
            exitValue = executor.execute(cmdLine);
        } catch (ExecuteException ee) {
            exitValue = ee.getExitValue();
            logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue);
            success = false;
            return false;
        } finally {
            output = outputStream.toString();
        }

        success = true;
        return true;
    } catch (IOException ioe) {
        logger.log(Level.SEVERE, null, ioe);
        success = false;
        return false;
    }
}

From source file:eu.creatingfuture.propeller.blocklyprop.propeller.PropellerLoad.java

protected List<String> getPorts(String executable) {
    List<String> ports = new ArrayList<>();
    try {/*from   w w  w .  ja  va 2  s .c  o  m*/
        CommandLine cmdLine = new CommandLine(executable);
        cmdLine.addArgument("-P");
        DefaultExecutor executor = new DefaultExecutor();
        //executor.setExitValues(new int[]{451, 301});

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);

        try {
            exitValue = executor.execute(cmdLine);
        } catch (ExecuteException ee) {
            exitValue = ee.getExitValue();
            logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue);
            return ports;
        } finally {
            output = outputStream.toString();
        }

        /*
         if (exitValue == 301) {
         return ports;
         }
         */
        //            System.out.println("output: " + output);
        Scanner scanner = new Scanner(output);

        while (scanner.hasNextLine()) {
            ports.add(scanner.nextLine());
        }

        return ports;
    } catch (IOException ioe) {
        logger.log(Level.SEVERE, null, ioe);
        return null;
    }
}

From source file:eu.creatingfuture.propeller.blocklyprop.propeller.PropellerLoad.java

protected boolean loadIntoEeprom(String executable, File eepromFile, String comPort) {
    try {//  w  w  w.j av a  2  s. c om
        Map map = new HashMap();
        map.put("eepromFile", eepromFile);

        CommandLine cmdLine = new CommandLine(executable);
        cmdLine.addArgument("-r");
        cmdLine.addArgument("-e");
        if (comPort != null) {
            cmdLine.addArgument("-p").addArgument(comPort);
        }
        cmdLine.addArgument("${eepromFile}");

        cmdLine.setSubstitutionMap(map);
        DefaultExecutor executor = new DefaultExecutor();
        //executor.setExitValues(new int[]{451, 301});

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);

        try {
            exitValue = executor.execute(cmdLine);
        } catch (ExecuteException ee) {
            exitValue = ee.getExitValue();
            logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue);
            success = false;
            return false;
        } finally {
            output = outputStream.toString();
        }

        success = true;
        return true;
    } catch (IOException ioe) {
        logger.log(Level.SEVERE, null, ioe);
        success = false;
        return false;
    }
}

From source file:eu.creatingfuture.propeller.webLoader.propeller.PropellerLoad.java

protected List<String> getPorts(String executable) {
    List<String> ports = new ArrayList<String>();
    try {// w ww  .  j  a va 2  s. c  om
        CommandLine cmdLine = new CommandLine(executable);
        cmdLine.addArgument("-P");
        DefaultExecutor executor = new DefaultExecutor();
        //executor.setExitValues(new int[]{451, 301});

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);

        try {
            exitValue = executor.execute(cmdLine);
        } catch (ExecuteException ee) {
            exitValue = ee.getExitValue();
            logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue);
            return ports;
        } finally {
            output = outputStream.toString();
        }

        /*
         if (exitValue == 301) {
         return ports;
         }
         */
        //            System.out.println("output: " + output);
        Scanner scanner = new Scanner(output);

        while (scanner.hasNextLine()) {
            ports.add(scanner.nextLine());
        }

        return ports;
    } catch (IOException ioe) {
        logger.log(Level.SEVERE, null, ioe);
        return null;
    }
}

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   w  w w  . ja  v a2s.  c  om*/
    }
    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:com.adaptris.hpcc.SprayDirectoryToThor.java

@Override
public void produce(AdaptrisMessage msg, ProduceDestination destination) throws ProduceException {
    int exit = 0;
    // Create DFU command
    // dfuplus action=spray srcfile=/var/lib/HPCCSystems/mydropzone/historical-weather/adapter-agility-historic-out/*
    // dstcluster=mythor dstname=zzlc::json::historical_weather_04 overwrite=1 PREFIX=FILENAME,FILESIZE
    // server= nosplit=1 username= password=
    try {/* w  w  w.j  av a2  s . c o m*/
        CommandLine commandLine = createSprayCommand(msg);
        commandLine.addArgument(String.format("srcfile=%s", getSource(msg)));
        commandLine.addArgument(String.format("dstname=%s", destination.getDestination(msg)));
        if (!isBlank(getPrefix())) {
            commandLine.addArgument(String.format("PREFIX=%s", getPrefix()));
        }
        commandLine.addArgument("nosplit=1");
        log.trace("Executing {}", commandLine);
        execute(commandLine);
        postSprayCleanup(msg);
    } catch (Exception e) {
        throw ExceptionHelper.wrapProduceException(e);
    }
}