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, RemoveListenerCallback removeListener)
28 : m_removeListener(removeListener)
30 m_jwListener = env->NewWeakGlobalRef(jListener);
33 JniOnGetListener::~JniOnGetListener()
35 LOGD("~JniOnGetListener");
39 JNIEnv *env = GetJNIEnv(ret);
45 env->DeleteWeakGlobalRef(m_jwListener);
46 m_jwListener = nullptr;
48 if (JNI_EDETACHED == ret)
50 g_jvm->DetachCurrentThread();
55 void JniOnGetListener::onGetCallback(const HeaderOptions& headerOptions,
56 const OCRepresentation& ocRepresentation, const int eCode)
58 jint envRet = JNI_ERR;
59 JNIEnv *env = GetJNIEnv(envRet);
65 jobject jListener = env->NewLocalRef(m_jwListener);
68 checkExAndRemoveListener(env);
69 if (JNI_EDETACHED == envRet)
71 g_jvm->DetachCurrentThread();
75 jclass clsL = env->GetObjectClass(jListener);
79 checkExAndRemoveListener(env);
80 if (JNI_EDETACHED == envRet)
82 g_jvm->DetachCurrentThread();
87 if (OC_STACK_OK != eCode)
89 jobject ex = GetOcException(eCode, "stack error in onGetCallback");
92 checkExAndRemoveListener(env);
93 if (JNI_EDETACHED == envRet)
95 g_jvm->DetachCurrentThread();
99 jmethodID midL = env->GetMethodID(clsL, "onGetFailed", "(Ljava/lang/Throwable;)V");
102 checkExAndRemoveListener(env);
103 if (JNI_EDETACHED == envRet)
105 g_jvm->DetachCurrentThread();
109 env->CallVoidMethod(jListener, midL, ex);
113 jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
114 if (!jHeaderOptionList)
116 checkExAndRemoveListener(env);
117 if (JNI_EDETACHED == envRet)
119 g_jvm->DetachCurrentThread();
124 OCRepresentation* rep = new OCRepresentation(ocRepresentation);
125 jlong handle = reinterpret_cast<jlong>(rep);
126 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
128 if (!jRepresentation)
131 checkExAndRemoveListener(env);
132 if (JNI_EDETACHED == envRet)
134 g_jvm->DetachCurrentThread();
139 jmethodID midL = env->GetMethodID(clsL, "onGetCompleted",
140 "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
144 checkExAndRemoveListener(env);
145 if (JNI_EDETACHED == envRet)
147 g_jvm->DetachCurrentThread();
151 env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
152 if (env->ExceptionCheck())
154 LOGE("Java exception is thrown");
159 checkExAndRemoveListener(env);
160 if (JNI_EDETACHED == envRet)
162 g_jvm->DetachCurrentThread();
166 void JniOnGetListener::checkExAndRemoveListener(JNIEnv* env)
168 if (env->ExceptionCheck())
170 jthrowable ex = env->ExceptionOccurred();
171 env->ExceptionClear();
172 m_removeListener(env, m_jwListener);
173 env->Throw((jthrowable)ex);
177 m_removeListener(env, m_jwListener);