From f750b0bdc58c315f8eb6c9677a5d8d4a68a68d66 Mon Sep 17 00:00:00 2001 From: Hao Hou Date: Wed, 5 Jul 2017 21:48:23 -0400 Subject: [PATCH 1/5] Start working on java support --- build/libplumber.cmake | 2 +- build/package.cmake | 1 + build/servlet.cmake | 1 + servlets/language/java/build.cmake | 31 +++++++++++++++++++ .../info/haohou/pservlet/Pservlet.java | 26 ++++++++++++++++ .../language/java/jni/pservlet_interface.c | 2 ++ servlets/language/java/servlet.c | 3 ++ 7 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 servlets/language/java/build.cmake create mode 100644 servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java create mode 100644 servlets/language/java/jni/pservlet_interface.c create mode 100644 servlets/language/java/servlet.c diff --git a/build/libplumber.cmake b/build/libplumber.cmake index 645a6435..a82fc4b9 100644 --- a/build/libplumber.cmake +++ b/build/libplumber.cmake @@ -4,7 +4,7 @@ file(GLOB_RECURSE src_files "${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_DIR}/*.c") if("${MODULE_TLS_ENABLED}" EQUAL "1") find_package(OpenSSL) - set(OPENSSL_INCLUDE_DIR "-I{OPENSSL_INCLUDE_DIR}") + set(OPENSSL_INCLUDE_DIR "-I${OPENSSL_INCLUDE_DIR}") else("${MODULE_TLS_ENABLED}" EQUAL "1") message("Notice: TLS support is disabled") set(OPENSSL_INCLUDE_DIR ) diff --git a/build/package.cmake b/build/package.cmake index 32ced760..b9e07947 100644 --- a/build/package.cmake +++ b/build/package.cmake @@ -10,6 +10,7 @@ macro(build_tool_dir dir) if(NOT "${build_${target}}" STREQUAL "no") if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${dir}/${target}") set(SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${dir}/${target}) + set(GEN_PATH ${CMAKE_CURRENT_BINARY_DIR}/${dir}/${target}) set(SOURCE "") set(LOCAL_CFLAGS ) set(LOCAL_LIBS ) diff --git a/build/servlet.cmake b/build/servlet.cmake index 61b81348..f763bcd8 100644 --- a/build/servlet.cmake +++ b/build/servlet.cmake @@ -6,6 +6,7 @@ foreach(servlet_cmake ${servlet_cmakes}) get_filename_component(servlet ${servlet_path} NAME) if(NOT "${build_${servlet}}" STREQUAL "no") set(SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${SERVLET_DIR}/${servlet_dir}/${servlet}) + set(GEN_PATH ${CMAKE_CURRENT_BINARY_DIR}/${SERVLET_DIR}/${servlet_dir}/${servlet}) set(SOURCE "") set(LOCAL_CFLAGS ) set(LOCAL_LIBS ) diff --git a/servlets/language/java/build.cmake b/servlets/language/java/build.cmake new file mode 100644 index 00000000..44a6a41e --- /dev/null +++ b/servlets/language/java/build.cmake @@ -0,0 +1,31 @@ +find_package(Java COMPONENTS Development Runtime REQUIRED) +get_filename_component(java_real_path ${Java_JAVAH_EXECUTABLE} REALPATH) +get_filename_component(java_bin_path ${java_real_path} DIRECTORY) +get_filename_component(JAVA_HOME ${java_bin_path}/../ ABSOLUTE) +find_package(JNI) +set(JNI_INCLUDE_FLAGS "") +foreach(incdir ${JNI_INCLUDE_DIRS}) + set(JNI_INCLUDE_FLAGS "${JNI_INCLUDE_FLAGS} -I${incdir}") +endforeach(incdir ${JNI_INCLUDE_DIRS}) + +include(UseJava) +set(CMAKE_JAVA_TARGET_OUTPUT_DIR ${GEN_PATH}) +file(GLOB_RECURSE java_source "${SOURCE_PATH}/javalib/*.java") +add_jar(java_pservlet ${java_source} + OUTPUT_NAME pservlet) +create_javah(TARGET java_pservlet_jni_header + GENERATED_FILES jni_headers + CLASSES info.haohou.pservlet.Pservlet + CLASSPATH ${GEN_PATH}/pservlet.jar + DEPENDS java_pservlet + OUTPUT_DIR ${GEN_PATH}) +file(GLOB_RECURSE jni_source "${SOURCE_PATH}/jni/*.c") +set_source_files_properties(${jni_source} PROPERTIES + COMPILE_FLAGS "${CFLAGS} ${JNI_INCLUDE_FLAGS} -I${GEN_PATH}") +add_library(java_pservlet_jni SHARED ${jni_source}) +target_link_libraries(java_pservlet_jni pservlet ${JNI_LIBERARIES}) +set(LOCAL_CFLAGS "-DSERVLET_JNI_PATH=\"${CMAKE_INSTALL_PREFIX}/lib/plumber/java\"") + +set(INSTALL yes) +install_includes("${GEN_PATH}" "lib/plumber/java" "*.jar") +install(TARGETS java_pservlet_jni DESTINATION lib/plumber/java) diff --git a/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java b/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java new file mode 100644 index 00000000..831dfceb --- /dev/null +++ b/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java @@ -0,0 +1,26 @@ +package info.haohou.pservlet; +import java.util.*; + +public class Pservlet { + public static native int pipeDefine(String name, int flags, String type_expr); + public static native Byte[] pipeRead(int pipe, int nbytes); + public static native int pipeWrite(int pipe, Byte[] data); + public static native int pipeWriteScopeToken(int pipe, int scope_token); + public static native int taskId(); + public static native void logWrite(int level, String message); + public static native boolean pipeEof(int pipe); + public static native String version(); + + private static native HashMap _getConst(); + + private static final HashMap _constants = _getConst(); + + public static final int PIPE_INPUT = _constants.get("PIPE_INPUT").intValue(); + + public static final int PIPE_OUTPUT = _constants.get("PIPE_OUTPUT").intValue(); + + public static final int PIPE_ASYNC = _constants.get("PIPE_ASYNC").intValue(); + + public static final int PIPE_SHADOW = _constants.get("PIPE_SHADOW").intValue(); + +}; diff --git a/servlets/language/java/jni/pservlet_interface.c b/servlets/language/java/jni/pservlet_interface.c new file mode 100644 index 00000000..e34ad212 --- /dev/null +++ b/servlets/language/java/jni/pservlet_interface.c @@ -0,0 +1,2 @@ +#include +#include diff --git a/servlets/language/java/servlet.c b/servlets/language/java/servlet.c new file mode 100644 index 00000000..69fc9a42 --- /dev/null +++ b/servlets/language/java/servlet.c @@ -0,0 +1,3 @@ +#include + + From 58f615f067e418ff28e3ffd514e9d332b18951ea Mon Sep 17 00:00:00 2001 From: Hao Hou Date: Wed, 5 Jul 2017 21:50:26 -0400 Subject: [PATCH 2/5] Start working on Java servlet --- servlets/language/java/servlet.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/servlets/language/java/servlet.c b/servlets/language/java/servlet.c index 69fc9a42..092e8eb0 100644 --- a/servlets/language/java/servlet.c +++ b/servlets/language/java/servlet.c @@ -1,3 +1,8 @@ #include +//useful links: +// Call C function from Java: http://web.archive.org/web/20120419230023/http://java.sun.com/docs/books/jni/html/start.html +// Call Java from C: http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/invocation.html +// AttachCurrentThread + From 7932aa1d04fb70c391e4481634ed8bc9c8603117 Mon Sep 17 00:00:00 2001 From: Hao Hou Date: Wed, 5 Jul 2017 21:51:12 -0400 Subject: [PATCH 3/5] update the stat script --- misc/stat | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/misc/stat b/misc/stat index 3ca1fa8b..751e25c1 100755 --- a/misc/stat +++ b/misc/stat @@ -1,5 +1,5 @@ #!/bin/zsh -for ext in {'[ch]pp','[ch]',py,js,cmake,vim,pss,ptype} +for ext in {'[ch]pp','[ch]',py,js,cmake,vim,pss,ptype,java} do if [ ! -z "$(git ls-files '*.'${ext})" ] then @@ -16,7 +16,8 @@ awk \ name["cmake"] = "CMake" name["vim"] = "Vim" name["pss"] = "PScript" - name["ptype"] = "TypeDef" + name["ptype"] = "TypeDef" + name["java"] = "Java" } { sum += $2; From 756b151caf87dcdd00b90b00bf83c6b2969a58f5 Mon Sep 17 00:00:00 2001 From: Hao Hou Date: Thu, 6 Jul 2017 10:30:54 -0400 Subject: [PATCH 4/5] Add some classes for the pservlet java binding --- servlets/language/java/build.cmake | 2 +- .../haohou/pservlet/FrameworkException.java | 41 +++++++ .../javalib/info/haohou/pservlet/PipeId.java | 36 ++++++ .../info/haohou/pservlet/Pservlet.java | 111 +++++++++++++++++- 4 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 servlets/language/java/javalib/info/haohou/pservlet/FrameworkException.java create mode 100644 servlets/language/java/javalib/info/haohou/pservlet/PipeId.java diff --git a/servlets/language/java/build.cmake b/servlets/language/java/build.cmake index 44a6a41e..d1f9426d 100644 --- a/servlets/language/java/build.cmake +++ b/servlets/language/java/build.cmake @@ -15,7 +15,7 @@ add_jar(java_pservlet ${java_source} OUTPUT_NAME pservlet) create_javah(TARGET java_pservlet_jni_header GENERATED_FILES jni_headers - CLASSES info.haohou.pservlet.Pservlet + CLASSES info.haohou.pservlet._Pservlet CLASSPATH ${GEN_PATH}/pservlet.jar DEPENDS java_pservlet OUTPUT_DIR ${GEN_PATH}) diff --git a/servlets/language/java/javalib/info/haohou/pservlet/FrameworkException.java b/servlets/language/java/javalib/info/haohou/pservlet/FrameworkException.java new file mode 100644 index 00000000..5b7f2f32 --- /dev/null +++ b/servlets/language/java/javalib/info/haohou/pservlet/FrameworkException.java @@ -0,0 +1,41 @@ +/** + * Copyright (C) 2017, Hao Hou + **/ +package info.haohou.pservlet; + +/** + * The general java exception + **/ +public class FrameworkException extends Exception { + + /** + * The reason code type + **/ + public enum Reason { + PIPE_DEFINE + }; + + /** + * The reason code for current exceptin + **/ + Reason _reason; + + /** + * Constructor + * @param message The message in the exception + **/ + public FrameworkException(String message, Reason reason) + { + super("Plumber Framework Exception: " + message); + _reason = reason; + } + + /** + * What causes this exception? + * @return The reason code + **/ + public Reason getReason() + { + return _reason; + } +} diff --git a/servlets/language/java/javalib/info/haohou/pservlet/PipeId.java b/servlets/language/java/javalib/info/haohou/pservlet/PipeId.java new file mode 100644 index 00000000..fb57c129 --- /dev/null +++ b/servlets/language/java/javalib/info/haohou/pservlet/PipeId.java @@ -0,0 +1,36 @@ +/** + * Copyright (C) 2017, Hao Hou + **/ +package info.haohou.pservlet; + +/** + * The object wrapper for a pipe id + **/ +public class PipeId { + + /** + * The internal pipe Id + **/ + private final int _id; + + /** + * Constructor + **/ + private PipeId(int id) { + _id = id; + } + + /** + * Get the pipe id object from int + **/ + public static PipeId fromId(int id) { + return new PipeId(id); + } + + /** + * Get the pipe Id number + **/ + public int id() { + return _id; + } +} diff --git a/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java b/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java index 831dfceb..c40fc684 100644 --- a/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java +++ b/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java @@ -1,26 +1,133 @@ +/** + * Copyright (C) 2017, Hao Hou + **/ + package info.haohou.pservlet; import java.util.*; +/** + * The actual Pservlet API wrapper + **/ public class Pservlet { + /** + * Define a pipe + * @param name The name of the pipe + * @param flags The flags of the pipe + * @param type_expr The type expression + * @return status code + **/ + public static PipeId pipeDefine(String name, int flags, String type_expr) + throws FrameworkException, IllegalArgumentException + { + if(null == name || null == type_expr) + { + throw new IllegalArgumentException(); + } + + int result = _Pservlet.pipeDefine(name, flags, type_expr); + + if(result == -1) + { + throw new FrameworkException("Cannot create pipe", FrameworkException.Reason.PIPE_DEFINE); + } + + return PipeId.fromId(result); + } +} + +/** + * The internal language binding for java + **/ +class _Pservlet { + /** + * Define a pipe + * @param name The name of the pipe + * @param flags The flags to the pipe + * @param type_expr The type expression + * @return The pipe id + **/ public static native int pipeDefine(String name, int flags, String type_expr); + + /** + * Read data from the pipe + * @param pipe The pipe id + * @param nbytes How many bytes we want to read from the pipe + * @return The result byte array + **/ public static native Byte[] pipeRead(int pipe, int nbytes); + + /** + * Write data to the pipe + * @param pipe The pipe id + * @param data The byte array to write + * @return The number of bytes has been written to pipe + **/ public static native int pipeWrite(int pipe, Byte[] data); - public static native int pipeWriteScopeToken(int pipe, int scope_token); + + /** + * Write a scope token + * @param pipe The pipe id + * @param scope_token The token needs to be written to pipe + * @return If the operation is sucessfully done + **/ + public static native boolean pipeWriteScopeToken(int pipe, int scope_token); + + /** + * Get current Task id + * @return The current task Id + **/ public static native int taskId(); + + /** + * Write log to the Plumber logging infrastructure + * @param level The level of the log + * @param message The message + * @return nothing + **/ public static native void logWrite(int level, String message); - public static native boolean pipeEof(int pipe); + + /** + * Check if we are currently at the end of the pipe + * @param pipe The pipe id + * @return The check result or error code + **/ + public static native int pipeEof(int pipe); + + /** + * Get the version number of the plumber environment + * @return The veresion string + **/ public static native String version(); + /** + * Get the constants defined by the Plumber framework + * @return A map from string to the constants + **/ private static native HashMap _getConst(); + /** + * The object that holds the collection of Plumber framework constants + **/ private static final HashMap _constants = _getConst(); + /** + * Indicates this is an input pipe + **/ public static final int PIPE_INPUT = _constants.get("PIPE_INPUT").intValue(); + /** + * Indicates this is an output pipe + **/ public static final int PIPE_OUTPUT = _constants.get("PIPE_OUTPUT").intValue(); + /** + * Indicates this is an asynchronized pipe + **/ public static final int PIPE_ASYNC = _constants.get("PIPE_ASYNC").intValue(); + /** + * Indicates this is a shadow pipe + **/ public static final int PIPE_SHADOW = _constants.get("PIPE_SHADOW").intValue(); }; From 520e4f9986c4dd31bd1e9aad9825aea8f7806fdd Mon Sep 17 00:00:00 2001 From: Hao Hou Date: Sat, 29 Jul 2017 15:43:40 -0600 Subject: [PATCH 5/5] Make the java servlet code compiles --- .../language/java/javalib/info/haohou/pservlet/Pservlet.java | 4 ++++ servlets/language/java/jni/pservlet_interface.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java b/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java index c40fc684..762123b5 100644 --- a/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java +++ b/servlets/language/java/javalib/info/haohou/pservlet/Pservlet.java @@ -130,4 +130,8 @@ class _Pservlet { **/ public static final int PIPE_SHADOW = _constants.get("PIPE_SHADOW").intValue(); + + static { + System.loadLibrary("java_pservlet_jni"); + } }; diff --git a/servlets/language/java/jni/pservlet_interface.c b/servlets/language/java/jni/pservlet_interface.c index e34ad212..3721594d 100644 --- a/servlets/language/java/jni/pservlet_interface.c +++ b/servlets/language/java/jni/pservlet_interface.c @@ -1,2 +1,2 @@ #include -#include +#include