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);
42 env->DeleteGlobalRef(m_jListener);
43 if (JNI_EDETACHED == ret)
45 g_jvm->DetachCurrentThread();
50 OCEntityHandlerResult JniEntityHandler::handleEntity(
51 const std::shared_ptr<OCResourceRequest> request)
53 LOGD("JniEntityHandler_handleEntity");
55 JNIEnv *env = GetJNIEnv(envRet);
61 JniOcResourceRequest* jniResReq = new JniOcResourceRequest(request);
62 jlong reqHandle = reinterpret_cast<jlong>(jniResReq);
63 jobject jResourceRequest =
64 env->NewObject(g_cls_OcResourceRequest,
65 g_mid_OcResourceRequest_N_ctor,
67 if (!jResourceRequest)
69 LOGE("Failed to create OcResourceRequest");
71 if (JNI_EDETACHED == envRet)
73 g_jvm->DetachCurrentThread();
79 jclass clsL = env->GetObjectClass(m_jListener);
82 if (JNI_EDETACHED == envRet)
84 g_jvm->DetachCurrentThread();
89 jmethodID midL = env->GetMethodID(clsL, "handleEntity",
90 "(Lorg/iotivity/base/OcResourceRequest;)Lorg/iotivity/base/EntityHandlerResult;");
93 if (JNI_EDETACHED == envRet)
95 g_jvm->DetachCurrentThread();
100 jobject entityHandlerResult = env->CallObjectMethod(m_jListener, midL, jResourceRequest);
101 if (env->ExceptionCheck())
103 if (JNI_EDETACHED == envRet)
105 g_jvm->DetachCurrentThread();
110 if (!entityHandlerResult)
112 ThrowOcException(JNI_INVALID_VALUE, "EntityHandlerResult cannot be null");
113 if (JNI_EDETACHED == envRet)
115 g_jvm->DetachCurrentThread();
120 jclass clsResult = env->GetObjectClass(entityHandlerResult);
123 if (JNI_EDETACHED == envRet)
125 g_jvm->DetachCurrentThread();
130 jmethodID getValue_ID = env->GetMethodID(clsResult, "getValue", "()I");
133 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
136 jint jResult = env->CallIntMethod(entityHandlerResult, getValue_ID);
137 if (env->ExceptionCheck())
139 LOGE("Java exception is thrown");
140 if (JNI_EDETACHED == envRet)
142 g_jvm->DetachCurrentThread();
148 if (JNI_EDETACHED == envRet)
150 g_jvm->DetachCurrentThread();
153 return JniUtils::getOCEntityHandlerResult(env, static_cast<int>(jResult));