Search in sources :

Example 16 with ProcessAdapter

use of com.intellij.execution.process.ProcessAdapter 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 17 with ProcessAdapter

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

the class RmiStubsGenerator method generateRmiStubs.

private ExitCode generateRmiStubs(final CompileContext context, Map<ModuleBuildTarget, Collection<ClassItem>> remoteClasses, ModuleChunk chunk, OutputConsumer outputConsumer) {
    ExitCode exitCode = ExitCode.NOTHING_DONE;
    final Collection<File> classpath = ProjectPaths.getCompilationClasspath(chunk, false);
    final StringBuilder buf = new StringBuilder();
    for (File file : classpath) {
        if (buf.length() > 0) {
            buf.append(File.pathSeparator);
        }
        buf.append(file.getPath());
    }
    final String classpathString = buf.toString();
    final String rmicPath = getPathToRmic(chunk);
    final RmicCompilerOptions options = getOptions(context);
    final List<ModuleBuildTarget> targetsProcessed = new ArrayList<>(remoteClasses.size());
    for (Map.Entry<ModuleBuildTarget, Collection<ClassItem>> entry : remoteClasses.entrySet()) {
        try {
            final ModuleBuildTarget target = entry.getKey();
            final Collection<String> cmdLine = createStartupCommand(target, rmicPath, classpathString, options, entry.getValue());
            final Process process = Runtime.getRuntime().exec(ArrayUtil.toStringArray(cmdLine));
            final BaseOSProcessHandler handler = new BaseOSProcessHandler(process, StringUtil.join(cmdLine, " "), null) {

                @NotNull
                @Override
                protected Future<?> executeOnPooledThread(@NotNull Runnable task) {
                    return SharedThreadPool.getInstance().executeOnPooledThread(task);
                }
            };
            final RmicOutputParser stdOutParser = new RmicOutputParser(context, getPresentableName());
            final RmicOutputParser stdErrParser = new RmicOutputParser(context, getPresentableName());
            handler.addProcessListener(new ProcessAdapter() {

                @Override
                public void onTextAvailable(ProcessEvent event, Key outputType) {
                    if (outputType == ProcessOutputTypes.STDOUT) {
                        stdOutParser.append(event.getText());
                    } else if (outputType == ProcessOutputTypes.STDERR) {
                        stdErrParser.append(event.getText());
                    }
                }

                @Override
                public void processTerminated(ProcessEvent event) {
                    super.processTerminated(event);
                }
            });
            handler.startNotify();
            handler.waitFor();
            targetsProcessed.add(target);
            if (stdErrParser.isErrorsReported() || stdOutParser.isErrorsReported()) {
                break;
            } else {
                final int exitValue = handler.getProcess().exitValue();
                if (exitValue != 0) {
                    context.processMessage(new CompilerMessage(getPresentableName(), BuildMessage.Kind.ERROR, "RMI stub generation failed"));
                    break;
                }
            }
        } catch (IOException e) {
            context.processMessage(new CompilerMessage(getPresentableName(), e));
            break;
        }
    }
    // registering generated files
    final Map<File, File[]> fsCache = new THashMap<>(FileUtil.FILE_HASHING_STRATEGY);
    for (ModuleBuildTarget target : targetsProcessed) {
        final Collection<ClassItem> items = remoteClasses.get(target);
        for (ClassItem item : items) {
            File[] children = fsCache.get(item.parentDir);
            if (children == null) {
                children = item.parentDir.listFiles();
                if (children == null) {
                    children = EMPTY_FILE_ARRAY;
                }
                fsCache.put(item.parentDir, children);
            }
            final Collection<File> files = item.selectGeneratedFiles(children);
            if (!files.isEmpty()) {
                final Collection<String> sources = item.compiledClass.getSourceFilesPaths();
                for (File generated : files) {
                    try {
                        outputConsumer.registerOutputFile(target, generated, sources);
                    } catch (IOException e) {
                        context.processMessage(new CompilerMessage(getPresentableName(), e));
                    }
                }
            }
        }
    }
    return exitCode;
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) NotNull(org.jetbrains.annotations.NotNull) THashMap(gnu.trove.THashMap) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) RmicCompilerOptions(org.jetbrains.jps.model.java.compiler.RmicCompilerOptions) IOException(java.io.IOException) BaseOSProcessHandler(com.intellij.execution.process.BaseOSProcessHandler) File(java.io.File) THashMap(gnu.trove.THashMap) Key(com.intellij.openapi.util.Key)

