Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnPublishResourceListener.cpp
1 /* ****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20 #include "JniOnPublishResourceListener.h"
21
22 JniOnPublishResourceListener::JniOnPublishResourceListener(JNIEnv *env, jobject jListener,
23     RemoveListenerCallback removeListenerCallback)
24 {
25     m_jwListener = env->NewWeakGlobalRef(jListener);
26     m_removeListenerCallback = removeListenerCallback;
27 }
28
29 JniOnPublishResourceListener::~JniOnPublishResourceListener()
30 {
31     LOGI("~JniOnPublishResourceListener()");
32     if (m_jwListener)
33     {
34         jint ret = JNI_ERR;
35         JNIEnv *env = GetJNIEnv(ret);
36         if (nullptr == env)
37         {
38             return;
39         }
40         env->DeleteWeakGlobalRef(m_jwListener);
41         m_jwListener = nullptr;
42         if (JNI_EDETACHED == ret)
43         {
44             g_jvm->DetachCurrentThread();
45         }
46     }
47 }
48
49 void JniOnPublishResourceListener::onPublishResourceCallback(
50         const OCRepresentation& ocRepresentation,
51         const int eCode)
52 {
53     jint envRet = JNI_ERR;
54     JNIEnv *env = GetJNIEnv(envRet);
55     if (nullptr == env)
56     {
57         return;
58     }
59
60     jobject jListener = env->NewLocalRef(m_jwListener);
61     if (!jListener)
62     {
63         checkExAndRemoveListener(env);
64         if (JNI_EDETACHED == envRet)
65         {
66             g_jvm->DetachCurrentThread();
67         }
68         return;
69     }
70     jclass clsL = env->GetObjectClass(jListener);
71
72     if (!clsL)
73     {
74         checkExAndRemoveListener(env);
75         if (JNI_EDETACHED == envRet)
76         {
77             g_jvm->DetachCurrentThread();
78         }
79         return;
80     }
81
82     if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode
83             && OC_STACK_RESOURCE_DELETED != eCode && OC_STACK_CONTINUE != eCode
84             && OC_STACK_RESOURCE_CHANGED != eCode)
85     {
86         jobject ex = GetOcException(eCode, "stack error in onPublishResourceCallback");
87         if (!ex)
88         {
89             goto CLEANUP;
90         }
91         jmethodID midL = env->GetMethodID(clsL, "onPublishResourceFailed",
92             "(Ljava/lang/Throwable;)V");
93         if (!midL)
94         {
95             goto CLEANUP;
96         }
97         env->CallVoidMethod(jListener, midL, ex);
98     }
99     else
100     {
101         OCRepresentation* rep = new OCRepresentation(ocRepresentation);
102         jlong handle = reinterpret_cast<jlong>(rep);
103         jobject jRepresentation = env->NewObject(g_cls_OcRepresentation,
104                                                  g_mid_OcRepresentation_N_ctor_bool,
105                                                  handle, true);
106         if (!jRepresentation)
107         {
108             delete rep;
109             goto CLEANUP;
110         }
111
112         jmethodID midL = env->GetMethodID(clsL, "onPublishResourceCompleted",
113             "(Lorg/iotivity/base/OcRepresentation;)V");
114         if (!midL)
115         {
116             delete rep;
117             goto CLEANUP;
118         }
119         env->CallVoidMethod(jListener, midL, jRepresentation);
120         if (env->ExceptionCheck())
121         {
122             LOGE("Java exception is thrown");
123             delete rep;
124         }
125     }
126
127 CLEANUP:
128     checkExAndRemoveListener(env);
129     if (JNI_EDETACHED == envRet)
130     {
131         g_jvm->DetachCurrentThread();
132     }
133 }
134
135 void JniOnPublishResourceListener::checkExAndRemoveListener(JNIEnv* env)
136 {
137     if (env->ExceptionCheck())
138     {
139         jthrowable ex = env->ExceptionOccurred();
140         env->ExceptionClear();
141         m_removeListenerCallback(env, m_jwListener);
142         env->Throw((jthrowable)ex);
143     }
144     else
145     {
146         m_removeListenerCallback(env, m_jwListener);
147     }
148 }