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