Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-encapsulation / android / service / src / main / jni / JniRcsLockedAttributes.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 "JniRcsLockedAttributes.h"
22
23 #include "JniRcsObject.h"
24 #include "JniRcsResourceAttributes.h"
25 #include "JniRcsValue.h"
26 #include "Log.h"
27 #include "Verify.h"
28
29 #include "RCSResourceObject.h"
30
31 #define LOG_TAG "JNI-RCSLockedAttributes"
32
33 using namespace OIC::Service;
34
35 namespace
36 {
37     inline RCSResourceObject::Ptr& getResource(JNIEnv* env, jobject obj)
38     {
39         return getNativeHandleAs< RCSResourceObject::Ptr >(env, obj);
40     }
41 }
42
43 // The prerequisite for below methods is for ResourceObject's attributes being locked.
44 // This is guaranteed by class named RCSResourceObject.AttributesLock on Java layer.
45
46 JNIEXPORT jboolean JNICALL
47 Java_org_iotivity_service_server_RcsLockedAttributes_nativeIsEmpty
48 (JNIEnv* env, jclass, jobject resourceObject)
49 {
50     auto res = getResource(env, resourceObject);
51     VERIFY_NO_EXC_RET_DEF(env);
52
53     return res->getAttributes().empty();
54 }
55
56 JNIEXPORT jint JNICALL
57 Java_org_iotivity_service_server_RcsLockedAttributes_nativeSize
58 (JNIEnv* env, jclass, jobject resourceObject)
59 {
60     auto res = getResource(env, resourceObject);
61     VERIFY_NO_EXC_RET_DEF(env);
62
63     return res->getAttributes().size();
64 }
65
66 JNIEXPORT jboolean JNICALL
67 Java_org_iotivity_service_server_RcsLockedAttributes_nativeRemove
68 (JNIEnv* env, jclass, jobject resourceObject, jstring keyObj)
69 {
70     EXPECT_RET_DEF(keyObj, "keyObj is null");
71
72     auto res = getResource(env, resourceObject);
73     VERIFY_NO_EXC_RET_DEF(env);
74
75     auto key = toStdString(env, keyObj);
76     VERIFY_NO_EXC_RET_DEF(env);
77
78     return res->getAttributes().erase(key);
79 }
80
81 JNIEXPORT void JNICALL
82 Java_org_iotivity_service_server_RcsLockedAttributes_nativeClear
83 (JNIEnv* env, jclass, jobject resourceObject)
84 {
85     auto res = getResource(env, resourceObject);
86     VERIFY_NO_EXC(env);
87
88     res->getAttributes().clear();
89 }
90
91 JNIEXPORT jboolean JNICALL
92 Java_org_iotivity_service_server_RcsLockedAttributes_nativeContains
93 (JNIEnv* env, jclass, jobject resourceObject, jstring keyObj)
94 {
95     EXPECT_RET_DEF(keyObj, "keyObj is null");
96
97     auto res = getResource(env, resourceObject);
98     VERIFY_NO_EXC_RET_DEF(env);
99
100     auto key = toStdString(env, keyObj);
101     VERIFY_NO_EXC_RET_DEF(env);
102
103     return res->getAttributes().contains(key);
104 }
105
106 JNIEXPORT void JNICALL
107 Java_org_iotivity_service_server_RcsLockedAttributes_nativeAddKeys
108 (JNIEnv* env, jclass, jobject resourceObject, jstring setObj)
109 {
110     EXPECT(setObj, "set is null.");
111
112     auto res = getResource(env, resourceObject);
113     VERIFY_NO_EXC(env);
114
115     for (const auto& keyValue : res->getAttributes())
116     {
117         JavaLocalString localObj{ env, env->NewStringUTF(keyValue.key().c_str()) };
118         VERIFY_NO_EXC(env);
119
120         invoke_Collection_add(env, setObj, localObj);
121         VERIFY_NO_EXC(env);
122     }
123 }
124
125 JNIEXPORT jobject JNICALL
126 Java_org_iotivity_service_server_RcsLockedAttributes_nativeAsJavaObject
127 (JNIEnv* env, jclass, jobject resourceObject, jstring keyObj)
128 {
129     EXPECT_RET_DEF(keyObj, "Key is null.");
130
131     auto res = getResource(env, resourceObject);
132     VERIFY_NO_EXC_RET_DEF(env);
133
134     auto key = toStdString(env, keyObj);
135     VERIFY_NO_EXC_RET_DEF(env);
136
137     auto& attrs = res->getAttributes();
138
139     EXPECT_RET_DEF(attrs.contains(key), "no matched value");
140
141     jobject valueObj = newRCSValueObject(env, attrs[key]);
142     VERIFY_NO_EXC_RET_DEF(env);
143
144     return valueObj;
145 }
146
147 JNIEXPORT void JNICALL
148 Java_org_iotivity_service_server_RcsLockedAttributes_nativeApply
149 (JNIEnv* env, jclass, jobject resourceObject, jstring cacheObj)
150 {
151     EXPECT(cacheObj, "cacheObj is null.");
152
153     auto res = getResource(env, resourceObject);
154     VERIFY_NO_EXC(env);
155
156     writeNativeAttributesFromMap(env, cacheObj, res->getAttributes());
157 }
158
159 JNIEXPORT void JNICALL
160 Java_org_iotivity_service_server_RcsLockedAttributes_nativeLock
161 (JNIEnv* env, jobject obj, jobject resourceObject)
162 {
163     auto res = getResource(env, resourceObject);
164     VERIFY_NO_EXC(env);
165
166     setSafeNativeHandle< RCSResourceObject::LockGuard >(env, obj, res);
167 }
168
169 JNIEXPORT void JNICALL
170 Java_org_iotivity_service_server_RcsLockedAttributes_nativeUnlock(JNIEnv* env, jobject obj)
171 {
172     releaseNativeHandle(env, obj);
173 }