Clean up some SonarQube warnings (trailing whitespace, etc).
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnDeleteListener.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
23 #include "JniOnDeleteListener.h"
24 #include "JniOcResource.h"
25 #include "JniUtils.h"
26
27 JniOnDeleteListener::JniOnDeleteListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
28     : m_ownerResource(owner)
29 {
30     m_jwListener = env->NewWeakGlobalRef(jListener);
31 }
32
33 JniOnDeleteListener::~JniOnDeleteListener()
34 {
35     if (m_jwListener)
36     {
37         jint ret;
38         JNIEnv *env = GetJNIEnv(ret);
39         if (NULL == env) return;
40         env->DeleteWeakGlobalRef(m_jwListener);
41         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
42     }
43 }
44
45 void JniOnDeleteListener::onDeleteCallback(const HeaderOptions& headerOptions, const int eCode)
46 {
47     jint envRet;
48     JNIEnv *env = GetJNIEnv(envRet);
49     if (NULL == env) return;
50
51     jobject jListener = env->NewLocalRef(m_jwListener);
52     if (!jListener)
53     {
54         checkExAndRemoveListener(env);
55         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
56         return;
57     }
58     jclass clsL = env->GetObjectClass(jListener);
59     if (!clsL)
60     {
61         checkExAndRemoveListener(env);
62         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
63         return;
64     }
65
66     if (OC_STACK_RESOURCE_DELETED != eCode)
67     {
68         jobject ex = GetOcException(eCode, "stack error in onDeleteCallback");
69         if (!ex)
70         {
71             checkExAndRemoveListener(env);
72             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
73             return;
74         }
75         jmethodID midL = env->GetMethodID(clsL, "onDeleteFailed", "(Ljava/lang/Throwable;)V");
76         if (!midL)
77         {
78             checkExAndRemoveListener(env);
79             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
80             return;
81         }
82         env->CallVoidMethod(jListener, midL, ex);
83     }
84     else
85     {
86         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
87         if (!jHeaderOptionList)
88         {
89             checkExAndRemoveListener(env);
90             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
91             return;
92         }
93
94         jmethodID midL = env->GetMethodID(clsL, "onDeleteCompleted", "(Ljava/util/List;)V");
95         if (!midL)
96         {
97             checkExAndRemoveListener(env);
98             if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
99             return;
100         }
101         env->CallVoidMethod(jListener, midL, jHeaderOptionList);
102     }
103
104     checkExAndRemoveListener(env);
105     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
106 }
107
108 void JniOnDeleteListener::checkExAndRemoveListener(JNIEnv* env)
109 {
110     if (env->ExceptionCheck())
111     {
112         jthrowable ex = env->ExceptionOccurred();
113         env->ExceptionClear();
114         m_ownerResource->removeOnDeleteListener(env, m_jwListener);
115         env->Throw((jthrowable)ex);
116     }
117     else
118     {
119         m_ownerResource->removeOnDeleteListener(env, m_jwListener);
120     }
121 }