860d52b6655c6600c0d79afdd8caff455ad72feb
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniEntityHandler.cpp
1 /*\r
2 * //******************************************************************\r
3 * //\r
4 * // Copyright 2015 Intel Corporation.\r
5 * //\r
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
7 * //\r
8 * // Licensed under the Apache License, Version 2.0 (the "License");\r
9 * // you may not use this file except in compliance with the License.\r
10 * // You may obtain a copy of the License at\r
11 * //\r
12 * //      http://www.apache.org/licenses/LICENSE-2.0\r
13 * //\r
14 * // Unless required by applicable law or agreed to in writing, software\r
15 * // distributed under the License is distributed on an "AS IS" BASIS,\r
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17 * // See the License for the specific language governing permissions and\r
18 * // limitations under the License.\r
19 * //\r
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
21 */\r
22 #include "JniEntityHandler.h"\r
23 #include "JniOcResourceRequest.h"\r
24 #include "JniOcResourceResponse.h"\r
25 #include "JniUtils.h"\r
26 \r
27 JniEntityHandler::JniEntityHandler(JNIEnv *env, jobject entityHandler)\r
28 {\r
29     m_jListener = env->NewGlobalRef(entityHandler);\r
30 }\r
31 \r
32 JniEntityHandler::~JniEntityHandler()\r
33 {\r
34     LOGD("~JniEntityHandler");\r
35     if (m_jListener)\r
36     {\r
37         jint ret;\r
38         JNIEnv *env = GetJNIEnv(ret);\r
39         if (NULL == env) return;\r
40         env->DeleteGlobalRef(m_jListener);\r
41         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();\r
42     }\r
43 }\r
44 \r
45 OCEntityHandlerResult JniEntityHandler::handleEntity(const std::shared_ptr<OCResourceRequest> request)\r
46 {\r
47     LOGD("JniEntityHandler_handleEntity");\r
48     jint envRet;\r
49     JNIEnv *env = GetJNIEnv(envRet);\r
50     if (NULL == env) return OC_EH_ERROR;\r
51 \r
52     JniOcResourceRequest* jniResReq = new JniOcResourceRequest(request);\r
53     jlong reqHandle = reinterpret_cast<jlong>(jniResReq);\r
54     jobject jResourceRequest = env->NewObject(g_cls_OcResourceRequest, g_mid_OcResourceRequest_N_ctor,\r
55         reqHandle);\r
56     if (!jResourceRequest)\r
57     {\r
58         LOGE("Failed to create OcResourceRequest");\r
59         delete jniResReq;\r
60         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
61         return OC_EH_ERROR;\r
62     }\r
63 \r
64     jclass clsL = env->GetObjectClass(m_jListener);\r
65     if (!clsL)\r
66     {\r
67         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
68         return OC_EH_ERROR;\r
69     }\r
70     jmethodID midL = env->GetMethodID(clsL, "handleEntity",\r
71         "(Lorg/iotivity/base/OcResourceRequest;)Lorg/iotivity/base/EntityHandlerResult;");\r
72     if (!midL)\r
73     {\r
74         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
75         return OC_EH_ERROR;\r
76     }\r
77     jobject entityHandlerResult = env->CallObjectMethod(m_jListener, midL, jResourceRequest);\r
78     if (env->ExceptionCheck())\r
79     {\r
80         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
81         return OC_EH_ERROR;\r
82     }\r
83     if (!entityHandlerResult)\r
84     {\r
85         ThrowOcException(JNI_INVALID_VALUE, "EntityHandlerResult cannot be null");\r
86         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
87         return OC_EH_ERROR;\r
88     }\r
89     jclass clsResult = env->GetObjectClass(entityHandlerResult);\r
90     if (!clsResult)\r
91     {\r
92         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
93         return OC_EH_ERROR;\r
94     }\r
95     jmethodID getValue_ID = env->GetMethodID(clsResult, "getValue", "()I");\r
96     if (!getValue_ID)\r
97     {\r
98         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
99         return OC_EH_ERROR;\r
100     }\r
101     jint jResult = env->CallIntMethod(entityHandlerResult, getValue_ID);\r
102     if (env->ExceptionCheck())\r
103     {\r
104         LOGE("Java exception is thrown");\r
105         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
106         return OC_EH_ERROR;\r
107     }\r
108 \r
109     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
110     return JniUtils::getOCEntityHandlerResult(env, static_cast<int>(jResult));\r
111 }