Added SystemInfo and FileSystems to query system and file system information, respectively.
This commit is contained in:
13
src/main/java/net/rubygrapefruit/platform/FileSystem.java
Normal file
13
src/main/java/net/rubygrapefruit/platform/FileSystem.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package net.rubygrapefruit.platform;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public interface FileSystem {
|
||||
File getMountPoint();
|
||||
|
||||
String getFileSystemType();
|
||||
|
||||
boolean isRemote();
|
||||
|
||||
String getDeviceName();
|
||||
}
|
||||
12
src/main/java/net/rubygrapefruit/platform/FileSystems.java
Normal file
12
src/main/java/net/rubygrapefruit/platform/FileSystems.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package net.rubygrapefruit.platform;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface FileSystems extends NativeIntegration {
|
||||
/**
|
||||
* Returns the set of all file systems for the current machine.
|
||||
*
|
||||
* @throws NativeException On failure.
|
||||
*/
|
||||
List<FileSystem> getFileSystems() throws NativeException;
|
||||
}
|
||||
@@ -6,9 +6,18 @@ public class Main {
|
||||
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch"));
|
||||
System.out.println("* JVM: " + System.getProperty("java.vm.vendor") + ' ' + System.getProperty("java.version"));
|
||||
|
||||
SystemInfo systemInfo = Native.get(SystemInfo.class);
|
||||
System.out.println("* Kernel: " + systemInfo.getKernelName() + ' ' + systemInfo.getKernelVersion() + ' ' + systemInfo.getMachineArchitecture());
|
||||
|
||||
Process process = Native.get(Process.class);
|
||||
System.out.println("* PID: " + process.getProcessId());
|
||||
|
||||
FileSystems fileSystems = Native.get(FileSystems.class);
|
||||
System.out.println("* File systems: ");
|
||||
for (FileSystem fileSystem : fileSystems.getFileSystems()) {
|
||||
System.out.println(" * " + fileSystem.getMountPoint() + ' ' + fileSystem.getFileSystemType() + ' ' + fileSystem.getDeviceName() + (fileSystem.isRemote() ? " remote" : " local"));
|
||||
}
|
||||
|
||||
TerminalAccess terminalAccess = Native.get(TerminalAccess.class);
|
||||
boolean stdoutIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stdout);
|
||||
boolean stderrIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stderr);
|
||||
|
||||
@@ -54,7 +54,6 @@ public class Native {
|
||||
loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static <T extends NativeIntegration> T get(Class<T> type) {
|
||||
@@ -70,6 +69,17 @@ public class Native {
|
||||
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());
|
||||
@@ -78,6 +88,11 @@ public class Native {
|
||||
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.",
|
||||
type.getName()));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package net.rubygrapefruit.platform;
|
||||
|
||||
public interface SystemInfo extends NativeIntegration {
|
||||
String getKernelName();
|
||||
|
||||
String getKernelVersion();
|
||||
|
||||
String getMachineArchitecture();
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
import net.rubygrapefruit.platform.FileSystem;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class DefaultFileSystem implements FileSystem {
|
||||
private final File mountPoint;
|
||||
private final String fileSystemType;
|
||||
private final String deviceName;
|
||||
private final boolean remote;
|
||||
|
||||
public DefaultFileSystem(File mountPoint, String fileSystemType, String deviceName, boolean remote) {
|
||||
this.mountPoint = mountPoint;
|
||||
this.fileSystemType = fileSystemType;
|
||||
this.deviceName = deviceName;
|
||||
this.remote = remote;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDeviceName() {
|
||||
return deviceName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getMountPoint() {
|
||||
return mountPoint;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileSystemType() {
|
||||
return fileSystemType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRemote() {
|
||||
return remote;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
import net.rubygrapefruit.platform.FileSystem;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FileSystemList {
|
||||
public final List<FileSystem> fileSystems = new ArrayList<FileSystem>();
|
||||
|
||||
public void add(String mountPoint, String fileSystemName, String deviceName, boolean remote) {
|
||||
fileSystems.add(new DefaultFileSystem(new File(mountPoint), fileSystemName, deviceName, remote));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
import net.rubygrapefruit.platform.SystemInfo;
|
||||
|
||||
public class MutableSystemInfo implements SystemInfo {
|
||||
public String osName;
|
||||
public String osVersion;
|
||||
public String characterEncoding;
|
||||
public String machineArchitecture;
|
||||
|
||||
@Override
|
||||
public String getKernelName() {
|
||||
return osName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKernelVersion() {
|
||||
return osVersion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMachineArchitecture() {
|
||||
return machineArchitecture;
|
||||
}
|
||||
}
|
||||
@@ -1,89 +1,98 @@
|
||||
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 if (osName.contains("sunos")) {
|
||||
platform = new Solaris();
|
||||
} 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 Unix extends Posix {
|
||||
@Override
|
||||
public String getLibraryName() {
|
||||
return "libnative-platform.so";
|
||||
}
|
||||
}
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
private static class Linux extends Unix {
|
||||
}
|
||||
|
||||
private static class Solaris extends Unix {
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
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 if (osName.contains("sunos")) {
|
||||
platform = new Solaris();
|
||||
} else {
|
||||
platform = new Unsupported();
|
||||
}
|
||||
}
|
||||
return platform;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isPosix() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isWindows() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isOsX() {
|
||||
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 Unix extends Posix {
|
||||
@Override
|
||||
public String getLibraryName() {
|
||||
return "libnative-platform.so";
|
||||
}
|
||||
}
|
||||
|
||||
private static class Linux extends Unix {
|
||||
}
|
||||
|
||||
private static class Solaris extends Unix {
|
||||
}
|
||||
|
||||
private static class OsX extends Posix {
|
||||
@Override
|
||||
public boolean isOsX() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
import net.rubygrapefruit.platform.FileSystem;
|
||||
import net.rubygrapefruit.platform.FileSystems;
|
||||
import net.rubygrapefruit.platform.NativeException;
|
||||
import net.rubygrapefruit.platform.internal.jni.PosixFileSystemFunctions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PosixFileSystems implements FileSystems {
|
||||
@Override
|
||||
public List<FileSystem> getFileSystems() {
|
||||
FunctionResult result = new FunctionResult();
|
||||
FileSystemList fileSystems = new FileSystemList();
|
||||
PosixFileSystemFunctions.listFileSystems(fileSystems, result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not query file systems: %s", result.getMessage()));
|
||||
}
|
||||
return fileSystems.fileSystems;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
package net.rubygrapefruit.platform.internal.jni;
|
||||
|
||||
import net.rubygrapefruit.platform.internal.FunctionResult;
|
||||
import net.rubygrapefruit.platform.internal.MutableSystemInfo;
|
||||
|
||||
public class NativeLibraryFunctions {
|
||||
public static final int VERSION = 4;
|
||||
public static final int VERSION = 5;
|
||||
|
||||
public static native int getVersion();
|
||||
|
||||
public static native void getSystemInfo(MutableSystemInfo systemInfo, FunctionResult result);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package net.rubygrapefruit.platform.internal.jni;
|
||||
|
||||
import net.rubygrapefruit.platform.internal.FileSystemList;
|
||||
import net.rubygrapefruit.platform.internal.FunctionResult;
|
||||
|
||||
public class PosixFileSystemFunctions {
|
||||
public static native void listFileSystems(FileSystemList fileSystems, FunctionResult result);
|
||||
}
|
||||
Reference in New Issue
Block a user