Moved creation of integrations to Platform.
This commit is contained in:
@@ -59,40 +59,11 @@ public class Native {
|
|||||||
static <T extends NativeIntegration> T get(Class<T> type) {
|
static <T extends NativeIntegration> T get(Class<T> type) {
|
||||||
init(null);
|
init(null);
|
||||||
Platform platform = Platform.current();
|
Platform platform = Platform.current();
|
||||||
if (platform.isPosix()) {
|
T integration = platform.get(type);
|
||||||
if (type.equals(PosixFile.class)) {
|
if (integration != null) {
|
||||||
return type.cast(new DefaultPosixFile());
|
return integration;
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
} else if (platform.isWindows()) {
|
|
||||||
if (type.equals(Process.class)) {
|
|
||||||
return type.cast(new DefaultProcess());
|
|
||||||
}
|
|
||||||
if (type.equals(TerminalAccess.class)) {
|
|
||||||
return type.cast(new WindowsTerminalAccess());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (platform.isOsX()) {
|
|
||||||
if (type.equals(FileSystems.class)) {
|
|
||||||
return type.cast(new PosixFileSystems());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new UnsupportedOperationException(String.format("Cannot load unsupported native integration %s.",
|
throw new UnsupportedOperationException(String.format("Cannot load unsupported native integration %s.",
|
||||||
type.getName()));
|
type.getName()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package net.rubygrapefruit.platform.internal;
|
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 {
|
public abstract class Platform {
|
||||||
private static Platform platform;
|
private static Platform platform;
|
||||||
|
|
||||||
@@ -7,11 +11,12 @@ public abstract class Platform {
|
|||||||
synchronized (Platform.class) {
|
synchronized (Platform.class) {
|
||||||
if (platform == null) {
|
if (platform == null) {
|
||||||
String osName = System.getProperty("os.name").toLowerCase();
|
String osName = System.getProperty("os.name").toLowerCase();
|
||||||
|
String arch = System.getProperty("os.arch");
|
||||||
if (osName.contains("windows")) {
|
if (osName.contains("windows")) {
|
||||||
platform = new Windows();
|
platform = new Windows();
|
||||||
} else if (osName.contains("linux")) {
|
} else if (osName.contains("linux")) {
|
||||||
platform = new 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();
|
platform = new OsX();
|
||||||
} else if (osName.contains("sunos")) {
|
} else if (osName.contains("sunos")) {
|
||||||
platform = new Solaris();
|
platform = new Solaris();
|
||||||
@@ -27,16 +32,12 @@ public abstract class Platform {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPosix() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isWindows() {
|
public boolean isWindows() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOsX() {
|
public <T extends NativeIntegration> T get(Class<T> type) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract String getLibraryName();
|
public abstract String getLibraryName();
|
||||||
@@ -51,12 +52,43 @@ public abstract class Platform {
|
|||||||
public String getLibraryName() {
|
public String getLibraryName() {
|
||||||
return "native-platform.dll";
|
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 {
|
private static abstract class Posix extends Platform {
|
||||||
@Override
|
@Override
|
||||||
public boolean isPosix() {
|
public <T extends NativeIntegration> T get(Class<T> type) {
|
||||||
return true;
|
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 {
|
private static class OsX extends Posix {
|
||||||
@Override
|
@Override
|
||||||
public boolean isOsX() {
|
public <T extends NativeIntegration> T get(Class<T> type) {
|
||||||
return true;
|
if (type.equals(FileSystems.class)) {
|
||||||
|
return type.cast(new PosixFileSystems());
|
||||||
|
}
|
||||||
|
return super.get(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user