diff --git a/os/src/main/java/io/smallrye/common/os/GetProcessInfoAction.java b/os/src/main/java/io/smallrye/common/os/GetProcessInfoAction.java index 454f524f..0234515e 100644 --- a/os/src/main/java/io/smallrye/common/os/GetProcessInfoAction.java +++ b/os/src/main/java/io/smallrye/common/os/GetProcessInfoAction.java @@ -18,9 +18,6 @@ package io.smallrye.common.os; -import static java.lang.Math.max; - -import java.io.File; import java.security.PrivilegedAction; /** @@ -33,50 +30,6 @@ public ProcessInfo run() { final ProcessHandle processHandle = ProcessHandle.current(); final long pid = processHandle.pid(); String processName = System.getProperty("jboss.process.name"); - if (processName == null) { - final String classPath = System.getProperty("java.class.path"); - final String commandLine = System.getProperty("sun.java.command"); - if (commandLine != null) { - if (classPath != null && commandLine.startsWith(classPath)) { - // OK probably a JAR launch - final int sepIdx = classPath.lastIndexOf(File.separatorChar); - if (sepIdx > 0) { - processName = classPath.substring(sepIdx + 1); - } else { - processName = classPath; - } - } else { - // probably a class name - // it might be a class name followed by args, like org.foo.Bar -baz -zap - int firstSpace = commandLine.indexOf(' '); - final String className; - if (firstSpace > 0) { - className = commandLine.substring(0, firstSpace); - } else { - className = commandLine; - } - // no args now - int lastDot = className.lastIndexOf('.', firstSpace); - if (lastDot > 0) { - processName = className.substring(lastDot + 1); - if (processName.equalsIgnoreCase("jar") || processName.equalsIgnoreCase("ȷar")) { - // oops, I guess it was a JAR name... let's just take a guess then - int secondLastDot = className.lastIndexOf('.', lastDot - 1); - int sepIdx = className.lastIndexOf(File.separatorChar); - int lastSep = secondLastDot == -1 ? sepIdx - : sepIdx == -1 ? secondLastDot : max(sepIdx, secondLastDot); - if (lastSep > 0) { - processName = className.substring(lastSep + 1); - } else { - processName = className; - } - } - } else { - processName = className; - } - } - } - } if (processName == null) { processName = processHandle.info().command().orElse(null); } diff --git a/os/src/main/java/io/smallrye/common/os/Process.java b/os/src/main/java/io/smallrye/common/os/Process.java index e72d705b..46526917 100644 --- a/os/src/main/java/io/smallrye/common/os/Process.java +++ b/os/src/main/java/io/smallrye/common/os/Process.java @@ -28,41 +28,39 @@ * @author David M. Lloyd */ public final class Process { - private static final ProcessInfo currentProcess; - - static { - currentProcess = doPrivileged(new GetProcessInfoAction()); - } - private Process() { } /** * Get the name of this process. If the process name is not known, then "<unknown>" is returned. + * The process name may be overridden by setting the {@code jboss.process.name} property. * * @return the process name (not {@code null}) */ public static String getProcessName() { - return currentProcess.getCommand(); + return doPrivileged(new GetProcessInfoAction()).getCommand(); } /** - * Get the ID of this process. This is the operating system specific PID. If the PID cannot be determined, - * -1 is returned. + * Get the ID of this process. This is the operating system specific PID. * - * @return the ID of this process, or -1 if it cannot be determined + * @return the ID of this process + * @deprecated Use {@link ProcessHandle#pid()} instead. */ + @Deprecated public static long getProcessId() { - return currentProcess.getId(); + return ProcessHandle.current().pid(); } /** * Returns information about the current process * * @return the current process + * @deprecated Use {@link ProcessHandle#current()} to get the current process information. */ + @Deprecated public static ProcessInfo getCurrentProcess() { - return currentProcess; + return new ProcessInfo(ProcessHandle.current().pid(), getProcessName()); } /** @@ -70,7 +68,9 @@ public static ProcessInfo getCurrentProcess() { * * @return a list of all the running processes. May throw an exception if running on an unsupported JDK * @throws UnsupportedOperationException if running on JDK 8 + * @deprecated Use {@link ProcessHandle#allProcesses()} instead. */ + @Deprecated public static List getAllProcesses() { return doPrivileged(new GetAllProcessesInfoAction()); }