diff --git a/java/org/servalproject/servald/AbstractJniResults.java b/java/org/servalproject/servald/AbstractJniResults.java new file mode 100644 index 00000000..cfe10159 --- /dev/null +++ b/java/org/servalproject/servald/AbstractJniResults.java @@ -0,0 +1,37 @@ +package org.servalproject.servald; + +public abstract class AbstractJniResults implements IJniResults { + + @Override + public void startResultSet(int columns) { + putBlob(Integer.toString(columns).getBytes()); + } + + @Override + public void setColumnName(int i, String name) { + putBlob(name.getBytes()); + } + + @Override + public void putString(String value) { + putBlob((value != null) ? value.getBytes() : null); + } + + @Override + public abstract void putBlob(byte[] value); + + @Override + public void putLong(long value) { + putBlob(Long.toString(value).getBytes()); + } + + @Override + public void putDouble(double value) { + putBlob(Double.toString(value).getBytes()); + } + + @Override + public void totalRowCount(int rows) { + + } +} diff --git a/java/org/servalproject/servald/IJniResults.java b/java/org/servalproject/servald/IJniResults.java new file mode 100644 index 00000000..21b66fab --- /dev/null +++ b/java/org/servalproject/servald/IJniResults.java @@ -0,0 +1,11 @@ +package org.servalproject.servald; + +public interface IJniResults { + public void startResultSet(int columns); + public void setColumnName(int column, String name); + public void putString(String value); + public void putBlob(byte[] value); + public void putLong(long value); + public void putDouble(double value); + public void totalRowCount(int rows); +} \ No newline at end of file diff --git a/java/org/servalproject/servald/JniResultsList.java b/java/org/servalproject/servald/JniResultsList.java new file mode 100644 index 00000000..ba934c76 --- /dev/null +++ b/java/org/servalproject/servald/JniResultsList.java @@ -0,0 +1,17 @@ +package org.servalproject.servald; + +import java.util.List; + +public class JniResultsList extends AbstractJniResults implements IJniResults { + final List list; + private byte[] empty = new byte[0]; + JniResultsList(List list) { + this.list = list; + } + + @Override + public void putBlob(byte[] value) { + list.add(value == null ? empty : value); + } + +} diff --git a/java/org/servalproject/servald/ServalD.java b/java/org/servalproject/servald/ServalD.java index 70f5484c..6e77c47c 100644 --- a/java/org/servalproject/servald/ServalD.java +++ b/java/org/servalproject/servald/ServalD.java @@ -28,17 +28,20 @@ class ServalD int status; List outv; - public ServalD() + static { + String property = System.getProperty("java.library.path"); + System.err.println("Attempting to load libservald.so from "+property); System.loadLibrary("servald"); } - public native int rawCommand(List outv, String... args); + private static native int rawCommand(IJniResults outv, String[] args); public void command(String... args) { this.outv = new LinkedList(); - this.status = this.rawCommand(this.outv, args); + IJniResults results = new JniResultsList(outv); + this.status = this.rawCommand(results, args); } public static void main(String[] args) diff --git a/java/org/servalproject/servald/ServalDTests.java b/java/org/servalproject/servald/ServalDTests.java index 9574cc32..f6ac6f4b 100644 --- a/java/org/servalproject/servald/ServalDTests.java +++ b/java/org/servalproject/servald/ServalDTests.java @@ -11,8 +11,7 @@ class ServalDTests public static void main(String[] args) { try { - Class cls = new Object() { }.getClass().getEnclosingClass(); - Method m = cls.getMethod(args[0], String[].class); + Method m = ServalDTests.class.getMethod(args[0], String[].class); m.invoke(null, (Object) Arrays.copyOfRange(args, 1, args.length)); } catch (Exception e) { diff --git a/tests/jni b/tests/jni index f23309db..96e824c3 100755 --- a/tests/jni +++ b/tests/jni @@ -35,6 +35,7 @@ compile_java_classes() { mkdir classes assert $JAVAC -Xlint:unchecked -d classes "$servald_source_root"/java/org/servalproject/servald/*.java assert [ -r classes/org/servalproject/servald/ServalD.class ] + assert [ -r classes/org/servalproject/servald/IJniResults.class ] assert [ -r classes/org/servalproject/servald/ServalDTests.class ] } @@ -46,7 +47,7 @@ assert_echo_works() { doc_Echo="Serval JNI echo Hello world" test_Echo() { - executeOk java -classpath "$PWD/classes" org.servalproject.servald.ServalD echo -e 'Hello,\ttab' 'world\0!' + executeOk java "-Djava.library.path=$LD_LIBRARY_PATH" -classpath "$PWD/classes" org.servalproject.servald.ServalD echo -e 'Hello,\ttab' 'world\0!' assertStdoutIs -e 'Hello,\ttab\nworld\0!\n' } @@ -63,7 +64,7 @@ test_Delim() { doc_Repeat="Serval JNI repeated calls in same process" test_Repeat() { - executeOk java -classpath "$PWD/classes" org.servalproject.servald.ServalDTests repeat 50 echo 'Hello,' 'world!' + executeOk java "-Djava.library.path=$LD_LIBRARY_PATH" -classpath "$PWD/classes" org.servalproject.servald.ServalDTests repeat 50 echo 'Hello,' 'world!' assertStdoutLineCount '==' 50 assertStdoutGrep --matches=50 '^0:Hello,:world!$' }