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, final boolean handleQuoting) 

Source Link

Document

Add a single argument.

Usage

From source file:org.openhab.io.net.exec.ExecUtil.java

/**
 * <p>// ww w . j a va  2  s .  co  m
 * Executes <code>commandLine</code>. Sometimes (especially observed on
 * MacOS) the commandLine isn't executed properly. In that cases another
 * exec-method is to be used. To accomplish this please use the special
 * delimiter '<code>@@</code>'. If <code>commandLine</code> contains this
 * delimiter it is split into a String[] array and the special exec-method
 * is used.
 * </p>
 * <p>
 * A possible {@link IOException} gets logged but no further processing is
 * done.
 * </p>
 * 
 * @param commandLine
 *            the command line to execute
 * @param timeout
 *            timeout for execution in milliseconds
 * @return response data from executed command line
 */
public static String executeCommandLineAndWaitResponse(String commandLine, int timeout) {
    String retval = null;

    CommandLine cmdLine = null;

    if (commandLine.contains(CMD_LINE_DELIMITER)) {
        String[] cmdArray = commandLine.split(CMD_LINE_DELIMITER);
        cmdLine = new CommandLine(cmdArray[0]);

        for (int i = 1; i < cmdArray.length; i++) {
            cmdLine.addArgument(cmdArray[i], false);
        }
    } else {
        cmdLine = CommandLine.parse(commandLine);
    }

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    Executor executor = new DefaultExecutor();

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(stdout);

    executor.setExitValue(1);
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(watchdog);

    try {
        executor.execute(cmdLine, resultHandler);
        logger.debug("executed commandLine '{}'", commandLine);
    } catch (ExecuteException e) {
        logger.error("couldn't execute commandLine '" + commandLine + "'", e);
    } catch (IOException e) {
        logger.error("couldn't execute commandLine '" + commandLine + "'", e);
    }

    // some time later the result handler callback was invoked so we
    // can safely request the exit code
    try {
        resultHandler.waitFor();
        int exitCode = resultHandler.getExitValue();
        retval = StringUtils.chomp(stdout.toString());
        if (resultHandler.getException() != null) {
            logger.warn(resultHandler.getException().getMessage());
        } else {
            logger.debug("exit code '{}', result '{}'", exitCode, retval);
        }

    } catch (InterruptedException e) {
        logger.error("Timeout occured when executing commandLine '" + commandLine + "'", e);
    }

    return retval;
}

From source file:org.owasp.goatdroid.gui.emulator.Emulator.java

