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 JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
28 : m_ownerResource(owner)
30 m_jwListener = env->NewWeakGlobalRef(jListener);
33 JniOnGetListener::~JniOnGetListener()
35 LOGD("~JniOnGetListener");
39 JNIEnv *env = GetJNIEnv(ret);
40 if (nullptr == env) return;
42 env->DeleteWeakGlobalRef(m_jwListener);
43 m_jwListener = nullptr;
45 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
49 void JniOnGetListener::onGetCallback(const HeaderOptions& headerOptions,
50 const OCRepresentation& ocRepresentation, const int eCode)
53 JNIEnv *env = GetJNIEnv(envRet);
54 if (nullptr == env) return;
56 jobject jListener = env->NewLocalRef(m_jwListener);
59 checkExAndRemoveListener(env);
60 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
63 jclass clsL = env->GetObjectClass(jListener);
67 checkExAndRemoveListener(env);
68 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
72 if (OC_STACK_OK != eCode)
74 jobject ex = GetOcException(eCode, "stack error in onGetCallback");
77 checkExAndRemoveListener(env);
78 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
81 jmethodID midL = env->GetMethodID(clsL, "onGetFailed", "(Ljava/lang/Throwable;)V");
84 checkExAndRemoveListener(env);
85 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
88 env->CallVoidMethod(jListener, midL, ex);
92 jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
93 if (!jHeaderOptionList)
95 checkExAndRemoveListener(env);
96 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
100 OCRepresentation* rep = new OCRepresentation(ocRepresentation);
101 jlong handle = reinterpret_cast<jlong>(rep);
102 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
104 if (!jRepresentation)
107 checkExAndRemoveListener(env);
108 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
112 jmethodID midL = env->GetMethodID(clsL, "onGetCompleted",
113 "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
117 checkExAndRemoveListener(env);
118 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
121 env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
122 if (env->ExceptionCheck())
124 LOGE("Java exception is thrown");
129 checkExAndRemoveListener(env);
130 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
133 void JniOnGetListener::checkExAndRemoveListener(JNIEnv* env)
135 if (env->ExceptionCheck())
137 jthrowable ex = env->ExceptionOccurred();
138 env->ExceptionClear();
139 m_ownerResource->removeOnGetListener(env, m_jwListener);
140 env->Throw((jthrowable)ex);
144 m_ownerResource->removeOnGetListener(env, m_jwListener);