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 "JniRcsResourceAttributes.h"
23 #include "JniRcsObject.h"
24 #include "JavaLocalRef.h"
25 #include "JniRcsValue.h"
29 using namespace OIC::Service;
31 #define LOG_TAG "JNI-RCSResourceAttributes"
35 jclass g_cls_RCSResourceAttributes;
37 jmethodID g_ctor_RCSResourceAttributes;
39 jfieldID g_field_mCache;
42 void initRCSResourceAttributes(JNIEnvWrapper* env)
44 g_cls_RCSResourceAttributes = env->FindClassAsGlobalRef(CLS_NAME_RESOURCEATTRIBUTES);
45 g_ctor_RCSResourceAttributes = env->GetConstructorID(g_cls_RCSResourceAttributes, "()V");
47 g_field_mCache = env->GetFieldID(g_cls_RCSResourceAttributes, "mCache", AS_SIG(CLS_NAME_MAP));
50 void clearRCSResourceAttributes(JNIEnvWrapper* env)
52 env->DeleteGlobalRef(g_cls_RCSResourceAttributes);
55 jobject newAttributesObject(JNIEnv* env, const RCSResourceAttributes& attrs)
57 jobject obj = env->NewObject(g_cls_RCSResourceAttributes, g_ctor_RCSResourceAttributes);
58 VERIFY_NO_EXC_RET_DEF(env);
60 setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
65 jobject newAttributesObject(JNIEnvWrapper* env, const RCSResourceAttributes& attrs)
67 jobject obj = env->NewObject(g_cls_RCSResourceAttributes, g_ctor_RCSResourceAttributes);
69 setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
74 RCSResourceAttributes toNativeAttributes(JNIEnv* env, jobject attrsObj)
76 EXPECT_RET(attrsObj, "attrsObj is null!", { });
78 JNIEnvWrapper envWrapper{ env };
82 return toNativeAttributes(&envWrapper, attrsObj);
84 catch (const JavaException&)
90 RCSResourceAttributes toNativeAttributes(JNIEnvWrapper* env, jobject attrsObj)
92 EXPECT_RET(attrsObj, "attrsObj is null!", { });
94 RCSResourceAttributes attrs;
96 if (hasNativeHandle(env, attrsObj))
98 attrs = getNativeHandleAs< RCSResourceAttributes >(env, attrsObj);
101 writeNativeAttributesFromMap(env,
102 JavaLocalObject{ env, env->GetObjectField(attrsObj, g_field_mCache) }, attrs);
107 void writeNativeAttributesFromMap(JNIEnv* env, jobject mapObj, RCSResourceAttributes& targetAttrs)
109 JNIEnvWrapper envWrapper{ env };
113 return writeNativeAttributesFromMap(&envWrapper, mapObj, targetAttrs);
115 catch (const JavaException&)
120 void writeNativeAttributesFromMap(JNIEnvWrapper* env, jobject mapObj,
121 RCSResourceAttributes& targetAttrs)
123 JavaLocalObject setObj{ env, invoke_Map_entrySet(env, mapObj) };
124 JavaLocalObject iterObj{ env, invoke_Set_iterator(env, setObj) };
126 while (invoke_Iterator_hasNext(env, iterObj))
128 JavaLocalObject entryObj{ env, invoke_Iterator_next(env, iterObj) };
129 JavaLocalObject keyObj{ env, invoke_MapEntry_getKey(env, entryObj) };
130 JavaLocalObject valueObj{ env, invoke_MapEntry_getValue(env, entryObj) };
132 auto key = toStdString(env, static_cast< jstring >(keyObj.get()));
134 targetAttrs[std::move(key)] = toNativeAttrsValue(env, valueObj);
138 JNIEXPORT jboolean JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeIsEmpty
139 (JNIEnv* env, jobject obj)
142 EXPECT_RET(hasNativeHandle(env, obj), "no native handle.", true);
144 auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
145 VERIFY_NO_EXC_RET_DEF(env);
147 return attrs.empty();
150 JNIEXPORT jint JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeSize
151 (JNIEnv* env, jobject obj)
154 EXPECT_RET(hasNativeHandle(env, obj), "no native handle.", 0);
156 auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
157 VERIFY_NO_EXC_RET_DEF(env);
162 JNIEXPORT jboolean JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeRemove
163 (JNIEnv* env, jobject obj, jstring keyObj)
166 EXPECT_RET_DEF(keyObj, "Key is null.");
167 EXPECT_RET_DEF(hasNativeHandle(env, obj), "no native handle.");
169 auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
170 VERIFY_NO_EXC_RET_DEF(env);
171 const auto& key = toStdString(env, keyObj);
172 VERIFY_NO_EXC_RET_DEF(env);
174 auto ret = attrs.erase(key);
176 if (attrs.empty()) releaseNativeHandle(env, obj);
181 JNIEXPORT void JNICALL
182 Java_org_iotivity_service_RcsResourceAttributes_nativeClear
183 (JNIEnv* env, jobject obj)
187 releaseNativeHandle(env, obj);
190 JNIEXPORT jboolean JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeContains
191 (JNIEnv *env, jobject obj, jstring keyObj)
194 EXPECT_RET(keyObj, "Key is null.", false);
195 EXPECT_RET(hasNativeHandle(env, obj), "no native handle.", false);
197 auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
198 VERIFY_NO_EXC_RET_DEF(env);
199 const auto& key = toStdString(env, keyObj);
200 VERIFY_NO_EXC_RET_DEF(env);
201 return attrs.contains(key);
204 JNIEXPORT void JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeAddKeys
205 (JNIEnv *env, jobject obj, jstring setObj)
208 EXPECT(setObj, "set is null.");
209 EXPECT(hasNativeHandle(env, obj), "no native handle.");
211 auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
214 for (const auto& keyValue : attrs)
216 JavaLocalString localObj{ env, env->NewStringUTF(keyValue.key().c_str()) };
219 invoke_Collection_add(env, setObj, localObj);
224 JNIEXPORT jobject JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeExtract
225 (JNIEnv* env, jobject obj, jstring keyObj)
228 EXPECT_RET_DEF(keyObj, "Key is null.");
229 EXPECT_RET_DEF(hasNativeHandle(env, obj), "no native handle.");
231 auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
232 VERIFY_NO_EXC_RET_DEF(env);
234 const auto& key = toStdString(env, keyObj);
235 VERIFY_NO_EXC_RET_DEF(env);
237 EXPECT_RET_DEF(attrs.contains(key), "no matched value");
239 jobject valueObj = newRCSValueObject(env, attrs[key]);
240 VERIFY_NO_EXC_RET_DEF(env);
243 if (attrs.empty()) releaseNativeHandle(env, obj);
247 JNIEXPORT void JNICALL
248 Java_org_iotivity_service_RcsResourceAttributes_nativeExtractAll
249 (JNIEnv* env, jobject obj, jobject mapObj)
252 EXPECT(mapObj, "Map is null.");
253 EXPECT(hasNativeHandle(env, obj), "no native handle.");
255 auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
258 for (const auto& p : attrs) {
259 JavaLocalObject keyObj{ env, newStringObject(env, p.key()) };
262 JavaLocalObject valueObj{ env, newRCSValueObject(env, p.value()) };
265 invoke_Map_put(env, mapObj, keyObj, valueObj);
270 releaseNativeHandle(env, obj);