Skip to content

Commit

Permalink
Introduce Process.getAllProcess
Browse files Browse the repository at this point in the history
Fixes #79
  • Loading branch information
gastaldi committed Mar 8, 2021
1 parent 4e852cd commit 338ed6f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.smallrye.common.os;

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

public class GetAllProcessesInfoAction implements PrivilegedAction<List<ProcessInfo>> {

@Override
public List<ProcessInfo> run() {
throw new UnsupportedOperationException("Listing all processes is not supported in JDK 8");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ final class GetProcessInfoAction implements PrivilegedAction<Object[]> {
GetProcessInfoAction() {
}

@Override
public Object[] run() {
long pid = -1L;
String processName = "<unknown>";
Expand Down
25 changes: 19 additions & 6 deletions os/src/main/java/io/smallrye/common/os/Process.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@

import static java.security.AccessController.doPrivileged;

import java.util.List;

/**
* Utilities for getting information about the current process.
*
* @author <a href="mailto:[email protected]">David M. Lloyd</a>
*/
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() {
Expand All @@ -44,7 +44,7 @@ private Process() {
* @return the process name (not {@code null})
*/
public static String getProcessName() {
return processName;
return currentProcess.getCommand();
}

/**
Expand All @@ -54,6 +54,19 @@ 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;
}

public static List<ProcessInfo> getAllProcess() {
return doPrivileged(new GetAllProcessesInfoAction());
}
}
22 changes: 22 additions & 0 deletions os/src/main/java/io/smallrye/common/os/ProcessInfo.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<List<ProcessInfo>> {

@Override
public List<ProcessInfo> run() {
return ProcessHandle.allProcesses()
.map(processHandle -> new ProcessInfo(processHandle.pid(), processHandle.info().command().orElse(null)))
.collect(Collectors.toList());
}
}
18 changes: 18 additions & 0 deletions os/src/test/java/io/smallrye/common/os/ProcessTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
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 {

@Test
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());
}

}

0 comments on commit 338ed6f

Please sign in to comment.