commit 02fd07ac3f082a3902298c7a131a6753ffb2e2c9 Author: Edouard DUPIN Date: Sun Jun 21 21:51:28 2015 +0200 [DEV] add a basic wrapper for jvm diff --git a/jvm-basics/debug.cpp b/jvm-basics/debug.cpp new file mode 100644 index 0000000..e899962 --- /dev/null +++ b/jvm-basics/debug.cpp @@ -0,0 +1,12 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2015, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ + +#include + +int32_t jvm_basics::getLogId() { + static int32_t g_val = etk::log::registerInstance("jvm-basics"); + return g_val; +} diff --git a/jvm-basics/debug.h b/jvm-basics/debug.h new file mode 100644 index 0000000..19d0d40 --- /dev/null +++ b/jvm-basics/debug.h @@ -0,0 +1,42 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2015, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ + +#ifndef __JVM_BASICS_DEBUG_H__ +#define __JVM_BASICS_DEBUG_H__ + +#include + +namespace jvm_basics { + int32_t getLogId(); +} +#define JVMB_BASE(info,data) TK_LOG_BASE(jvm_basics::getLogId(),info,data) + +#define JVMB_PRINT(data) JVMB_BASE(-1, data) +#define JVMB_CRITICAL(data) JVMB_BASE(1, data) +#define JVMB_ERROR(data) JVMB_BASE(2, data) +#define JVMB_WARNING(data) JVMB_BASE(3, data) +#ifdef DEBUG + #define JVMB_INFO(data) JVMB_BASE(4, data) + #define JVMB_DEBUG(data) JVMB_BASE(5, data) + #define JVMB_VERBOSE(data) JVMB_BASE(6, data) + #define JVMB_TODO(data) JVMB_BASE(4, "TODO : " << data) +#else + #define JVMB_INFO(data) do { } while(false) + #define JVMB_DEBUG(data) do { } while(false) + #define JVMB_VERBOSE(data) do { } while(false) + #define JVMB_TODO(data) do { } while(false) +#endif + +#define JVMB_ASSERT(cond,data) \ + do { \ + if (!(cond)) { \ + JVMB_CRITICAL(data); \ + assert(!#cond); \ + } \ + } while (0) + +#endif + diff --git a/jvm-basics/jvm-basics.cpp b/jvm-basics/jvm-basics.cpp new file mode 100644 index 0000000..ee18a4d --- /dev/null +++ b/jvm-basics/jvm-basics.cpp @@ -0,0 +1,49 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2015, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ + +#include +#include +#include +#include +#include + +// jni doc : /usr/lib/jvm/java-1.6.0-openjdk/include + +// Global JVM (can only have 1) +JavaVM*& jvm_basics::getJavaVM() { + static JavaVM* g_JavaVM=nullptr; // global acces on the unique JVM !!! + return g_JavaVM; +} +std::mutex& jvm_basics::getMutexJavaVM() { + static std::mutex g_jvmMutex; + return g_jvmMutex; +} + +void jvm_basics::checkExceptionJavaVM(JNIEnv* _env) { + if (_env->ExceptionOccurred()) { + JVMB_ERROR("C->java : EXEPTION ..."); + _env->ExceptionDescribe(); + _env->ExceptionClear(); + } +} + +extern "C" { + // JNI onLoad + JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* _jvm, void* _reserved) { + // get the java virtual machine handle ... + std::unique_lock lock(jvm_basics::getMutexJavaVM()); + jvm_basics::getJavaVM() = _jvm; + JVMB_INFO("JNI-> load the jvm ..." ); + return JNI_VERSION_1_6; + } + // JNI onUnLoad + JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* _vm, void *_reserved) { + std::unique_lock lock(jvm_basics::getMutexJavaVM()); + jvm_basics::getJavaVM() = nullptr; + JVMB_INFO("JNI-> Un-load the jvm ..." ); + } +} + diff --git a/jvm-basics/jvm-basics.h b/jvm-basics/jvm-basics.h new file mode 100644 index 0000000..96ad97f --- /dev/null +++ b/jvm-basics/jvm-basics.h @@ -0,0 +1,16 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2015, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ + +#ifndef __JVM_BASICS_H__ +#define __JVM_BASICS_H__ + +namespace jvm_basics { + JavaVM*& getJavaVM(); + std::mutex& getMutexJavaVM(); + void checkExceptionJavaVM(JNIEnv* _env); +} + +#endif \ No newline at end of file diff --git a/lutin_jvm-basics.py b/lutin_jvm-basics.py new file mode 100644 index 0000000..c4a7c39 --- /dev/null +++ b/lutin_jvm-basics.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +import lutin.module as module +import lutin.tools as tools + +def get_desc(): + return "jvm basis call for all element who have jvm access" + +def create(target): + # module name is 'edn' and type binary. + myModule = module.Module(__file__, 'jvm-basics', 'LIBRARY') + myModule.add_module_depend(['etk']) + # add extra compilation flags : + myModule.add_extra_compile_flags() + # add the file to compile: + myModule.add_src_file([ + 'jvm-basics/debug.cpp', + 'jvm-basics/jvm-basics.cpp' + ]) + myModule.compile_version_XX(2011) + myModule.add_export_path(tools.get_current_path(__file__)) + return myModule + +