Added concept of platform to the internals.

This commit is contained in:
Adam Murdoch
2012-08-04 11:41:19 +10:00
parent 0c8a041a6c
commit dadf93caf8
6 changed files with 77 additions and 13 deletions

View File

@@ -5,10 +5,10 @@ public class Main {
System.out.println(); System.out.println();
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch")); 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()); 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 stdoutIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stdout);
boolean stderrIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stderr); boolean stderrIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stderr);
System.out.println("* stdout: " + (stdoutIsTerminal ? "terminal" : "not a terminal")); System.out.println("* stdout: " + (stdoutIsTerminal ? "terminal" : "not a terminal"));
@@ -34,7 +34,8 @@ public class Main {
terminal.normal(); terminal.normal();
System.out.println(); System.out.println();
} }
System.out.println();
} }
System.out.println();
} }
} }

View File

@@ -9,26 +9,28 @@ import java.io.PrintStream;
/** /**
* Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration. * 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 final Object lock = new Object();
private static boolean loaded; private static boolean loaded;
static <T extends NativeIntegration> T get(Class<T> type) { static <T extends NativeIntegration> T get(Class<T> type) {
synchronized (lock) { synchronized (lock) {
if (!loaded) { 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 { try {
File libFile = new File("build/binaries/libnative-platform.dylib"); File libFile = new File("build/binaries/" + platform.getLibraryName());
if (!libFile.isFile()) {
libFile = new File("build/binaries/libnative-platform.so");
}
System.load(libFile.getCanonicalPath()); System.load(libFile.getCanonicalPath());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
int nativeVersion = NativeLibraryFunctions.getVersion(); int nativeVersion = NativeLibraryFunctions.getVersion();
if (nativeVersion != NativeLibraryFunctions.VERSION) { 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; loaded = true;
} }

View File

@@ -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();
}
}
}

View File

@@ -6,7 +6,7 @@ import org.junit.rules.TemporaryFolder
class PosixFileTest extends Specification { class PosixFileTest extends Specification {
@Rule TemporaryFolder tmpDir @Rule TemporaryFolder tmpDir
final PosixFile file = Platform.get(PosixFile.class) final PosixFile file = Native.get(PosixFile.class)
def "can set mode on a file"() { def "can set mode on a file"() {
def testFile = tmpDir.newFile("test.txt") def testFile = tmpDir.newFile("test.txt")

View File

@@ -6,7 +6,7 @@ import spock.lang.Specification
class ProcessTest extends Specification { class ProcessTest extends Specification {
@Rule TemporaryFolder tmpDir @Rule TemporaryFolder tmpDir
final Process process = Platform.get(Process.class) final Process process = Native.get(Process.class)
def "can get PID"() { def "can get PID"() {
expect: expect:

View File

@@ -6,7 +6,7 @@ import spock.lang.Specification
class TerminalTest extends Specification { class TerminalTest extends Specification {
@Rule TemporaryFolder tmpDir @Rule TemporaryFolder tmpDir
final TerminalAccess terminal = Platform.get(TerminalAccess.class) final TerminalAccess terminal = Native.get(TerminalAccess.class)
def "can check if attached to terminal"() { def "can check if attached to terminal"() {
expect: expect: