Merge tizen_5.0 codes into tizen_4.0
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnPutListener.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 "JniOnPutListener.h"
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
25 #include "JniUtils.h"
26
27 JniOnPutListener::JniOnPutListener(JNIEnv *env, jobject jListener, RemoveListenerCallback removeListener)
28     : m_removeListener(removeListener)
29 {
30     m_jwListener = env->NewWeakGlobalRef(jListener);
31 }
32
33 JniOnPutListener::~JniOnPutListener()
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 JniOnPutListener::onPutCallback(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_CHANGED != eCode)
86     {
87         jobject ex = GetOcException(eCode, "stack error in onPutCallback");
88         if (!ex)
89         {
90             checkExAndRemoveListener(env);
91             if (JNI_EDETACHED == envRet)
92             {
93                 g_jvm->DetachCurrentThread();
94             }
95             return;
96         }
97         jmethodID midL = env->GetMethodID(clsL, "onPutFailed", "(Ljava/lang/Throwable;)V");
98         if (!midL)
99         {
100             checkExAndRemoveListener(env);
101             if (JNI_EDETACHED == envRet)
102             {
103                 g_jvm->DetachCurrentThread();
104             }
105             return;
106         }
107         env->CallVoidMethod(jListener, midL, ex);
108     }
109     else
110     {
111         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
112         if (!jHeaderOptionList)
113         {
114             checkExAndRemoveListener(env);
115             if (JNI_EDETACHED == envRet)
116             {
117                 g_jvm->DetachCurrentThread();
118             }
119             return;
120         }
121
122         OCRepresentation * rep = new OCRepresentation(ocRepresentation);
123         jlong handle = reinterpret_cast<jlong>(rep);
124         jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
125             handle, true);
126         if (!jRepresentation)
127         {
128             delete rep;
129             checkExAndRemoveListener(env);
130             if (JNI_EDETACHED == envRet)
131             {
132                 g_jvm->DetachCurrentThread();
133             }
134             return;
135         }
136
137         jmethodID midL = env->GetMethodID(clsL, "onPutCompleted",
138             "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
139
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 JniOnPutListener::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 }