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