-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #299 from dmlloyd/pid
Do not cache PID or process name
- Loading branch information
Showing
4 changed files
with
37 additions
and
100 deletions.
There are no files selected for viewing
88 changes: 0 additions & 88 deletions
88
os/src/main/java/io/smallrye/common/os/GetProcessInfoAction.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
|
||
import static java.security.AccessController.doPrivileged; | ||
|
||
import java.security.PrivilegedAction; | ||
import java.util.List; | ||
|
||
/** | ||
|
@@ -28,49 +29,66 @@ | |
* @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 "<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((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()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters