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

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

Introduction

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

Prototype

public String[] getArguments() 

Source Link

Document

Returns the expanded and quoted command line arguments.

Usage

From source file:com.tascape.qa.th.android.comm.Adb.java

public List<String> adb(final List<Object> arguments) throws IOException {
    CommandLine cmdLine = new CommandLine(ADB);
    if (!this.serial.isEmpty()) {
        cmdLine.addArgument("-s");
        cmdLine.addArgument(serial);/*from w ww .  j  av a  2s .co m*/
    }
    arguments.forEach((arg) -> {
        cmdLine.addArgument(arg + "");
    });
    LOG.debug("[{} {}]", cmdLine.getExecutable(), StringUtils.join(cmdLine.getArguments(), " "));
    List<String> output = new ArrayList<>();
    Executor executor = new DefaultExecutor();
    executor.setStreamHandler(new ESH(output));
    if (executor.execute(cmdLine) != 0) {
        throw new IOException(cmdLine + " failed");
    }
    return output;
}

From source file:com.tascape.qa.th.android.comm.Adb.java

public ExecuteWatchdog adbAsync(final List<Object> arguments, long timeoutMillis) throws IOException {
    CommandLine cmdLine = new CommandLine(ADB);
    if (!this.serial.isEmpty()) {
        cmdLine.addArgument("-s");
        cmdLine.addArgument(serial);//from w  w w. j  a v  a2  s .c  om
    }
    arguments.forEach((arg) -> {
        cmdLine.addArgument(arg + "");
    });
    LOG.debug("[{} {}]", cmdLine.getExecutable(), StringUtils.join(cmdLine.getArguments(), " "));
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeoutMillis);
    Executor executor = new DefaultExecutor();
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(new ESH());
    executor.execute(cmdLine, new DefaultExecuteResultHandler());

    return watchdog;
}

From source file:net.hasor.maven.ExecMojoTest.java

private String getCommandLineAsString(CommandLine commandline) {
    //for the sake of the test comparisons, cut out the eventual
    //cmd /c *.bat conversion
    String result = commandline.getExecutable();
    boolean isCmd = false;
    if (OS.isFamilyWindows() && result.equals("cmd")) {
        result = "";
        isCmd = true;/*from  ww  w.j av  a 2s  . c  o  m*/
    }
    String[] arguments = commandline.getArguments();
    for (int i = 0; i < arguments.length; i++) {
        String arg = arguments[i];
        if (isCmd && i == 0 && "/c".equals(arg)) {
            continue;
        }
        if (isCmd && i == 1 && arg.endsWith(".bat")) {
            arg = arg.substring(0, arg.length() - ".bat".length());
        }
        result += (result.length() == 0 ? "" : " ") + arg;
    }
    return result;
}

From source file:org.apache.zeppelin.python.PythonInterpreter.java

private void createGatewayServerAndStartScript() throws IOException {
    // start gateway server in JVM side
    int port = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces();
    // use the FQDN as the server address instead of 127.0.0.1 so that python process in docker
    // container can also connect to this gateway server.
    String serverAddress = PythonUtils.getLocalIP(properties);
    String secret = PythonUtils.createSecret(256);
    this.gatewayServer = PythonUtils.createGatewayServer(this, serverAddress, port, secret, usePy4jAuth);
    gatewayServer.start();/*from   www  . j a  v a  2s  .c  o  m*/

    // launch python process to connect to the gateway server in JVM side
    createPythonScript();
    String pythonExec = getPythonExec();
    CommandLine cmd = CommandLine.parse(pythonExec);
    if (!pythonExec.endsWith(".py")) {
        // PythonDockerInterpreter set pythonExec with script
        cmd.addArgument(pythonWorkDir + "/zeppelin_python.py", false);
    }
    cmd.addArgument(serverAddress, false);
    cmd.addArgument(Integer.toString(port), false);

    executor = new DefaultExecutor();
    outputStream = new InterpreterOutputStream(LOGGER);
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
    Map<String, String> env = setupPythonEnv();
    if (usePy4jAuth) {
        env.put("PY4J_GATEWAY_SECRET", secret);
    }
    LOGGER.info("Launching Python Process Command: " + cmd.getExecutable() + " "
            + StringUtils.join(cmd.getArguments(), " "));
    executor.execute(cmd, env, this);
    pythonScriptRunning.set(true);
}

From source file:org.codehaus.mojo.exec.ExecMojoTest.java

