2 * //******************************************************************
4 * // Copyright 2017 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "JniKeepAliveListener.h"
24 #include "JniOcRepresentation.h"
27 JniKeepAliveListener::JniKeepAliveListener(JNIEnv *env, jobject jListener,
28 RemoveListenerCallback removeListenerCallback)
30 m_jwListener = env->NewWeakGlobalRef(jListener);
31 m_removeListenerCallback = removeListenerCallback;
34 JniKeepAliveListener::~JniKeepAliveListener()
36 LOGI("~JniKeepAliveListener()");
40 JNIEnv *env = GetJNIEnv(ret);
45 env->DeleteWeakGlobalRef(m_jwListener);
46 m_jwListener = nullptr;
47 if (JNI_EDETACHED == ret)
49 g_jvm->DetachCurrentThread();
54 void JniKeepAliveListener::onKeepAliveListener(const int result, const OC::OCRepresentation& rep)
57 JNIEnv *env = GetJNIEnv(ret);
63 jobject jListener = env->NewLocalRef(m_jwListener);
66 checkExAndRemoveListener(env);
67 if (JNI_EDETACHED == ret)
69 g_jvm->DetachCurrentThread();
74 jclass clsL = env->GetObjectClass(jListener);
77 checkExAndRemoveListener(env);
78 if (JNI_EDETACHED == ret)
80 g_jvm->DetachCurrentThread();
86 OCRepresentation* nRep = new OCRepresentation(rep);
87 jlong handle = reinterpret_cast<jlong>(nRep);
88 jobject jRep = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
94 checkExAndRemoveListener(env);
95 if (JNI_EDETACHED == ret)
97 g_jvm->DetachCurrentThread();
103 jint jRet = static_cast<jint>(result);
105 jmethodID midL = env->GetMethodID(clsL, "onKeepAliveListener",
106 "(Lorg/iotivity/base/OcRepresentation;I)V");
110 checkExAndRemoveListener(env);
111 if (JNI_EDETACHED == ret)
113 g_jvm->DetachCurrentThread();
118 env->CallVoidMethod(jListener, midL, jRep, jRet);
119 if (env->ExceptionCheck())
121 LOGE("Java exception is thrown");
125 checkExAndRemoveListener(env);
127 if (JNI_EDETACHED == ret)
129 g_jvm->DetachCurrentThread();
133 void JniKeepAliveListener::checkExAndRemoveListener(JNIEnv* env)
135 if (env->ExceptionCheck())
137 jthrowable ex = env->ExceptionOccurred();
138 env->ExceptionClear();
139 m_removeListenerCallback(env, m_jwListener);
140 env->Throw((jthrowable)ex);
144 m_removeListenerCallback(env, m_jwListener);