Added concept of platform to the internals.
This commit is contained in:
@@ -5,10 +5,10 @@ public class Main {
|
||||
System.out.println();
|
||||
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch"));
|
||||
|
||||
Process process = Platform.get(Process.class);
|
||||
Process process = Native.get(Process.class);
|
||||
System.out.println("* PID: " + process.getPid());
|
||||
|
||||
TerminalAccess terminalAccess = Platform.get(TerminalAccess.class);
|
||||
TerminalAccess terminalAccess = Native.get(TerminalAccess.class);
|
||||
boolean stdoutIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stdout);
|
||||
boolean stderrIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stderr);
|
||||
System.out.println("* stdout: " + (stdoutIsTerminal ? "terminal" : "not a terminal"));
|
||||
@@ -34,7 +34,8 @@ public class Main {
|
||||
terminal.normal();
|
||||
System.out.println();
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,26 +9,28 @@ import java.io.PrintStream;
|
||||
/**
|
||||
* Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration.
|
||||
*/
|
||||
public class Platform {
|
||||
public class Native {
|
||||
private static final Object lock = new Object();
|
||||
private static boolean loaded;
|
||||
|
||||
static <T extends NativeIntegration> T get(Class<T> type) {
|
||||
synchronized (lock) {
|
||||
if (!loaded) {
|
||||
System.setProperty("java.library.path", new File("build/binaries").getAbsolutePath());
|
||||
Platform platform = Platform.current();
|
||||
if (!platform.isSupported()) {
|
||||
throw new NativeException(String.format("The current platform is not supported."));
|
||||
}
|
||||
try {
|
||||
File libFile = new File("build/binaries/libnative-platform.dylib");
|
||||
if (!libFile.isFile()) {
|
||||
libFile = new File("build/binaries/libnative-platform.so");
|
||||
}
|
||||
File libFile = new File("build/binaries/" + platform.getLibraryName());
|
||||
System.load(libFile.getCanonicalPath());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
int nativeVersion = NativeLibraryFunctions.getVersion();
|
||||
if (nativeVersion != NativeLibraryFunctions.VERSION) {
|
||||
throw new NativeException(String.format("Unexpected native library version loaded. Expected %s, was %s.", nativeVersion, NativeLibraryFunctions.VERSION));
|
||||
throw new NativeException(String.format(
|
||||
"Unexpected native library version loaded. Expected %s, was %s.", nativeVersion,
|
||||
NativeLibraryFunctions.VERSION));
|
||||
}
|
||||
loaded = true;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
public abstract class Platform {
|
||||
private static Platform platform;
|
||||
|
||||
public static Platform current() {
|
||||
synchronized (Platform.class) {
|
||||
if (platform == null) {
|
||||
String osName = System.getProperty("os.name").toLowerCase();
|
||||
if (osName.contains("windows")) {
|
||||
platform = new Windows();
|
||||
} else if (osName.contains("linux")) {
|
||||
platform = new Linux();
|
||||
} else if (osName.contains("os x")) {
|
||||
platform = new OsX();
|
||||
} else {
|
||||
platform = new Unsupported();
|
||||
}
|
||||
}
|
||||
return platform;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public abstract String getLibraryName();
|
||||
|
||||
private static class Windows extends Platform {
|
||||
@Override
|
||||
public String getLibraryName() {
|
||||
return "native-platform.dll";
|
||||
}
|
||||
}
|
||||
|
||||
private static class Linux extends Platform {
|
||||
@Override
|
||||
public String getLibraryName() {
|
||||
return "libnative-platform.so";
|
||||
}
|
||||
}
|
||||
|
||||
private static class OsX extends Platform {
|
||||
@Override
|
||||
public String getLibraryName() {
|
||||
return "libnative-platform.dylib";
|
||||
}
|
||||
}
|
||||
|
||||
private static class Unsupported extends Platform {
|
||||
@Override
|
||||
public boolean isSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getLibraryName() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user