Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-container / android / resource-container / src / main / jni / JniMain.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 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
21 #include "JniRcsObject.h"
22
23 #include "JniRcsResourceContainer.h"
24 #include "JavaClasses.h"
25 #include "JavaExceptions.h"
26 #include "JNIEnvWrapper.h"
27 #include "Log.h"
28
29 #define LOG_TAG "JNI-Main"
30
31 #define JNI_CURRENT_VERSION JNI_VERSION_1_6
32
33 JavaVM *g_jvm;
34
35 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
36 {
37     LOGI("JNI_OnLoad");
38     JNIEnv *env;
39     g_jvm = vm;
40
41     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
42     {
43         LOGE("Failed to get the environment using GetEnv()");
44         return JNI_ERR;
45     }
46
47     JNIEnvWrapper envWrapper { env };
48
49     try
50     {
51         initJavaClasses(&envWrapper);
52         initJavaExceptions(&envWrapper);
53         initRCSObject(&envWrapper);
54         initRCSResourceContainer(&envWrapper);
55     }
56     catch (const JavaException &)
57     {
58         if (env->ExceptionCheck()) env->ExceptionDescribe();
59         return JNI_ERR;
60     }
61
62     return JNI_CURRENT_VERSION;
63 }
64
65 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
66 {
67     LOGI("JNI_OnUnload");
68     JNIEnv *env;
69
70     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
71     {
72         LOGE("Failed to get the environment using GetEnv()");
73         return;
74     }
75
76     JNIEnvWrapper envWrapper { env };
77
78     try
79     {
80         clearRCSResourceContainer(&envWrapper);
81         clearRCSObject(&envWrapper);
82         clearJavaExceptions(&envWrapper);
83         clearJavaClasses(&envWrapper);
84     }
85     catch (const JavaException &)
86     {
87     }
88 }