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);
35 m_ownerAccountManager = nullptr;
40 JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner)
41 : m_ownerAccountManager(owner)
43 m_jwListener = env->NewWeakGlobalRef(jListener);
44 m_ownerResource = nullptr;
48 JniOnGetListener::~JniOnGetListener()
50 LOGD("~JniOnGetListener");
54 JNIEnv *env = GetJNIEnv(ret);
60 env->DeleteWeakGlobalRef(m_jwListener);
61 m_jwListener = nullptr;
63 if (JNI_EDETACHED == ret)
65 g_jvm->DetachCurrentThread();
70 void JniOnGetListener::onGetCallback(const HeaderOptions& headerOptions,
71 const OCRepresentation& ocRepresentation, const int eCode)
73 jint envRet = JNI_ERR;
74 JNIEnv *env = GetJNIEnv(envRet);
80 jobject jListener = env->NewLocalRef(m_jwListener);
83 checkExAndRemoveListener(env);
84 if (JNI_EDETACHED == envRet)
86 g_jvm->DetachCurrentThread();
90 jclass clsL = env->GetObjectClass(jListener);
94 checkExAndRemoveListener(env);
95 if (JNI_EDETACHED == envRet)
97 g_jvm->DetachCurrentThread();
102 if (OC_STACK_OK != eCode)
104 jobject ex = GetOcException(eCode, "stack error in onGetCallback");
107 checkExAndRemoveListener(env);
108 if (JNI_EDETACHED == envRet)
110 g_jvm->DetachCurrentThread();
114 jmethodID midL = env->GetMethodID(clsL, "onGetFailed", "(Ljava/lang/Throwable;)V");
117 checkExAndRemoveListener(env);
118 if (JNI_EDETACHED == envRet)
120 g_jvm->DetachCurrentThread();
124 env->CallVoidMethod(jListener, midL, ex);
128 jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
129 if (!jHeaderOptionList)
131 checkExAndRemoveListener(env);
132 if (JNI_EDETACHED == envRet)
134 g_jvm->DetachCurrentThread();
139 OCRepresentation* rep = new OCRepresentation(ocRepresentation);
140 jlong handle = reinterpret_cast<jlong>(rep);
141 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
143 if (!jRepresentation)
146 checkExAndRemoveListener(env);
147 if (JNI_EDETACHED == envRet)
149 g_jvm->DetachCurrentThread();
154 jmethodID midL = env->GetMethodID(clsL, "onGetCompleted",
155 "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
159 checkExAndRemoveListener(env);
160 if (JNI_EDETACHED == envRet)
162 g_jvm->DetachCurrentThread();
166 env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
167 if (env->ExceptionCheck())
169 LOGE("Java exception is thrown");
174 checkExAndRemoveListener(env);
175 if (JNI_EDETACHED == envRet)
177 g_jvm->DetachCurrentThread();
181 void JniOnGetListener::checkExAndRemoveListener(JNIEnv* env)
183 if (env->ExceptionCheck())
185 jthrowable ex = env->ExceptionOccurred();
186 env->ExceptionClear();
188 m_ownerResource->removeOnGetListener(env, m_jwListener);
190 if (nullptr != m_ownerResource)
192 m_ownerResource->removeOnGetListener(env, m_jwListener);
194 if (nullptr != m_ownerAccountManager)
196 m_ownerAccountManager->removeOnGetListener(env, m_jwListener);
199 env->Throw((jthrowable)ex);
204 m_ownerResource->removeOnGetListener(env, m_jwListener);
206 if (nullptr != m_ownerResource)
208 m_ownerResource->removeOnGetListener(env, m_jwListener);
210 if (nullptr != m_ownerAccountManager)
212 m_ownerAccountManager->removeOnGetListener(env, m_jwListener);