Skip to content

Commit

Permalink
Do not cache PID or process name
Browse files Browse the repository at this point in the history
Also simplify the computation of process name, and deprecate these methods in favor of the far superior `ProcessHandle` API.
  • Loading branch information
dmlloyd committed Apr 3, 2024
1 parent 9a34989 commit 4d2e161
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 59 deletions.
47 changes: 0 additions & 47 deletions os/src/main/java/io/smallrye/common/os/GetProcessInfoAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

package io.smallrye.common.os;

import static java.lang.Math.max;

import java.io.File;
import java.security.PrivilegedAction;

/**
Expand All @@ -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);
}
Expand Down
24 changes: 12 additions & 12 deletions os/src/main/java/io/smallrye/common/os/Process.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,49 @@
* @author <a href="mailto:[email protected]">David M. Lloyd</a>
*/
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 "&lt;unknown&gt;" 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());
}

/**
* Returns all the running processes.
*
* @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<ProcessInfo> getAllProcesses() {
return doPrivileged(new GetAllProcessesInfoAction());
}
Expand Down
3 changes: 3 additions & 0 deletions os/src/main/java/io/smallrye/common/os/ProcessInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

/**
* Returns information about a Process
*
* @deprecated Use the {@link ProcessHandle} API instead.
*/
@Deprecated
public class ProcessInfo {
private final long id;
private final String command;
Expand Down
4 changes: 4 additions & 0 deletions os/src/main/java/io/smallrye/common/os/ProcessRedirect.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

package io.smallrye.common.os;

/**
* @deprecated Use {@link ProcessBuilder.Redirect#DISCARD} instead.
*/
@Deprecated
public final class ProcessRedirect {
private ProcessRedirect() {
}
Expand Down

0 comments on commit 4d2e161

Please sign in to comment.