added code to close the netlink socket.
[contrib/iotivity.git] / android / android_api / base / jni / JniOcStack.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Intel Corporation.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22 #include "JniOcStack.h"
23 #include "OCApi.h"
24 #include "OCPlatform.h"
25 #include "OCRepresentation.h"
26 #include "JniUtils.h"
27
28 JavaVM* g_jvm = nullptr;
29
30 jclass g_cls_Integer = nullptr;
31 jclass g_cls_int1DArray = nullptr;
32 jclass g_cls_int2DArray = nullptr;
33 jclass g_cls_Double = nullptr;
34 jclass g_cls_double1DArray = nullptr;
35 jclass g_cls_double2DArray = nullptr;
36 jclass g_cls_Boolean = nullptr;
37 jclass g_cls_boolean1DArray = nullptr;
38 jclass g_cls_boolean2DArray = nullptr;
39 jclass g_cls_String = nullptr;
40 jclass g_cls_String1DArray = nullptr;
41 jclass g_cls_String2DArray = nullptr;
42 jclass g_cls_LinkedList = nullptr;
43 jclass g_cls_Map = nullptr;
44 jclass g_cls_MapEntry = nullptr;
45 jclass g_cls_Set = nullptr;
46 jclass g_cls_Iterator = nullptr;
47 jclass g_cls_HashMap = nullptr;
48 jclass g_cls_OcException = nullptr;
49 jclass g_cls_OcResource = nullptr;
50 jclass g_cls_OcRepresentation = nullptr;
51 jclass g_cls_OcRepresentation1DArray = nullptr;
52 jclass g_cls_OcRepresentation2DArray = nullptr;
53 jclass g_cls_OcResourceRequest = nullptr;
54 jclass g_cls_OcResourceResponse = nullptr;
55 jclass g_cls_OcResourceHandle = nullptr;
56 jclass g_cls_OcPresenceHandle = nullptr;
57 jclass g_cls_OcRequestHandle = nullptr;
58 jclass g_cls_OcPresenceStatus = nullptr;
59 jclass g_cls_OcHeaderOption = nullptr;
60 jclass g_cls_ObservationInfo = nullptr;
61 jclass g_cls_OcResourceIdentifier = nullptr;
62 jclass g_cls_OcProvisionResult = nullptr;
63 jclass g_cls_OcSecureResource = nullptr;
64 jclass g_cls_OcOicSecAcl = nullptr;
65
66 jmethodID g_mid_Integer_ctor = nullptr;
67 jmethodID g_mid_Double_ctor = nullptr;
68 jmethodID g_mid_Boolean_ctor = nullptr;
69 jmethodID g_mid_LinkedList_ctor = nullptr;
70 jmethodID g_mid_LinkedList_add_object = nullptr;
71 jmethodID g_mid_Map_entrySet = nullptr;
72 jmethodID g_mid_MapEntry_getKey = nullptr;
73 jmethodID g_mid_MapEntry_getValue = nullptr;
74 jmethodID g_mid_Set_iterator = nullptr;
75 jmethodID g_mid_Iterator_hasNext = nullptr;
76 jmethodID g_mid_Iterator_next = nullptr;
77 jmethodID g_mid_HashMap_ctor = nullptr;
78 jmethodID g_mid_HashMap_put = nullptr;
79 jmethodID g_mid_OcException_ctor = nullptr;
80 jmethodID g_mid_OcException_setNativeExceptionLocation = nullptr;
81 jmethodID g_mid_OcResource_ctor = nullptr;
82 jmethodID g_mid_OcRepresentation_N_ctor = nullptr;
83 jmethodID g_mid_OcRepresentation_N_ctor_bool = nullptr;
84 jmethodID g_mid_OcResourceRequest_N_ctor = nullptr;
85 jmethodID g_mid_OcResourceResponse_N_ctor = nullptr;
86 jmethodID g_mid_OcResourceHandle_N_ctor = nullptr;
87 jmethodID g_mid_OcPresenceHandle_N_ctor = nullptr;
88 jmethodID g_mid_OcRequestHandle_N_ctor = nullptr;
89 jmethodID g_mid_OcHeaderOption_ctor = nullptr;
90 jmethodID g_mid_OcHeaderOption_get_id = nullptr;
91 jmethodID g_mid_OcHeaderOption_get_data = nullptr;
92 jmethodID g_mid_ObservationInfo_N_ctor = nullptr;
93 jmethodID g_mid_OcPresenceStatus_get = nullptr;
94 jmethodID g_mid_OcResourceIdentifier_N_ctor = nullptr;
95 jmethodID g_mid_OcProvisionResult_ctor = nullptr;
96 jmethodID g_mid_OcSecureResource_ctor = nullptr;
97
98 jmethodID g_mid_OcOicSecAcl_get_subject = nullptr;
99 jmethodID g_mid_OcOicSecAcl_get_resources_cnt = nullptr;
100 jmethodID g_mid_OcOicSecAcl_get_resources = nullptr;
101 jmethodID g_mid_OcOicSecAcl_get_permission = nullptr;
102 jmethodID g_mid_OcOicSecAcl_get_periods_cnt = nullptr;
103 jmethodID g_mid_OcOicSecAcl_get_periods = nullptr;
104 jmethodID g_mid_OcOicSecAcl_get_recurrences = nullptr;
105 jmethodID g_mid_OcOicSecAcl_get_owners_cnt = nullptr;
106 jmethodID g_mid_OcOicSecAcl_get_owners = nullptr;
107
108 jobject getOcException(JNIEnv* env, const char* file, const char* functionName,
109     const int line, const int code, const char* message)
110 {
111     std::string codeStr = JniUtils::stackResultToStr(code);
112     if (codeStr.empty())
113     {
114         codeStr = JniUtils::stackResultToStr(JNI_INVALID_VALUE);
115     }
116     jobject ex = env->NewObject(g_cls_OcException,
117         g_mid_OcException_ctor,
118         env->NewStringUTF(codeStr.c_str()),
119         env->NewStringUTF(message));
120     if (!ex)
121     {
122         return nullptr;
123     }
124     env->CallVoidMethod(ex,
125         g_mid_OcException_setNativeExceptionLocation,
126         env->NewStringUTF(file),
127         env->NewStringUTF(functionName),
128         line);
129     if (env->ExceptionCheck())
130     {
131         return nullptr;
132     }
133     return ex;
134 }
135
136 void throwOcException(JNIEnv* env, jobject ex)
137 {
138     env->Throw((jthrowable)ex);
139 }
140
141 // JNI OnLoad
142 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
143 {
144     LOGI("JNI_OnLoad");
145     JNIEnv* env;
146     g_jvm = vm;
147
148     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
149     {
150         LOGE("Failed to get the environment using GetEnv()");
151         return JNI_ERR;
152     }
153
154     jclass clazz = nullptr;
155
156     //Integer
157     clazz = env->FindClass("java/lang/Integer");
158     if (!clazz) return JNI_ERR;
159     g_cls_Integer = (jclass)env->NewGlobalRef(clazz);
160     env->DeleteLocalRef(clazz);
161
162     g_mid_Integer_ctor = env->GetMethodID(g_cls_Integer, "<init>", "(I)V");
163     if (!g_mid_Integer_ctor) return JNI_ERR;
164
165     clazz = env->FindClass("[I");
166     if (!clazz) return JNI_ERR;
167     g_cls_int1DArray = (jclass)env->NewGlobalRef(clazz);
168     env->DeleteLocalRef(clazz);
169
170     clazz = env->FindClass("[[I");
171     if (!clazz) return JNI_ERR;
172     g_cls_int2DArray = (jclass)env->NewGlobalRef(clazz);
173     env->DeleteLocalRef(clazz);
174
175     //Double
176     clazz = env->FindClass("java/lang/Double");
177     if (!clazz) return JNI_ERR;
178     g_cls_Double = (jclass)env->NewGlobalRef(clazz);
179     env->DeleteLocalRef(clazz);
180
181     g_mid_Double_ctor = env->GetMethodID(g_cls_Double, "<init>", "(D)V");
182     if (!g_mid_Double_ctor) return JNI_ERR;
183
184     clazz = env->FindClass("[D");
185     if (!clazz) return JNI_ERR;
186     g_cls_double1DArray = (jclass)env->NewGlobalRef(clazz);
187     env->DeleteLocalRef(clazz);
188
189     clazz = env->FindClass("[[D");
190     if (!clazz) return JNI_ERR;
191     g_cls_double2DArray = (jclass)env->NewGlobalRef(clazz);
192     env->DeleteLocalRef(clazz);
193
194     //Boolean
195     clazz = env->FindClass("java/lang/Boolean");
196     if (!clazz) return JNI_ERR;
197     g_cls_Boolean = (jclass)env->NewGlobalRef(clazz);
198     env->DeleteLocalRef(clazz);
199
200     g_mid_Boolean_ctor = env->GetMethodID(g_cls_Boolean, "<init>", "(Z)V");
201     if (!g_mid_Boolean_ctor) return JNI_ERR;
202
203     clazz = env->FindClass("[Z");
204     if (!clazz) return JNI_ERR;
205     g_cls_boolean1DArray = (jclass)env->NewGlobalRef(clazz);
206     env->DeleteLocalRef(clazz);
207
208     clazz = env->FindClass("[[Z");
209     if (!clazz) return JNI_ERR;
210     g_cls_boolean2DArray = (jclass)env->NewGlobalRef(clazz);
211     env->DeleteLocalRef(clazz);
212
213     //String
214     clazz = env->FindClass("java/lang/String");
215     if (!clazz) return JNI_ERR;
216     g_cls_String = (jclass)env->NewGlobalRef(clazz);
217     env->DeleteLocalRef(clazz);
218
219     clazz = env->FindClass("[Ljava/lang/String;");
220     if (!clazz) return JNI_ERR;
221     g_cls_String1DArray = (jclass)env->NewGlobalRef(clazz);
222     env->DeleteLocalRef(clazz);
223
224     clazz = env->FindClass("[[Ljava/lang/String;");
225     if (!clazz) return JNI_ERR;
226     g_cls_String2DArray = (jclass)env->NewGlobalRef(clazz);
227     env->DeleteLocalRef(clazz);
228
229     //LinkedList
230     clazz = env->FindClass("java/util/LinkedList");
231     if (!clazz) return JNI_ERR;
232     g_cls_LinkedList = (jclass)env->NewGlobalRef(clazz);
233     env->DeleteLocalRef(clazz);
234
235     g_mid_LinkedList_ctor = env->GetMethodID(g_cls_LinkedList, "<init>", "()V");
236     if (!g_mid_LinkedList_ctor) return JNI_ERR;
237
238     g_mid_LinkedList_add_object = env->GetMethodID(g_cls_LinkedList, "add", "(Ljava/lang/Object;)Z");
239     if (!g_mid_LinkedList_add_object) return JNI_ERR;
240
241     //Map
242     clazz = env->FindClass("java/util/Map");
243     if (!clazz) return JNI_ERR;
244     g_cls_Map = (jclass)env->NewGlobalRef(clazz);
245     env->DeleteLocalRef(clazz);
246
247     g_mid_Map_entrySet = env->GetMethodID(g_cls_Map, "entrySet", "()Ljava/util/Set;");
248     if (!g_mid_Map_entrySet) return JNI_ERR;
249
250     //MapEntry
251     clazz = env->FindClass("java/util/Map$Entry");
252     if (!clazz) return JNI_ERR;
253     g_cls_MapEntry = (jclass)env->NewGlobalRef(clazz);
254     env->DeleteLocalRef(clazz);
255
256     g_mid_MapEntry_getKey = env->GetMethodID(g_cls_MapEntry, "getKey", "()Ljava/lang/Object;");
257     if (!g_mid_MapEntry_getKey) return JNI_ERR;
258     g_mid_MapEntry_getValue = env->GetMethodID(g_cls_MapEntry, "getValue", "()Ljava/lang/Object;");
259     if (!g_mid_MapEntry_getValue) return JNI_ERR;
260
261     //Set
262     clazz = env->FindClass("java/util/Set");
263     if (!clazz) return JNI_ERR;
264     g_cls_Set = (jclass)env->NewGlobalRef(clazz);
265     env->DeleteLocalRef(clazz);
266
267     g_mid_Set_iterator = env->GetMethodID(g_cls_Set, "iterator", "()Ljava/util/Iterator;");
268     if (!g_mid_Set_iterator) return JNI_ERR;
269
270     //Iterator
271     clazz = env->FindClass("java/util/Iterator");
272     if (!clazz) return JNI_ERR;
273     g_cls_Iterator = (jclass)env->NewGlobalRef(clazz);
274     env->DeleteLocalRef(clazz);
275
276     g_mid_Iterator_hasNext = env->GetMethodID(g_cls_Iterator, "hasNext", "()Z");
277     if (!g_mid_Iterator_hasNext) return JNI_ERR;
278
279     g_mid_Iterator_next = env->GetMethodID(g_cls_Iterator, "next", "()Ljava/lang/Object;");
280     if (!g_mid_Iterator_next) return JNI_ERR;
281
282     //HashMap
283     clazz = env->FindClass("java/util/HashMap");
284     if (!clazz) return JNI_ERR;
285     g_cls_HashMap = (jclass)env->NewGlobalRef(clazz);
286     env->DeleteLocalRef(clazz);
287
288     g_mid_HashMap_ctor = env->GetMethodID(g_cls_HashMap, "<init>", "()V");
289     if (!g_mid_HashMap_ctor) return JNI_ERR;
290
291     g_mid_HashMap_put = env->GetMethodID(g_cls_HashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
292     if (!g_mid_HashMap_put) return JNI_ERR;
293
294     //OcException
295     clazz = env->FindClass("org/iotivity/base/OcException");
296     if (!clazz) return JNI_ERR;
297     g_cls_OcException = (jclass)env->NewGlobalRef(clazz);
298     env->DeleteLocalRef(clazz);
299
300     g_mid_OcException_ctor = env->GetMethodID(g_cls_OcException, "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
301     if (!g_mid_OcException_ctor) return JNI_ERR;
302
303     g_mid_OcException_setNativeExceptionLocation = env->GetMethodID(g_cls_OcException, "setNativeExceptionLocation",
304         "(Ljava/lang/String;""Ljava/lang/String;""I)V");
305     if (!g_mid_OcException_setNativeExceptionLocation) return JNI_ERR;
306
307     //OcResource
308     clazz = env->FindClass("org/iotivity/base/OcResource");
309     if (!clazz) return JNI_ERR;
310     g_cls_OcResource = (jclass)env->NewGlobalRef(clazz);
311     env->DeleteLocalRef(clazz);
312
313     g_mid_OcResource_ctor = env->GetMethodID(g_cls_OcResource, "<init>", "(J)V");
314     if (!g_mid_OcResource_ctor) return JNI_ERR;
315
316     //OcRepresentation
317     clazz = env->FindClass("org/iotivity/base/OcRepresentation");
318     if (!clazz) return JNI_ERR;
319     g_cls_OcRepresentation = (jclass)env->NewGlobalRef(clazz);
320     env->DeleteLocalRef(clazz);
321
322     g_mid_OcRepresentation_N_ctor = env->GetMethodID(g_cls_OcRepresentation, "<init>", "(J)V");
323     if (!g_mid_OcRepresentation_N_ctor) return JNI_ERR;
324
325     g_mid_OcRepresentation_N_ctor_bool = env->GetMethodID(g_cls_OcRepresentation, "<init>", "(JZ)V");
326     if (!g_mid_OcRepresentation_N_ctor_bool) return JNI_ERR;
327
328     clazz = env->FindClass("[Lorg/iotivity/base/OcRepresentation;");
329     if (!clazz) return JNI_ERR;
330     g_cls_OcRepresentation1DArray = (jclass)env->NewGlobalRef(clazz);
331     env->DeleteLocalRef(clazz);
332
333     clazz = env->FindClass("[[Lorg/iotivity/base/OcRepresentation;");
334     if (!clazz) return JNI_ERR;
335     g_cls_OcRepresentation2DArray = (jclass)env->NewGlobalRef(clazz);
336     env->DeleteLocalRef(clazz);
337
338     //HeaderOptions
339     clazz = env->FindClass("org/iotivity/base/OcHeaderOption");
340     if (!clazz) return JNI_ERR;
341     g_cls_OcHeaderOption = (jclass)env->NewGlobalRef(clazz);
342     env->DeleteLocalRef(clazz);
343     g_mid_OcHeaderOption_ctor = env->GetMethodID(g_cls_OcHeaderOption, "<init>", "(ILjava/lang/String;)V");
344     if (!g_mid_OcHeaderOption_ctor) return JNI_ERR;
345
346     g_mid_OcHeaderOption_get_id = env->GetMethodID(g_cls_OcHeaderOption, "getOptionId", "()I");
347     if (!g_mid_OcHeaderOption_get_id) return JNI_ERR;
348
349     g_mid_OcHeaderOption_get_data = env->GetMethodID(g_cls_OcHeaderOption, "getOptionData", "()Ljava/lang/String;");
350     if (!g_mid_OcHeaderOption_get_data) return JNI_ERR;
351
352     //OcResourceRequest
353     clazz = env->FindClass("org/iotivity/base/OcResourceRequest");
354     if (!clazz) return JNI_ERR;
355     g_cls_OcResourceRequest = (jclass)env->NewGlobalRef(clazz);
356     env->DeleteLocalRef(clazz);
357
358     g_mid_OcResourceRequest_N_ctor = env->GetMethodID(g_cls_OcResourceRequest, "<init>", "(J)V");
359     if (!g_mid_OcResourceRequest_N_ctor) return JNI_ERR;
360
361     //OcResourceResponse
362     clazz = env->FindClass("org/iotivity/base/OcResourceResponse");
363     if (!clazz) return JNI_ERR;
364     g_cls_OcResourceResponse = (jclass)env->NewGlobalRef(clazz);
365     env->DeleteLocalRef(clazz);
366
367     g_mid_OcResourceResponse_N_ctor = env->GetMethodID(g_cls_OcResourceResponse, "<init>", "(J)V");
368     if (!g_mid_OcResourceResponse_N_ctor) return JNI_ERR;
369
370     //OcResourceHandle
371     clazz = env->FindClass("org/iotivity/base/OcResourceHandle");
372     if (!clazz) return JNI_ERR;
373     g_cls_OcResourceHandle = (jclass)env->NewGlobalRef(clazz);
374     env->DeleteLocalRef(clazz);
375     g_mid_OcResourceHandle_N_ctor = env->GetMethodID(g_cls_OcResourceHandle, "<init>", "(J)V");
376     if (!g_mid_OcResourceHandle_N_ctor) return JNI_ERR;
377
378     //OcPresenceHandle
379     clazz = env->FindClass("org/iotivity/base/OcPresenceHandle");
380     if (!clazz) return JNI_ERR;
381     g_cls_OcPresenceHandle = (jclass)env->NewGlobalRef(clazz);
382     env->DeleteLocalRef(clazz);
383     g_mid_OcPresenceHandle_N_ctor = env->GetMethodID(g_cls_OcPresenceHandle, "<init>", "(J)V");
384     if (!g_mid_OcPresenceHandle_N_ctor) return JNI_ERR;
385
386     //OcRequestHandle
387     clazz = env->FindClass("org/iotivity/base/OcRequestHandle");
388     if (!clazz) return JNI_ERR;
389     g_cls_OcRequestHandle = (jclass)env->NewGlobalRef(clazz);
390     env->DeleteLocalRef(clazz);
391     g_mid_OcRequestHandle_N_ctor = env->GetMethodID(g_cls_OcRequestHandle, "<init>", "(J)V");
392     if (!g_mid_OcRequestHandle_N_ctor) return JNI_ERR;
393
394     //OcPresenceStatus
395     clazz = env->FindClass("org/iotivity/base/OcPresenceStatus");
396     if (!clazz) return JNI_ERR;
397     g_cls_OcPresenceStatus = (jclass)env->NewGlobalRef(clazz);
398     env->DeleteLocalRef(clazz);
399     g_mid_OcPresenceStatus_get = env->GetStaticMethodID(g_cls_OcPresenceStatus, "get",
400         "(Ljava/lang/String;)Lorg/iotivity/base/OcPresenceStatus;");
401     if (!g_mid_OcPresenceStatus_get) return JNI_ERR;
402
403     //ObservationInfo
404     clazz = env->FindClass("org/iotivity/base/ObservationInfo");
405     if (!clazz) return JNI_ERR;
406     g_cls_ObservationInfo = (jclass)env->NewGlobalRef(clazz);
407     env->DeleteLocalRef(clazz);
408     g_mid_ObservationInfo_N_ctor = env->GetMethodID(g_cls_ObservationInfo, "<init>", "(IB)V");
409     if (!g_mid_ObservationInfo_N_ctor) return JNI_ERR;
410
411     clazz = env->FindClass("org/iotivity/base/OcResourceIdentifier");
412     if (!clazz) return JNI_ERR;
413     g_cls_OcResourceIdentifier = (jclass)env->NewGlobalRef(clazz);
414     env->DeleteLocalRef(clazz);
415     g_mid_OcResourceIdentifier_N_ctor = env->GetMethodID(g_cls_OcResourceIdentifier, "<init>", "(J)V");
416     if (!g_mid_OcResourceIdentifier_N_ctor) return JNI_ERR;
417
418     //OcSecureResource
419     clazz = env->FindClass("org/iotivity/base/OcSecureResource");
420     if (!clazz) return JNI_ERR;
421     g_cls_OcSecureResource =  (jclass)env->NewGlobalRef(clazz);
422     env->DeleteLocalRef(clazz);
423     g_mid_OcSecureResource_ctor = env->GetMethodID(g_cls_OcSecureResource, "<init>", "(J)V");
424     if (!g_mid_OcSecureResource_ctor) return JNI_ERR;
425
426     //ProvisionResult
427     clazz = env->FindClass("org/iotivity/base/ProvisionResult");
428     if (!clazz) return JNI_ERR;
429     g_cls_OcProvisionResult =  (jclass)env->NewGlobalRef(clazz);
430     env->DeleteLocalRef(clazz);
431     g_mid_OcProvisionResult_ctor = env->GetMethodID(g_cls_OcProvisionResult, "<init>", "(Ljava/lang/String;I)V");
432     if (!g_mid_OcProvisionResult_ctor) return JNI_ERR;
433
434     //OicSecAcl
435     clazz = env->FindClass("org/iotivity/base/OicSecAcl");
436     if (!clazz) return JNI_ERR;
437     g_cls_OcOicSecAcl =  (jclass)env->NewGlobalRef(clazz);
438     env->DeleteLocalRef(clazz);
439
440     g_mid_OcOicSecAcl_get_subject = env->GetMethodID(g_cls_OcOicSecAcl, "getSubject", "()Ljava/lang/String;");
441     if (!g_mid_OcOicSecAcl_get_subject) return JNI_ERR;
442
443     g_mid_OcOicSecAcl_get_resources_cnt = env->GetMethodID(g_cls_OcOicSecAcl, "getResourcesCount", "()I");
444     if (!g_mid_OcOicSecAcl_get_resources_cnt) return JNI_ERR;
445
446     g_mid_OcOicSecAcl_get_resources = env->GetMethodID(g_cls_OcOicSecAcl, "getResources", "(I)Ljava/lang/String;");
447     if (!g_mid_OcOicSecAcl_get_resources) return JNI_ERR;
448
449     g_mid_OcOicSecAcl_get_permission = env->GetMethodID(g_cls_OcOicSecAcl, "getPermission", "()I");
450     if (!g_mid_OcOicSecAcl_get_permission) return JNI_ERR;
451
452     g_mid_OcOicSecAcl_get_periods_cnt = env->GetMethodID(g_cls_OcOicSecAcl, "getPeriodsCount", "()I");
453     if (!g_mid_OcOicSecAcl_get_periods_cnt) return JNI_ERR;
454
455     g_mid_OcOicSecAcl_get_periods = env->GetMethodID(g_cls_OcOicSecAcl, "getPeriods", "(I)Ljava/lang/String;");
456     if (!g_mid_OcOicSecAcl_get_periods) return JNI_ERR;
457
458     g_mid_OcOicSecAcl_get_recurrences = env->GetMethodID(g_cls_OcOicSecAcl, "getRecurrences", "(I)Ljava/lang/String;");
459     if (!g_mid_OcOicSecAcl_get_recurrences) return JNI_ERR;
460
461     g_mid_OcOicSecAcl_get_owners_cnt = env->GetMethodID(g_cls_OcOicSecAcl, "getOwnersCount", "()I");
462     if (!g_mid_OcOicSecAcl_get_owners_cnt) return JNI_ERR;
463
464     g_mid_OcOicSecAcl_get_owners = env->GetMethodID(g_cls_OcOicSecAcl, "getOwners", "(I)Ljava/lang/String;");
465     if (!g_mid_OcOicSecAcl_get_owners) return JNI_ERR;
466
467     return JNI_CURRENT_VERSION;
468 }
469
470 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
471 {
472     LOGI("JNI_OnUnload");
473     JNIEnv* env;
474
475     if (vm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
476     {
477         LOGE("Failed to get the environment using GetEnv()");
478         return;
479     }
480
481     env->DeleteGlobalRef(g_cls_Integer);
482     env->DeleteGlobalRef(g_cls_int1DArray);
483     env->DeleteGlobalRef(g_cls_int2DArray);
484     env->DeleteGlobalRef(g_cls_Double);
485     env->DeleteGlobalRef(g_cls_double1DArray);
486     env->DeleteGlobalRef(g_cls_double2DArray);
487     env->DeleteGlobalRef(g_cls_Boolean);
488     env->DeleteGlobalRef(g_cls_boolean1DArray);
489     env->DeleteGlobalRef(g_cls_boolean2DArray);
490     env->DeleteGlobalRef(g_cls_String);
491     env->DeleteGlobalRef(g_cls_String1DArray);
492     env->DeleteGlobalRef(g_cls_String2DArray);
493     env->DeleteGlobalRef(g_cls_LinkedList);
494     env->DeleteGlobalRef(g_cls_Map);
495     env->DeleteGlobalRef(g_cls_MapEntry);
496     env->DeleteGlobalRef(g_cls_Set);
497     env->DeleteGlobalRef(g_cls_Iterator);
498     env->DeleteGlobalRef(g_cls_HashMap);
499     env->DeleteGlobalRef(g_cls_OcResource);
500     env->DeleteGlobalRef(g_cls_OcException);
501     env->DeleteGlobalRef(g_cls_OcRepresentation);
502     env->DeleteGlobalRef(g_cls_OcRepresentation1DArray);
503     env->DeleteGlobalRef(g_cls_OcRepresentation2DArray);
504     env->DeleteGlobalRef(g_cls_OcResourceRequest);
505     env->DeleteGlobalRef(g_cls_OcResourceResponse);
506     env->DeleteGlobalRef(g_cls_OcResourceHandle);
507     env->DeleteGlobalRef(g_cls_OcPresenceHandle);
508     env->DeleteGlobalRef(g_cls_OcRequestHandle);
509     env->DeleteGlobalRef(g_cls_OcPresenceStatus);
510     env->DeleteGlobalRef(g_cls_OcHeaderOption);
511     env->DeleteGlobalRef(g_cls_ObservationInfo);
512     env->DeleteGlobalRef(g_cls_OcResourceIdentifier);
513     env->DeleteGlobalRef(g_cls_OcSecureResource);
514     env->DeleteGlobalRef(g_cls_OcProvisionResult);
515     env->DeleteGlobalRef(g_cls_OcOicSecAcl);
516 }