static public boolean isAndroidDeviceAvailable() {

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    try {/*w  w  w . j  a v  a2 s.c om*/

        CommandLine cmdLine = new CommandLine(
                Config.getSDKPath() + getSlash() + "platform-tools" + getSlash() + "adb");
        cmdLine.addArgument("devices", false);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(psh);
        executor.execute(cmdLine);
        String stringy = stdout.toString();
        stdout.close();
        /*
         * Shell command response will be 27 characters in length if there
         * are no devices currently attached.
         * 
         * Output is: List of devices attached \n\n
         */
        if (stringy.length() > 27)
            return true;
        else
            return false;
    } catch (CorruptConfigException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
}

From source file:org.owasp.goatdroid.gui.emulator.Emulator.java

static public String[] getAvailableAndroidDevices() {

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    try {/* ww w  .java 2 s.c o m*/

        CommandLine cmdLine = new CommandLine(
                Config.getSDKPath() + getSlash() + "platform-tools" + getSlash() + "adb");
        cmdLine.addArgument("devices", false);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(psh);
        executor.execute(cmdLine);
        String output = stdout.toString();
        /*
         * Shell command response will be 27 characters in length if there
         * are no devices currently attached.
         * 
         * Output is: List of devices attached \n\n
         */
        if (output.length() > 27) {
            List<String> devices = new ArrayList<String>(Arrays.asList(output.split("\n")));
            /*
             * This removes the first line.. We don't care about the first
             * one [0] because that's just the first line (List of devices
             * attached \n)
             */
            devices.remove(0);
            for (int count = 0; count < devices.size(); count++) {
                devices.set(count, devices.get(count).replaceAll("\t.*", "").trim());
            }
            return devices.toArray(new String[devices.size()]);
            /*
             * If there were no devices, return an empty list
             */
        } else {
            return new String[0];
        }
    }

    catch (CorruptConfigException e) {
        return new String[0];
    } catch (IOException e) {
        return new String[0];
    }
}

From source file:org.owasp.goatdroid.gui.emulator.EmulatorWorker.java

static public String pushAppOntoDevice(String appPath, String deviceSerial) {

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    CommandLine cmdLine = new CommandLine(sdkPath + getSlash() + "platform-tools" + getSlash() + "adb");
    Map<String, String> map = new HashMap<String, String>();
    map.put("deviceSerial", deviceSerial);
    cmdLine.addArgument("-s", false);
    cmdLine.addArgument("${deviceSerial}", false);
    cmdLine.addArgument("install", false);
    cmdLine.addArgument(appPath, false);
    cmdLine.setSubstitutionMap(map);//from   ww  w. ja v  a  2  s.com
    DefaultExecutor executor = new DefaultExecutor();
    try {
        executor.setStreamHandler(psh);
        executor.execute(cmdLine);
        return stdout.toString();
    } catch (ExecuteException e) {
        return Constants.UNEXPECTED_ERROR;
    } catch (IOException e) {
        return Constants.UNEXPECTED_ERROR;
    }
}

From source file:org.silverpeas.core.io.media.video.ffmpeg.FFmpegUtil.java

static CommandLine buildFFmpegThumbnailExtractorCommandLine(File inputFile, File outputFile, double position) {
    Map<String, File> files = new HashMap<>(2);
    files.put("inputFile", inputFile);
    files.put("outputFile", outputFile);
    CommandLine commandLine = new CommandLine("ffmpeg");
    // Time of extract in seconds
    commandLine.addArgument("-ss", false);
    commandLine.addArgument(Double.toString(position), false);
    commandLine.addArgument("-i", false);
    commandLine.addArgument("${inputFile}", false);
    // Only one frame
    commandLine.addArgument("-vframes", false);
    commandLine.addArgument("1", false);
    // Resize/scale of output picture keeping aspect ratio
    commandLine.addArgument("-vf", false);
    commandLine.addArgument("scale=600:-1", false);
    commandLine.addArgument("${outputFile}", false);
    commandLine.setSubstitutionMap(files);
    return commandLine;
}

From source file:org.silverpeas.core.viewer.util.JsonPdfUtil.java

static CommandLine buildJsonPdfCommandLine(File inputFile, File outputFile) {
    Map<String, File> files = new HashMap<String, File>(2);
    files.put("inputFile", inputFile);
    files.put("outputFile", outputFile);
    CommandLine commandLine = new CommandLine("pdf2json");
    commandLine.addArgument("${inputFile}", false);
    commandLine.addArguments(PDF_TO_JSON_COMMON_PARAMS, false);
    commandLine.addArgument("${outputFile}", false);
    commandLine.setSubstitutionMap(files);
    return commandLine;
}

From source file:org.silverpeas.core.viewer.util.SwfUtil.java

static CommandLine buildPdfToSwfCommandLine(final String endingCommand, File inputFile, File outputFile) {
    Map<String, File> files = new HashMap<>(2);
    files.put("inputFile", inputFile);
    files.put("outputFile", outputFile);
    CommandLine commandLine = new CommandLine("pdf2swf");
    commandLine.addArgument("${inputFile}", false);
    commandLine.addArgument(OUTPUT_COMMAND);
    commandLine.addArgument("${outputFile}", false);
    commandLine.addArguments(TO_SWF_ENDING_COMMAND, false);
    if (StringUtil.isDefined(endingCommand)) {
        commandLine.addArguments(endingCommand, false);
    }/*from ww w.j  a va  2 s .c o m*/
    commandLine.setSubstitutionMap(files);
    return commandLine;
}

From source file:org.silverpeas.media.video.ffmpeg.FFmpegUtil.java

static CommandLine buildFFmpegThumbnailExtractorCommandLine(File inputFile, File outputFile, int seconds) {
    Map<String, File> files = new HashMap<String, File>(2);
    files.put("inputFile", inputFile);
    files.put("outputFile", outputFile);
    CommandLine commandLine = new CommandLine("ffmpeg");
    // Time of extract in seconds
    commandLine.addArgument("-ss", false);
    commandLine.addArgument(Integer.toString(seconds), false);
    commandLine.addArgument("-i", false);
    commandLine.addArgument("${inputFile}", false);
    // Only one frame
    commandLine.addArgument("-vframes", false);
    commandLine.addArgument("1", false);
    // Resize/scale of output picture keeping aspect ratio
    commandLine.addArgument("-vf", false);
    commandLine.addArgument("scale=600:-1", false);
    commandLine.addArgument("${outputFile}", false);
    commandLine.setSubstitutionMap(files);
    return commandLine;
}

From source file:org.silverpeas.viewer.util.SwfUtil.java

static CommandLine buildPdfToSwfCommandLine(final String endingCommand, File inputFile, File outputFile) {
    Map<String, File> files = new HashMap<String, File>(2);
    files.put("inputFile", inputFile);
    files.put("outputFile", outputFile);
    CommandLine commandLine = new CommandLine("pdf2swf");
    commandLine.addArgument("${inputFile}", false);
    commandLine.addArgument(OUTPUT_COMMAND);
    commandLine.addArgument("${outputFile}", false);
    commandLine.addArguments(TO_SWF_ENDING_COMMAND, false);
    if (StringUtil.isDefined(endingCommand)) {
        commandLine.addArguments(endingCommand, false);
    }//w w  w  .j  ava  2 s .co m
    commandLine.setSubstitutionMap(files);
    return commandLine;
}

From source file:org.silverpeas.viewer.util.SwfUtil.java

static CommandLine buildSwfToImageCommandLine(File inputFile, File outputFile) {
    Map<String, File> files = new HashMap<String, File>(2);
    files.put("inputFile", inputFile);
    files.put("outputFile", outputFile);
    CommandLine commandLine = new CommandLine("swfrender");
    commandLine.addArgument("${inputFile}", false);
    commandLine.addArgument(OUTPUT_COMMAND);
    commandLine.addArgument("${outputFile}", false);
    commandLine.setSubstitutionMap(files);
    return commandLine;
}