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

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

Introduction

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

Prototype

public static CommandLine parse(final String line) 

Source Link

Document

Create a command line from a string.

Usage

From source file:org.apache.stratos.integration.tests.SampleApplicationTests.java

private void executeCommand(String commandText) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {/*from   w  w w  . j av a 2 s  .  co  m*/
        CommandLine commandline = CommandLine.parse(commandText);
        DefaultExecutor exec = new DefaultExecutor();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        exec.setStreamHandler(streamHandler);
        exec.execute(commandline);
        log.info(outputStream.toString());
    } catch (Exception e) {
        log.error(outputStream.toString(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.stratos.python.cartridge.agent.integration.tests.PythonAgentIntegrationTest.java

/**
 * Execute shell command//from w  w w .j a va2s  .c  o  m
 *
 * @param commandText Command string to be executed
 */
protected ByteArrayOutputStreamLocal executeCommand(final String commandText, int timeout) {
    final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal();
    try {
        CommandLine commandline = CommandLine.parse(commandText);
        DefaultExecutor exec = new DefaultExecutor();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        exec.setWorkingDirectory(new File(PythonAgentIntegrationTest.class.getResource(PATH_SEP).getPath()
                + PATH_SEP + ".." + PATH_SEP + PYTHON_AGENT_DIR_NAME));
        exec.setStreamHandler(streamHandler);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        exec.setWatchdog(watchdog);
        exec.execute(commandline, new ExecuteResultHandler() {
            @Override
            public void onProcessComplete(int i) {
                log.info(commandText + " process completed");
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                log.error(commandText + " process failed", e);
            }
        });
        executorList.put(commandText, exec);
        return outputStream;
    } catch (Exception e) {
        log.error(outputStream.toString(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.stratos.python.cartridge.agent.test.PythonAgentTestManager.java

/**
 * Execute shell command//from w  ww .  j av  a  2 s.com
 *
 * @param commandText
 */
protected ByteArrayOutputStreamLocal executeCommand(final String commandText) {
    final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal();
    try {
        CommandLine commandline = CommandLine.parse(commandText);
        DefaultExecutor exec = new DefaultExecutor();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        exec.setWorkingDirectory(new File(PythonAgentTestManager.class.getResource(PATH_SEP).getPath()
                + PATH_SEP + ".." + PATH_SEP + PYTHON_AGENT_DIR_NAME));
        exec.setStreamHandler(streamHandler);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
        exec.setWatchdog(watchdog);
        exec.execute(commandline, new ExecuteResultHandler() {
            @Override
            public void onProcessComplete(int i) {
                log.info(commandText + " process completed");
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                log.error(commandText + " process failed", e);
            }
        });
        executorList.put(commandText, exec);
        return outputStream;
    } catch (Exception e) {
        log.error(outputStream.toString(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.stratos.python.cartridge.agent.test.PythonCartridgeAgentTest.java

/**
 * Execute shell command/*from  ww  w. jav a2  s. c  om*/
 *
 * @param commandText
 */
private ByteArrayOutputStreamLocal executeCommand(final String commandText) {
    final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal();
    try {
        CommandLine commandline = CommandLine.parse(commandText);
        DefaultExecutor exec = new DefaultExecutor();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        exec.setStreamHandler(streamHandler);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
        exec.setWatchdog(watchdog);
        exec.execute(commandline, new ExecuteResultHandler() {
            @Override
            public void onProcessComplete(int i) {
                log.info(commandText + " process completed");
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                log.error(commandText + " process failed", e);
            }
        });
        executorList.put(commandText, exec);
        return outputStream;
    } catch (Exception e) {
        log.error(outputStream.toString(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.tika.parser.ocr.TesseractOCRParser.java

/**
 * This method is used to process the image to an OCR-friendly format.
 * @param streamingObject input image to be processed
 * @param config TesseractOCRconfig class to get ImageMagick properties
 * @throws IOException if an input error occurred
 * @throws TikaException if an exception timed out
 *///  ww w .jav a2 s.com
private void processImage(File streamingObject, TesseractOCRConfig config) throws IOException, TikaException {

    // fetch rotation script from resources
    InputStream in = getClass().getResourceAsStream("rotation.py");
    TemporaryResources tmp = new TemporaryResources();
    File rotationScript = tmp.createTemporaryFile();
    Files.copy(in, rotationScript.toPath(), StandardCopyOption.REPLACE_EXISTING);

    String cmd = "python " + rotationScript.getAbsolutePath() + " -f " + streamingObject.getAbsolutePath();
    String angle = "0";

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

    // determine the angle of rotation required to make the text horizontal
    CommandLine cmdLine = CommandLine.parse(cmd);
    if (hasPython()) {
        try {
            executor.execute(cmdLine);
            angle = outputStream.toString("UTF-8").trim();
        } catch (Exception e) {

        }
    }

    // process the image - parameter values can be set in TesseractOCRConfig.properties
    String line = "convert -density " + config.getDensity() + " -depth " + config.getDepth() + " -colorspace "
            + config.getColorspace() + " -filter " + config.getFilter() + " -resize " + config.getResize()
            + "% -rotate " + angle + " " + streamingObject.getAbsolutePath() + " "
            + streamingObject.getAbsolutePath();
    cmdLine = CommandLine.parse(line);
    try {
        executor.execute(cmdLine);
    } catch (Exception e) {

    }

    tmp.close();
}

From source file:org.apache.zeppelin.interpreter.remote.RemoteInterpreterManagedProcess.java

@Override
public void start(String userName, Boolean isUserImpersonate) {
    // start server process
    try {/*from www  .  j a  va2s  .  c  o m*/
        port = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces();
    } catch (IOException e1) {
        throw new InterpreterException(e1);
    }

    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
    cmdLine.addArgument("-d", false);
    cmdLine.addArgument(interpreterDir, false);
    cmdLine.addArgument("-p", false);
    cmdLine.addArgument(Integer.toString(port), false);
    if (isUserImpersonate && !userName.equals("anonymous")) {
        cmdLine.addArgument("-u", false);
        cmdLine.addArgument(userName, false);
    }
    cmdLine.addArgument("-l", false);
    cmdLine.addArgument(localRepoDir, false);

    executor = new DefaultExecutor();

    ByteArrayOutputStream cmdOut = new ByteArrayOutputStream();
    ProcessLogOutputStream processOutput = new ProcessLogOutputStream(logger);
    processOutput.setOutputStream(cmdOut);

    executor.setStreamHandler(new PumpStreamHandler(processOutput));
    watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    executor.setWatchdog(watchdog);

    try {
        Map procEnv = EnvironmentUtils.getProcEnvironment();
        procEnv.putAll(env);

        logger.info("Run interpreter process {}", cmdLine);
        executor.execute(cmdLine, procEnv, this);
        running = true;
    } catch (IOException e) {
        running = false;
        throw new InterpreterException(e);
    }

    long startTime = System.currentTimeMillis();
    while (System.currentTimeMillis() - startTime < getConnectTimeout()) {
        if (!running) {
            try {
                cmdOut.flush();
            } catch (IOException e) {
                // nothing to do
            }
            throw new InterpreterException(new String(cmdOut.toByteArray()));
        }

        try {
            if (RemoteInterpreterUtils.checkIfRemoteEndpointAccessible("localhost", port)) {
                break;
            } else {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    logger.error("Exception in RemoteInterpreterProcess while synchronized reference "
                            + "Thread.sleep", e);
                }
            }
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Remote interpreter not yet accessible at localhost:" + port);
            }
        }
    }
    processOutput.setOutputStream(null);
}

From source file:org.apache.zeppelin.interpreter.remote.RemoteInterpreterProcess.java

public int reference(InterpreterGroup interpreterGroup) {
    synchronized (referenceCount) {
        if (executor == null) {
            // start server process
            try {
                port = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces();
            } catch (IOException e1) {
                throw new InterpreterException(e1);
            }//w  w  w .  j a  v  a  2  s . c  o  m

            CommandLine cmdLine = CommandLine.parse(interpreterRunner);
            cmdLine.addArgument("-d", false);
            cmdLine.addArgument(interpreterDir, false);
            cmdLine.addArgument("-p", false);
            cmdLine.addArgument(Integer.toString(port), false);

            executor = new DefaultExecutor();

            watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
            executor.setWatchdog(watchdog);

            running = true;
            try {
                Map procEnv = EnvironmentUtils.getProcEnvironment();
                procEnv.putAll(env);

                logger.info("Run interpreter process {}", cmdLine);
                executor.execute(cmdLine, procEnv, this);
            } catch (IOException e) {
                running = false;
                throw new InterpreterException(e);
            }

            long startTime = System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < connectTimeout) {
                if (RemoteInterpreterUtils.checkIfRemoteEndpointAccessible("localhost", port)) {
                    break;
                } else {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
            }

            clientPool = new GenericObjectPool<Client>(new ClientFactory("localhost", port));

            remoteInterpreterEventPoller.setInterpreterGroup(interpreterGroup);
            remoteInterpreterEventPoller.setInterpreterProcess(this);
            remoteInterpreterEventPoller.start();
        }
        return referenceCount.incrementAndGet();
    }
}

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

private void launchIPythonKernel(int ipythonPort) throws IOException {
    LOGGER.info("Launching IPython Kernel at port: " + ipythonPort);
    // copy the python scripts to a temp directory, then launch ipython kernel in that folder
    File pythonWorkDir = Files.createTempDirectory("zeppelin_ipython").toFile();
    String[] ipythonScripts = { "ipython_server.py", "ipython_pb2.py", "ipython_pb2_grpc.py" };
    for (String ipythonScript : ipythonScripts) {
        URL url = getClass().getClassLoader().getResource("grpc/python" + "/" + ipythonScript);
        FileUtils.copyURLToFile(url, new File(pythonWorkDir, ipythonScript));
    }//w  w  w  .  j a va  2s  .c  om

    CommandLine cmd = CommandLine.parse(pythonExecutable);
    cmd.addArgument(pythonWorkDir.getAbsolutePath() + "/ipython_server.py");
    cmd.addArgument(ipythonPort + "");
    DefaultExecutor executor = new DefaultExecutor();
    ProcessLogOutputStream processOutput = new ProcessLogOutputStream(LOGGER);
    executor.setStreamHandler(new PumpStreamHandler(processOutput));
    watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    executor.setWatchdog(watchDog);

    if (useBuiltinPy4j) {
        //TODO(zjffdu) don't do hard code on py4j here
        File py4jDestFile = new File(pythonWorkDir, "py4j-src-0.10.7.zip");
        FileUtils.copyURLToFile(getClass().getClassLoader().getResource("python/py4j-src-0.10.7.zip"),
                py4jDestFile);
        if (additionalPythonPath != null) {
            // put the py4j at the end, because additionalPythonPath may already contain py4j.
            // e.g. PySparkInterpreter
            additionalPythonPath = additionalPythonPath + ":" + py4jDestFile.getAbsolutePath();
        } else {
            additionalPythonPath = py4jDestFile.getAbsolutePath();
        }
    }

    Map<String, String> envs = setupIPythonEnv();
    executor.execute(cmd, envs, this);

    // wait until IPython kernel is started or timeout
    long startTime = System.currentTimeMillis();
    while (true) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            LOGGER.error("Interrupted by something", e);
        }

        try {
            StatusResponse response = ipythonClient.status(StatusRequest.newBuilder().build());
            if (response.getStatus() == IPythonStatus.RUNNING) {
                LOGGER.info("IPython Kernel is Running");
                break;
            } else {
                LOGGER.info("Wait for IPython Kernel to be started");
            }
        } catch (Exception e) {
            // ignore the exception, because is may happen when grpc server has not started yet.
            LOGGER.info("Wait for IPython Kernel to be started");
        }

        if ((System.currentTimeMillis() - startTime) > ipythonLaunchTimeout) {
            throw new IOException(
                    "Fail to launch IPython Kernel in " + ipythonLaunchTimeout / 1000 + " seconds");
        }
    }
}

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();//w w  w .j  av a2 s.  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.apache.zeppelin.shell.security.ShellSecurityImpl.java

public static void createSecureConfiguration(Properties properties, String shell) {

    String authType = properties.getProperty("zeppelin.shell.auth.type").trim().toUpperCase();

    switch (authType) {
    case "KERBEROS":
        CommandLine cmdLine = CommandLine.parse(shell);
        cmdLine.addArgument("-c", false);
        String kinitCommand = String.format("kinit -k -t %s %s",
                properties.getProperty("zeppelin.shell.keytab.location"),
                properties.getProperty("zeppelin.shell.principal"));
        cmdLine.addArgument(kinitCommand, false);
        DefaultExecutor executor = new DefaultExecutor();

        try {//from  w ww .jav  a2  s .c  om
            int exitVal = executor.execute(cmdLine);
        } catch (Exception e) {
            LOGGER.error("Unable to run kinit for zeppelin user " + kinitCommand, e);
            throw new InterpreterException(e);
        }
    }
}