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 "JniOnObserveListener.h"
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
27 JniOnObserveListener::JniOnObserveListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
28 : m_ownerResource(owner)
30 m_jwListener = env->NewWeakGlobalRef(jListener);
33 JniOnObserveListener::~JniOnObserveListener()
38 JNIEnv *env = GetJNIEnv(ret);
44 env->DeleteWeakGlobalRef(m_jwListener);
45 m_jwListener = nullptr;
47 if (JNI_EDETACHED == ret)
49 g_jvm->DetachCurrentThread();
54 void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
55 const OCRepresentation& ocRepresentation, const int& eCode, const int& sequenceNumber)
57 jint envRet = JNI_ERR;
58 JNIEnv *env = GetJNIEnv(envRet);
64 jobject jListener = env->NewLocalRef(m_jwListener);
67 checkExAndRemoveListener(env);
68 if (JNI_EDETACHED == envRet)
70 g_jvm->DetachCurrentThread();
74 jclass clsL = env->GetObjectClass(jListener);
77 checkExAndRemoveListener(env);
78 if (JNI_EDETACHED == envRet)
80 g_jvm->DetachCurrentThread();
85 if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode &&
86 OC_STACK_RESOURCE_DELETED != eCode && OC_STACK_RESOURCE_CHANGED != eCode)
88 jobject ex = GetOcException(eCode, "stack error in onObserveCallback");
91 checkExAndRemoveListener(env);
92 if (JNI_EDETACHED == envRet)
94 g_jvm->DetachCurrentThread();
98 jmethodID midL = env->GetMethodID(clsL, "onObserveFailed", "(Ljava/lang/Throwable;)V");
101 checkExAndRemoveListener(env);
102 if (JNI_EDETACHED == envRet)
104 g_jvm->DetachCurrentThread();
108 env->CallVoidMethod(jListener, midL, ex);
112 jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
113 if (!jHeaderOptionList)
115 checkExAndRemoveListener(env);
116 if (JNI_EDETACHED == envRet)
118 g_jvm->DetachCurrentThread();
123 OCRepresentation * rep = new OCRepresentation(ocRepresentation);
124 jlong handle = reinterpret_cast<jlong>(rep);
125 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation,
126 g_mid_OcRepresentation_N_ctor_bool, handle, true);
127 if (!jRepresentation)
130 checkExAndRemoveListener(env);
131 if (JNI_EDETACHED == envRet)
133 g_jvm->DetachCurrentThread();
138 jmethodID midL = env->GetMethodID(clsL, "onObserveCompleted",
139 "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;I)V");
142 checkExAndRemoveListener(env);
143 if (JNI_EDETACHED == envRet)
145 g_jvm->DetachCurrentThread();
150 env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation,
151 static_cast<jint>(sequenceNumber));
152 if (env->ExceptionCheck())
154 LOGE("Java exception is thrown");
156 jthrowable ex = env->ExceptionOccurred();
157 env->ExceptionClear();
158 m_ownerResource->removeOnObserveListener(env, m_jwListener);
159 env->Throw((jthrowable)ex);
162 if (OC_OBSERVE_DEREGISTER == sequenceNumber)
164 checkExAndRemoveListener(env);
168 if (JNI_EDETACHED == envRet)
170 g_jvm->DetachCurrentThread();
174 void JniOnObserveListener::checkExAndRemoveListener(JNIEnv* env)
176 if (env->ExceptionCheck())
178 jthrowable ex = env->ExceptionOccurred();
179 env->ExceptionClear();
180 m_ownerResource->removeOnObserveListener(env, m_jwListener);
181 env->Throw((jthrowable)ex);
185 m_ownerResource->removeOnObserveListener(env, m_jwListener);