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

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

Introduction

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

Prototype

public CommandLine(final CommandLine other) 

Source Link

Document

Copy constructor.

Usage

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

static public String[] getAvailableAndroidDevices() {

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    try {// w  w w. ja va  2s  .  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 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 startEmulator(String deviceName)
        throws FileNotFoundException, IOException, CorruptConfigException {
    HashMap<String, String> settings = Config.readConfig();
    String emulator = settings.get("sdk_path") + getSlash() + "tools" + getSlash();
    if (getOperatingSystem().startsWith("windows"))
        emulator += "emulator-arm.exe";
    else/*from  w w  w.  j  av a 2  s . c o  m*/
        emulator += "emulator";
    CommandLine cmdLine = new CommandLine(emulator);
    // If the Windows path has spaces, we don't even want to deal with it
    // will ignore parameters, known issue
    if (getOperatingSystem().startsWith("windows")) {
        cmdLine.addArgument("@" + deviceName.toLowerCase().replace(".avd", ""));
        cmdLine.addArgument("-scale");
        if (Config.getAndroidEmulatorScreenSize() == 0)
            cmdLine.addArgument("1");
        else if (Config.getAndroidEmulatorScreenSize() > 3)
            cmdLine.addArgument("3");
        else
            cmdLine.addArgument(Integer.toString(Config.getAndroidEmulatorScreenSize()));
    } else {
        cmdLine.addArgument("-avd", false);
        cmdLine.addArgument(deviceName.toLowerCase().replace(".avd", ""), false);
        cmdLine.addArgument("-scale");
        if (Config.getAndroidEmulatorScreenSize() < 1)
            cmdLine.addArgument("1");
        else if (Config.getAndroidEmulatorScreenSize() > 3)
            cmdLine.addArgument("3");
        else
            cmdLine.addArgument(Integer.toString(Config.getAndroidEmulatorScreenSize()));
    }
    if (!(settings.get("proxy_host").equals("") || settings.get("proxy_port").equals(""))) {
        cmdLine.addArgument("-http-proxy");
        cmdLine.addArgument("http://" + settings.get("proxy_host") + ":" + settings.get("proxy_port"));
    }
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    DefaultExecutor executor = new DefaultExecutor();
    try {
        executor.setStreamHandler(psh);
        executor.execute(cmdLine);
        return Constants.SUCCESS;
    } catch (ExecuteException e) {
        return stdout.toString();
    } catch (IOException e) {
        return Constants.INVALID_SDK_PATH;
    }
}

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

static public String startAndroidSDKManager()
        throws FileNotFoundException, IOException, CorruptConfigException {

    HashMap<String, String> settings = Config.readConfig();
    String emulator = "";
    if (getOperatingSystem().startsWith("windows"))
        if (Emulator.doesToolExist("", "AVD Manager.exe"))
            emulator = settings.get("sdk_path") + getSlash() + "AVD Manager.exe";
        else//from   w w w  .ja  v a  2s.  c  o  m
            emulator = settings.get("sdk_path") + getSlash() + "tools" + getSlash() + "android";
    else
        emulator = settings.get("sdk_path") + getSlash() + "tools" + getSlash() + "android";
    CommandLine cmdLine = new CommandLine(emulator);

    DefaultExecutor executor = new DefaultExecutor();
    try {
        executor.execute(cmdLine);
        return Constants.SUCCESS;
    } catch (ExecuteException e) {
        return Constants.UNEXPECTED_ERROR;
    } catch (IOException e) {
        return Constants.UNEXPECTED_ERROR;
    }
}

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

static public String pushAppOntoDevice(String appPath) {

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    CommandLine cmdLine = new CommandLine(sdkPath + getSlash() + "platform-tools" + getSlash() + "adb");
    cmdLine.addArgument("install");
    cmdLine.addArgument(appPath, false);
    DefaultExecutor executor = new DefaultExecutor();
    try {/*w w w .  ja  va  2  s. co  m*/
        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.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);/*  w  w w. j  a  va 2 s.  co m*/
    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.rbr8.script_runner.util.BatchScriptRunner.java

public void processFile(File file) throws IOException {

    CommandLine cmdLine = new CommandLine(executable);
    cmdLine.addArguments(arguments);//  www  . j  a v a 2s .  c o  m

    Map<String, Object> map = new HashMap<>();
    map.put(SubstitutionHelper.FILE, file);
    cmdLine.setSubstitutionMap(map);

    DefaultExecutor executor = new DefaultExecutor();
    //        executor.setExitValue(1);

    //        NotifierLogOutputStream outputLog = new NotifierLogOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(logOutputStream);
    executor.setStreamHandler(psh);

    //        ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    //        executor.setWatchdog(watchdog);
    int exitValue = executor.execute(cmdLine);
}

From source file:org.sculptor.maven.plugin.GraphvizMojo.java

/**
 * Builds command line for starting the <code>dot</code>.
 * /*from  w  w  w .j a va  2s . c o m*/
 * @param generatedFiles
 *            list of generated files from the
 *            {@link AbstractGeneratorMojo#statusFile}
 */
protected CommandLine getDotCommandLine(Set<String> generatedFiles) throws MojoExecutionException {
    CommandLine cl = new CommandLine(command);
    if (isVerbose()) {
        cl.addArgument("-v");
    }
    cl.addArgument("-Tpng");
    cl.addArgument("-O");
    for (String generatedFile : generatedFiles) {
        if (generatedFile.endsWith(".dot")) {
            cl.addArgument(generatedFile);
        }
    }
    if (isVerbose()) {
        getLog().info("Commandline: " + cl);
    }
    return cl;
}

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

@Override
public void init() throws Exception {

    // SwfTools settings
    for (final Map.Entry<String, String> entry : System.getenv().entrySet()) {
        if ("path".equals(entry.getKey().toLowerCase())) {
            try {
                CommandLine commandLine = new CommandLine("ffmpeg");
                commandLine.addArgument("-version");
                ExternalExecution.exec(commandLine, Config.init().doNotDisplayErrorTrace());
                isActivated = true;/* w  ww.j  a  va 2  s. co m*/
            } catch (final Exception e) {
                // FFmpeg is not installed
                System.err.println("ffmpeg is not installed");
            }
        }
    }
}

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.service.JsonPdfToolManager.java

@Override
public void init() throws Exception {

    // pdf2json settings
    for (final Map.Entry<String, String> entry : System.getenv().entrySet()) {
        if ("path".equals(entry.getKey().toLowerCase())) {
            try {
                CommandLine commandLine = new CommandLine("pdf2json");
                commandLine.addArgument("-v");
                ExternalExecution.exec(commandLine,
                        Config.init().successfulExitStatusValueIs(1).doNotDisplayErrorTrace());
                isActivated = true;//from  ww  w.j av  a 2 s  . c o  m
            } catch (final Exception e) {
                // pdf2json is not installed
                System.err.println("pdf2json is not installed");
            }
        }
    }
}