Moved creation of integrations to Platform.

This commit is contained in:
Adam Murdoch
2012-08-12 07:45:10 +10:00
parent 97038bbf6e
commit 2e5f9cb546
2 changed files with 50 additions and 44 deletions

View File

@@ -1,5 +1,9 @@
package net.rubygrapefruit.platform.internal;
import net.rubygrapefruit.platform.*;
import net.rubygrapefruit.platform.Process;
import net.rubygrapefruit.platform.internal.jni.NativeLibraryFunctions;
public abstract class Platform {
private static Platform platform;
@@ -7,11 +11,12 @@ public abstract class Platform {
synchronized (Platform.class) {
if (platform == null) {
String osName = System.getProperty("os.name").toLowerCase();
String arch = System.getProperty("os.arch");
if (osName.contains("windows")) {
platform = new Windows();
} else if (osName.contains("linux")) {
platform = new Linux();
} else if (osName.contains("os x")) {
} else if (osName.contains("os x") && (arch.equals("i386") || arch.equals("x86_64"))) {
platform = new OsX();
} else if (osName.contains("sunos")) {
platform = new Solaris();
@@ -27,16 +32,12 @@ public abstract class Platform {
return true;
}
public boolean isPosix() {
return false;
}
public boolean isWindows() {
return false;
}
public boolean isOsX() {
return false;
public <T extends NativeIntegration> T get(Class<T> type) {
return null;
}
public abstract String getLibraryName();
@@ -51,12 +52,43 @@ public abstract class Platform {
public String getLibraryName() {
return "native-platform.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(TerminalAccess.class)) {
return type.cast(new WindowsTerminalAccess());
}
return super.get(type);
}
}
private static abstract class Posix extends Platform {
@Override
public boolean isPosix() {
return true;
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(TerminalAccess.class)) {
return type.cast(new TerminfoTerminalAccess());
}
if (type.equals(SystemInfo.class)) {
MutableSystemInfo systemInfo = new MutableSystemInfo();
FunctionResult result = new FunctionResult();
NativeLibraryFunctions.getSystemInfo(systemInfo, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not fetch system information: %s",
result.getMessage()));
}
System.out.println("=> CHARACTER ENCODING: " + systemInfo.characterEncoding);
return type.cast(systemInfo);
}
return super.get(type);
}
}
@@ -75,8 +107,11 @@ public abstract class Platform {
private static class OsX extends Posix {
@Override
public boolean isOsX() {
return true;
public <T extends NativeIntegration> T get(Class<T> type) {
if (type.equals(FileSystems.class)) {
return type.cast(new PosixFileSystems());
}
return super.get(type);
}
@Override