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 JniOnPostListener::JniOnPostListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
28 : m_ownerResource(owner)
30 m_jwListener = env->NewWeakGlobalRef(jListener);
33 JniOnPostListener::~JniOnPostListener()
38 JNIEnv *env = GetJNIEnv(ret);
39 if (NULL == env) return;
41 env->DeleteWeakGlobalRef(m_jwListener);
42 m_jwListener = nullptr;
44 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
48 void JniOnPostListener::onPostCallback(const HeaderOptions& headerOptions,
49 const OCRepresentation& ocRepresentation, const int eCode)
52 JNIEnv *env = GetJNIEnv(envRet);
53 if (NULL == env) return;
55 jobject jListener = env->NewLocalRef(m_jwListener);
58 checkExAndRemoveListener(env);
59 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
62 jclass clsL = env->GetObjectClass(jListener);
65 checkExAndRemoveListener(env);
66 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
70 if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode)
72 jobject ex = GetOcException(eCode, "stack error in onPostCallback");
75 checkExAndRemoveListener(env);
76 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
79 jmethodID midL = env->GetMethodID(clsL, "onPostFailed", "(Ljava/lang/Throwable;)V");
82 checkExAndRemoveListener(env);
83 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
86 env->CallVoidMethod(jListener, midL, ex);
90 jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
91 if (!jHeaderOptionList)
93 checkExAndRemoveListener(env);
94 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
98 OCRepresentation * rep = new OCRepresentation(ocRepresentation);
99 jlong handle = reinterpret_cast<jlong>(rep);
100 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
102 if (!jRepresentation)
105 checkExAndRemoveListener(env);
106 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
110 jmethodID midL = env->GetMethodID(clsL, "onPostCompleted",
111 "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
114 checkExAndRemoveListener(env);
115 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
119 env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
120 if (env->ExceptionCheck())
122 LOGE("Java exception is thrown");
127 checkExAndRemoveListener(env);
128 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
131 void JniOnPostListener::checkExAndRemoveListener(JNIEnv* env)
133 if (env->ExceptionCheck())
135 jthrowable ex = env->ExceptionOccurred();
136 env->ExceptionClear();
137 m_ownerResource->removeOnPostListener(env, m_jwListener);
138 env->Throw((jthrowable)ex);
142 m_ownerResource->removeOnPostListener(env, m_jwListener);