Example 18 with ProcessAdapter

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

the class DebuggerTestCase method createLocalSession.

protected DebuggerSession createLocalSession(final JavaParameters javaParameters) throws ExecutionException, InterruptedException {
    createBreakpoints(javaParameters.getMainClass());
    DebuggerSettings.getInstance().DEBUGGER_TRANSPORT = DebuggerSettings.SOCKET_TRANSPORT;
    GenericDebuggerRunnerSettings debuggerRunnerSettings = new GenericDebuggerRunnerSettings();
    debuggerRunnerSettings.LOCAL = true;
    final RemoteConnection debugParameters = DebuggerManagerImpl.createDebugParameters(javaParameters, debuggerRunnerSettings, false);
    ExecutionEnvironment environment = new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance()).runnerSettings(debuggerRunnerSettings).runProfile(new MockConfiguration()).build();
    final JavaCommandLineState javaCommandLineState = new JavaCommandLineState(environment) {

        @Override
        protected JavaParameters createJavaParameters() {
            return javaParameters;
        }

        @Override
        protected GeneralCommandLine createCommandLine() throws ExecutionException {
            return getJavaParameters().toCommandLine();
        }
    };
    ApplicationManager.getApplication().invokeAndWait(() -> {
        try {
            myDebuggerSession = DebuggerManagerEx.getInstanceEx(myProject).attachVirtualMachine(new DefaultDebugEnvironment(new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance()).runProfile(new MockConfiguration()).build(), javaCommandLineState, debugParameters, false));
            XDebuggerManager.getInstance(myProject).startSession(javaCommandLineState.getEnvironment(), new XDebugProcessStarter() {

                @Override
                @NotNull
                public XDebugProcess start(@NotNull XDebugSession session) {
                    return JavaDebugProcess.create(session, myDebuggerSession);
                }
            });
        } catch (ExecutionException e) {
            LOG.error(e);
        }
    });
    myDebugProcess = myDebuggerSession.getProcess();
    myDebugProcess.addProcessListener(new ProcessAdapter() {

        @Override
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            print(event.getText(), outputType);
        }
    });
    assertNotNull(myDebuggerSession);
    assertNotNull(myDebugProcess);
    return myDebuggerSession;
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) NotNull(org.jetbrains.annotations.NotNull) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key)

Example 19 with ProcessAdapter

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

the class BaseRemoteProcessHandler method startNotify.

@Override
public void startNotify() {
    notifyTextAvailable(myCommandLine + '\n', ProcessOutputTypes.SYSTEM);
    addProcessListener(new ProcessAdapter() {

        @Override
        public void startNotified(final ProcessEvent event) {
            try {
                final RemoteOutputReader stdoutReader = new RemoteOutputReader(myProcess.getInputStream(), getCharset(), myProcess, myCommandLine) {

                    @Override
                    protected void onTextAvailable(@NotNull String text) {
                        notifyTextAvailable(text, ProcessOutputTypes.STDOUT);
                    }

                    @NotNull
                    @Override
                    protected Future<?> executeOnPooledThread(@NotNull Runnable runnable) {
                        return BaseRemoteProcessHandler.executeOnPooledThread(runnable);
                    }
                };
                final RemoteOutputReader stderrReader = new RemoteOutputReader(myProcess.getErrorStream(), getCharset(), myProcess, myCommandLine) {

                    @Override
                    protected void onTextAvailable(@NotNull String text) {
                        notifyTextAvailable(text, ProcessOutputTypes.STDERR);
                    }

                    @NotNull
                    @Override
                    protected Future<?> executeOnPooledThread(@NotNull Runnable runnable) {
                        return BaseRemoteProcessHandler.executeOnPooledThread(runnable);
                    }
                };
                myWaitFor.setTerminationCallback(exitCode -> {
                    try {
                        try {
                            stderrReader.waitFor();
                            stdoutReader.waitFor();
                        } catch (InterruptedException ignore) {
                        }
                    } finally {
                        onOSProcessTerminated(exitCode);
                    }
                });
            } finally {
                removeProcessListener(this);
            }
        }
    });
    super.startNotify();
}
Also used : OutputStream(java.io.OutputStream) ProcessOutputTypes(com.intellij.execution.process.ProcessOutputTypes) StringUtil(com.intellij.openapi.util.text.StringUtil) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable) Future(java.util.concurrent.Future) Charset(java.nio.charset.Charset) ProcessWaitFor(com.intellij.execution.process.ProcessWaitFor) CommandLineUtil(com.intellij.execution.CommandLineUtil) ProcessEvent(com.intellij.execution.process.ProcessEvent) AppExecutorUtil(com.intellij.util.concurrency.AppExecutorUtil) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) TaskExecutor(com.intellij.execution.TaskExecutor) BaseOutputReader(com.intellij.util.io.BaseOutputReader) InputStream(java.io.InputStream) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) Future(java.util.concurrent.Future) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with ProcessAdapter

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

