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 (NULL == env) return;
40 env->DeleteWeakGlobalRef(m_jwListener);
41 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
45 void JniOnPlatformInfoListener::foundPlatformCallback(const OC::OCRepresentation& ocRepresentation)
48 JNIEnv *env = GetJNIEnv(ret);
49 if (NULL == env) return;
51 jobject jListener = env->NewLocalRef(m_jwListener);
54 LOGI("Java onPlatformInfoListener object is already destroyed, quiting");
55 checkExAndRemoveListener(env);
56 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
60 OCRepresentation* rep = new OCRepresentation(ocRepresentation);
61 jlong handle = reinterpret_cast<jlong>(rep);
62 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
67 checkExAndRemoveListener(env);
68 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
72 jclass clsL = env->GetObjectClass(jListener);
76 checkExAndRemoveListener(env);
77 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
80 jmethodID midL = env->GetMethodID(clsL, "onPlatformFound", "(Lorg/iotivity/base/OcRepresentation;)V");
84 checkExAndRemoveListener(env);
85 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
89 env->CallVoidMethod(jListener, midL, jRepresentation);
90 if (env->ExceptionCheck())
92 LOGE("Java exception is thrown");
94 checkExAndRemoveListener(env);
97 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
100 void JniOnPlatformInfoListener::checkExAndRemoveListener(JNIEnv* env)
102 if (env->ExceptionCheck())
104 jthrowable ex = env->ExceptionOccurred();
105 env->ExceptionClear();
106 m_removeListenerCallback(env, m_jwListener);
107 env->Throw((jthrowable)ex);
111 m_removeListenerCallback(env, m_jwListener);