Implemented SystemInfo for windows.

This commit is contained in:
Adam Murdoch
2012-09-09 17:07:38 +10:00
parent dd255be667
commit de4e340e2c
5 changed files with 784 additions and 721 deletions

View File

@@ -1,21 +1,53 @@
package net.rubygrapefruit.platform.internal;
import net.rubygrapefruit.platform.SystemInfo;
public class MutableSystemInfo implements SystemInfo {
public String osName;
public String osVersion;
public String machineArchitecture;
public String getKernelName() {
return osName;
}
public String getKernelVersion() {
return osVersion;
}
public String getMachineArchitecture() {
return machineArchitecture;
}
}
package net.rubygrapefruit.platform.internal;
import net.rubygrapefruit.platform.SystemInfo;
public class MutableSystemInfo implements SystemInfo {
public String osName;
public String osVersion;
public String machineArchitecture;
public String getKernelName() {
return osName;
}
public String getKernelVersion() {
return osVersion;
}
public String getMachineArchitecture() {
return machineArchitecture;
}
void windows(int major, int minor, int build, boolean workstation, String arch) {
osName = toWindowsVersionName(major, minor, workstation);
osVersion = String.format("%s.%s (build %s)", major, minor, build);
machineArchitecture = arch;
}
private String toWindowsVersionName(int major, int minor, boolean workstation) {
switch (major) {
case 5:
switch (minor) {
case 0:
return "Windows 2000";
case 1:
return "Windows XP";
case 2:
return "Windows Server 2003";
}
break;
case 6:
switch (minor) {
case 0:
return workstation ? "Windows Vista" : "Windows Server 2008";
case 1:
return workstation ? "Windows 7" : "Windows Server 2008 R2";
case 2:
return workstation ? "Windows 8" : "Windows Server 2012";
}
break;
}
return "Windows (unknown version)";
}
}

View File

