StopProcessAction for an external process started out of RunContentDescription

This commit is contained in:
Sergey Simonchik
2013-05-07 18:07:07 +04:00
parent 20d0fa4847
commit 5def978a4f
2 changed files with 120 additions and 0 deletions
@@ -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();
}
}
}
@@ -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();
}
}
}
}
}