Restructuring of the easy-setup service.
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / jni / JniJvm.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20 #include "JniJvm.h"
21
22 JavaVM *g_jvm = NULL;
23
24 jclass g_cls_RemoteEnrollee = NULL;
25 jclass g_cls_ESException = NULL;
26
27 jmethodID g_mid_RemoteEnrollee_ctor = NULL;
28 jmethodID g_mid_ESException_ctor = NULL;
29
30 // JNI OnLoad
31 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
32 {
33     LOGI("JNI_OnLoad");
34     JNIEnv *env;
35     g_jvm = vm;
36
37     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
38     {
39         LOGE("Failed to get the environment using GetEnv()");
40         return JNI_ERR;
41     }
42
43     jclass clazz = nullptr;
44
45     //Remote Enrollee
46     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/RemoteEnrollee");
47     if (!clazz) return JNI_ERR;
48     g_cls_RemoteEnrollee = (jclass)env->NewGlobalRef(clazz);
49     env->DeleteLocalRef(clazz);
50
51     g_mid_RemoteEnrollee_ctor = env->GetMethodID(g_cls_RemoteEnrollee, "<init>",
52                                 "(J)V");
53     if (!g_mid_RemoteEnrollee_ctor) return JNI_ERR;
54
55     //ESException
56     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/ESException");
57     if (!clazz) return JNI_ERR;
58     g_cls_ESException = (jclass)env->NewGlobalRef(clazz);
59     env->DeleteLocalRef(clazz);
60
61     g_mid_ESException_ctor = env->GetMethodID(g_cls_ESException, "<init>", "(Ljava/lang/String;)V");
62     if (!g_mid_ESException_ctor) return JNI_ERR;
63
64     return JNI_CURRENT_VERSION;
65 }
66
67 //JNI OnUnload
68 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
69 {
70     LOGI("JNI_OnUnload");
71     JNIEnv *env;
72
73     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
74     {
75         LOGE("Failed to get the environment using GetEnv()");
76         return;
77     }
78     env->DeleteGlobalRef(g_cls_RemoteEnrollee);
79     env->DeleteGlobalRef(g_cls_ESException);
80 }