Package org.apache.commons.exec

Examples of org.apache.commons.exec.DefaultExecutor


        executor.execute(commandLine);
    }

    protected boolean isUnixPidRunning(String pid) {

        Executor executor = new DefaultExecutor();
        executor.setWorkingDirectory(getBinDir());
        PumpStreamHandler streamHandler = new PumpStreamHandler(createNullOutputStream(), createNullOutputStream());
        executor.setStreamHandler(streamHandler);
        org.apache.commons.exec.CommandLine commandLine;
        commandLine = new org.apache.commons.exec.CommandLine("kill").addArgument("-0").addArgument(pid);

        boolean isRunning = true; // assume it is running
        try {
            int code = executor.execute(commandLine);
            if (code != 0) {
                isRunning = false;
            }
        } catch (ExecuteException ee) {
            log.debug("kill -0 for pid [" + pid + "] threw exception with exit value [" + ee.getExitValue() + "]");
View Full Code Here


    final ByteArrayOutputStream err = new ByteArrayOutputStream();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final CommandLine cmdLine = new CommandLine(kill_cmd);
    cmdLine.addArguments(kill_cmd_params);
    cmdLine.addArguments(pid);
    final Executor executor = new DefaultExecutor();
    executor.setExitValue(1);
    final DefaultExecuteResultHandler derh = new DefaultExecuteResultHandler();
//    executor.setProcessDestroyer(MPlayerFileService.PROCESS_SHUTDOWN_HOOK);
    executor.setStreamHandler(new PumpStreamHandler(out, err));
    try {
      logger.info("execute " + kill_cmd_log + " " + pid);
      executor.execute(cmdLine, derh);
      derh.waitFor();
      return true;
    } catch (final ExecuteException e) {
      logger.error("error on " + kill_cmd_log + pid, e);
    } catch (final IOException e) {
View Full Code Here

  private List<ProcessToWatch> getProcessList() {
    final ByteArrayOutputStream err = new ByteArrayOutputStream();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final CommandLine cmdLine = new CommandLine(list_cmd);
    cmdLine.addArguments(list_cmd_params);
    final Executor executor = new DefaultExecutor();
    executor.setExitValue(1);

    final DefaultExecuteResultHandler derh = new DefaultExecuteResultHandler();
    executor.setProcessDestroyer(MPlayerFileService.PROCESS_SHUTDOWN_HOOK);
    executor.setStreamHandler(new PumpStreamHandler(out, err));
    try {
      logger.info("execute " + list_cmd_log);
      executor.execute(cmdLine, derh);
      derh.waitFor();
     
    } catch (final ExecuteException e) {
      logger.error("error on " + list_cmd_log, e);
    } catch (final IOException e) {
View Full Code Here

//    if (! path.toLowerCase().endsWith("mts")) {
      final CommandLine cmdLine = new CommandLine(this.mplayer);
      cmdLine.addArguments(this.arguments);
      cmdLine.addArgument(this.path, false);
//      ExecuteWatchdog watchdog = new ExecuteWatchdog(30*1000);
      final Executor executor = new DefaultExecutor();
      executor.setExitValue(1);
//      executor.setWatchdog(watchdog);
//      executor.setProcessDestroyer(MPlayerFileService.PROCESS_SHUTDOWN_HOOK);
      executor.setStreamHandler(new PumpStreamHandler(this.out, this.err));
      executor.setWorkingDirectory(this.tempFolder);
      executor.execute(cmdLine, this);
     
//    } else {
//      throw new ExecuteException("mts will not work with mplayer -identify", 0);
//    }
    return this;
View Full Code Here

//    if (! path.toLowerCase().endsWith("mts")) {
      final CommandLine cmdLine = new CommandLine(this.mplayer);
      cmdLine.addArguments(this.arguments);
      cmdLine.addArgument(this.path, false);
//      ExecuteWatchdog watchdog = new ExecuteWatchdog(5*1000);
      final Executor executor = new DefaultExecutor();
      executor.setExitValue(1);
//      executor.setWatchdog(watchdog);
//      executor.setProcessDestroyer(MPlayerFileService.PROCESS_SHUTDOWN_HOOK);
      executor.setStreamHandler(new PumpStreamHandler(this.out, this.err));
      executor.execute(cmdLine, this);

//    } else {
//      throw new ExecuteException("mts will not work with mplayer -identify", 0);
//    }
    return this;
View Full Code Here

     * @return a reader containing the output of the process
     * @throws IOException starting the process failed
     */
    protected BufferedReader runProcEnvCommand() throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Executor exe = new DefaultExecutor();
        exe.setStreamHandler(new PumpStreamHandler(out));
        // ignore the exit value - Just try to use what we got
        exe.execute(getProcEnvCommand());
        return new BufferedReader(new StringReader(toString(out)));
    }
View Full Code Here

        notNull(command, "command");

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();

        DefaultExecutor executor = prepareDefaultExecutor(command);
        // handle error and output of the process and write them to the given
        // out stream
        PumpStreamHandler handler = new PumpStreamHandler(out, err, command.getInput());
        executor.setStreamHandler(handler);

        CommandLine cl = toCommandLine(command);

        try {
            int exitValue = executor.execute(cl);
            // if the size is zero, we have no output, so construct the result
            // with null (required by ExecResult)
            InputStream stdout = out.size() == 0 ? null : new ByteArrayInputStream(out.toByteArray());
            InputStream stderr = err.size() == 0 ? null : new ByteArrayInputStream(err.toByteArray());
            ExecResult result = new ExecResult(command, stdout, stderr, exitValue);
View Full Code Here

            IOUtils.closeQuietly(command.getInput());
        }
    }

    protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) {
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValues(null);

        if (execCommand.getWorkingDir() != null) {
            executor.setWorkingDirectory(new File(execCommand.getWorkingDir()).getAbsoluteFile());
        }
        if (execCommand.getTimeout() != ExecEndpoint.NO_TIMEOUT) {
            executor.setWatchdog(new ExecuteWatchdog(execCommand.getTimeout()));
        }
        executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
        return executor;
    }
View Full Code Here

    public static NginxCompileParameters extract(VirtualFile from) throws PlatformDependentTools.ThisIsNotNginxExecutableException {

        NginxCompileParameters result = new NginxCompileParameters();

        Executor executor = new DefaultExecutor();
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            executor.setStreamHandler(new PumpStreamHandler(os, os));
            executor.execute(CommandLine.parse(from.getPath() + " -V"));
        } catch (IOException e) {
            throw new PlatformDependentTools.ThisIsNotNginxExecutableException(e);
        }

        String output = os.toString();
View Full Code Here

        }
    }

    private ExecBean auxRun(String program, List<String> args, Map<String, String> env)
        throws NotAuthorizedException, ExecuteException, IOException {
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValues(null);

        // Setup stdout and stderr
        int nbytes = appConf.getInt(AppConfig.EXEC_MAX_BYTES_NAME, -1);
        ByteArrayOutputStream outStream = new MaxByteArrayOutputStream(nbytes);
        ByteArrayOutputStream errStream = new MaxByteArrayOutputStream(nbytes);
        executor.setStreamHandler(new PumpStreamHandler(outStream, errStream));

        // Only run for N milliseconds
        int timeout = appConf.getInt(AppConfig.EXEC_TIMEOUT_NAME, 0);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);

        CommandLine cmd = makeCommandLine(program, args);

        LOG.info("Running: " + cmd);
        ExecBean res = new ExecBean();
        res.exitcode = executor.execute(cmd, execEnv(env));
        String enc = appConf.get(AppConfig.EXEC_ENCODING_NAME);
        res.stdout = outStream.toString(enc);
        res.stderr = errStream.toString(enc);

        return res;
View Full Code Here

TOP

Related Classes of org.apache.commons.exec.DefaultExecutor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.