public void testGetExecutablePathPreferExecutableExtensionsOnWindows() throws IOException {
    // this test is for Windows
    if (!OS.isFamilyWindows()) {
        return;/*from  w ww. j av a 2s .  c o m*/
    }
    final ExecMojo realMojo = new ExecMojo();

    final String tmp = System.getProperty("java.io.tmpdir");
    final File workdir = new File(tmp, "testGetExecutablePathPreferExecutableExtensionsOnWindows");
    workdir.mkdirs();

    final Map<String, String> enviro = new HashMap<String, String>();

    final File f = new File(workdir, "mycmd");
    final File fCmd = new File(workdir, "mycmd.cmd");
    f.createNewFile();
    fCmd.createNewFile();
    assertTrue("file exists...", f.exists());
    assertTrue("file exists...", fCmd.exists());

    realMojo.setExecutable("mycmd");

    final CommandLine cmd = realMojo.getExecutablePath(enviro, workdir);
    // cmdline argumets are: [/c, %path-to-temp%\mycmd.cmd], so check second argument
    assertTrue("File should have cmd extension", cmd.getArguments()[1].endsWith("mycmd.cmd"));

    f.delete();
    fCmd.delete();
    assertFalse("file deleted...", f.exists());
    assertFalse("file deleted...", fCmd.exists());

}

From source file:org.codehaus.mojo.exec.ExecMojoTest.java

private String getCommandLineAsString(CommandLine commandline) {
    // for the sake of the test comparisons, cut out the eventual
    // cmd /c *.bat conversion
    String result = commandline.getExecutable();
    boolean isCmd = false;
    if (OS.isFamilyWindows() && result.equals("cmd")) {
        result = "";
        isCmd = true;/*from w w  w . ja  va  2  s. c  o m*/
    }
    String[] arguments = commandline.getArguments();
    for (int i = 0; i < arguments.length; i++) {
        String arg = arguments[i];
        if (isCmd && i == 0 && "/c".equals(arg)) {
            continue;
        }
        if (isCmd && i == 1 && arg.endsWith(".bat")) {
            arg = arg.substring(0, arg.length() - ".bat".length());
        }
        result += (result.length() == 0 ? "" : " ") + arg;
    }
    return result;
}

From source file:org.eclipse.php.composer.ui.terminal.ComposerLauncher.java

@Override
public void launch(String argument, String... params)
        throws ExecuteException, IOException, InterruptedException {
    CommandLine cmd = environment.getCommand();
    cmd.addArgument(argument);//from   www.j  a  v  a2s . c om
    cmd.addArguments(params);

    Map<String, String> env = new HashMap<String, String>(System.getenv());
    PHPLaunchUtilities.appendExecutableToPathEnv(env, new File(cmd.getExecutable()).getParentFile());
    PHPLaunchUtilities.appendLibrarySearchPathEnv(env, new File(cmd.getExecutable()).getParentFile());

    Map<String, Object> properties = new HashMap<String, Object>();
    List<String> envs = new ArrayList<>(env.size());
    for (String key : env.keySet()) {
        envs.add(key + '=' + env.get(key));
    }
    properties.put(ITerminalsConnectorConstants.PROP_PROCESS_ENVIRONMENT,
            envs.toArray(new String[envs.size()]));
    properties.put(ITerminalsConnectorConstants.PROP_PROCESS_MERGE_ENVIRONMENT, true);
    ITerminalServiceOutputStreamMonitorListener[] outListeners = new ITerminalServiceOutputStreamMonitorListener[] {
            new ITerminalServiceOutputStreamMonitorListener() {

                @Override
                public void onContentReadFromStream(byte[] byteBuffer, int bytesRead) {
                    for (ExecutionResponseListener handler : getListeners()) {
                        handler.executionMessage(new String(byteBuffer, 0, bytesRead));
                    }
                }

            } };
    properties.put(ITerminalsConnectorConstants.PROP_STDOUT_LISTENERS, outListeners);

    // workaround for colored output on Windows
    if (Platform.OS_WIN32.equals(Platform.getOS())) {
        StringBuilder builder = new StringBuilder();
        builder.append("@echo off").append(WINDOWS_END_OF_LINE); // $NON-NLS-1$

        // 65001 - UTF-8
        builder.append("chcp 65001").append(WINDOWS_END_OF_LINE); // $NON-NLS-1$

        builder.append("cls").append(WINDOWS_END_OF_LINE); //$NON-NLS-1$
        builder.append(escapePath(cmd.getExecutable())).append(' ');
        for (String arg : cmd.getArguments()) {
            builder.append(arg).append(' ');
        }

        File file = File.createTempFile("composer_windows_", ".bat"); // $NON-NLS-1$ //$NON-NLS-2$
        file.deleteOnExit();
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) {
            writer.write(builder.toString());
        } catch (FileNotFoundException ex) {
            ComposerPlugin.logException(ex);
        }

        properties.put(ITerminalsConnectorConstants.PROP_PROCESS_PATH, "cmd"); //$NON-NLS-1$
        String args = "/C " + file.getAbsolutePath(); //$NON-NLS-1$
        properties.put(ITerminalsConnectorConstants.PROP_PROCESS_ARGS, args);
    } else {
        properties.put(ITerminalsConnectorConstants.PROP_PROCESS_PATH, escapePath(cmd.getExecutable()));

        StringBuilder builder = new StringBuilder();
        for (String arg : cmd.getArguments()) {
            builder.append(arg + ' ');
        }
        properties.put(ITerminalsConnectorConstants.PROP_PROCESS_ARGS, builder.toString());
    }

    Logger.debug("Setting executor working directory to " + project.getLocation().toOSString()); //$NON-NLS-1$
    properties.put(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR, project.getLocation().toOSString());

    String title = MessageFormat.format(Messages.ComposerConsoleManager_ConsoleLabel,
            Messages.ComposerConsoleManager_ConsoleName, project.getName());
    IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();

    final CountDownLatch latch = new CountDownLatch(1);
    terminalConsole = new TerminalConsole(title, 0, properties, new Done() {

        @Override
        public void done(IStatus status) {
            latch.countDown();
        }
    });

    for (ExecutionResponseListener handler : getListeners()) {
        handler.executionStarted();
    }

    consoleManager.addConsoles(new IConsole[] { terminalConsole });
    consoleManager.showConsoleView(terminalConsole);

    latch.await();

    for (ExecutionResponseListener handler : getListeners()) {
        handler.executionFinished("", 0); //$NON-NLS-1$
    }
}

