-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #79
- Loading branch information
Showing
6 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
os/src/main/java/io/smallrye/common/os/GetAllProcessesInfoAction.java
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 |
---|---|---|
@@ -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"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
|
||
package io.smallrye.common.os; | ||
|
||
import java.util.List; | ||
|
||
import static java.security.AccessController.doPrivileged; | ||
|
||
/** | ||
|
@@ -26,13 +28,11 @@ | |
* @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() { | ||
|
@@ -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<ProcessInfo> getAllProcess() { | ||
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
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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
os/src/main/java9/io/smallrye/common/os/GetAllProcessesInfoAction.java
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 |
---|---|---|
@@ -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()); | ||
} | ||
} |
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 |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |