[DEV] add a basic wrapper for jvm

This commit is contained in:
Edouard DUPIN 2015-06-21 21:51:28 +02:00
commit 02fd07ac3f
5 changed files with 142 additions and 0 deletions

12
jvm-basics/debug.cpp Normal file
View File

@ -0,0 +1,12 @@
/** @file
* @author Edouard DUPIN
* @copyright 2015, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <jvm-basics/debug.h>
int32_t jvm_basics::getLogId() {
static int32_t g_val = etk::log::registerInstance("jvm-basics");
return g_val;
}

42
jvm-basics/debug.h Normal file
View File

@ -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 <etk/log.h>
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

49
jvm-basics/jvm-basics.cpp Normal file
View File

@ -0,0 +1,49 @@
/** @file
* @author Edouard DUPIN
* @copyright 2015, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <jni.h>
#include <pthread.h>
#include <mutex>
#include <jvm-basics/debug.h>
#include <jvm-basics/jvm-basics.h>
// 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<std::mutex> 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<std::mutex> lock(jvm_basics::getMutexJavaVM());
jvm_basics::getJavaVM() = nullptr;
JVMB_INFO("JNI-> Un-load the jvm ..." );
}
}

16
jvm-basics/jvm-basics.h Normal file
View File

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

23
lutin_jvm-basics.py Normal file
View File

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