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

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

Introduction

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

Prototype

public String getExecutable() 

Source Link

Document

Returns the executable.

Usage

From source file:com.dangdang.ddframe.job.cloud.scheduler.mesos.TaskLaunchProcessor.java

private Protos.CommandInfo buildCommand(final Protos.CommandInfo.URI uri, final String bootstrapScript,
        final ShardingContexts shardingContexts, final boolean useDefaultExecutor) {
    Protos.CommandInfo.Builder result = Protos.CommandInfo.newBuilder().addUris(uri).setShell(true);
    if (useDefaultExecutor) {
        CommandLine commandLine = CommandLine.parse(bootstrapScript);
        commandLine.addArgument(GsonFactory.getGson().toJson(shardingContexts), false);
        result.setValue(Joiner.on(" ").join(commandLine.getExecutable(),
                Joiner.on(" ").join(commandLine.getArguments())));
    } else {/*from w  w  w.j  a  v a 2s .c o  m*/
        result.setValue(bootstrapScript);
    }
    return result.build();
}

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

public void testGetExecutablePath() throws IOException {

    ExecMojo realMojo = new ExecMojo();

    File workdir = new File(System.getProperty("user.dir"));
    Map enviro = new HashMap();

    String myJavaPath = "target" + File.separator + "javax";
    File f = new File(myJavaPath);
    assertTrue("file created...", f.createNewFile());
    assertTrue("file exists...", f.exists());

    realMojo.setExecutable(myJavaPath);//from ww  w  .jav  a 2s  . c o  m

    CommandLine cmd = realMojo.getExecutablePath(enviro, workdir);
    assertTrue("File exists so path is absolute",
            cmd.getExecutable().startsWith(System.getProperty("user.dir")));

    f.delete();
    assertFalse("file deleted...", f.exists());
    cmd = realMojo.getExecutablePath(enviro, workdir);
    assertEquals("File doesn't exist. Let the system find it (in that PATH?)", myJavaPath, cmd.getExecutable());

    if (OS.isFamilyWindows()) //how to make this part of the test run on other platforms as well??
    {

        myJavaPath = "target" + File.separator + "javax.bat";
        f = new File(myJavaPath);
        assertTrue("file created...", f.createNewFile());
        assertTrue("file exists...", f.exists());

        realMojo.setExecutable("javax.bat");
        cmd = realMojo.getExecutablePath(enviro, workdir);
        assertTrue("is bat file on windows, execute using cmd.", cmd.getExecutable().equals("cmd"));

        enviro.put("PATH", workdir.getAbsolutePath() + File.separator + "target");
        cmd = realMojo.getExecutablePath(enviro, workdir);
        assertTrue("is bat file on windows' PATH, execute using cmd.", cmd.getExecutable().equals("cmd"));
        f.delete();
        assertFalse("file deleted...", f.exists());
    }
}

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 w ww.j a  v  a  2 s  .c om*/
    }
    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: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 w  w .  java2 s  .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);//  w  w w  . j av a2  s.  c o  m
    }
    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: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();//ww  w .j  a v  a  2  s. c  om

    // 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 testGetExecutablePath() throws IOException {

    ExecMojo realMojo = new ExecMojo();

    File workdir = new File(System.getProperty("user.dir"));
    Map<String, String> enviro = new HashMap<String, String>();

    String myJavaPath = "target" + File.separator + "javax";
    File f = new File(myJavaPath);
    assertTrue("file created...", f.createNewFile());
    assertTrue("file exists...", f.exists());

    realMojo.setExecutable(myJavaPath);/*from  w ww . j  av  a2 s  . c o m*/

    CommandLine cmd = realMojo.getExecutablePath(enviro, workdir);
    assertTrue("File exists so path is absolute",
            cmd.getExecutable().startsWith(System.getProperty("user.dir")));

    f.delete();
    assertFalse("file deleted...", f.exists());
    cmd = realMojo.getExecutablePath(enviro, workdir);
    assertEquals("File doesn't exist. Let the system find it (in that PATH?)", myJavaPath, cmd.getExecutable());

    if (OS.isFamilyWindows()) // Exec Maven Plugin only supports Batch detection and PATH search on Windows
    {
        myJavaPath = "target" + File.separator + "javax.bat";
        f = new File(myJavaPath);
        assertTrue("file created...", f.createNewFile());
        assertTrue("file exists...", f.exists());

        final String comSpec = System.getenv("ComSpec");

        realMojo.setExecutable("javax.bat");
        cmd = realMojo.getExecutablePath(enviro, workdir);
        assertTrue("is bat file on windows, execute using ComSpec.", cmd.getExecutable().equals(comSpec));

        enviro.put("PATH", workdir.getAbsolutePath() + File.separator + "target");
        cmd = realMojo.getExecutablePath(enviro, workdir);
        assertTrue("is bat file on windows' PATH, execute using ComSpec.", cmd.getExecutable().equals(comSpec));
        f.delete();
        assertFalse("file deleted...", f.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 www.j a v  a  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.core.launch.execution.ScriptExecutor.java

public void execute(CommandLine cmd, Map<String, String> env) {
    try {/*from   ww w .j  av  a  2s.c o m*/
        for (ExecutionResponseListener handler : listeners) {
            handler.executionAboutToStart();
        }

        Logger.debug("executing command using executable: " + cmd.getExecutable()); //$NON-NLS-1$
        executor.setExitValue(0);
        executor.execute(cmd, env, handler);

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

        handler.waitFor();
    } catch (Exception e) {
        for (ExecutionResponseListener handler : listeners) {
            handler.executionFailed("", e); //$NON-NLS-1$
        }
    }
}

From source file:org.eclipse.php.composer.core.launch.ScriptLauncher.java

public void launch(String argument, String[] params)
        throws ExecuteException, IOException, InterruptedException {
    CommandLine cmd = environment.getCommand();
    cmd.addArgument(argument);/*www. j  a v a2  s  . c om*/
    cmd.addArguments(params);

    executor = new ScriptExecutor();

    if (timeout != null) {
        executor.setTimeout(timeout);
    }

    Logger.debug("Setting executor working directory to " + project.getLocation().toOSString()); //$NON-NLS-1$
    executor.setWorkingDirectory(project.getLocation().toFile());

    for (ExecutionResponseListener listener : listeners) {
        executor.addResponseListener(listener);
    }

    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());

    executor.execute(cmd, env);
}