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