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 016ad93
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 100 deletions.
88 changes: 0 additions & 88 deletions os/src/main/java/io/smallrye/common/os/GetProcessInfoAction.java

This file was deleted.

42 changes: 30 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 @@ -20,6 +20,7 @@

import static java.security.AccessController.doPrivileged;

import java.security.PrivilegedAction;
import java.util.List;

/**
Expand All @@ -28,49 +29,66 @@
* @author <a href="mailto:david.lloyd@redhat.com">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((PrivilegedAction<String>) Process::computeProcessName);
}

private static String computeProcessName() {
final ProcessHandle processHandle = ProcessHandle.current();
String processName = System.getProperty("jboss.process.name");
if (processName == null) {
processName = processHandle.info().command().orElse(null);
}
if (processName == null) {
processName = "<unknown>";
}
return processName;
}

/**
* 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(since = "2.4", forRemoval = true)
public static long getProcessId() {
return currentProcess.getId();
return currentProcess().pid();
}

/**
* Returns information about the current process
*
* @return the current process
* @deprecated Use {@link ProcessHandle#current()} to get the current process information.
*/
@Deprecated(since = "2.4", forRemoval = true)
public static ProcessInfo getCurrentProcess() {
return currentProcess;
return new ProcessInfo(currentProcess().pid(), getProcessName());
}

// do not make this public
private static ProcessHandle currentProcess() {
return doPrivileged((PrivilegedAction<ProcessHandle>) ProcessHandle::current);
}

/**
* 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(since = "2.4", forRemoval = true)
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(since = "2.4", forRemoval = true)
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(since = "2.4", forRemoval = true)
public final class ProcessRedirect {
private ProcessRedirect() {
}
Expand Down

0 comments on commit 016ad93

Please sign in to comment.