First pass for windows support.
This commit is contained in:
82
src/main/java/net/rubygrapefruit/platform/Main.java
Normal file → Executable file
82
src/main/java/net/rubygrapefruit/platform/Main.java
Normal file → Executable file
@@ -1,41 +1,41 @@
|
||||
package net.rubygrapefruit.platform;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println();
|
||||
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch"));
|
||||
|
||||
Process process = Native.get(Process.class);
|
||||
System.out.println("* PID: " + process.getPid());
|
||||
|
||||
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"));
|
||||
System.out.println("* stderr: " + (stderrIsTerminal ? "terminal" : "not a terminal"));
|
||||
if (stdoutIsTerminal) {
|
||||
Terminal terminal = terminalAccess.getTerminal(TerminalAccess.Output.Stdout);
|
||||
TerminalSize terminalSize = terminal.getTerminalSize();
|
||||
System.out.println("* terminal size: " + terminalSize.getCols() + " cols x " + terminalSize.getRows() + " rows");
|
||||
System.out.println();
|
||||
System.out.println("TERMINAL OUTPUT");
|
||||
System.out.print("[normal] ");
|
||||
terminal.bold();
|
||||
System.out.print("[bold]");
|
||||
terminal.normal();
|
||||
System.out.println(" [normal]");
|
||||
|
||||
System.out.println("here are the colors:");
|
||||
for (Terminal.Color color : Terminal.Color.values()) {
|
||||
terminal.foreground(color);
|
||||
System.out.print(String.format("[%s] ", color.toString().toLowerCase()));
|
||||
terminal.bold();
|
||||
System.out.print(String.format("[%s]", color.toString().toLowerCase()));
|
||||
terminal.normal();
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
package net.rubygrapefruit.platform;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println();
|
||||
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch"));
|
||||
|
||||
Process process = Native.get(Process.class);
|
||||
System.out.println("* PID: " + process.getProcessId());
|
||||
|
||||
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"));
|
||||
System.out.println("* stderr: " + (stderrIsTerminal ? "terminal" : "not a terminal"));
|
||||
if (stdoutIsTerminal) {
|
||||
Terminal terminal = terminalAccess.getTerminal(TerminalAccess.Output.Stdout);
|
||||
TerminalSize terminalSize = terminal.getTerminalSize();
|
||||
System.out.println("* terminal size: " + terminalSize.getCols() + " cols x " + terminalSize.getRows() + " rows");
|
||||
System.out.println();
|
||||
System.out.println("TERMINAL OUTPUT");
|
||||
System.out.print("[normal] ");
|
||||
terminal.bold();
|
||||
System.out.print("[bold]");
|
||||
terminal.normal();
|
||||
System.out.println(" [normal]");
|
||||
|
||||
System.out.println("here are the colors:");
|
||||
for (Terminal.Color color : Terminal.Color.values()) {
|
||||
terminal.foreground(color);
|
||||
System.out.print(String.format("[%s] ", color.toString().toLowerCase()));
|
||||
terminal.bold();
|
||||
System.out.print(String.format("[%s]", color.toString().toLowerCase()));
|
||||
terminal.normal();
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
383
src/main/java/net/rubygrapefruit/platform/Native.java
Normal file → Executable file
383
src/main/java/net/rubygrapefruit/platform/Native.java
Normal file → Executable file
@@ -1,181 +1,202 @@
|
||||
package net.rubygrapefruit.platform;
|
||||
|
||||
import net.rubygrapefruit.platform.internal.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration.
|
||||
*/
|
||||
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) {
|
||||
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/" + 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));
|
||||
}
|
||||
loaded = 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 DefaultTerminalAccess());
|
||||
}
|
||||
throw new UnsupportedOperationException(String.format("Cannot load unknown native integration %s.",
|
||||
type.getName()));
|
||||
}
|
||||
|
||||
private static class DefaultPosixFile implements PosixFile {
|
||||
@Override
|
||||
public void setMode(File file, int perms) {
|
||||
FunctionResult result = new FunctionResult();
|
||||
PosixFileFunctions.chmod(file.getPath(), perms, result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not set UNIX mode on %s: %s", file, result.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMode(File file) {
|
||||
FunctionResult result = new FunctionResult();
|
||||
FileStat stat = new FileStat();
|
||||
PosixFileFunctions.stat(file.getPath(), stat, result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not get UNIX mode on %s: %s", file, result.getMessage()));
|
||||
}
|
||||
return stat.mode;
|
||||
}
|
||||
}
|
||||
|
||||
private static class DefaultProcess implements Process {
|
||||
@Override
|
||||
public int getPid() throws NativeException {
|
||||
return PosixProcessFunctions.getPid();
|
||||
}
|
||||
}
|
||||
|
||||
private static class DefaultTerminalAccess implements TerminalAccess {
|
||||
private static Output currentlyOpen;
|
||||
|
||||
@Override
|
||||
public boolean isTerminal(Output output) {
|
||||
return PosixTerminalFunctions.isatty(output.ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal getTerminal(Output output) {
|
||||
if (currentlyOpen != null) {
|
||||
throw new UnsupportedOperationException("Currently only one output can be used as a terminal.");
|
||||
}
|
||||
|
||||
DefaultTerminal terminal = new DefaultTerminal(output);
|
||||
terminal.init();
|
||||
|
||||
currentlyOpen = output;
|
||||
return terminal;
|
||||
}
|
||||
}
|
||||
|
||||
private static class DefaultTerminal implements Terminal {
|
||||
private final TerminalAccess.Output output;
|
||||
private final PrintStream stream;
|
||||
private Color foreground;
|
||||
|
||||
public DefaultTerminal(TerminalAccess.Output output) {
|
||||
this.output = output;
|
||||
stream = output == TerminalAccess.Output.Stdout ? System.out : System.err;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.initTerminal(output.ordinal(), result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not open terminal: %s", result.getMessage()));
|
||||
}
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(){
|
||||
@Override
|
||||
public void run() {
|
||||
reset();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminalSize getTerminalSize() {
|
||||
MutableTerminalSize terminalSize = new MutableTerminalSize();
|
||||
FunctionResult result = new FunctionResult();
|
||||
PosixTerminalFunctions.getTerminalSize(output.ordinal(), terminalSize, result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not get terminal size: %s", result.getMessage()));
|
||||
}
|
||||
return terminalSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal foreground(Color color) {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.foreground(color.ordinal(), result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not switch foreground color: %s", result.getMessage()));
|
||||
}
|
||||
foreground = color;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal bold() {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.bold(result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not switch to bold mode: %s", result.getMessage()));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal normal() {
|
||||
reset();
|
||||
if (foreground != null) {
|
||||
foreground(foreground);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal reset() {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.reset(result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not reset terminal: %s", result.getMessage()));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
package net.rubygrapefruit.platform;
|
||||
|
||||
import net.rubygrapefruit.platform.internal.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration.
|
||||
*/
|
||||
public class Native {
|
||||
private static final Object lock = new Object();
|
||||
private static boolean loaded;
|
||||
|
||||
static <T extends NativeIntegration> T get(Class<T> type) {
|
||||
Platform platform = Platform.current();
|
||||
synchronized (lock) {
|
||||
if (!loaded) {
|
||||
if (!platform.isSupported()) {
|
||||
throw new NativeException(String.format("The current platform is not supported."));
|
||||
}
|
||||
try {
|
||||
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));
|
||||
}
|
||||
loaded = true;
|
||||
}
|
||||
}
|
||||
if (platform.isPosix()) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
throw new UnsupportedOperationException(String.format("Cannot load unsupported native integration %s.",
|
||||
type.getName()));
|
||||
}
|
||||
|
||||
private static class DefaultPosixFile implements PosixFile {
|
||||
@Override
|
||||
public void setMode(File file, int perms) {
|
||||
FunctionResult result = new FunctionResult();
|
||||
PosixFileFunctions.chmod(file.getPath(), perms, result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not set UNIX mode on %s: %s", file, result.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMode(File file) {
|
||||
FunctionResult result = new FunctionResult();
|
||||
FileStat stat = new FileStat();
|
||||
PosixFileFunctions.stat(file.getPath(), stat, result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not get UNIX mode on %s: %s", file, result.getMessage()));
|
||||
}
|
||||
return stat.mode;
|
||||
}
|
||||
}
|
||||
|
||||
private static class DefaultProcess implements Process {
|
||||
@Override
|
||||
public int getProcessId() throws NativeException {
|
||||
return PosixProcessFunctions.getPid();
|
||||
}
|
||||
}
|
||||
|
||||
private static class TerminfoTerminalAccess implements TerminalAccess {
|
||||
private static Output currentlyOpen;
|
||||
|
||||
@Override
|
||||
public boolean isTerminal(Output output) {
|
||||
return PosixTerminalFunctions.isatty(output.ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal getTerminal(Output output) {
|
||||
if (currentlyOpen != null) {
|
||||
throw new UnsupportedOperationException("Currently only one output can be used as a terminal.");
|
||||
}
|
||||
|
||||
DefaultTerminal terminal = new DefaultTerminal(output);
|
||||
terminal.init();
|
||||
|
||||
currentlyOpen = output;
|
||||
return terminal;
|
||||
}
|
||||
}
|
||||
|
||||
private static class DefaultTerminal implements Terminal {
|
||||
private final TerminalAccess.Output output;
|
||||
private final PrintStream stream;
|
||||
private Color foreground;
|
||||
|
||||
public DefaultTerminal(TerminalAccess.Output output) {
|
||||
this.output = output;
|
||||
stream = output == TerminalAccess.Output.Stdout ? System.out : System.err;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.initTerminal(output.ordinal(), result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not open terminal: %s", result.getMessage()));
|
||||
}
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(){
|
||||
@Override
|
||||
public void run() {
|
||||
reset();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminalSize getTerminalSize() {
|
||||
MutableTerminalSize terminalSize = new MutableTerminalSize();
|
||||
FunctionResult result = new FunctionResult();
|
||||
PosixTerminalFunctions.getTerminalSize(output.ordinal(), terminalSize, result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not get terminal size: %s", result.getMessage()));
|
||||
}
|
||||
return terminalSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal foreground(Color color) {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.foreground(color.ordinal(), result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not switch foreground color: %s", result.getMessage()));
|
||||
}
|
||||
foreground = color;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal bold() {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.bold(result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not switch to bold mode: %s", result.getMessage()));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal normal() {
|
||||
reset();
|
||||
if (foreground != null) {
|
||||
foreground(foreground);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal reset() {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.reset(result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not reset terminal: %s", result.getMessage()));
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
16
src/main/java/net/rubygrapefruit/platform/Process.java
Normal file → Executable file
16
src/main/java/net/rubygrapefruit/platform/Process.java
Normal file → Executable file
@@ -1,8 +1,8 @@
|
||||
package net.rubygrapefruit.platform;
|
||||
|
||||
/**
|
||||
* Functions to query and modify a process' meta-data
|
||||
*/
|
||||
public interface Process extends NativeIntegration {
|
||||
int getPid() throws NativeException;
|
||||
}
|
||||
package net.rubygrapefruit.platform;
|
||||
|
||||
/**
|
||||
* Functions to query and modify a process' meta-data
|
||||
*/
|
||||
public interface Process extends NativeIntegration {
|
||||
int getProcessId() throws NativeException;
|
||||
}
|
||||
|
||||
142
src/main/java/net/rubygrapefruit/platform/internal/Platform.java
Normal file → Executable file
142
src/main/java/net/rubygrapefruit/platform/internal/Platform.java
Normal file → Executable file
@@ -1,61 +1,81 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
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 boolean isPosix() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isWindows() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public abstract String getLibraryName();
|
||||
|
||||
private static class Windows extends Platform {
|
||||
@Override
|
||||
public boolean isWindows() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLibraryName() {
|
||||
return "native-platform.dll";
|
||||
}
|
||||
}
|
||||
|
||||
private static abstract class Posix extends Platform {
|
||||
@Override
|
||||
public boolean isPosix() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Linux extends Posix {
|
||||
@Override
|
||||
public String getLibraryName() {
|
||||
return "libnative-platform.so";
|
||||
}
|
||||
}
|
||||
|
||||
private static class OsX extends Posix {
|
||||
@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