1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "JniRcsLockedAttributes.h"
23 #include "JniRcsObject.h"
24 #include "JniRcsResourceAttributes.h"
25 #include "JniRcsValue.h"
28 #include "JavaExceptions.h"
30 #include "RCSResourceObject.h"
32 #define LOG_TAG "JNI-RCSLockedAttributes"
34 using namespace OIC::Service;
38 inline RCSResourceObject::Ptr& getResource(JNIEnv* env, jobject obj)
40 return getNativeHandleAs< RCSResourceObject::Ptr >(env, obj);
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.
47 JNIEXPORT jboolean JNICALL
48 Java_org_iotivity_service_server_RcsLockedAttributes_nativeIsEmpty
49 (JNIEnv* env, jclass, jobject resourceObject)
51 auto res = getResource(env, resourceObject);
52 VERIFY_NO_EXC_RET_DEF(env);
54 return res->getAttributes().empty();
57 JNIEXPORT jint JNICALL
58 Java_org_iotivity_service_server_RcsLockedAttributes_nativeSize
59 (JNIEnv* env, jclass, jobject resourceObject)
61 auto res = getResource(env, resourceObject);
62 VERIFY_NO_EXC_RET_DEF(env);
64 return res->getAttributes().size();
67 JNIEXPORT jboolean JNICALL
68 Java_org_iotivity_service_server_RcsLockedAttributes_nativeRemove
69 (JNIEnv* env, jclass, jobject resourceObject, jstring keyObj)
71 EXPECT_RET_DEF(keyObj, "keyObj is null");
73 auto res = getResource(env, resourceObject);
74 VERIFY_NO_EXC_RET_DEF(env);
76 auto key = toStdString(env, keyObj);
77 VERIFY_NO_EXC_RET_DEF(env);
79 return res->getAttributes().erase(key);
82 JNIEXPORT void JNICALL
83 Java_org_iotivity_service_server_RcsLockedAttributes_nativeClear
84 (JNIEnv* env, jclass, jobject resourceObject)
86 auto res = getResource(env, resourceObject);
89 res->getAttributes().clear();
92 JNIEXPORT jboolean JNICALL
93 Java_org_iotivity_service_server_RcsLockedAttributes_nativeContains
94 (JNIEnv* env, jclass, jobject resourceObject, jstring keyObj)
96 EXPECT_RET_DEF(keyObj, "keyObj is null");
98 auto res = getResource(env, resourceObject);
99 VERIFY_NO_EXC_RET_DEF(env);
101 auto key = toStdString(env, keyObj);
102 VERIFY_NO_EXC_RET_DEF(env);
104 return res->getAttributes().contains(key);
107 JNIEXPORT void JNICALL
108 Java_org_iotivity_service_server_RcsLockedAttributes_nativeAddKeys
109 (JNIEnv* env, jclass, jobject resourceObject, jstring setObj)
111 EXPECT(setObj, "set is null.");
113 auto res = getResource(env, resourceObject);
116 for (const auto& keyValue : res->getAttributes())
118 JavaLocalString localObj{ env, env->NewStringUTF(keyValue.key().c_str()) };
121 invoke_Collection_add(env, setObj, localObj);
126 JNIEXPORT jobject JNICALL
127 Java_org_iotivity_service_server_RcsLockedAttributes_nativeAsJavaObject
128 (JNIEnv* env, jclass, jobject resourceObject, jstring keyObj)
130 EXPECT_RET_DEF(keyObj, "Key is null.");
132 auto res = getResource(env, resourceObject);
133 VERIFY_NO_EXC_RET_DEF(env);
135 auto key = toStdString(env, keyObj);
136 VERIFY_NO_EXC_RET_DEF(env);
138 auto& attrs = res->getAttributes();
140 EXPECT_RET_DEF(attrs.contains(key), "no matched value");
142 jobject valueObj = newRCSValueObject(env, attrs[key]);
143 VERIFY_NO_EXC_RET_DEF(env);
148 JNIEXPORT void JNICALL
149 Java_org_iotivity_service_server_RcsLockedAttributes_nativeApply
150 (JNIEnv* env, jclass, jobject resourceObject, jstring cacheObj)
152 EXPECT(cacheObj, "cacheObj is null.");
154 auto res = getResource(env, resourceObject);
159 RCSResourceObject::LockGuard lock(res);
160 writeNativeAttributesFromMap(env, cacheObj, res->getAttributes());
162 catch (const RCSPlatformException& e)
164 throwPlatformException(env, e);
168 JNIEXPORT void JNICALL
169 Java_org_iotivity_service_server_RcsLockedAttributes_nativeLock
170 (JNIEnv* env, jobject obj, jobject resourceObject)
172 auto res = getResource(env, resourceObject);
175 setSafeNativeHandle< RCSResourceObject::LockGuard >(env, obj,
176 res, RCSResourceObject::AutoNotifyPolicy::NEVER);
179 JNIEXPORT void JNICALL
180 Java_org_iotivity_service_server_RcsLockedAttributes_nativeUnlock(JNIEnv* env, jobject obj)
182 releaseNativeHandle(env, obj);