1 //******************************************************************
3 // Copyright 2015 Intel Corporation.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "JniEntityHandler.h"
22 #include "JniOcResourceRequest.h"
23 #include "JniOcResourceResponse.h"
26 JniEntityHandler::JniEntityHandler(JNIEnv *env, jobject entityHandler)
28 m_jListener = env->NewGlobalRef(entityHandler);
31 JniEntityHandler::~JniEntityHandler()
33 LOGD("~JniEntityHandler");
37 JNIEnv *env = GetJNIEnv(ret);
43 env->DeleteGlobalRef(m_jListener);
44 m_jListener = nullptr;
46 if (JNI_EDETACHED == ret)
48 g_jvm->DetachCurrentThread();
53 OCEntityHandlerResult JniEntityHandler::handleEntity(
54 const std::shared_ptr<OCResourceRequest> request)
56 LOGD("JniEntityHandler_handleEntity");
57 jint envRet = JNI_ERR;
58 JNIEnv *env = GetJNIEnv(envRet);
64 JniOcResourceRequest* jniResReq = new JniOcResourceRequest(request);
65 jlong reqHandle = reinterpret_cast<jlong>(jniResReq);
66 jobject jResourceRequest =
67 env->NewObject(g_cls_OcResourceRequest,
68 g_mid_OcResourceRequest_N_ctor,
70 if (!jResourceRequest)
72 LOGE("Failed to create OcResourceRequest");
74 if (JNI_EDETACHED == envRet)
76 g_jvm->DetachCurrentThread();
82 jclass clsL = env->GetObjectClass(m_jListener);
85 if (JNI_EDETACHED == envRet)
87 g_jvm->DetachCurrentThread();
92 jmethodID midL = env->GetMethodID(clsL, "handleEntity",
93 "(Lorg/iotivity/base/OcResourceRequest;)Lorg/iotivity/base/EntityHandlerResult;");
96 if (JNI_EDETACHED == envRet)
98 g_jvm->DetachCurrentThread();
103 jobject entityHandlerResult = env->CallObjectMethod(m_jListener, midL, jResourceRequest);
104 if (env->ExceptionCheck())
106 if (JNI_EDETACHED == envRet)
108 g_jvm->DetachCurrentThread();
113 if (!entityHandlerResult)
115 ThrowOcException(JNI_INVALID_VALUE, "EntityHandlerResult cannot be null");
116 if (JNI_EDETACHED == envRet)
118 g_jvm->DetachCurrentThread();
123 jclass clsResult = env->GetObjectClass(entityHandlerResult);
126 if (JNI_EDETACHED == envRet)
128 g_jvm->DetachCurrentThread();
133 jmethodID getValue_ID = env->GetMethodID(clsResult, "getValue", "()I");
136 if (JNI_EDETACHED == envRet)
138 g_jvm->DetachCurrentThread();
142 jint jResult = env->CallIntMethod(entityHandlerResult, getValue_ID);
143 if (env->ExceptionCheck())
145 LOGE("Java exception is thrown");
146 if (JNI_EDETACHED == envRet)
148 g_jvm->DetachCurrentThread();
154 if (JNI_EDETACHED == envRet)
156 g_jvm->DetachCurrentThread();
159 return JniUtils::getOCEntityHandlerResult(env, static_cast<int>(jResult));