Added SystemInfo and FileSystems to query system and file system information, respectively.

This commit is contained in:
Adam Murdoch
2012-08-12 06:17:58 +10:00
parent 5f3761dd41
commit 3a58e27eeb
18 changed files with 612 additions and 327 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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