resource: Add test for ConstructResourceObject without coap scheme
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / jni / tm / src / jni_group_synchronization_callbacks.cpp
1 /* *****************************************************************
2  *
3  * Copyright 2015 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
21 #include "jni_group_synchronization_callbacks.h"
22 #include "JniOcResource.h"
23 #include "GroupSynchronization.h"
24 #include "jni_things_manager_jvm.h"
25 #include "jni_things_manager_util.h"
26 #include "jni_string.h"
27
28 #define LOG_TAG "GroupSynchronizationCallbacks"
29
30 #define METHOD_ONGROUP_FIND_CALLBACK    "(" TM_SERVICE_OCRESOURCE_TYPE")V"
31
32 void GroupSynchronizationCallbacks::onFoundGroup(std::shared_ptr<OC::OCResource> groupResource)
33 {
34     LOGI("FindGroup : Enter");
35
36     if (NULL == groupResource.get())
37     {
38         LOGE("FindGroup : Invalid received GroupResource!");
39         return;
40     }
41
42     JNIEnv *env = ThingsManagerJVM::getEnv();
43     if (env == NULL)
44     {
45         LOGE("FindGroup : Getting JNIEnv failed");
46         return;
47     }
48
49     // Get ThingsManagerCallback class reference
50     jclass groupSynchronizationCallbacks = GetJClass(TM_SERVICE_GROUP_SYNCHRONIZATION_CLASS_PATH);
51     if (NULL == groupSynchronizationCallbacks)
52     {
53         LOGE("FindGroup : GetJClass TMServiceCallbackInterface failed");
54         ThingsManagerJVM::releaseEnv();
55         return;
56     }
57
58     // Get the ThingsManagerCallback class instance
59     jobject jobjectCallback = GetJObjectInstance(TM_SERVICE_GROUP_SYNCHRONIZATION_CLASS_PATH);
60     if (NULL == jobjectCallback)
61     {
62         LOGE("FindGroup: getInstance failed!");
63         ThingsManagerJVM::releaseEnv();
64         return;
65     }
66
67     // Get onGroupFindCallback method reference
68     jmethodID method_id = env->GetMethodID(groupSynchronizationCallbacks,
69                                            "onGroupFindCallback",
70                                            METHOD_ONGROUP_FIND_CALLBACK);
71     if (NULL == method_id)
72     {
73         LOGE("FindGroup : GetMethodID failed");
74         ThingsManagerJVM::releaseEnv();
75         return;
76     }
77
78     if ((env)->ExceptionCheck())
79     {
80         LOGE("FindGroup : ExceptionCheck failed");
81         ThingsManagerJVM::releaseEnv();
82         return;
83     }
84
85     JniOcResource *jniOcResource = new JniOcResource(groupResource);
86     if (!jniOcResource)
87     {
88         LOGE("FindGroup : groupResource is invalid!");
89         ThingsManagerJVM::releaseEnv();
90         return;
91     }
92
93     jobject resource = OcResourceToJava(env, reinterpret_cast<jlong>(jniOcResource));
94
95     env->CallVoidMethod(jobjectCallback, method_id, resource);
96
97     if ((env)->ExceptionCheck())
98     {
99         LOGE("FindGroup : CallVoidMethod failed");
100         ThingsManagerJVM::releaseEnv();
101         return;
102     }
103
104     ThingsManagerJVM::releaseEnv();
105     LOGI("FindGroup : Exit");
106 }