From source file:org.fornax.toolsupport.sculptor.maven.plugin.GeneratorMojoTest.java

public void testGeneratorCommandLineNoChangedFiles() throws Exception {
    GeneratorMojo mojo = createMojo(createProject("test1"));

    CommandLine commandline = mojo.getGeneratorCommandLine(null);
    assertNotNull(commandline);/*from  ww w  . j ava  2s. c  o m*/
    String[] arguments = commandline.getArguments();
    assertEquals(8, arguments.length);
    assertEquals("-client", arguments[0]);
    assertEquals("-Xms100m", arguments[1]);
    assertEquals("-Xmx500m", arguments[2]);
    assertTrue(arguments[3].startsWith("-D" + GeneratorMojo.LOGBACK_CONFIGURATION_FILE_PROPERTY + "="));
    assertTrue(arguments[3].endsWith(GeneratorMojo.LOGBACK_NORMAL_CONFIGURATION_FILE_NAME));
    assertEquals("-classpath", arguments[4]);
    assertEquals(mojo.getGeneratorClasspath(), arguments[5]);
    assertEquals(GeneratorMojo.MWE2_WORKFLOW_LAUNCHER, arguments[6]);
    assertEquals(mojo.getWorkflowDescriptor(), arguments[7]);
}

From source file:org.fornax.toolsupport.sculptor.maven.plugin.GeneratorMojoTest.java

public void testGeneratorCommandLineWithChangedFiles() throws Exception {
    GeneratorMojo mojo = createMojo(createProject("test1"));
    Set<String> changedFiles = new HashSet<String>();
    changedFiles.add("file1");
    changedFiles.add("file2");
    changedFiles.add("file3");

    CommandLine commandline = mojo.getGeneratorCommandLine(changedFiles);
    assertNotNull(commandline);/*from  ww w. ja v  a 2  s . co  m*/
    String[] arguments = commandline.getArguments();
    assertEquals(9, arguments.length);
    assertEquals("-client", arguments[0]);
    assertEquals("-Xms100m", arguments[1]);
    assertEquals("-Xmx500m", arguments[2]);
    assertTrue(arguments[3].startsWith("-D" + GeneratorMojo.CHANGED_FILES_PROPERTY + "="));
    assertTrue(arguments[3].contains("file1"));
    assertTrue(arguments[3].contains("file2"));
    assertTrue(arguments[3].contains("file3"));
    assertTrue(arguments[4].startsWith("-D" + GeneratorMojo.LOGBACK_CONFIGURATION_FILE_PROPERTY + "="));
    assertTrue(arguments[4].endsWith(GeneratorMojo.LOGBACK_NORMAL_CONFIGURATION_FILE_NAME));
    assertEquals("-classpath", arguments[5]);
    assertEquals(mojo.getGeneratorClasspath(), arguments[6]);
    assertEquals(GeneratorMojo.MWE2_WORKFLOW_LAUNCHER, arguments[7]);
    assertEquals(mojo.getWorkflowDescriptor(), arguments[8]);
}

From source file:org.fornax.toolsupport.sculptor.maven.plugin.GraphvizMojoTest.java

public void testDotCommandLine() throws Exception {
    GraphvizMojo mojo = createMojo(createProject("test1"));
    setVariableValueToObject(mojo, "verbose", true);

    Set<String> changedDotFiles = new HashSet<String>();
    changedDotFiles.add("file1.dot");
    changedDotFiles.add("file2.dot");
    changedDotFiles.add("file3.dot");

    CommandLine commandline = mojo.getDotCommandLine(changedDotFiles);
    assertNotNull(commandline);//from   w  w w  .  j  a  va 2s .  co m
    String[] arguments = commandline.getArguments();
    assertEquals(6, arguments.length);
    assertEquals("-q", arguments[0]);
    assertEquals("-Tpng", arguments[1]);
    assertEquals("-O", arguments[2]);
    assertEquals("file1.dot", arguments[3]);
    assertEquals("file2.dot", arguments[4]);
    assertEquals("file3.dot", arguments[5]);
}