Imported Upstream version 1.1.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 "JniRcsResourceContainer.h"
22 #include "JniRcsResourceAttributes.h"
23 #include "JavaClasses.h"
24 #include "JNIEnvWrapper.h"
25 #include "Log.h"
26 #include "JniRcsValue.h"
27 #include "JavaExceptions.h"
28 #include "JniRcsValue.h"
29 #include  "JniBundleResource.h"
30
31 #define LOG_TAG "JNI-Main"
32
33 #define JNI_CURRENT_VERSION JNI_VERSION_1_6
34
35 JavaVM *g_jvm;
36
37 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
38 {
39     LOGI("JNI_OnLoad");
40     JNIEnv *env;
41     g_jvm = vm;
42
43     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
44     {
45         LOGE("Failed to get the environment using GetEnv()");
46         return JNI_ERR;
47     }
48
49     JNIEnvWrapper envWrapper { env };
50
51     try
52     {
53         initJavaClasses(&envWrapper);
54         initJavaExceptions(&envWrapper);
55         initRCSValue(&envWrapper);
56         initRCSResourceContainer(&envWrapper);
57         initRCSResourceAttributes(&envWrapper);
58         initRCSJniBundleResource(&envWrapper);
59     }
60     catch (const JavaException &)
61     {
62         if (env->ExceptionCheck()) env->ExceptionDescribe();
63         return JNI_ERR;
64     }
65
66     return JNI_CURRENT_VERSION;
67 }
68
69 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
70 {
71     LOGI("JNI_OnUnload");
72     JNIEnv *env;
73
74     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
75     {
76         LOGE("Failed to get the environment using GetEnv()");
77         return;
78     }
79
80     JNIEnvWrapper envWrapper { env };
81
82     try
83     {
84         clearRCSResourceContainer(&envWrapper);
85         clearJavaClasses(&envWrapper);
86     }
87     catch (const JavaException &)
88     {
89     }
90 }