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"
29 #include "RCSResourceObject.h"
31 #define LOG_TAG "JNI-RCSLockedAttributes"
33 using namespace OIC::Service;
37 inline RCSResourceObject::Ptr& getResource(JNIEnv* env, jobject obj)
39 return getNativeHandleAs< RCSResourceObject::Ptr >(env, obj);
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.
46 JNIEXPORT jboolean JNICALL
47 Java_org_iotivity_service_server_RcsLockedAttributes_nativeIsEmpty
48 (JNIEnv* env, jclass, jobject resourceObject)
50 auto res = getResource(env, resourceObject);
51 VERIFY_NO_EXC_RET_DEF(env);
53 return res->getAttributes().empty();
56 JNIEXPORT jint JNICALL
57 Java_org_iotivity_service_server_RcsLockedAttributes_nativeSize
58 (JNIEnv* env, jclass, jobject resourceObject)
60 auto res = getResource(env, resourceObject);
61 VERIFY_NO_EXC_RET_DEF(env);
63 return res->getAttributes().size();
66 JNIEXPORT jboolean JNICALL
67 Java_org_iotivity_service_server_RcsLockedAttributes_nativeRemove
68 (JNIEnv* env, jclass, jobject resourceObject, jstring keyObj)
70 EXPECT_RET_DEF(keyObj, "keyObj is null");
72 auto res = getResource(env, resourceObject);
73 VERIFY_NO_EXC_RET_DEF(env);
75 auto key = toStdString(env, keyObj);
76 VERIFY_NO_EXC_RET_DEF(env);
78 return res->getAttributes().erase(key);
81 JNIEXPORT void JNICALL
82 Java_org_iotivity_service_server_RcsLockedAttributes_nativeClear
83 (JNIEnv* env, jclass, jobject resourceObject)
85 auto res = getResource(env, resourceObject);
88 res->getAttributes().clear();
91 JNIEXPORT jboolean JNICALL
92 Java_org_iotivity_service_server_RcsLockedAttributes_nativeContains
93 (JNIEnv* env, jclass, jobject resourceObject, jstring keyObj)
95 EXPECT_RET_DEF(keyObj, "keyObj is null");
97 auto res = getResource(env, resourceObject);
98 VERIFY_NO_EXC_RET_DEF(env);
100 auto key = toStdString(env, keyObj);
101 VERIFY_NO_EXC_RET_DEF(env);
103 return res->getAttributes().contains(key);
106 JNIEXPORT void JNICALL
107 Java_org_iotivity_service_server_RcsLockedAttributes_nativeAddKeys
108 (JNIEnv* env, jclass, jobject resourceObject, jstring setObj)
110 EXPECT(setObj, "set is null.");
112 auto res = getResource(env, resourceObject);
115 for (const auto& keyValue : res->getAttributes())
117 JavaLocalString localObj{ env, env->NewStringUTF(keyValue.key().c_str()) };
120 invoke_Collection_add(env, setObj, localObj);
125 JNIEXPORT jobject JNICALL
126 Java_org_iotivity_service_server_RcsLockedAttributes_nativeAsJavaObject
127 (JNIEnv* env, jclass, jobject resourceObject, jstring keyObj)
129 EXPECT_RET_DEF(keyObj, "Key is null.");
131 auto res = getResource(env, resourceObject);
132 VERIFY_NO_EXC_RET_DEF(env);
134 auto key = toStdString(env, keyObj);
135 VERIFY_NO_EXC_RET_DEF(env);
137 auto& attrs = res->getAttributes();
139 EXPECT_RET_DEF(attrs.contains(key), "no matched value");
141 jobject valueObj = newRCSValueObject(env, attrs[key]);
142 VERIFY_NO_EXC_RET_DEF(env);
147 JNIEXPORT void JNICALL
148 Java_org_iotivity_service_server_RcsLockedAttributes_nativeApply
149 (JNIEnv* env, jclass, jobject resourceObject, jstring cacheObj)
151 EXPECT(cacheObj, "cacheObj is null.");
153 auto res = getResource(env, resourceObject);
156 writeNativeAttributesFromMap(env, cacheObj, res->getAttributes());
159 JNIEXPORT void JNICALL
160 Java_org_iotivity_service_server_RcsLockedAttributes_nativeLock
161 (JNIEnv* env, jobject obj, jobject resourceObject)
163 auto res = getResource(env, resourceObject);
166 setSafeNativeHandle< RCSResourceObject::LockGuard >(env, obj, res);
169 JNIEXPORT void JNICALL
170 Java_org_iotivity_service_server_RcsLockedAttributes_nativeUnlock(JNIEnv* env, jobject obj)
172 releaseNativeHandle(env, obj);