Browse Source

...

JNI-44
Volker Birk 7 years ago
parent
commit
06dae8b02e
2 changed files with 162 additions and 0 deletions
  1. +27
    -0
      src/Makefile
  2. +135
    -0
      src/org_pEp_jniadapter_Engine.cc

+ 27
- 0
src/Makefile View File

@ -0,0 +1,27 @@
LIBRARY=libpEpJNI.a
JAR=jniadapter.jar
all: $(JAR) $(LIBRARY)
JAVASOURCE=$(wildcard org/pEp/jniadapter/*.java)
$(JAR): $(JAVASOURCE)
javac $<
jar cf $@ org/pEp/jniadapter/*.class
org_pEp_jniadapter_Engine.h: org/pEp/jniadapter/Engine.java
javah org.pEp.jniadapter.Engine
org_pEp_jniadapter_Engine.o: org_pEp_jniadapter_Engine.h org_pEp_jniadapter_Engine.cc
$(LIBRARY): org_pEp_jniadapter_Engine.o
ar -r $@ $<
.PHONY: clean
clean:
rm -f $(JAR) $(LIBRARY)
rm -f org_pEp_jniadapter_Engine.o
rm -f org_pEp_jniadapter_Engine.h
rm -f org/pEp/jniadapter/*.class

+ 135
- 0
src/org_pEp_jniadapter_Engine.cc View File

@ -0,0 +1,135 @@
#include "org_pEp_jniadapter_Engine.h"
#include <stdexcept>
#include <typeinfo>
#include <assert.h>
#include <pEp/pEpEngine.h>
namespace pEp {
namespace JNIAdapter {
jint throw_pEp_Exception(JNIEnv *env, PEP_STATUS status)
{
jclass ex;
const char *ex_name;
switch (status) {
case PEP_INIT_CANNOT_LOAD_GPGME:
ex_name = "org/pEp/jniadapter/InitCannotLoadGPGME";
break;
case PEP_INIT_GPGME_INIT_FAILED:
ex_name = "org/pEp/jniadapter/GPGMEInitFailed";
break;
case PEP_INIT_SQLITE3_WITHOUT_MUTEX:
ex_name = "org/pEp/jniadapter/SQLite3WithoutMutex";
break;
case PEP_INIT_CANNOT_OPEN_DB:
ex_name = "org/pEp/jniadapter/InitCannotOpenDB";
break;
case PEP_INIT_CANNOT_OPEN_SYSTEM_DB:
ex_name = "org/pEp/jniadapter/InitCannotOpenSystemDB";
break;
default:
assert(0);
ex_name = "Exception";
}
ex = env->FindClass(ex_name);
assert(ex);
if (ex == NULL) {
ex = env->FindClass("java/lang/NoClassDefFoundError");
assert(ex);
}
return env->ThrowNew(ex, ex_name);
}
jfieldID getFieldID(
JNIEnv *env,
const char *classname,
const char *fieldname,
const char *signature
)
{
jclass engine = env->FindClass(classname);
assert(engine);
if (engine == NULL) {
jclass ex = env->FindClass("java/lang/NoClassDefFoundError");
assert(ex);
env->ThrowNew(ex, classname);
throw std::bad_cast();
}
jfieldID field = env->GetFieldID(engine, fieldname, signature);
assert(field);
if (field == NULL) {
jclass ex = env->FindClass("java/lang/NoSuchFieldError");
assert(ex);
env->ThrowNew(ex, fieldname);
throw std::invalid_argument(fieldname);
}
return field;
}
};
};
extern "C" {
using namespace pEp::JNIAdapter;
JNIEXPORT void JNICALL Java_org_pEp_jniadapter_Engine_init(
JNIEnv *env,
jobject me
)
{
PEP_SESSION session = NULL;
jfieldID handle;
PEP_STATUS status = init(&session);
assert(status == PEP_STATUS_OK);
if (status != PEP_STATUS_OK) {
throw_pEp_Exception(env, status);
return;
}
assert(session);
try {
handle = getFieldID(env, "org/pEp/jniadapter/Engine", "handle", "J");
}
catch (std::exception& ex) {
assert(0);
return;
}
jlong _session = (jlong) session;
env->SetLongField(me, handle, _session);
}
JNIEXPORT void JNICALL Java_org_pEp_jniadapter_Engine_release(
JNIEnv *env,
jobject me
)
{
PEP_SESSION session = NULL;
jfieldID handle;
try {
handle = getFieldID(env, "org/pEp/jniadapter/Engine", "handle", "J");
}
catch (std::exception& ex) {
assert(0);
return;
}
session = (PEP_SESSION) env->GetLongField(me, handle);
if (session)
release(session);
else
env->SetLongField(me, handle, jlong(0));
}
}

Loading…
Cancel
Save