Merge branch 'master' into notification-service
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnObserveListener.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 "JniOnObserveListener.h"
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
25 #include "JniUtils.h"
26
27 JniOnObserveListener::JniOnObserveListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
28     : m_ownerResource(owner)
29 {
30     m_jwListener = env->NewWeakGlobalRef(jListener);
31 }
32
33 JniOnObserveListener::~JniOnObserveListener()
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 JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
55     const OCRepresentation& ocRepresentation, const int& eCode, const int& sequenceNumber)
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_DELETED != eCode && OC_STACK_RESOURCE_CHANGED != eCode)
87     {
88         jobject ex = GetOcException(eCode, "stack error in onObserveCallback");
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, "onObserveFailed", "(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,
126             g_mid_OcRepresentation_N_ctor_bool, 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, "onObserveCompleted",
139             "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;I)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             static_cast<jint>(sequenceNumber));
152         if (env->ExceptionCheck())
153         {
154             LOGE("Java exception is thrown");
155             delete rep;
156             jthrowable ex = env->ExceptionOccurred();
157             env->ExceptionClear();
158             m_ownerResource->removeOnObserveListener(env, m_jwListener);
159             env->Throw((jthrowable)ex);
160         }
161
162         if (OC_OBSERVE_DEREGISTER == sequenceNumber)
163         {
164             checkExAndRemoveListener(env);
165         }
166     }
167
168     if (JNI_EDETACHED == envRet)
169     {
170         g_jvm->DetachCurrentThread();
171     }
172 }
173
174 void JniOnObserveListener::checkExAndRemoveListener(JNIEnv* env)
175 {
176     if (env->ExceptionCheck())
177     {
178         jthrowable ex = env->ExceptionOccurred();
179         env->ExceptionClear();
180         m_ownerResource->removeOnObserveListener(env, m_jwListener);
181         env->Throw((jthrowable)ex);
182     }
183     else
184     {
185         m_ownerResource->removeOnObserveListener(env, m_jwListener);
186     }
187 }