First pass for windows support.

This commit is contained in:
Adam Murdoch
2012-08-04 12:32:15 +10:00
parent dadf93caf8
commit e5537494b0
11 changed files with 689 additions and 617 deletions

11
build.gradle Normal file → Executable file
View File

@@ -15,19 +15,28 @@ dependencies {
mainClassName = 'net.rubygrapefruit.platform.Main' mainClassName = 'net.rubygrapefruit.platform.Main'
def nativeHeadersDir = file("$buildDir/nativeHeaders") def nativeHeadersDir = file("$buildDir/nativeHeaders")
sourceCompatibility = 1.5
targetCompatibility = 1.5 targetCompatibility = 1.5
println org.gradle.internal.jvm.Jvm.current().javaHome
println org.gradle.internal.jvm.Jvm.current().toolsJar
libraries { libraries {
main { main {
spec { spec {
includes([nativeHeadersDir]) includes([nativeHeadersDir])
if (org.gradle.internal.os.OperatingSystem.current().macOsX) { if (org.gradle.internal.os.OperatingSystem.current().macOsX) {
includes(['/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/']) includes(['/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/'])
args("-lcurses")
} else if (org.gradle.internal.os.OperatingSystem.current().windows) {
includes(["${org.gradle.internal.jvm.Jvm.current().javaHome}/include"])
includes(["${org.gradle.internal.jvm.Jvm.current().javaHome}/include/win32"])
args("/DWIN32")
} else { } else {
includes(["${org.gradle.internal.jvm.Jvm.current().javaHome}/include"]) includes(["${org.gradle.internal.jvm.Jvm.current().javaHome}/include"])
includes(["${org.gradle.internal.jvm.Jvm.current().javaHome}/include/linux"]) includes(["${org.gradle.internal.jvm.Jvm.current().javaHome}/include/linux"])
args("-lcurses")
} }
args("-lcurses")
} }
} }
} }

9
src/main/cpp/generic.c Executable file
View File

@@ -0,0 +1,9 @@
/*
* Generic functions
*/
#include "native.h"
JNIEXPORT jint JNICALL
Java_net_rubygrapefruit_platform_internal_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) {
return 2;
}

13
src/main/cpp/posixFunctions.c → src/main/cpp/posix.c Normal file → Executable file
View File

@@ -1,3 +1,5 @@
#ifndef WIN32
#include "native.h" #include "native.h"
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
@@ -28,15 +30,6 @@ void mark_failed_with_message(JNIEnv *env, const char* message, jobject result)
env->CallVoidMethod(result, method, message_str); env->CallVoidMethod(result, method, message_str);
} }
/*
* Generic functions
*/
JNIEXPORT jint JNICALL
Java_net_rubygrapefruit_platform_internal_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) {
return 2;
}
/* /*
* File functions * File functions
*/ */
@@ -176,3 +169,5 @@ Java_net_rubygrapefruit_platform_internal_TerminfoFunctions_foreground(JNIEnv *e
return; return;
} }
} }
#endif

15
src/main/cpp/win.c Executable file
View File

@@ -0,0 +1,15 @@
#ifdef WIN32
#include "native.h"
#include <windows.h>
/*
* Process functions
*/
JNIEXPORT jint JNICALL
Java_net_rubygrapefruit_platform_internal_PosixProcessFunctions_getPid(JNIEnv *env, jclass target) {
return GetCurrentProcessId();
}
#endif

2
src/main/java/net/rubygrapefruit/platform/Main.java Normal file → Executable file
View File

@@ -6,7 +6,7 @@ public class Main {
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 = Native.get(Process.class); Process process = Native.get(Process.class);
System.out.println("* PID: " + process.getPid()); System.out.println("* PID: " + process.getProcessId());
TerminalAccess terminalAccess = Native.get(TerminalAccess.class); TerminalAccess terminalAccess = Native.get(TerminalAccess.class);
boolean stdoutIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stdout); boolean stdoutIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stdout);

45
src/main/java/net/rubygrapefruit/platform/Native.java Normal file → Executable file
View File

