Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add a default posix handler implementation with reasonable defaults, …

…make it convenient to use from POSIXFactory
  • Loading branch information...
commit 895e3790132bb2b5c92af13760245f55b9e36581 1 parent e545a79
@brianm brianm authored
View
2  .gitignore
@@ -1,3 +1,5 @@
+.idea
+*.iml
*.orig$
*.rej$
*.class$
View
13 src/main/java/jnr/posix/POSIXFactory.java
@@ -4,6 +4,8 @@
import jnr.ffi.LibraryOption;
import jnr.ffi.Struct;
import java.util.HashMap;
+
+import jnr.posix.util.DefaultPOSIXHandler;
import jnr.posix.util.Platform;
import java.util.Map;
@@ -20,6 +22,15 @@ public static POSIX getPOSIX(POSIXHandler handler, boolean useNativePOSIX) {
return new LazyPOSIX(handler, useNativePOSIX);
}
+ /**
+ * This will use {@link DefaultPOSIXHandler} and the native POSIX implementation
+ *
+ * @return a POSIX implementation
+ */
+ public static POSIX getPOSIX() {
+ return getPOSIX(new DefaultPOSIXHandler(), true);
+ }
+
static POSIX loadPOSIX(POSIXHandler handler, boolean useNativePOSIX) {
POSIX posix = null;
@@ -125,7 +136,7 @@ public final LibC getLibC() {
}
private static final class WindowsLibCProvider implements LibCProvider {
-
+
static final class SingletonHolder {
public static LibC libc = Library.loadLibrary(WindowsLibC.class, getOptions(), "msvcrt", "kernel32");
}
View
81 src/main/java/jnr/posix/util/DefaultPOSIXHandler.java
@@ -0,0 +1,81 @@
+package jnr.posix.util;
+
+import jnr.constants.platform.Errno;
+import jnr.posix.POSIXHandler;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.Arrays;
+import java.util.IllegalFormatException;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * A POSIXHandler with reasonable default behavior.
+ */
+public class DefaultPOSIXHandler implements POSIXHandler
+{
+ public void error(Errno error, String extraData)
+ {
+ throw new RuntimeException("native error " + error.description() + " " + extraData);
+ }
+
+ public void unimplementedError(String methodName)
+ {
+ throw new IllegalStateException(methodName + " is not implemented in jnr-posix");
+ }
+
+ public void warn(WARNING_ID id, String message, Object... data)
+ {
+ String msg;
+ try {
+ msg = String.format(message, data);
+ }
+ catch (IllegalFormatException e) {
+ msg = message + " " + Arrays.toString(data);
+ }
+ Logger.getLogger("jnr-posix").log(Level.WARNING, msg);
+ }
+
+ public boolean isVerbose()
+ {
+ return false;
+ }
+
+ public File getCurrentWorkingDirectory()
+ {
+ return new File(".");
+ }
+
+ public String[] getEnv()
+ {
+ String[] envp = new String[System.getenv().size()];
+ int i = 0;
+ for (Map.Entry<String, String> pair : System.getenv().entrySet()) {
+ envp[i++] = new StringBuilder(pair.getKey()).append("=").append(pair.getValue()).toString();
+ }
+ return envp;
+ }
+
+ public InputStream getInputStream()
+ {
+ return System.in;
+ }
+
+ public PrintStream getOutputStream()
+ {
+ return System.out;
+ }
+
+ public int getPID()
+ {
+ return 0;
+ }
+
+ public PrintStream getErrorStream()
+ {
+ return System.err;
+ }
+}
View
25 src/test/java/jnr/posix/util/DefaultPOSIXHandlerTest.java
@@ -0,0 +1,25 @@
+package jnr.posix.util;
+
+import jnr.posix.POSIX;
+import jnr.posix.POSIXFactory;
+import org.junit.Test;
+
+import java.lang.management.ManagementFactory;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+public class DefaultPOSIXHandlerTest
+{
+ @Test
+ public void testGetPid() throws Exception
+ {
+ int pid_from_jmx = Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
+
+ POSIX posix = POSIXFactory.getPOSIX(new DefaultPOSIXHandler(), true);
+ int pid = posix.getpid();
+ assertThat(pid, equalTo(pid_from_jmx));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.