Skip to content

Commit

Permalink
Merge pull request oshi#7 from ptitvert/master
Browse files Browse the repository at this point in the history
Added Mac OS X support
  • Loading branch information
dblock committed Nov 11, 2014
2 parents 86e463b + fb5dd3f commit 757f509
Show file tree
Hide file tree
Showing 13 changed files with 595 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
language: java
os:
- osx
- linux
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Next
* [#5](https://github.com/dblock/oshi/pull/5): Added Linux support - [@ptitvert](https://github.com/ptitvert).
* [#3](https://github.com/dblock/oshi/pull/3): Mavenized project - [@le-yams](https://github.com/le-yams).
* Added Travis-CI - [@dblock](https://github.com/dblock).
* Added Mac OS X Support [@ptitvert](https://github.com/ptitvert).
* Your contribution here.

1.1 (10/13/2013)
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/oshi/PlatformEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
*
*/
package oshi;

/**
* @author alessandro[at]perucchi[dot]org
*/

public enum PlatformEnum {
WINDOWS, LINUX, MACOSX, UNKNOWN;
}
63 changes: 47 additions & 16 deletions src/main/java/oshi/SystemInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,85 @@
*/
package oshi;

import com.sun.jna.Platform;

import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;
import oshi.software.os.linux.LinuxHardwareAbstractionLayer;
import oshi.software.os.linux.LinuxOperatingSystem;
import oshi.software.os.mac.MacHardwareAbstractionLayer;
import oshi.software.os.mac.MacOperatingSystem;
import oshi.software.os.windows.WindowsHardwareAbstractionLayer;
import oshi.software.os.windows.WindowsOperatingSystem;

import com.sun.jna.Platform;

/**
* Current system information.
*
* @author dblock[at]dblock[dot]org
*/
public class SystemInfo {
private OperatingSystem _os = null;
private HardwareAbstractionLayer _hardware = null;

private PlatformEnum currentPlatformEnum;

{
if (Platform.isWindows())
currentPlatformEnum = PlatformEnum.WINDOWS;
else if (Platform.isLinux())
currentPlatformEnum = PlatformEnum.LINUX;
else if (Platform.isMac())
currentPlatformEnum = PlatformEnum.MACOSX;
else
currentPlatformEnum = PlatformEnum.UNKNOWN;
}

/**
* Retrieves operating system information.
* @return
* Operating system information.
*
* @return Operating system information.
*/
public OperatingSystem getOperatingSystem() {
if (_os == null) {
if (Platform.isWindows()) {
switch (currentPlatformEnum) {

case WINDOWS:
_os = new WindowsOperatingSystem();
} else if (Platform.isLinux()) {
break;
case LINUX:
_os = new LinuxOperatingSystem();
} else {
throw new RuntimeException("Operating system not supported: " + Platform.getOSType());
break;
case MACOSX:
_os = new MacOperatingSystem();
break;
default:
throw new RuntimeException("Operating system not supported: "
+ Platform.getOSType());
}
}
return _os;
}

/**
* Retrieves hardware information.
* @return
* Hardware abstraction layer.
*
* @return Hardware abstraction layer.
*/
public HardwareAbstractionLayer getHardware() {
if (_hardware == null) {
if (Platform.isWindows()) {
switch (currentPlatformEnum) {

case WINDOWS:
_hardware = new WindowsHardwareAbstractionLayer();
} else if (Platform.isLinux()) {
break;
case LINUX:
_hardware = new LinuxHardwareAbstractionLayer();
} else {
throw new RuntimeException("Operating system not supported: " + Platform.getOSType());
break;
case MACOSX:
_hardware = new MacHardwareAbstractionLayer();
break;
default:
throw new RuntimeException("Operating system not supported: "
+ Platform.getOSType());
}
}
return _hardware;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import oshi.software.os.linux.proc.CentralProcessor;
import oshi.software.os.linux.proc.GlobalMemory;

/**
* @author alessandro[at]perucchi[dot]org
*/

public class LinuxHardwareAbstractionLayer implements HardwareAbstractionLayer {

private static final String SEPARATOR = "\\s+:\\s";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void setFamily(String _family) {

@Override
public String toString() {
return _name;
return getName();
}

}
48 changes: 31 additions & 17 deletions src/main/java/oshi/software/os/linux/proc/OSVersionInfoEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Contains operating system version information. The information includes major
* and minor version numbers, a build number, a platform identifier, and
* descriptive text about the operating system.
*
*
* @author alessandro[at]perucchi[dot]org
*/
public class OSVersionInfoEx implements OperatingSystemVersion {
Expand All @@ -40,32 +40,38 @@ public OSVersionInfoEx() {
setVersion(splittedLine[1]);
}
if (splittedLine[0].equals("VERSION")) {
setCodeName(splittedLine[1].split("[()]")[1]);
// Check basically if the code is between parenthesis or after
// the coma-space

// Basically, until now, that seems to be the standard to use
// parenthesis for the codename.
String[] split = splittedLine[1].split("[()]");
if (split.length <= 1)
// We are probably with Ubuntu, so need to get that part
// correctly.
split = splittedLine[1].split(", ");

if (split.length > 1)
setCodeName(split[1]);
else
setCodeName(splittedLine[1]);
}
}
in.close();
}

/**
* @return the _version
*/
public String getVersion() {
return _version;
}

/**
* @param _version
* the _version to set
* @return the _codeName
*/
public void setVersion(String _version) {
this._version = _version;
public String getCodeName() {
return _codeName;
}

/**
* @return the _codeName
* @return the _version
*/
public String getCodeName() {
return _codeName;
public String getVersion() {
return _version;
}

/**
Expand All @@ -76,10 +82,18 @@ public void setCodeName(String _codeName) {
this._codeName = _codeName;
}

/**
* @param _version
* the _version to set
*/
public void setVersion(String _version) {
this._version = _version;
}

@Override
public String toString() {
if (version == null) {
version=getVersion()+" ("+getCodeName()+")";
version = getVersion() + " (" + getCodeName() + ")";
}
return version;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
*
*/
package oshi.software.os.mac;

import java.util.ArrayList;
import java.util.List;

import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.Memory;
import oshi.hardware.Processor;
import oshi.software.os.mac.local.CentralProcessor;
import oshi.software.os.mac.local.GlobalMemory;
import oshi.util.ExecutingCommand;

/**
* @author alessandro[at]perucchi[dot]org
*/

public class MacHardwareAbstractionLayer implements HardwareAbstractionLayer {

private Processor[] _processors;
private Memory _memory;

/*
* (non-Javadoc)
*
* @see oshi.hardware.HardwareAbstractionLayer#getProcessors()
*/
public Processor[] getProcessors() {
if (_processors == null) {
List<Processor> processors = new ArrayList<Processor>();
int nbCPU = new Integer(
ExecutingCommand.getFirstAnswer("sysctl -n hw.availcpu"));
for (int i = 0; i < nbCPU; i++)
processors.add(new CentralProcessor());

_processors = processors.toArray(new Processor[0]);
}
return _processors;

}

/*
* (non-Javadoc)
*
* @see oshi.hardware.HardwareAbstractionLayer#getMemory()
*/
public Memory getMemory() {
if (_memory == null) {
_memory = new GlobalMemory();
}
return _memory;
}

}
47 changes: 47 additions & 0 deletions src/main/java/oshi/software/os/mac/MacOperatingSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
*
*/
package oshi.software.os.mac;

import oshi.software.os.OperatingSystem;
import oshi.software.os.OperatingSystemVersion;
import oshi.software.os.mac.local.OSVersionInfoEx;
import oshi.util.ExecutingCommand;

/**
* @author alessandro[at]perucchi[dot]org
*/

public class MacOperatingSystem implements OperatingSystem {
private String _family;

private OperatingSystemVersion _version = null;

public OperatingSystemVersion getVersion() {
if (_version == null) {
_version = new OSVersionInfoEx();
}
return _version;
}

public String getFamily() {
if (_family == null)
_family = ExecutingCommand.getFirstAnswer("sw_vers -productName");

return _family;
}

public String getManufacturer() {
return "Apple";
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getManufacturer());
sb.append(" ");
sb.append(getFamily());
sb.append(" ");
sb.append(getVersion().toString());
return sb.toString();
}
}
Loading

0 comments on commit 757f509

Please sign in to comment.