Skip to content

Commit

Permalink
OSProcess constructor with PID (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
Potat0x committed Aug 19, 2019
1 parent e63b9ec commit 455fd7d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
92 changes: 92 additions & 0 deletions oshi-core/src/main/java/oshi/software/os/OSProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
*/
package oshi.software.os;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;
import oshi.software.os.linux.LinuxOperatingSystem;
import oshi.software.os.mac.MacOperatingSystem;
import oshi.software.os.unix.freebsd.FreeBsdOperatingSystem;
import oshi.software.os.unix.solaris.SolarisOperatingSystem;
import oshi.software.os.windows.WindowsOperatingSystem;

import java.io.Serializable;

/**
Expand All @@ -35,6 +44,8 @@ public class OSProcess implements Serializable {

private static final long serialVersionUID = 3L;

private static final Logger LOG = LoggerFactory.getLogger(OSProcess.class);

private String name = "";
private String path = "";
private String commandLine = "";
Expand Down Expand Up @@ -95,6 +106,44 @@ public enum State {
OTHER
}

/**
* <p>
* Constructor for OSProcess.
* </p>
*/
public OSProcess() {
}

/**
* <p>
* Constructor for OSProcess.
* </p>
*
* @param processID
* process ID
*/
public OSProcess(int processID) {
this.processID = processID;
updateAttributes();
}

/**
* Updates all process attributes.
*/
public void updateAttributes() {
OperatingSystem operatingSystem = getCurrentOperatingSystem();
if (operatingSystem != null) {
OSProcess process = operatingSystem.getProcess(this.processID);
if (process != null) {
copyValuesToThisProcess(process);
} else {
LOG.error("No process found: {}", this.processID);
}
} else {
LOG.error("Unsupported platform. No update performed.");
}
}

/**
* <p>
* Getter for the field <code>name</code>.
Expand Down Expand Up @@ -674,4 +723,47 @@ public int getBitness() {
public void setBitness(int bitness) {
this.bitness = bitness;
}

private OperatingSystem getCurrentOperatingSystem() {
switch (SystemInfo.getCurrentPlatformEnum()) {
case WINDOWS:
return new WindowsOperatingSystem();
case LINUX:
return new LinuxOperatingSystem();
case MACOSX:
return new MacOperatingSystem();
case SOLARIS:
return new SolarisOperatingSystem();
case FREEBSD:
return new FreeBsdOperatingSystem();
}
return null;
}

private void copyValuesToThisProcess(OSProcess sourceProcess) {
this.name = sourceProcess.name;
this.path = sourceProcess.path;
this.commandLine = sourceProcess.commandLine;
this.currentWorkingDirectory = sourceProcess.currentWorkingDirectory;
this.user = sourceProcess.user;
this.userID = sourceProcess.userID;
this.group = sourceProcess.group;
this.groupID = sourceProcess.groupID;
this.state = sourceProcess.state;
this.processID = sourceProcess.processID;
this.parentProcessID = sourceProcess.parentProcessID;
this.threadCount = sourceProcess.threadCount;
this.priority = sourceProcess.priority;
this.virtualSize = sourceProcess.virtualSize;
this.residentSetSize = sourceProcess.residentSetSize;
this.kernelTime = sourceProcess.kernelTime;
this.userTime = sourceProcess.userTime;
this.startTime = sourceProcess.startTime;
this.upTime = sourceProcess.upTime;
this.bytesRead = sourceProcess.bytesRead;
this.bytesWritten = sourceProcess.bytesWritten;
this.openFiles = sourceProcess.openFiles;
this.bitness = sourceProcess.bitness;
this.cpuPercent = sourceProcess.cpuPercent;
}
}
13 changes: 13 additions & 0 deletions oshi-core/src/test/java/oshi/software/os/OperatingSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,17 @@ public void testGetCommandLine() {

assertTrue(processesWithNonEmptyCmdLine >= 1);
}

@Test
public void testConstructProcessWithGivenPid() {
SystemInfo si = new SystemInfo();
OperatingSystem os = si.getOperatingSystem();
OSProcess oldProcess = os.getProcess(os.getProcessId());
int givenPid = oldProcess.getProcessID();

OSProcess newProcess = new OSProcess(givenPid);

assertEquals(oldProcess.getPath(), newProcess.getPath());
assertEquals(oldProcess.getProcessID(), newProcess.getProcessID());
}
}

0 comments on commit 455fd7d

Please sign in to comment.