Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-encapsulation / android / service / 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 "JniRcsDiscoveryManager.h"
22 #include "JniRcsObject.h"
23 #include "JniRcsRemoteResourceObject.h"
24 #include "JniRcsResourceAttributes.h"
25 #include "JniRcsResourceObject.h"
26 #include "JniRcsValue.h"
27 #include "JavaClasses.h"
28 #include "JavaExceptions.h"
29 #include "JNIEnvWrapper.h"
30 #include "Log.h"
31
32 #define LOG_TAG "JNI-Main"
33
34 #define JNI_CURRENT_VERSION JNI_VERSION_1_6
35
36 JavaVM *g_jvm;
37
38 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
39 {
40     LOGI("JNI_OnLoad");
41     JNIEnv *env;
42     g_jvm = vm;
43
44     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
45     {
46         LOGE("Failed to get the environment using GetEnv()");
47         return JNI_ERR;
48     }
49
50     JNIEnvWrapper envWrapper{ env };
51
52     try
53     {
54         initJavaClasses(&envWrapper);
55         initJavaExceptions(&envWrapper);
56         initRCSValue(&envWrapper);
57         initRCSResourceAttributes(&envWrapper);
58         initRCSDiscoveryManager(&envWrapper);
59         initRCSRemoteResourceObject(&envWrapper);
60         initRCSObject(&envWrapper);
61         initRCSResourceObject(&envWrapper);
62     }
63     catch (const JavaException&)
64     {
65         if (env->ExceptionCheck()) env->ExceptionDescribe();
66         return JNI_ERR;
67     }
68
69     return JNI_CURRENT_VERSION;
70 }
71
72 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
73 {
74     LOGI("JNI_OnUnload");
75     JNIEnv *env;
76
77     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
78     {
79         LOGE("Failed to get the environment using GetEnv()");
80         return;
81     }
82
83     JNIEnvWrapper envWrapper{ env };
84
85     try
86     {
87         clearRCSResourceObject(&envWrapper);
88         clearRCSObject(&envWrapper);
89         clearRCSRemoteResourceObject(&envWrapper);
90         clearRCSDiscoveryManager(&envWrapper);
91         clearRCSResourceAttributes(&envWrapper);
92         clearRCSValue(&envWrapper);
93         clearJavaExceptions(&envWrapper);
94         clearJavaClasses(&envWrapper);
95     }
96     catch (const JavaException&)
97     {
98     }
99 }