2 * //******************************************************************
\r
4 * // Copyright 2015 Intel Corporation.
\r
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\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
12 * // http://www.apache.org/licenses/LICENSE-2.0
\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
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
22 #include "JniEntityHandler.h"
\r
23 #include "JniOcResourceRequest.h"
\r
24 #include "JniOcResourceResponse.h"
\r
25 #include "JniUtils.h"
\r
27 JniEntityHandler::JniEntityHandler(JNIEnv *env, jobject entityHandler)
\r
29 m_jListener = env->NewGlobalRef(entityHandler);
\r
32 JniEntityHandler::~JniEntityHandler()
\r
34 LOGD("~JniEntityHandler");
\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
45 OCEntityHandlerResult JniEntityHandler::handleEntity(const std::shared_ptr<OCResourceRequest> request)
\r
47 LOGD("JniEntityHandler_handleEntity");
\r
49 JNIEnv *env = GetJNIEnv(envRet);
\r
50 if (NULL == env) return OC_EH_ERROR;
\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
56 if (!jResourceRequest)
\r
58 LOGE("Failed to create OcResourceRequest");
\r
60 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
\r
64 jclass clsL = env->GetObjectClass(m_jListener);
\r
67 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
\r
70 jmethodID midL = env->GetMethodID(clsL, "handleEntity",
\r
71 "(Lorg/iotivity/base/OcResourceRequest;)Lorg/iotivity/base/EntityHandlerResult;");
\r
74 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
\r
77 jobject entityHandlerResult = env->CallObjectMethod(m_jListener, midL, jResourceRequest);
\r
78 if (env->ExceptionCheck())
\r
80 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
\r
83 if (!entityHandlerResult)
\r
85 ThrowOcException(JNI_INVALID_VALUE, "EntityHandlerResult cannot be null");
\r
86 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
\r
89 jclass clsResult = env->GetObjectClass(entityHandlerResult);
\r
92 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
\r
95 jmethodID getValue_ID = env->GetMethodID(clsResult, "getValue", "()I");
\r
98 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
\r
101 jint jResult = env->CallIntMethod(entityHandlerResult, getValue_ID);
\r
102 if (env->ExceptionCheck())
\r
104 LOGE("Java exception is thrown");
\r
105 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
\r
106 return OC_EH_ERROR;
\r
109 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
\r
110 return JniUtils::getOCEntityHandlerResult(env, static_cast<int>(jResult));
\r