diff --git a/platform/lang-impl/src/com/intellij/execution/actions/StopProcessAction.java b/platform/lang-impl/src/com/intellij/execution/actions/StopProcessAction.java new file mode 100644 index 000000000000..e38cac95243c --- /dev/null +++ b/platform/lang-impl/src/com/intellij/execution/actions/StopProcessAction.java @@ -0,0 +1,91 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.execution.actions; + +import com.intellij.execution.KillableProcess; +import com.intellij.execution.process.ProcessHandler; +import com.intellij.icons.AllIcons; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.Presentation; +import com.intellij.openapi.project.DumbAwareAction; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +/** + * @author Sergey Simonchik + */ +public class StopProcessAction extends DumbAwareAction implements AnAction.TransparentUpdate { + + private final ProcessHandler myProcessHandler; + + public StopProcessAction(@NotNull String text, @Nullable String description, @NotNull ProcessHandler processHandler) { + super(text, description, AllIcons.Actions.Suspend); + myProcessHandler = processHandler; + } + + @Override + public void update(final AnActionEvent e) { + update(e.getPresentation(), getTemplatePresentation(), myProcessHandler); + } + + public static void update(@NotNull Presentation presentation, + @NotNull Presentation templatePresentation, + @Nullable ProcessHandler processHandler) { + boolean enable = false; + Icon icon = templatePresentation.getIcon(); + String description = templatePresentation.getDescription(); + if (processHandler != null && !processHandler.isProcessTerminated()) { + enable = true; + if (processHandler.isProcessTerminating() && processHandler instanceof KillableProcess) { + KillableProcess killableProcess = (KillableProcess) processHandler; + if (killableProcess.canKillProcess()) { + // 'force quite' action presentation + icon = AllIcons.Debugger.KillProcess; + description = "Kill process"; + } + } + } + presentation.setEnabled(enable); + presentation.setIcon(icon); + presentation.setDescription(description); + } + + + @Override + public void actionPerformed(AnActionEvent e) { + stopProcess(myProcessHandler); + } + + public static void stopProcess(@NotNull ProcessHandler processHandler) { + if (processHandler instanceof KillableProcess && processHandler.isProcessTerminating()) { + // process termination was requested, but it's still alive + // in this case 'force quit' will be performed + ((KillableProcess)processHandler).killProcess(); + return; + } + + if (processHandler.detachIsDefault()) { + processHandler.detachProcess(); + } + else { + processHandler.destroyProcess(); + } + } + +} diff --git a/platform/platform-impl/src/com/intellij/execution/process/ScriptRunnerUtil.java b/platform/platform-impl/src/com/intellij/execution/process/ScriptRunnerUtil.java index fd0d79be3c62..c3028a9be72e 100644 --- a/platform/platform-impl/src/com/intellij/execution/process/ScriptRunnerUtil.java +++ b/platform/platform-impl/src/com/intellij/execution/process/ScriptRunnerUtil.java @@ -17,6 +17,7 @@ package com.intellij.execution.process; import com.intellij.execution.ExecutionBundle; import com.intellij.execution.ExecutionException; +import com.intellij.execution.KillableProcess; import com.intellij.execution.configurations.GeneralCommandLine; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Condition; @@ -201,4 +202,32 @@ public final class ScriptRunnerUtil { myMergedOutput.append(text); } } + + /** + * Gracefully terminates a process. + * Initially, 'soft kill' is performed (on UNIX it's equivalent to SIGINT signal sending). + * If the process didn't terminate within a given timeout, 'force quite' is performed (on UNIX it's equivalent to SIGKILL + * signal sending). + * + * @param processHandler {@link ProcessHandler} instance + */ + public static void terminateProcess(@NotNull ProcessHandler processHandler, + long millisTimeout, + @Nullable String commandLine) { + if (processHandler.isProcessTerminated()) { + LOG.warn("Process '" + commandLine + "' is already terminated!"); + return; + } + processHandler.destroyProcess(); + if (processHandler instanceof KillableProcess) { + KillableProcess killableProcess = (KillableProcess) processHandler; + if (killableProcess.canKillProcess()) { + if (!processHandler.waitFor(millisTimeout)) { + // doing 'force quite' + killableProcess.killProcess(); + } + } + } + } + }