Package org.apache.commons.exec

Examples of org.apache.commons.exec.DefaultExecutor


            throws MuleControllerException
    {
        CommandLine commandLine = new CommandLine(muleBin);
        commandLine.addArgument(command);
        commandLine.addArguments(args);
        DefaultExecutor executor = new DefaultExecutor();
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);
        executor.setStreamHandler(new PumpStreamHandler());
        return doExecution(executor, commandLine, newEnv);
    }
View Full Code Here


    @Override
    public int getProcessId()
    {
        Map<Object, Object> newEnv = this.copyEnvironmentVariables();
        DefaultExecutor executor = new DefaultExecutor();
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);
        if (this.doExecution(executor, new CommandLine(this.muleBin).addArgument("status"), newEnv) == 0)
        {
            Matcher matcher = STATUS_PATTERN.matcher(outputStream.toString());
            if (matcher.find())
            {
View Full Code Here

    }

    public static int executeCommand(String command, String... envVars) throws IOException
    {
        CommandLine cmdLine = CommandLine.parse(command);
        DefaultExecutor executor = new DefaultExecutor();
        Map<String, String> env = addEnvProperties(envVars);
        ExecuteWatchdog watchDog = new ExecuteWatchdog(TIMEOUT);
        executor.setWatchdog(watchDog);
        executor.setStreamHandler(new PumpStreamHandler());
        int result = executor.execute(cmdLine, env);
        if (executor.isFailure(result))
        {
            if (watchDog.killedProcess())
            {
                throw new RuntimeException("Reached timeout while running: " + cmdLine);
            }
View Full Code Here

    @Override
    public boolean run(boolean displayCmd, boolean throwFailure) throws Exception {
        List<String> cmd = buildCommand();
        displayCmd(displayCmd, cmd);
        Executor exec = new DefaultExecutor();

        //err and out are redirected to out
        if (!_redirectToLog) {
          exec.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in));
        } else {
            exec.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
                private LevelState _previous = new LevelState();
               
                @Override
                protected void processLine(String line, int level) {
                  try {
                    _previous = LogProcessorUtils.levelStateOf(line, _previous);
                    switch (_previous.level) {
                    case ERROR:
                      requester.getLog().error(line);
                      break;
                    case WARNING:
                      requester.getLog().warn(line);
                      break;
                    default:
                      requester.getLog().info(line);
                      break;
                    }
                  } catch (Exception e) {
                    e.printStackTrace();
                  }
                }
            }));
        }

        CommandLine cl = new CommandLine(cmd.get(0));
        for (int i = 1; i < cmd.size(); i++) {
            cl.addArgument(cmd.get(i), false);
        }
        try {
            int exitValue = exec.execute(cl);
            if (exitValue != 0) {
                if (throwFailure) {
                    throw new MojoFailureException("command line returned non-zero value:" + exitValue);
                }
                return false;
View Full Code Here

        try {
            if (isWindows()) {
                command = "cmd /c " + command;
            }
            CommandLine cmdLine = CommandLine.parse(command);
            DefaultExecutor executor = new DefaultExecutor();
            executor.setWorkingDirectory(yeomanProjectDirectory);
            executor.execute(cmdLine);
        } catch (IOException e) {
            throw new MojoExecutionException("Error during : " + command, e);
        }
    }
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();

    if(Shell.WINDOWS){
      //The default executor is sometimes causing failure on windows. hcat
      // command sometimes returns non zero exit status with it. It seems
      // to hit some race conditions on windows.
      env = execEnv(env);
      String[] envVals = new String[env.size()];
      int i=0;
      for( Entry<String, String> kv : env.entrySet()){
        envVals[i++] = kv.getKey() + "=" + kv.getValue();
        LOG.info("Setting " +  kv.getKey() + "=" + kv.getValue());
      }

      Process proc;
      synchronized (WindowsProcessLaunchLock) {
        // To workaround the race condition issue with child processes
        // inheriting unintended handles during process launch that can
        // lead to hangs on reading output and error streams, we
        // serialize process creation. More info available at:
        // http://support.microsoft.com/kb/315939
        proc = Runtime.getRuntime().exec(cmd.toStrings(), envVals);
      }

      //consume stderr
      StreamOutputWriter errorGobbler = new
        StreamOutputWriter(proc.getErrorStream(), "ERROR", errStream);

      //consume stdout
      StreamOutputWriter outputGobbler = new
        StreamOutputWriter(proc.getInputStream(), "OUTPUT", outStream);

      //start collecting input streams
      errorGobbler.start();
      outputGobbler.start();
      //execute
      try{
        res.exitcode = proc.waitFor();
      } catch (InterruptedException e) {
        throw new IOException(e);
      }
      //flush
      errorGobbler.out.flush();
      outputGobbler.out.flush();
    }
    else {
      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);
View Full Code Here

      throws IOException
  {
    LOG.debug("Execute command : {}", command);

    CommandLine cmdLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(out, err));

    int exitValue = executor.execute(cmdLine);
    LOG.debug("Execution finished with exit code : {}", exitValue);
    return exitValue;
  }
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();

    if(Shell.WINDOWS){
      //The default executor is sometimes causing failure on windows. hcat
      // command sometimes returns non zero exit status with it. It seems
      // to hit some race conditions on windows.
      env = execEnv(env);
      String[] envVals = new String[env.size()];
      int i=0;
      for( Entry<String, String> kv : env.entrySet()){
        envVals[i++] = kv.getKey() + "=" + kv.getValue();
        LOG.info("Setting " +  kv.getKey() + "=" + kv.getValue());
      }

      Process proc;
      synchronized (WindowsProcessLaunchLock) {
        // To workaround the race condition issue with child processes
        // inheriting unintended handles during process launch that can
        // lead to hangs on reading output and error streams, we
        // serialize process creation. More info available at:
        // http://support.microsoft.com/kb/315939
        proc = Runtime.getRuntime().exec(cmd.toStrings(), envVals);
      }

      //consume stderr
      StreamOutputWriter errorGobbler = new
        StreamOutputWriter(proc.getErrorStream(), "ERROR", errStream);

      //consume stdout
      StreamOutputWriter outputGobbler = new
        StreamOutputWriter(proc.getInputStream(), "OUTPUT", outStream);

      //start collecting input streams
      errorGobbler.start();
      outputGobbler.start();
      //execute
      try{
        res.exitcode = proc.waitFor();
      } catch (InterruptedException e) {
        throw new IOException(e);
      }
      //flush
      errorGobbler.out.flush();
      outputGobbler.out.flush();
    }
    else {
      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);
View Full Code Here

     *
     * @param output
     * @return a configured Executor
     */
    private Executor buildExecutor(OutputStream output) {
        DefaultExecutor executor = new DefaultExecutor();
        ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
        executor.setWatchdog(watchdog);
        ExecuteStreamHandler psh = new PumpStreamHandler(output);
        executor.setStreamHandler(psh);
        // We want to handle all exit values directly (not as ExecuteException).
        executor.setExitValues(null);
        return executor;
    }
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.