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 "JniOnGetListener.h"
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
27 #include "JniOcAccountManager.h"
30 JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
31 : m_ownerResource(owner)
33 m_jwListener = env->NewWeakGlobalRef(jListener);
37 JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner)
38 : m_ownerAccountManager(owner)
40 m_jwListener = env->NewWeakGlobalRef(jListener);
44 JniOnGetListener::~JniOnGetListener()
46 LOGD("~JniOnGetListener");
50 JNIEnv *env = GetJNIEnv(ret);
56 env->DeleteWeakGlobalRef(m_jwListener);
57 m_jwListener = nullptr;
59 if (JNI_EDETACHED == ret)
61 g_jvm->DetachCurrentThread();
66 void JniOnGetListener::onGetCallback(const HeaderOptions& headerOptions,
67 const OCRepresentation& ocRepresentation, const int eCode)
69 jint envRet = JNI_ERR;
70 JNIEnv *env = GetJNIEnv(envRet);
76 jobject jListener = env->NewLocalRef(m_jwListener);
79 checkExAndRemoveListener(env);
80 if (JNI_EDETACHED == envRet)
82 g_jvm->DetachCurrentThread();
86 jclass clsL = env->GetObjectClass(jListener);
90 checkExAndRemoveListener(env);
91 if (JNI_EDETACHED == envRet)
93 g_jvm->DetachCurrentThread();
98 if (OC_STACK_OK != eCode)
100 jobject ex = GetOcException(eCode, "stack error in onGetCallback");
103 checkExAndRemoveListener(env);
104 if (JNI_EDETACHED == envRet)
106 g_jvm->DetachCurrentThread();
110 jmethodID midL = env->GetMethodID(clsL, "onGetFailed", "(Ljava/lang/Throwable;)V");
113 checkExAndRemoveListener(env);
114 if (JNI_EDETACHED == envRet)
116 g_jvm->DetachCurrentThread();
120 env->CallVoidMethod(jListener, midL, ex);
124 jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
125 if (!jHeaderOptionList)
127 checkExAndRemoveListener(env);
128 if (JNI_EDETACHED == envRet)
130 g_jvm->DetachCurrentThread();
135 OCRepresentation* rep = new OCRepresentation(ocRepresentation);
136 jlong handle = reinterpret_cast<jlong>(rep);
137 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
139 if (!jRepresentation)
142 checkExAndRemoveListener(env);
143 if (JNI_EDETACHED == envRet)
145 g_jvm->DetachCurrentThread();
150 jmethodID midL = env->GetMethodID(clsL, "onGetCompleted",
151 "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
155 checkExAndRemoveListener(env);
156 if (JNI_EDETACHED == envRet)
158 g_jvm->DetachCurrentThread();
162 env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
163 if (env->ExceptionCheck())
165 LOGE("Java exception is thrown");
170 checkExAndRemoveListener(env);
171 if (JNI_EDETACHED == envRet)
173 g_jvm->DetachCurrentThread();
177 void JniOnGetListener::checkExAndRemoveListener(JNIEnv* env)
179 if (env->ExceptionCheck())
181 jthrowable ex = env->ExceptionOccurred();
182 env->ExceptionClear();
184 m_ownerResource->removeOnGetListener(env, m_jwListener);
188 m_ownerResource->removeOnGetListener(env, m_jwListener);
190 if (m_ownerAccountManager)
192 m_ownerAccountManager->removeOnGetListener(env, m_jwListener);
195 env->Throw((jthrowable)ex);
200 m_ownerResource->removeOnGetListener(env, m_jwListener);
204 m_ownerResource->removeOnGetListener(env, m_jwListener);
206 if (m_ownerAccountManager)
208 m_ownerAccountManager->removeOnGetListener(env, m_jwListener);