Merge branch 'master' into 'security-summit' branch.
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnGetListener.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 "JniOnGetListener.h"
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
25 #include "JniUtils.h"
26
27 JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
28     : m_ownerResource(owner)
29 {
30     m_jwListener = env->NewWeakGlobalRef(jListener);
31 }
32
33 JniOnGetListener::~JniOnGetListener()
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         m_jwListener = nullptr;
43
44         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
45     }
46 }
47
48 void JniOnGetListener::onGetCallback(const HeaderOptions& headerOptions,
49     const OCRepresentation& ocRepresentation, const int eCode)
50 {
51     jint envRet;
52     JNIEnv *env = GetJNIEnv(envRet);
53     if (NULL == 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
64     if (!clsL)
65     {
66         checkExAndRemoveListener(env);
67         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
68         return;
69     }
70
71     if (OC_STACK_OK != eCode)
72     {
73         jobject ex = GetOcException(eCode, "stack error in onGetCallback");
74         if (!ex)
75         {
76             checkExAndRemoveListener(env);
77             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
78             return;
79         }
80         jmethodID midL = env->GetMethodID(clsL, "onGetFailed", "(Ljava/lang/Throwable;)V");
81         if (!midL)
82         {
83             checkExAndRemoveListener(env);
84             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
85             return;
86         }
87         env->CallVoidMethod(jListener, midL, ex);
88     }
89     else
90     {
91         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
92         if (!jHeaderOptionList)
93         {
94             checkExAndRemoveListener(env);
95             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
96             return;
97         }
98
99         OCRepresentation* rep = new OCRepresentation(ocRepresentation);
100         jlong handle = reinterpret_cast<jlong>(rep);
101         jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
102             handle, true);
103         if (!jRepresentation)
104         {
105             delete rep;
106             checkExAndRemoveListener(env);
107             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
108             return;
109         }
110
111         jmethodID midL = env->GetMethodID(clsL, "onGetCompleted",
112             "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
113         if (!midL)
114         {
115             delete rep;
116             checkExAndRemoveListener(env);
117             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
118             return;
119         }
120         env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
121         if (env->ExceptionCheck())
122         {
123             LOGE("Java exception is thrown");
124             delete rep;
125         }
126     }
127
128     checkExAndRemoveListener(env);
129     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
130 }
131
132 void JniOnGetListener::checkExAndRemoveListener(JNIEnv* env)
133 {
134     if (env->ExceptionCheck())
135     {
136         jthrowable ex = env->ExceptionOccurred();
137         env->ExceptionClear();
138         m_ownerResource->removeOnGetListener(env, m_jwListener);
139         env->Throw((jthrowable)ex);
140     }
141     else
142     {
143         m_ownerResource->removeOnGetListener(env, m_jwListener);
144     }
145 }