Merge tizen_5.0 codes into tizen_4.0
[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, RemoveListenerCallback removeListener)
28     : m_removeListener(removeListener)
29 {
30     m_jwListener = env->NewWeakGlobalRef(jListener);
31 }
32
33 JniOnGetListener::~JniOnGetListener()
34 {
35     LOGD("~JniOnGetListener");
36     if (m_jwListener)
37     {
38         jint ret = JNI_ERR;
39         JNIEnv *env = GetJNIEnv(ret);
40         if (nullptr == env)
41         {
42             return;
43         }
44
45         env->DeleteWeakGlobalRef(m_jwListener);
46         m_jwListener = nullptr;
47
48         if (JNI_EDETACHED == ret)
49         {
50             g_jvm->DetachCurrentThread();
51         }
52     }
53 }
54
55 void JniOnGetListener::onGetCallback(const HeaderOptions& headerOptions,
56     const OCRepresentation& ocRepresentation, const int eCode)
57 {
58     jint envRet = JNI_ERR;
59     JNIEnv *env = GetJNIEnv(envRet);
60     if (nullptr == env)
61     {
62         return;
63     }
64
65     jobject jListener = env->NewLocalRef(m_jwListener);
66     if (!jListener)
67     {
68         checkExAndRemoveListener(env);
69         if (JNI_EDETACHED == envRet)
70         {
71             g_jvm->DetachCurrentThread();
72         }
73         return;
74     }
75     jclass clsL = env->GetObjectClass(jListener);
76
77     if (!clsL)
78     {
79         checkExAndRemoveListener(env);
80         if (JNI_EDETACHED == envRet)
81         {
82             g_jvm->DetachCurrentThread();
83         }
84         return;
85     }
86
87     if (OC_STACK_OK != eCode)
88     {
89         jobject ex = GetOcException(eCode, "stack error in onGetCallback");
90         if (!ex)
91         {
92             checkExAndRemoveListener(env);
93             if (JNI_EDETACHED == envRet)
94             {
95                 g_jvm->DetachCurrentThread();
96             }
97             return;
98         }
99         jmethodID midL = env->GetMethodID(clsL, "onGetFailed", "(Ljava/lang/Throwable;)V");
100         if (!midL)
101         {
102             checkExAndRemoveListener(env);
103             if (JNI_EDETACHED == envRet)
104             {
105                 g_jvm->DetachCurrentThread();
106             }
107             return;
108         }
109         env->CallVoidMethod(jListener, midL, ex);
110     }
111     else
112     {
113         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
114         if (!jHeaderOptionList)
115         {
116             checkExAndRemoveListener(env);
117             if (JNI_EDETACHED == envRet)
118             {
119                 g_jvm->DetachCurrentThread();
120             }
121             return;
122         }
123
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,
127             handle, true);
128         if (!jRepresentation)
129         {
130             delete rep;
131             checkExAndRemoveListener(env);
132             if (JNI_EDETACHED == envRet)
133             {
134                 g_jvm->DetachCurrentThread();
135             }
136             return;
137         }
138
139         jmethodID midL = env->GetMethodID(clsL, "onGetCompleted",
140             "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
141         if (!midL)
142         {
143             delete rep;
144             checkExAndRemoveListener(env);
145             if (JNI_EDETACHED == envRet)
146             {
147                 g_jvm->DetachCurrentThread();
148             }
149             return;
150         }
151         env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
152         if (env->ExceptionCheck())
153         {
154             LOGE("Java exception is thrown");
155             delete rep;
156         }
157     }
158
159     checkExAndRemoveListener(env);
160     if (JNI_EDETACHED == envRet)
161     {
162         g_jvm->DetachCurrentThread();
163     }
164 }
165
166 void JniOnGetListener::checkExAndRemoveListener(JNIEnv* env)
167 {
168     if (env->ExceptionCheck())
169     {
170         jthrowable ex = env->ExceptionOccurred();
171         env->ExceptionClear();
172         m_removeListener(env, m_jwListener);
173         env->Throw((jthrowable)ex);
174     }
175     else
176     {
177         m_removeListener(env, m_jwListener);
178     }
179 }