Merge branch 'master' into 'security-basecamp'
[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;
38         JNIEnv *env = GetJNIEnv(ret);
39         if (NULL == env) return;
40
41         env->DeleteWeakGlobalRef(m_jwListener);
42
43         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
44     }
45 }
46
47 void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
48     const OCRepresentation& ocRepresentation, const int& eCode, const int& sequenceNumber)
49 {
50     jint envRet;
51     JNIEnv *env = GetJNIEnv(envRet);
52     if (NULL == env) return;
53
54     jobject jListener = env->NewLocalRef(m_jwListener);
55     if (!jListener)
56     {
57         checkExAndRemoveListener(env);
58         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
59         return;
60     }
61     jclass clsL = env->GetObjectClass(jListener);
62     if (!clsL)
63     {
64         checkExAndRemoveListener(env);
65         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
66         return;
67     }
68
69     if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode && OC_STACK_RESOURCE_DELETED != eCode)
70     {
71         jobject ex = GetOcException(eCode, "stack error in onObserveCallback");
72         if (!ex)
73         {
74             checkExAndRemoveListener(env);
75             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
76             return;
77         }
78         jmethodID midL = env->GetMethodID(clsL, "onObserveFailed", "(Ljava/lang/Throwable;)V");
79         if (!midL)
80         {
81             checkExAndRemoveListener(env);
82             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
83             return;
84         }
85         env->CallVoidMethod(jListener, midL, ex);
86     }
87     else
88     {
89         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
90         if (!jHeaderOptionList)
91         {
92             checkExAndRemoveListener(env);
93             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
94             return;
95         }
96
97         OCRepresentation * rep = new OCRepresentation(ocRepresentation);
98         jlong handle = reinterpret_cast<jlong>(rep);
99         jobject jRepresentation = env->NewObject(g_cls_OcRepresentation,
100             g_mid_OcRepresentation_N_ctor_bool, handle, true);
101         if (!jRepresentation)
102         {
103             delete rep;
104             checkExAndRemoveListener(env);
105             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
106             return;
107         }
108
109         jmethodID midL = env->GetMethodID(clsL, "onObserveCompleted",
110             "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;I)V");
111         if (!midL)
112         {
113             checkExAndRemoveListener(env);
114             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
115             return;
116         }
117
118         env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation,
119             static_cast<jint>(sequenceNumber));
120         if (env->ExceptionCheck())
121         {
122             LOGE("Java exception is thrown");
123             delete rep;
124             jthrowable ex = env->ExceptionOccurred();
125             env->ExceptionClear();
126             m_ownerResource->removeOnObserveListener(env, m_jwListener);
127             env->Throw((jthrowable)ex);
128         }
129     }
130
131     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
132 }
133
134 void JniOnObserveListener::checkExAndRemoveListener(JNIEnv* env)
135 {
136     if (env->ExceptionCheck())
137     {
138         jthrowable ex = env->ExceptionOccurred();
139         env->ExceptionClear();
140         m_ownerResource->removeOnObserveListener(env, m_jwListener);
141         env->Throw((jthrowable)ex);
142     }
143     else
144     {
145         m_ownerResource->removeOnObserveListener(env, m_jwListener);
146     }
147 }