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