mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-190226 Get rid of using current approach for OSProcessUtil#getCurrentProcessId()
This commit is contained in:
@@ -10,7 +10,6 @@ import com.pty4j.windows.WinPtyProcess;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jvnet.winp.WinProcess;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -112,13 +111,16 @@ public class OSProcessUtil {
|
||||
}
|
||||
|
||||
private static String getCurrentProcessId() {
|
||||
try {
|
||||
String name = ManagementFactory.getRuntimeMXBean().getName();
|
||||
return name.split("@")[0];
|
||||
int pid;
|
||||
|
||||
if (SystemInfo.isWindows) {
|
||||
pid = WinProcessManager.getCurrentProcessId();
|
||||
}
|
||||
catch (Exception e) {
|
||||
return "-1";
|
||||
else {
|
||||
pid = UnixProcessManager.getCurrentProcessId();
|
||||
}
|
||||
|
||||
return String.valueOf(pid);
|
||||
}
|
||||
|
||||
public static String getApplicationPid() {
|
||||
@@ -126,7 +128,7 @@ public class OSProcessUtil {
|
||||
ourPid = getCurrentProcessId();
|
||||
}
|
||||
|
||||
return String.valueOf(ourPid);
|
||||
return ourPid;
|
||||
}
|
||||
|
||||
/** @deprecated trivial; use {@link #getProcessList()} directly (to be removed in IDEA 2019) */
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.intellij.util;
|
||||
|
||||
import com.intellij.execution.configurations.PathEnvironmentVariableUtil;
|
||||
import com.intellij.execution.process.UnixProcessManager;
|
||||
import com.intellij.execution.process.WinProcessManager;
|
||||
import com.intellij.ide.actions.CreateDesktopEntryAction;
|
||||
import com.intellij.jna.JnaLoader;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
@@ -113,7 +114,7 @@ public class Restarter {
|
||||
Kernel32 kernel32 = Native.loadLibrary("kernel32", Kernel32.class);
|
||||
Shell32 shell32 = Native.loadLibrary("shell32", Shell32.class);
|
||||
|
||||
int pid = kernel32.GetCurrentProcessId();
|
||||
int pid = WinProcessManager.getCurrentProcessId();
|
||||
IntByReference argc = new IntByReference();
|
||||
Pointer argvPtr = shell32.CommandLineToArgvW(kernel32.GetCommandLineW(), argc);
|
||||
String[] argv = getRestartArgv(argvPtr.getWideStringArray(0, argc.getValue()));
|
||||
|
||||
+17
-2
@@ -6,6 +6,7 @@ import com.intellij.openapi.util.SystemInfo;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.util.ReflectionUtil;
|
||||
import com.sun.jna.Native;
|
||||
import com.sun.jna.Pointer;
|
||||
import com.sun.jna.platform.win32.Kernel32;
|
||||
import com.sun.jna.platform.win32.WinNT;
|
||||
@@ -17,7 +18,7 @@ import static com.intellij.util.ObjectUtils.assertNotNull;
|
||||
*
|
||||
* @author Alexey.Ushakov
|
||||
*/
|
||||
class WinProcessManager {
|
||||
public class WinProcessManager {
|
||||
private static final Logger LOG = Logger.getInstance(WinProcessManager.class);
|
||||
|
||||
private WinProcessManager() { }
|
||||
@@ -42,6 +43,11 @@ class WinProcessManager {
|
||||
throw new IllegalStateException("Unable to get PID from instance of " + process.getClass() + ", OS: " + SystemInfo.OS_NAME);
|
||||
}
|
||||
|
||||
public static int getCurrentProcessId() {
|
||||
Kernel32 kernel32 = Native.loadLibrary("kernel32", Kernel32.class);
|
||||
return kernel32.GetCurrentProcessId();
|
||||
}
|
||||
|
||||
public static boolean kill(Process process, boolean tree) {
|
||||
return kill(-1, process, tree);
|
||||
}
|
||||
@@ -64,7 +70,7 @@ class WinProcessManager {
|
||||
String output = FileUtil.loadTextAndClose(p.getInputStream());
|
||||
int res = p.waitFor();
|
||||
|
||||
if (res != 0 && (process == null || process.isAlive())) {
|
||||
if (res != 0 && (process == null || isAlive(process))) {
|
||||
LOG.warn(StringUtil.join(cmdArray, " ") + " failed: " + output);
|
||||
return false;
|
||||
}
|
||||
@@ -79,4 +85,13 @@ class WinProcessManager {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isAlive(Process process) {
|
||||
try {
|
||||
process.exitValue();
|
||||
return false;
|
||||
} catch(IllegalThreadStateException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user