@@ -1,145 +1,152 @@
package net.rubygrapefruit.platform.internal;
import net.rubygrapefruit.platform.*;
import net.rubygrapefruit.platform.Process;
public abstract class Platform {
private static Platform platform;
public static Platform current() {
synchronized (Platform.class) {
if (platform == null) {
String osName = getOperatingSystem().toLowerCase();
String arch = getArchitecture();
if (osName.contains("windows")) {
platform = new Windows();
} else if (osName.contains("linux")) {
platform = new Linux();
} else if (osName.contains("os x") && (arch.equals("i386") || arch.equals("x86_64"))) {
platform = new OsX();
} else if (osName.contains("sunos")) {
platform = new Solaris();
} else {
platform = new Unsupported();
}
}
return platform;
}
}
public boolean isWindows() {
return false;
}
public <T extends NativeIntegration> T get(Class<T> type) {
throw new NativeIntegrationUnavailableException(String.format("Native integration %s is not supported on this operating system (%s %s)",
type.getSimpleName(), getOperatingSystem(), getArchitecture()));
}
public abstract String getLibraryName() throws NativeIntegrationUnavailableException;
private static class Windows extends Platform {
@Override
public boolean isWindows() {
return true;
}
@Override
public String getLibraryName() {
return "native-platform-win32.dll";
}
@Override
public <T extends NativeIntegration> T get(Class<T> type) {
if (type.equals(net.rubygrapefruit.platform.Process.class)) {
return type.cast(new DefaultProcess());
}
if (type.equals(Terminals.class)) {
return type.cast(new WindowsTerminals());
}
return super.get(type);
}
}
private static abstract class Posix extends Platform {
@Override
public <T extends NativeIntegration> T get(Class<T> type) {
if (type.equals(PosixFile.class)) {
return type.cast(new DefaultPosixFile());
}
if (type.equals(Process.class)) {
return type.cast(new DefaultProcess());
}
if (type.equals(Terminals.class)) {
return type.cast(new TerminfoTerminals());
}
if (type.equals(SystemInfo.class)) {
return type.cast(new DefaultSystemInfo());
}
return super.get(type);
}
}
private abstract static class Unix extends Posix {
}
private static class Linux extends Unix {
@Override
public <T extends NativeIntegration> T get(Class<T> type) {
if (type.equals(FileSystems.class)) {
return type.cast(new PosixFileSystems());
}
return super.get(type);
}
@Override
public String getLibraryName() {
if (getArchitecture().equals("amd64")) {
return "libnative-platform-linux-amd64.so";
}
if (getArchitecture().equals("i386") || getArchitecture().equals("x86")) {
return "libnative-platform-linux-i386.so";
}
throw new NativeIntegrationUnavailableException(String.format(
"Native integration is not available for this architecture (%s) on Linux.", getArchitecture()));
}
}
private static String getArchitecture() {
return System.getProperty("os.arch");
}
private static class Solaris extends Unix {
@Override
public String getLibraryName() {
return "libnative-platform-solaris.so";
}
}
private static class OsX extends Posix {
@Override
public <T extends NativeIntegration> T get(Class<T> type) {
if (type.equals(FileSystems.class)) {
return type.cast(new PosixFileSystems());
}
return super.get(type);
}
@Override
public String getLibraryName() {
return "libnative-platform-osx-universal.dylib";
}
}
private static class Unsupported extends Platform {
public String getLibraryName() {
throw new NativeIntegrationUnavailableException(String.format(
"Native integration is not available for this operating system (%s %s)", getOperatingSystem(),
getArchitecture()));
}
}
private static String getOperatingSystem() {
return System.getProperty("os.name");
}
}
package net.rubygrapefruit.platform.internal;
import net.rubygrapefruit.platform.*;
import net.rubygrapefruit.platform.Process;
public abstract class Platform {
private static Platform platform;
public static Platform current() {
synchronized (Platform.class) {
if (platform == null) {
String osName = getOperatingSystem().toLowerCase();
String arch = getArchitecture();
if (osName.contains("windows")) {
platform = new Windows();
} else if (osName.contains("linux")) {
platform = new Linux();
} else if (osName.contains("os x") && (arch.equals("i386") || arch.equals("x86_64"))) {
platform = new OsX();
} else if (osName.contains("sunos")) {
platform = new Solaris();
} else {
platform = new Unsupported();
}
}
return platform;
}
}
public boolean isWindows() {
return false;
}
public <T extends NativeIntegration> T get(Class<T> type) {
throw new NativeIntegrationUnavailableException(String.format("Native integration %s is not supported on this operating system (%s %s)",
type.getSimpleName(), getOperatingSystem(), getArchitecture()));
}
public abstract String getLibraryName() throws NativeIntegrationUnavailableException;
private static class Windows extends Platform {
@Override
public boolean isWindows() {
return true;
}
@Override
public String getLibraryName() {
if (getArchitecture().equals("x86")) {
return "native-platform-windows-i386.dll";
}
throw new NativeIntegrationUnavailableException(String.format(
"Native integration is not available for this architecture (%s) on Windows.", getArchitecture()));
}
@Override
public <T extends NativeIntegration> T get(Class<T> type) {
if (type.equals(net.rubygrapefruit.platform.Process.class)) {
return type.cast(new DefaultProcess());
}
if (type.equals(Terminals.class)) {
return type.cast(new WindowsTerminals());
}
if (type.equals(SystemInfo.class)) {
return type.cast(new DefaultSystemInfo());
}
return super.get(type);
}
}
private static abstract class Posix extends Platform {
@Override
public <T extends NativeIntegration> T get(Class<T> type) {
if (type.equals(PosixFile.class)) {
return type.cast(new DefaultPosixFile());
}
if (type.equals(Process.class)) {
return type.cast(new DefaultProcess());
}
if (type.equals(Terminals.class)) {
return type.cast(new TerminfoTerminals());
}
if (type.equals(SystemInfo.class)) {
return type.cast(new DefaultSystemInfo());
}
return super.get(type);
}
}
private abstract static class Unix extends Posix {
}
private static class Linux extends Unix {
@Override
public <T extends NativeIntegration> T get(Class<T> type) {
if (type.equals(FileSystems.class)) {
return type.cast(new PosixFileSystems());
}
return super.get(type);
}
@Override
public String getLibraryName() {
if (getArchitecture().equals("amd64")) {
return "libnative-platform-linux-amd64.so";
}
if (getArchitecture().equals("i386") || getArchitecture().equals("x86")) {
return "libnative-platform-linux-i386.so";
}
throw new NativeIntegrationUnavailableException(String.format(
"Native integration is not available for this architecture (%s) on Linux.", getArchitecture()));
}
}
private static String getArchitecture() {
return System.getProperty("os.arch");
}
private static class Solaris extends Unix {
@Override
public String getLibraryName() {
return "libnative-platform-solaris.so";
}
}
private static class OsX extends Posix {
@Override
public <T extends NativeIntegration> T get(Class<T> type) {
if (type.equals(FileSystems.class)) {
return type.cast(new PosixFileSystems());
}
return super.get(type);
}
@Override
public String getLibraryName() {
return "libnative-platform-osx-universal.dylib";
}
}
private static class Unsupported extends Platform {
public String getLibraryName() {
throw new NativeIntegrationUnavailableException(String.format(
"Native integration is not available for this operating system (%s %s)", getOperatingSystem(),
getArchitecture()));
}
}
private static String getOperatingSystem() {
return System.getProperty("os.name");
}
}