Merge branch 'master' into cloud-interface
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnGetListener.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Intel Corporation.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
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
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
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.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22 #include "JniOnGetListener.h"
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
25 #include "JniUtils.h"
26
27 JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
28     : m_ownerResource(owner)
29 {
30     m_jwListener = env->NewWeakGlobalRef(jListener);
31 }
32
33 JniOnGetListener::~JniOnGetListener()
34 {
35     LOGD("~JniOnGetListener");
36     if (m_jwListener)
37     {
38         jint ret;
39         JNIEnv *env = GetJNIEnv(ret);
40         if (nullptr == env) return;
41
42         env->DeleteWeakGlobalRef(m_jwListener);
43         m_jwListener = nullptr;
44
45         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
46     }
47 }
48
49 void JniOnGetListener::onGetCallback(const HeaderOptions& headerOptions,
50     const OCRepresentation& ocRepresentation, const int eCode)
51 {
52     jint envRet;
53     JNIEnv *env = GetJNIEnv(envRet);
54     if (nullptr == env) return;
55
56     jobject jListener = env->NewLocalRef(m_jwListener);
57     if (!jListener)
58     {
59         checkExAndRemoveListener(env);
60         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
61         return;
62     }
63     jclass clsL = env->GetObjectClass(jListener);
64
65     if (!clsL)
66     {
67         checkExAndRemoveListener(env);
68         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
69         return;
70     }
71
72     if (OC_STACK_OK != eCode)
73     {
74         jobject ex = GetOcException(eCode, "stack error in onGetCallback");
75         if (!ex)
76         {
77             checkExAndRemoveListener(env);
78             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
79             return;
80         }
81         jmethodID midL = env->GetMethodID(clsL, "onGetFailed", "(Ljava/lang/Throwable;)V");
82         if (!midL)
83         {
84             checkExAndRemoveListener(env);
85             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
86             return;
87         }
88         env->CallVoidMethod(jListener, midL, ex);
89     }
90     else
91     {
92         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
93         if (!jHeaderOptionList)
94         {
95             checkExAndRemoveListener(env);
96             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
97             return;
98         }
99
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,
103             handle, true);
104         if (!jRepresentation)
105         {
106             delete rep;
107             checkExAndRemoveListener(env);
108             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
109             return;
110         }
111
112         jmethodID midL = env->GetMethodID(clsL, "onGetCompleted",
113             "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
114         if (!midL)
115         {
116             delete rep;
117             checkExAndRemoveListener(env);
118             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
119             return;
120         }
121         env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
122         if (env->ExceptionCheck())
123         {
124             LOGE("Java exception is thrown");
125             delete rep;
126         }
127     }
128
129     checkExAndRemoveListener(env);
130     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
131 }
132
133 void JniOnGetListener::checkExAndRemoveListener(JNIEnv* env)
134 {
135     if (env->ExceptionCheck())
136     {
137         jthrowable ex = env->ExceptionOccurred();
138         env->ExceptionClear();
139         m_ownerResource->removeOnGetListener(env, m_jwListener);
140         env->Throw((jthrowable)ex);
141     }
142     else
143     {
144         m_ownerResource->removeOnGetListener(env, m_jwListener);
145     }
146 }