Java Code Examples for com.intellij.execution.process.ProcessHandler#waitFor()

The following examples show how to use com.intellij.execution.process.ProcessHandler#waitFor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExecutionHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static Runnable createTimelimitedExecutionProcess(final ProcessHandler processHandler, final int timeout, @Nonnull final String presentableCmdline) {
  return new Runnable() {
    private final Semaphore mySemaphore = new Semaphore();

    private final Runnable myProcessThread = new Runnable() {
      @Override
      public void run() {
        try {
          final boolean finished = processHandler.waitFor(1000 * timeout);
          if (!finished) {
            final String msg = "Timeout (" + timeout + " sec) on executing: " + presentableCmdline;
            LOG.error(msg);
            processHandler.destroyProcess();
          }
        }
        finally {
          mySemaphore.up();
        }
      }
    };

    @Override
    public void run() {
      mySemaphore.down();
      ApplicationManager.getApplication().executeOnPooledThread(myProcessThread);

      mySemaphore.waitFor();
    }
  };
}
 
Example 2
Source File: ExternalExec.java    From idea-gitignore with MIT License 4 votes vote down vote up
/**
 * Runs {@link IgnoreLanguage} executable with the given command and current working directory.
 *
 * @param language  current language
 * @param command   to call
 * @param directory current working directory
 * @param parser    {@link ExecutionOutputParser} implementation
 * @param <T>       return type
 * @return result of the call
 */
@Nullable
private static <T> ArrayList<T> run(@NotNull IgnoreLanguage language,
                                    @NotNull String command,
                                    @Nullable VirtualFile directory,
                                    @Nullable final ExecutionOutputParser<T> parser) {
    final String bin = bin(language);
    if (bin == null) {
        return null;
    }

    try {
        final String cmd = bin + " " + command;
        final File workingDirectory = directory != null ? new File(directory.getPath()) : null;
        final Process process = Runtime.getRuntime().exec(cmd, null, workingDirectory);

        ProcessHandler handler = new BaseOSProcessHandler(process, StringUtil.join(cmd, " "), null) {
            @NotNull
            @Override
            public Future<?> executeTask(@NotNull Runnable task) {
                return SharedThreadPool.getInstance().submit(task);
            }

            @Override
            public void notifyTextAvailable(@NotNull String text, @NotNull Key outputType) {
                if (parser != null) {
                    parser.onTextAvailable(text, outputType);
                }
            }
        };

        handler.startNotify();
        if (!handler.waitFor(DEFAULT_TIMEOUT)) {
            return null;
        }
        if (parser != null) {
            parser.notifyFinished(process.exitValue());
            if (parser.isErrorsReported()) {
                return null;
            }
            return parser.getOutput();
        }
    } catch (IOException ignored) {
    }

    return null;
}
 
Example 3
Source File: ExecutionHelper.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static Runnable createCancelableExecutionProcess(final ProcessHandler processHandler, final Function<Object, Boolean> cancelableFun) {
  return new Runnable() {
    private ProgressIndicator myProgressIndicator;
    private final Semaphore mySemaphore = new Semaphore();

    private final Runnable myWaitThread = new Runnable() {
      @Override
      public void run() {
        try {
          processHandler.waitFor();
        }
        finally {
          mySemaphore.up();
        }
      }
    };

    private final Runnable myCancelListener = new Runnable() {
      @Override
      public void run() {
        for (; ; ) {
          if ((myProgressIndicator != null && (myProgressIndicator.isCanceled() || !myProgressIndicator.isRunning())) ||
              (cancelableFun != null && cancelableFun.fun(null).booleanValue()) ||
              processHandler.isProcessTerminated()) {

            if (!processHandler.isProcessTerminated()) {
              try {
                processHandler.destroyProcess();
              }
              finally {
                mySemaphore.up();
              }
            }
            break;
          }
          try {
            synchronized (this) {
              wait(1000);
            }
          }
          catch (InterruptedException e) {
            //Do nothing
          }
        }
      }
    };

    @Override
    public void run() {
      myProgressIndicator = ProgressManager.getInstance().getProgressIndicator();
      if (myProgressIndicator != null && StringUtil.isEmpty(myProgressIndicator.getText())) {
        myProgressIndicator.setText("Please wait...");
      }

      LOG.assertTrue(myProgressIndicator != null || cancelableFun != null, "Cancelable process must have an opportunity to be canceled!");
      mySemaphore.down();
      ApplicationManager.getApplication().executeOnPooledThread(myWaitThread);
      ApplicationManager.getApplication().executeOnPooledThread(myCancelListener);

      mySemaphore.waitFor();
    }
  };
}