@@ -14,9 +14,9 @@ public class Native {
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) {
Platform platform = Platform.current();
synchronized (lock) { synchronized (lock) {
if (!loaded) { if (!loaded) {
Platform platform = Platform.current();
if (!platform.isSupported()) { if (!platform.isSupported()) {
throw new NativeException(String.format("The current platform is not supported.")); throw new NativeException(String.format("The current platform is not supported."));
} }
@@ -35,16 +35,25 @@ public class Native {
loaded = true; loaded = true;
} }
} }
if (type.equals(PosixFile.class)) { if (platform.isPosix()) {
return type.cast(new DefaultPosixFile()); 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());
}
} 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 (type.equals(Process.class)) { throw new UnsupportedOperationException(String.format("Cannot load unsupported native integration %s.",
return type.cast(new DefaultProcess());
}
if (type.equals(TerminalAccess.class)) {
return type.cast(new DefaultTerminalAccess());
}
throw new UnsupportedOperationException(String.format("Cannot load unknown native integration %s.",
type.getName())); type.getName()));
} }
@@ -72,12 +81,12 @@ public class Native {
private static class DefaultProcess implements Process { private static class DefaultProcess implements Process {
@Override @Override
public int getPid() throws NativeException { public int getProcessId() throws NativeException {
return PosixProcessFunctions.getPid(); return PosixProcessFunctions.getPid();
} }
} }
private static class DefaultTerminalAccess implements TerminalAccess { private static class TerminfoTerminalAccess implements TerminalAccess {
private static Output currentlyOpen; private static Output currentlyOpen;
@Override @Override
@@ -178,4 +187,16 @@ public class Native {
return this; return this;
} }
} }
private static class WindowsTerminalAccess implements TerminalAccess {
@Override
public boolean isTerminal(Output output) {
return false;
}
@Override
public Terminal getTerminal(Output output) {
throw new UnsupportedOperationException();
}
}
} }

2
src/main/java/net/rubygrapefruit/platform/Process.java Normal file → Executable file
View File

@@ -4,5 +4,5 @@ package net.rubygrapefruit.platform;
* Functions to query and modify a process' meta-data * Functions to query and modify a process' meta-data
*/ */
public interface Process extends NativeIntegration { public interface Process extends NativeIntegration {
int getPid() throws NativeException; int getProcessId() throws NativeException;
} }

View File

@@ -25,23 +25,43 @@ public abstract class Platform {
return true; return true;
} }
public boolean isPosix() {
return false;
}
public boolean isWindows() {
return false;
}
public abstract String getLibraryName(); public abstract String getLibraryName();
private static class Windows extends Platform { private static class Windows extends Platform {
@Override
public boolean isWindows() {
return true;
}
@Override @Override
public String getLibraryName() { public String getLibraryName() {
return "native-platform.dll"; return "native-platform.dll";
} }
} }
private static class Linux extends Platform { private static abstract class Posix extends Platform {
@Override
public boolean isPosix() {
return true;
}
}
private static class Linux extends Posix {
@Override @Override
public String getLibraryName() { public String getLibraryName() {
return "libnative-platform.so"; return "libnative-platform.so";
} }
} }
private static class OsX extends Platform { private static class OsX extends Posix {
@Override @Override
public String getLibraryName() { public String getLibraryName() {
return "libnative-platform.dylib"; return "libnative-platform.dylib";

View File

@@ -3,7 +3,10 @@ package net.rubygrapefruit.platform
import spock.lang.Specification import spock.lang.Specification
import org.junit.Rule import org.junit.Rule
import org.junit.rules.TemporaryFolder import org.junit.rules.TemporaryFolder
import spock.lang.IgnoreIf
import net.rubygrapefruit.platform.internal.Platform
@IgnoreIf({Platform.current().windows})
class PosixFileTest extends Specification { class PosixFileTest extends Specification {
@Rule TemporaryFolder tmpDir @Rule TemporaryFolder tmpDir
final PosixFile file = Native.get(PosixFile.class) final PosixFile file = Native.get(PosixFile.class)

View File

@@ -10,6 +10,6 @@ class ProcessTest extends Specification {
def "can get PID"() { def "can get PID"() {
expect: expect:
process.getPid() != 0 process.getProcessId() != 0
} }
} }

View File

@@ -14,7 +14,7 @@ class TerminalTest extends Specification {
!terminal.isTerminal(TerminalAccess.Output.Stderr); !terminal.isTerminal(TerminalAccess.Output.Stderr);
} }
def "cannot determine terminal size from a test"() { def "cannot access terminal from a test"() {
when: when:
terminal.getTerminal(TerminalAccess.Output.Stdout) terminal.getTerminal(TerminalAccess.Output.Stdout)