2 * //******************************************************************
4 * // Copyright 2015 Intel Corporation.
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 "JniOnPlatformInfoListener.h"
23 #include "JniOcRepresentation.h"
25 JniOnPlatformInfoListener::JniOnPlatformInfoListener(JNIEnv *env, jobject jListener,
26 RemoveListenerCallback removeListenerCallback)
28 m_jwListener = env->NewWeakGlobalRef(jListener);
29 m_removeListenerCallback = removeListenerCallback;
32 JniOnPlatformInfoListener::~JniOnPlatformInfoListener()
34 LOGI("~JniOnPlatformInfoListener");
38 JNIEnv *env = GetJNIEnv(ret);
39 if (nullptr == env) return;
41 env->DeleteWeakGlobalRef(m_jwListener);
42 m_jwListener = nullptr;
44 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
48 void JniOnPlatformInfoListener::foundPlatformCallback(const OC::OCRepresentation& ocRepresentation)
51 JNIEnv *env = GetJNIEnv(ret);
52 if (nullptr == env) return;
54 jobject jListener = env->NewLocalRef(m_jwListener);
57 LOGI("Java onPlatformInfoListener object is already destroyed, quiting");
58 checkExAndRemoveListener(env);
59 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
63 OCRepresentation* rep = new OCRepresentation(ocRepresentation);
64 jlong handle = reinterpret_cast<jlong>(rep);
65 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
70 checkExAndRemoveListener(env);
71 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
75 jclass clsL = env->GetObjectClass(jListener);
79 checkExAndRemoveListener(env);
80 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
83 jmethodID midL = env->GetMethodID(clsL, "onPlatformFound", "(Lorg/iotivity/base/OcRepresentation;)V");
87 checkExAndRemoveListener(env);
88 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
92 env->CallVoidMethod(jListener, midL, jRepresentation);
93 if (env->ExceptionCheck())
95 LOGE("Java exception is thrown");
97 checkExAndRemoveListener(env);
100 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
103 void JniOnPlatformInfoListener::checkExAndRemoveListener(JNIEnv* env)
105 if (env->ExceptionCheck())
107 jthrowable ex = env->ExceptionOccurred();
108 env->ExceptionClear();
109 m_removeListenerCallback(env, m_jwListener);
110 env->Throw((jthrowable)ex);
114 m_removeListenerCallback(env, m_jwListener);