Merge remote-tracking branch 'origin/master' into notification-service
[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 #ifdef WITH_CLOUD
27 #include "JniOcAccountManager.h"
28 #endif
29
30 JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
31     : m_ownerResource(owner)
32 {
33     m_jwListener = env->NewWeakGlobalRef(jListener);
34 }
35
36 #ifdef WITH_CLOUD
37 JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner)
38     : m_ownerAccountManager(owner)
39 {
40     m_jwListener = env->NewWeakGlobalRef(jListener);
41 }
42 #endif
43
44 JniOnGetListener::~JniOnGetListener()
45 {
46     LOGD("~JniOnGetListener");
47     if (m_jwListener)
48     {
49         jint ret = JNI_ERR;
50         JNIEnv *env = GetJNIEnv(ret);
51         if (nullptr == env)
52         {
53             return;
54         }
55
56         env->DeleteWeakGlobalRef(m_jwListener);
57         m_jwListener = nullptr;
58
59         if (JNI_EDETACHED == ret)
60         {
61             g_jvm->DetachCurrentThread();
62         }
63     }
64 }
65
66 void JniOnGetListener::onGetCallback(const HeaderOptions& headerOptions,
67     const OCRepresentation& ocRepresentation, const int eCode)
68 {
69     jint envRet = JNI_ERR;
70     JNIEnv *env = GetJNIEnv(envRet);
71     if (nullptr == env)
72     {
73         return;
74     }
75
76     jobject jListener = env->NewLocalRef(m_jwListener);
77     if (!jListener)
78     {
79         checkExAndRemoveListener(env);
80         if (JNI_EDETACHED == envRet)
81         {
82             g_jvm->DetachCurrentThread();
83         }
84         return;
85     }
86     jclass clsL = env->GetObjectClass(jListener);
87
88     if (!clsL)
89     {
90         checkExAndRemoveListener(env);
91         if (JNI_EDETACHED == envRet)
92         {
93             g_jvm->DetachCurrentThread();
94         }
95         return;
96     }
97
98     if (OC_STACK_OK != eCode)
99     {
100         jobject ex = GetOcException(eCode, "stack error in onGetCallback");
101         if (!ex)
102         {
103             checkExAndRemoveListener(env);
104             if (JNI_EDETACHED == envRet)
105             {
106                 g_jvm->DetachCurrentThread();
107             }
108             return;
109         }
110         jmethodID midL = env->GetMethodID(clsL, "onGetFailed", "(Ljava/lang/Throwable;)V");
111         if (!midL)
112         {
113             checkExAndRemoveListener(env);
114             if (JNI_EDETACHED == envRet)
115             {
116                 g_jvm->DetachCurrentThread();
117             }
118             return;
119         }
120         env->CallVoidMethod(jListener, midL, ex);
121     }
122     else
123     {
124         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
125         if (!jHeaderOptionList)
126         {
127             checkExAndRemoveListener(env);
128             if (JNI_EDETACHED == envRet)
129             {
130                 g_jvm->DetachCurrentThread();
131             }
132             return;
133         }
134
135         OCRepresentation* rep = new OCRepresentation(ocRepresentation);
136         jlong handle = reinterpret_cast<jlong>(rep);
137         jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
138             handle, true);
139         if (!jRepresentation)
140         {
141             delete rep;
142             checkExAndRemoveListener(env);
143             if (JNI_EDETACHED == envRet)
144             {
145                 g_jvm->DetachCurrentThread();
146             }
147             return;
148         }
149
150         jmethodID midL = env->GetMethodID(clsL, "onGetCompleted",
151             "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
152         if (!midL)
153         {
154             delete rep;
155             checkExAndRemoveListener(env);
156             if (JNI_EDETACHED == envRet)
157             {
158                 g_jvm->DetachCurrentThread();
159             }
160             return;
161         }
162         env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
163         if (env->ExceptionCheck())
164         {
165             LOGE("Java exception is thrown");
166             delete rep;
167         }
168     }
169
170     checkExAndRemoveListener(env);
171     if (JNI_EDETACHED == envRet)
172     {
173         g_jvm->DetachCurrentThread();
174     }
175 }
176
177 void JniOnGetListener::checkExAndRemoveListener(JNIEnv* env)
178 {
179     if (env->ExceptionCheck())
180     {
181         jthrowable ex = env->ExceptionOccurred();
182         env->ExceptionClear();
183 #ifndef WITH_CLOUD
184         m_ownerResource->removeOnGetListener(env, m_jwListener);
185 #else
186         if (m_ownerResource)
187         {
188             m_ownerResource->removeOnGetListener(env, m_jwListener);
189         }
190         if (m_ownerAccountManager)
191         {
192             m_ownerAccountManager->removeOnGetListener(env, m_jwListener);
193         }
194 #endif
195         env->Throw((jthrowable)ex);
196     }
197     else
198     {
199 #ifndef WITH_CLOUD
200         m_ownerResource->removeOnGetListener(env, m_jwListener);
201 #else
202         if (m_ownerResource)
203         {
204             m_ownerResource->removeOnGetListener(env, m_jwListener);
205         }
206         if (m_ownerAccountManager)
207         {
208             m_ownerAccountManager->removeOnGetListener(env, m_jwListener);
209         }
210 #endif
211     }
212 }