Search in sources :

Example 41 with ProcessHandler

use of com.intellij.execution.process.ProcessHandler in project intellij-community by JetBrains.

the class RemoteProcessSupport method getProcessListener.

private ProcessListener getProcessListener(@NotNull final Pair<Target, Parameters> key) {
    return new ProcessListener() {

        @Override
        public void startNotified(ProcessEvent event) {
            ProcessHandler processHandler = event.getProcessHandler();
            processHandler.putUserData(ProcessHandler.SILENTLY_DESTROY_ON_CLOSE, Boolean.TRUE);
            Info o;
            synchronized (myProcMap) {
                o = myProcMap.get(key);
                if (o instanceof PendingInfo) {
                    myProcMap.put(key, new PendingInfo(((PendingInfo) o).ref, processHandler));
                }
            }
        }

        @Override
        public void processTerminated(ProcessEvent event) {
            if (dropProcessInfo(key, null, event.getProcessHandler())) {
                fireModificationCountChanged();
            }
        }

        @Override
        public void processWillTerminate(ProcessEvent event, boolean willBeDestroyed) {
            if (dropProcessInfo(key, null, event.getProcessHandler())) {
                fireModificationCountChanged();
            }
        }

        @Override
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            String text = StringUtil.notNullize(event.getText());
            if (outputType == ProcessOutputTypes.STDERR) {
                LOG.warn(text.trim());
            } else {
                LOG.info(text.trim());
            }
            RunningInfo result = null;
            PendingInfo info;
            synchronized (myProcMap) {
                Info o = myProcMap.get(key);
                logText(key.second, event, outputType, o);
                if (o instanceof PendingInfo) {
                    info = (PendingInfo) o;
                    if (outputType == ProcessOutputTypes.STDOUT) {
                        String prefix = "Port/ID:";
                        if (text.startsWith(prefix)) {
                            String pair = text.substring(prefix.length()).trim();
                            int idx = pair.indexOf("/");
                            result = new RunningInfo(info.handler, Integer.parseInt(pair.substring(0, idx)), pair.substring(idx + 1));
                            myProcMap.put(key, result);
                            myProcMap.notifyAll();
                        }
                    } else if (outputType == ProcessOutputTypes.STDERR) {
                        info.stderr.append(text);
                    }
                } else {
                    info = null;
                }
            }
            if (result != null) {
                synchronized (info.ref) {
                    info.ref.set(result);
                    info.ref.notifyAll();
                }
                fireModificationCountChanged();
                try {
                    RemoteDeadHand.TwoMinutesTurkish.startCooking("localhost", result.port);
                } catch (Throwable e) {
                    LOG.warn("The cook failed to start due to " + ExceptionUtil.getRootCause(e));
                }
            }
        }
    };
}
Also used : ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessListener(com.intellij.execution.process.ProcessListener) ProcessHandler(com.intellij.execution.process.ProcessHandler) Key(com.intellij.openapi.util.Key)

Example 42 with ProcessHandler

use of com.intellij.execution.process.ProcessHandler in project intellij-community by JetBrains.

the class RunContentManagerImpl method waitForProcess.

private void waitForProcess(final RunContentDescriptor descriptor, final boolean modal) {
    final ProcessHandler processHandler = descriptor.getProcessHandler();
    final boolean killable = !modal && (processHandler instanceof KillableProcess) && ((KillableProcess) processHandler).canKillProcess();
    String title = ExecutionBundle.message("terminating.process.progress.title", descriptor.getDisplayName());
    ProgressManager.getInstance().run(new Task.Backgroundable(myProject, title, true) {

        {
            if (killable) {
                String cancelText = ExecutionBundle.message("terminating.process.progress.kill");
                setCancelText(cancelText);
                setCancelTooltipText(cancelText);
            }
        }

        @Override
        public boolean isConditionalModal() {
            return modal;
        }

        @Override
        public boolean shouldStartInBackground() {
            return !modal;
        }

        @Override
        public void run(@NotNull final ProgressIndicator progressIndicator) {
            final Semaphore semaphore = new Semaphore();
            semaphore.down();
            ApplicationManager.getApplication().executeOnPooledThread(() -> {
                final ProcessHandler processHandler1 = descriptor.getProcessHandler();
                try {
                    if (processHandler1 != null) {
                        processHandler1.waitFor();
                    }
                } finally {
                    semaphore.up();
                }
            });
            progressIndicator.setText(ExecutionBundle.message("waiting.for.vm.detach.progress.text"));
            ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {

                @Override
                public void run() {
                    while (true) {
                        if (progressIndicator.isCanceled() || !progressIndicator.isRunning()) {
                            semaphore.up();
                            break;
                        }
                        try {
                            //noinspection SynchronizeOnThis
                            synchronized (this) {
                                //noinspection SynchronizeOnThis
                                wait(2000L);
                            }
                        } catch (InterruptedException ignore) {
                        }
                    }
                }
            });
            semaphore.waitFor();
        }

        @Override
        public void onCancel() {
            if (killable && !processHandler.isProcessTerminated()) {
                ((KillableProcess) processHandler).killProcess();
            }
        }
    });
}
Also used : Task(com.intellij.openapi.progress.Task) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProcessHandler(com.intellij.execution.process.ProcessHandler) Semaphore(com.intellij.util.concurrency.Semaphore)

Example 43 with ProcessHandler

use of com.intellij.execution.process.ProcessHandler in project intellij-community by JetBrains.

the class DefaultJavaProgramRunner method doExecute.

@Override
protected RunContentDescriptor doExecute(@NotNull RunProfileState state, @NotNull ExecutionEnvironment env) throws ExecutionException {
    FileDocumentManager.getInstance().saveAllDocuments();
    ExecutionResult executionResult;
    boolean shouldAddDefaultActions = true;
    if (state instanceof JavaCommandLine) {
        final JavaParameters parameters = ((JavaCommandLine) state).getJavaParameters();
        patch(parameters, env.getRunnerSettings(), env.getRunProfile(), true);
        ProcessProxy proxy = ProcessProxyFactory.getInstance().createCommandLineProxy((JavaCommandLine) state);
        executionResult = state.execute(env.getExecutor(), this);
        if (proxy != null) {
            ProcessHandler handler = executionResult != null ? executionResult.getProcessHandler() : null;
            if (handler != null) {
                proxy.attach(handler);
                handler.addProcessListener(new ProcessAdapter() {

                    @Override
                    public void processTerminated(ProcessEvent event) {
                        proxy.destroy();
                    }
                });
            } else {
                proxy.destroy();
            }
        }
        if (state instanceof JavaCommandLineState && !((JavaCommandLineState) state).shouldAddJavaProgramRunnerActions()) {
            shouldAddDefaultActions = false;
        }
    } else {
        executionResult = state.execute(env.getExecutor(), this);
    }
    if (executionResult == null) {
        return null;
    }
    onProcessStarted(env.getRunnerSettings(), executionResult);
    final RunContentBuilder contentBuilder = new RunContentBuilder(executionResult, env);
    if (shouldAddDefaultActions) {
        addDefaultActions(contentBuilder, executionResult);
    }
    return contentBuilder.showRunContent(env.getContentToReuse());
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) CapturingProcessAdapter(com.intellij.execution.process.CapturingProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessHandler(com.intellij.execution.process.ProcessHandler) ExecutionResult(com.intellij.execution.ExecutionResult)

Example 44 with ProcessHandler

use of com.intellij.execution.process.ProcessHandler in project intellij-plugins by JetBrains.

the class SendToMayaCommand method run.

public void run() {
    try {
        final ProcessHandler process = createRunInMayaProcessHandler();
        new RunContentExecutor(myProject, process).withTitle(getTitle()).withRerun(() -> this.run()).withStop(() -> process.destroyProcess(), () -> !process.isProcessTerminated()).run();
    } catch (ExecutionException e) {
        Messages.showErrorDialog(myProject, e.getMessage(), getTitle());
    }
}
Also used : ProcessHandler(com.intellij.execution.process.ProcessHandler) RunContentExecutor(com.intellij.execution.RunContentExecutor) ExecutionException(com.intellij.execution.ExecutionException)

Example 45 with ProcessHandler

use of com.intellij.execution.process.ProcessHandler in project android by JetBrains.

the class ProcessHandlerConsolePrinterTest method testSetProcessHandler.

public void testSetProcessHandler() {
    ProcessHandlerConsolePrinter printer = new ProcessHandlerConsolePrinter(null);
    printer.stdout("stdout1");
    printer.stderr("stderr1");
    ProcessHandler handler = mock(ProcessHandler.class);
    printer.setProcessHandler(handler);
    // The stored messages are sent to the newly-set process handler.
    InOrder inOrder = Mockito.inOrder(handler);
    inOrder.verify(handler).notifyTextAvailable("stdout1\n", STDOUT);
    inOrder.verify(handler).notifyTextAvailable("stderr1\n", STDERR);
    printer.stdout("stdout2");
    // New messages are sent to the process handler.
    verify(handler).notifyTextAvailable("stdout2\n", STDOUT);
}
Also used : InOrder(org.mockito.InOrder) ProcessHandler(com.intellij.execution.process.ProcessHandler)

Aggregations

ProcessHandler (com.intellij.execution.process.ProcessHandler)100 NotNull (org.jetbrains.annotations.NotNull)31 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)24 ExecutionException (com.intellij.execution.ExecutionException)17 Project (com.intellij.openapi.project.Project)17 ConsoleView (com.intellij.execution.ui.ConsoleView)16 DefaultExecutionResult (com.intellij.execution.DefaultExecutionResult)15 ProcessEvent (com.intellij.execution.process.ProcessEvent)14 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)14 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)13 ProgramRunner (com.intellij.execution.runners.ProgramRunner)12 Nullable (org.jetbrains.annotations.Nullable)10 Executor (com.intellij.execution.Executor)9 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)7 Disposable (com.intellij.openapi.Disposable)7 RunProfile (com.intellij.execution.configurations.RunProfile)6 OSProcessHandler (com.intellij.execution.process.OSProcessHandler)6 SMTRunnerConsoleView (com.intellij.execution.testframework.sm.runner.ui.SMTRunnerConsoleView)6 File (java.io.File)6 DefaultRunExecutor (com.intellij.execution.executors.DefaultRunExecutor)5