the class SMTestRunnerConnectionUtil method attachEventsProcessors.

private static void attachEventsProcessors(TestConsoleProperties consoleProperties, SMTestRunnerResultsForm resultsViewer, ProcessHandler processHandler, String testFrameworkName, @Nullable SMTestLocator locator, boolean idBasedTestTree, @Nullable TestProxyPrinterProvider printerProvider) {
    // build messages consumer
    final OutputToGeneralTestEventsConverter outputConsumer;
    if (consoleProperties instanceof SMCustomMessagesParsing) {
        outputConsumer = ((SMCustomMessagesParsing) consoleProperties).createTestEventsConverter(testFrameworkName, consoleProperties);
    } else {
        outputConsumer = new OutputToGeneralTestEventsConverter(testFrameworkName, consoleProperties);
    }
    // events processor
    final GeneralTestEventsProcessor eventsProcessor;
    if (idBasedTestTree) {
        eventsProcessor = new GeneralIdBasedToSMTRunnerEventsConvertor(consoleProperties.getProject(), resultsViewer.getTestsRootNode(), testFrameworkName);
    } else {
        eventsProcessor = new GeneralToSMTRunnerEventsConvertor(consoleProperties.getProject(), resultsViewer.getTestsRootNode(), testFrameworkName);
    }
    if (locator != null) {
        eventsProcessor.setLocator(locator);
    }
    if (printerProvider != null) {
        eventsProcessor.setPrinterProvider(printerProvider);
    }
    // UI actions
    final SMTRunnerUIActionsHandler uiActionsHandler = new SMTRunnerUIActionsHandler(consoleProperties);
    // subscribe to events
    // subscribes event processor on output consumer events
    outputConsumer.setProcessor(eventsProcessor);
    // subscribes result viewer on event processor
    eventsProcessor.addEventsListener(resultsViewer);
    // subscribes test runner's actions on results viewer events
    resultsViewer.addEventsListener(uiActionsHandler);
    processHandler.addProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(final ProcessEvent event) {
            outputConsumer.flushBufferBeforeTerminating();
            eventsProcessor.onFinishTesting();
            Disposer.dispose(eventsProcessor);
            Disposer.dispose(outputConsumer);
        }

        @Override
        public void startNotified(final ProcessEvent event) {
            eventsProcessor.onStartTesting();
            outputConsumer.onStartTesting();
        }

        @Override
        public void onTextAvailable(final ProcessEvent event, final Key outputType) {
            outputConsumer.process(event.getText(), outputType);
        }
    });
}
Also used : SMTRunnerUIActionsHandler(com.intellij.execution.testframework.sm.runner.ui.SMTRunnerUIActionsHandler) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) Key(com.intellij.openapi.util.Key)

Aggregations

ProcessAdapter (com.intellij.execution.process.ProcessAdapter)62 ProcessEvent (com.intellij.execution.process.ProcessEvent)59 Key (com.intellij.openapi.util.Key)19 NotNull (org.jetbrains.annotations.NotNull)15 ProcessHandler (com.intellij.execution.process.ProcessHandler)12 OSProcessHandler (com.intellij.execution.process.OSProcessHandler)10 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)10 ExecutionException (com.intellij.execution.ExecutionException)9 IOException (java.io.IOException)8 BaseOSProcessHandler (com.intellij.execution.process.BaseOSProcessHandler)7 Nullable (org.jetbrains.annotations.Nullable)7 Project (com.intellij.openapi.project.Project)5 ExecutionEnvironmentBuilder (com.intellij.execution.runners.ExecutionEnvironmentBuilder)4 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)4 File (java.io.File)4 ExecutionResult (com.intellij.execution.ExecutionResult)3 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)3 Disposable (com.intellij.openapi.Disposable)3 Semaphore (com.intellij.util.concurrency.Semaphore)3 RunProfile (com.intellij.execution.configurations.RunProfile)2