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 "JniOnPostListener.h"
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
27 #include "JniOcAccountManager.h"
30 JniOnPostListener::JniOnPostListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
31 : m_ownerResource(owner)
33 m_jwListener = env->NewWeakGlobalRef(jListener);
35 m_ownerAccountManager = nullptr;
40 JniOnPostListener::JniOnPostListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner)
41 : m_ownerAccountManager(owner)
43 m_jwListener = env->NewWeakGlobalRef(jListener);
44 m_ownerResource = nullptr;
48 JniOnPostListener::~JniOnPostListener()
53 JNIEnv *env = GetJNIEnv(ret);
59 env->DeleteWeakGlobalRef(m_jwListener);
60 m_jwListener = nullptr;
62 if (JNI_EDETACHED == ret)
64 g_jvm->DetachCurrentThread();
69 void JniOnPostListener::onPostCallback(const HeaderOptions& headerOptions,
70 const OCRepresentation& ocRepresentation, const int eCode)
72 jint envRet = JNI_ERR;
73 JNIEnv *env = GetJNIEnv(envRet);
79 jobject jListener = env->NewLocalRef(m_jwListener);
82 checkExAndRemoveListener(env);
83 if (JNI_EDETACHED == envRet)
85 g_jvm->DetachCurrentThread();
89 jclass clsL = env->GetObjectClass(jListener);
92 checkExAndRemoveListener(env);
93 if (JNI_EDETACHED == envRet)
95 g_jvm->DetachCurrentThread();
100 if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode &&
101 OC_STACK_RESOURCE_CHANGED != eCode)
103 jobject ex = GetOcException(eCode, "stack error in onPostCallback");
106 checkExAndRemoveListener(env);
107 if (JNI_EDETACHED == envRet)
109 g_jvm->DetachCurrentThread();
113 jmethodID midL = env->GetMethodID(clsL, "onPostFailed", "(Ljava/lang/Throwable;)V");
116 checkExAndRemoveListener(env);
117 if (JNI_EDETACHED == envRet)
119 g_jvm->DetachCurrentThread();
123 env->CallVoidMethod(jListener, midL, ex);
127 jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
128 if (!jHeaderOptionList)
130 checkExAndRemoveListener(env);
131 if (JNI_EDETACHED == envRet)
133 g_jvm->DetachCurrentThread();
138 OCRepresentation * rep = new OCRepresentation(ocRepresentation);
139 jlong handle = reinterpret_cast<jlong>(rep);
140 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
142 if (!jRepresentation)
145 checkExAndRemoveListener(env);
146 if (JNI_EDETACHED == envRet)
148 g_jvm->DetachCurrentThread();
153 jmethodID midL = env->GetMethodID(clsL, "onPostCompleted",
154 "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
157 checkExAndRemoveListener(env);
158 if (JNI_EDETACHED == envRet)
160 g_jvm->DetachCurrentThread();
165 env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
166 if (env->ExceptionCheck())
168 LOGE("Java exception is thrown");
173 checkExAndRemoveListener(env);
174 if (JNI_EDETACHED == envRet)
176 g_jvm->DetachCurrentThread();
180 void JniOnPostListener::checkExAndRemoveListener(JNIEnv* env)
182 if (env->ExceptionCheck())
184 jthrowable ex = env->ExceptionOccurred();
185 env->ExceptionClear();
187 m_ownerResource->removeOnPostListener(env, m_jwListener);
189 if (nullptr != m_ownerResource)
191 m_ownerResource->removeOnPostListener(env, m_jwListener);
193 if (nullptr != m_ownerAccountManager)
195 m_ownerAccountManager->removeOnPostListener(env, m_jwListener);
198 env->Throw((jthrowable)ex);
203 m_ownerResource->removeOnPostListener(env, m_jwListener);
205 if (nullptr != m_ownerResource)
207 m_ownerResource->removeOnPostListener(env, m_jwListener);
209 if (nullptr != m_ownerAccountManager)
211 m_ownerAccountManager->removeOnPostListener(env, m_jwListener);