diff --git a/os/src/main/java/io/smallrye/common/os/GetAllProcessesInfoAction.java b/os/src/main/java/io/smallrye/common/os/GetAllProcessesInfoAction.java new file mode 100644 index 00000000..a11a8d77 --- /dev/null +++ b/os/src/main/java/io/smallrye/common/os/GetAllProcessesInfoAction.java @@ -0,0 +1,12 @@ +package io.smallrye.common.os; + +import java.security.PrivilegedAction; +import java.util.List; + +public class GetAllProcessesInfoAction implements PrivilegedAction> { + + @Override + public List run() { + throw new UnsupportedOperationException("Listing all processes is not supported in JDK 8"); + } +} 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 0148b722..e74b08ae 100644 --- a/os/src/main/java/io/smallrye/common/os/GetProcessInfoAction.java +++ b/os/src/main/java/io/smallrye/common/os/GetProcessInfoAction.java @@ -31,6 +31,7 @@ final class GetProcessInfoAction implements PrivilegedAction { GetProcessInfoAction() { } + @Override public Object[] run() { long pid = -1L; String processName = ""; 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 b9b102ef..23cb254c 100644 --- a/os/src/main/java/io/smallrye/common/os/Process.java +++ b/os/src/main/java/io/smallrye/common/os/Process.java @@ -18,6 +18,8 @@ package io.smallrye.common.os; +import java.util.List; + import static java.security.AccessController.doPrivileged; /** @@ -26,13 +28,11 @@ * @author David M. Lloyd */ public final class Process { - private static final long processId; - private static final String processName; + private static final ProcessInfo currentProcess; static { Object[] array = doPrivileged(new GetProcessInfoAction()); - processId = (Long) array[0]; - processName = (String) array[1]; + currentProcess = new ProcessInfo((Long) array[0], (String) array[1]); } private Process() { @@ -44,7 +44,7 @@ private Process() { * @return the process name (not {@code null}) */ public static String getProcessName() { - return processName; + return currentProcess.getCommand(); } /** @@ -54,6 +54,25 @@ public static String getProcessName() { * @return the ID of this process, or -1 if it cannot be determined */ public static long getProcessId() { - return processId; + return currentProcess.getId(); + } + + /** + * Returns information about the current process + * + * @return the current process + */ + public static ProcessInfo getCurrentProcess() { + return currentProcess; + } + + /** + * Returns all the running process. + * + * @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 + */ + public static List getAllProcess() { + return doPrivileged(new GetAllProcessesInfoAction()); } } diff --git a/os/src/main/java/io/smallrye/common/os/ProcessInfo.java b/os/src/main/java/io/smallrye/common/os/ProcessInfo.java new file mode 100644 index 00000000..fde7b6d0 --- /dev/null +++ b/os/src/main/java/io/smallrye/common/os/ProcessInfo.java @@ -0,0 +1,22 @@ +package io.smallrye.common.os; + +/** + * Returns information about a Process + */ +public class ProcessInfo { + private final long id; + private final String command; + + public ProcessInfo(long id, String command) { + this.id = id; + this.command = command; + } + + public long getId() { + return id; + } + + public String getCommand() { + return command; + } +} diff --git a/os/src/main/java9/io/smallrye/common/os/GetAllProcessesInfoAction.java b/os/src/main/java9/io/smallrye/common/os/GetAllProcessesInfoAction.java new file mode 100644 index 00000000..bb0eed34 --- /dev/null +++ b/os/src/main/java9/io/smallrye/common/os/GetAllProcessesInfoAction.java @@ -0,0 +1,15 @@ +package io.smallrye.common.os; + +import java.security.PrivilegedAction; +import java.util.List; +import java.util.stream.Collectors; + +public class GetAllProcessesInfoAction implements PrivilegedAction> { + + @Override + public List run() { + return ProcessHandle.allProcesses() + .map(processHandle -> new ProcessInfo(processHandle.pid(), processHandle.info().command().orElse(null))) + .collect(Collectors.toList()); + } +} diff --git a/os/src/test/java/io/smallrye/common/os/ProcessTest.java b/os/src/test/java/io/smallrye/common/os/ProcessTest.java index d3de07c0..61fb371d 100644 --- a/os/src/test/java/io/smallrye/common/os/ProcessTest.java +++ b/os/src/test/java/io/smallrye/common/os/ProcessTest.java @@ -1,9 +1,14 @@ package io.smallrye.common.os; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnJre; +import org.junit.jupiter.api.condition.EnabledOnJre; +import org.junit.jupiter.api.condition.JRE; class ProcessTest { @@ -11,6 +16,19 @@ class ProcessTest { public void testProcessInfo() { assertNotEquals(0L, Process.getProcessId()); assertNotNull(Process.getProcessName()); + assertNotNull(Process.getCurrentProcess()); + } + + @Test + @EnabledOnJre(JRE.JAVA_8) + public void testAllProcessInfoInJDK8() { + assertThrows(UnsupportedOperationException.class, Process::getAllProcess); + } + + @Test + @DisabledOnJre(JRE.JAVA_8) + public void testAllProcessInfo() { + assertFalse(Process.getAllProcess().isEmpty()); } }