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 "simulator_resource_model_jni.h"
22 #include "simulator_common_jni.h"
23 #include "resource_attributes_jni.h"
24 #include "simulator_error_codes.h"
25 #include "simulator_jni_utils.h"
29 extern SimulatorClassRefs gSimulatorClassRefs;
31 JSimulatorResourceModel::JSimulatorResourceModel(SimulatorResourceModel resModel)
32 : m_resourceModel(resModel)
35 JSimulatorResourceModel::JSimulatorResourceModel(SimulatorResourceModelSP resModel)
36 : m_resModelPtr(resModel)
39 bool JSimulatorResourceModel::getResourceModel(JNIEnv *env, jobject thiz,
40 SimulatorResourceModel &resModel)
42 JSimulatorResourceModel *resource = GetHandle<JSimulatorResourceModel>(env, thiz);
43 if (env->ExceptionCheck())
48 if (nullptr != resource->m_resModelPtr)
49 resModel = *(resource->m_resModelPtr.get());
51 resModel = resource->m_resourceModel;
55 SimulatorResourceModelSP JSimulatorResourceModel::getResourceModelPtr(JNIEnv *env, jobject thiz)
57 JSimulatorResourceModel *resource = GetHandle<JSimulatorResourceModel>(env, thiz);
58 if (env->ExceptionCheck())
63 if (nullptr != resource->m_resModelPtr)
64 return resource->m_resModelPtr;
68 jobject JSimulatorResourceModel::toJava(JNIEnv *env, jlong nativeHandle)
70 jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResourceModel,
71 gSimulatorClassRefs.classSimulatorResourceModelCtor, nativeHandle);
79 void JSimulatorResourceModel::toJava(JNIEnv *env, jobject thiz, jlong nativeHandle)
81 if (env && thiz && nativeHandle)
83 env->SetLongField(thiz, GetHandleField(env, thiz), nativeHandle);
87 static jobject createHashMap(JNIEnv *env)
89 jobject mapobj = env->NewObject(gSimulatorClassRefs.classHashMap,
90 gSimulatorClassRefs.classHashMapCtor);
94 static void addEntryToHashMap(JNIEnv *env, jobject mapobj, jobject key, jobject value)
96 if (!mapobj || !key || !value)
101 env->CallObjectMethod(mapobj, gSimulatorClassRefs.classHashMapPut, key, value);
104 JNIEXPORT void JNICALL
105 Java_org_oic_simulator_SimulatorResourceModel_create
106 (JNIEnv *env, jobject thiz)
108 SimulatorResourceModelSP resModel = std::make_shared<SimulatorResourceModel>();
109 JSimulatorResourceModel *jresModel = new JSimulatorResourceModel(resModel);
110 JSimulatorResourceModel::toJava(env, thiz, reinterpret_cast<jlong>(jresModel));
113 JNIEXPORT jint JNICALL
114 Java_org_oic_simulator_SimulatorResourceModel_size
115 (JNIEnv *env, jobject thiz)
117 SimulatorResourceModel resourceModel;
118 bool result = JSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
121 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
122 return SIMULATOR_BAD_OBJECT;
125 return resourceModel.size();
128 JNIEXPORT jobject JNICALL
129 Java_org_oic_simulator_SimulatorResourceModel_getAttributes
130 (JNIEnv *env, jobject thiz)
132 SimulatorResourceModel resourceModel;
133 bool result = JSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
136 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
140 map<string, SimulatorResourceModel::Attribute> attributesMap = resourceModel.getAttributes();
142 // Create Java HashMap object
143 jobject jHashMap = NULL;
144 jHashMap = createHashMap(env);
147 throwSimulatorException(env, SIMULATOR_ERROR, "Java map creation failed!");
151 for (auto & attributeEntry : attributesMap)
153 SimulatorResourceModel::Attribute attribute(attributeEntry.second);
155 // Create a object of ResourceAttribute java class
156 JResourceAttributeConverter converter(attribute);
157 jobject jAttribute = converter.toJava(env);
159 // Add an entry with attribute.first and javaSimualatorResourceAttribute to the HashMap
160 jstring jAttrName = env->NewStringUTF((attributeEntry.first).c_str());
161 addEntryToHashMap(env, jHashMap, static_cast<jobject>(jAttrName), jAttribute);
162 env->DeleteLocalRef(jAttrName);
168 JNIEXPORT jobject JNICALL
169 Java_org_oic_simulator_SimulatorResourceModel_getAttribute
170 (JNIEnv *env, jobject thiz, jstring jAttrName)
174 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid attribute name!");
178 const char *attrName = env->GetStringUTFChars(jAttrName, NULL);
181 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
185 SimulatorResourceModel resourceModel;
186 bool result = JSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
189 env->ReleaseStringUTFChars(jAttrName, attrName);
190 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
194 SimulatorResourceModel::Attribute attribute;
195 bool found = resourceModel.getAttribute(attrName, attribute);
198 env->ReleaseStringUTFChars(jAttrName, attrName);
199 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Attribute does not exist!");
203 env->ReleaseStringUTFChars(jAttrName, attrName);
205 // Create a object of ResourceAttribute java class
206 JResourceAttributeConverter converter(attribute);
207 return converter.toJava(env);
210 JNIEXPORT void JNICALL
211 Java_org_oic_simulator_SimulatorResourceModel_addAttributeInt
212 (JNIEnv *env, jobject thiz, jstring jname, jint jvalue)
216 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid attribute name!");
220 SimulatorResourceModelSP resModelPtr;
221 resModelPtr = JSimulatorResourceModel::getResourceModelPtr(env, thiz);
224 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
228 const char *nameCstr = env->GetStringUTFChars(jname, NULL);
231 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
235 std::string attrName(nameCstr);
236 int value = static_cast<int>(jvalue);
237 resModelPtr->addAttribute(attrName, value);
239 // Release created c string
240 env->ReleaseStringUTFChars(jname, nameCstr);
243 JNIEXPORT void JNICALL
244 Java_org_oic_simulator_SimulatorResourceModel_addAttributeDouble
245 (JNIEnv *env, jobject thiz, jstring jname, jdouble jvalue)
249 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid attribute name!");
253 SimulatorResourceModelSP resModelPtr;
254 resModelPtr = JSimulatorResourceModel::getResourceModelPtr(env, thiz);
257 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
261 const char *nameCstr = env->GetStringUTFChars(jname, NULL);
264 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
268 std::string attrName(nameCstr);
269 double value = static_cast<double>(jvalue);
270 resModelPtr->addAttribute(attrName, value);
272 // Release created c string
273 env->ReleaseStringUTFChars(jname, nameCstr);
276 JNIEXPORT void JNICALL
277 Java_org_oic_simulator_SimulatorResourceModel_addAttributeBoolean
278 (JNIEnv *env, jobject thiz, jstring jname, jboolean jvalue)
282 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid attribute name!");
286 SimulatorResourceModelSP resModelPtr;
287 resModelPtr = JSimulatorResourceModel::getResourceModelPtr(env, thiz);
290 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
294 const char *nameCstr = env->GetStringUTFChars(jname, NULL);
297 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
301 std::string attrName(nameCstr);
302 bool value = static_cast<bool>(jvalue);
303 resModelPtr->addAttribute(attrName, value);
305 // Release created c string
306 env->ReleaseStringUTFChars(jname, nameCstr);
309 JNIEXPORT void JNICALL
310 Java_org_oic_simulator_SimulatorResourceModel_addAttributeString
311 (JNIEnv *env, jobject thiz, jstring jname, jstring jvalue)
315 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid attribute name!");
321 throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Attribute value cannot be null!");
325 SimulatorResourceModelSP resModelPtr;
326 resModelPtr = JSimulatorResourceModel::getResourceModelPtr(env, thiz);
329 throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
333 const char *nameCstr = env->GetStringUTFChars(jname, NULL);
336 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
340 const char *valueCstr = env->GetStringUTFChars(jvalue, NULL);
343 env->ReleaseStringUTFChars(jname, nameCstr);
344 throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
348 std::string attrName(nameCstr);
349 std::string value(valueCstr);
350 resModelPtr->addAttribute(attrName, value);
352 // Release created c string
353 env->ReleaseStringUTFChars(jname, nameCstr);
354 env->ReleaseStringUTFChars(jvalue, valueCstr);
357 JNIEXPORT void JNICALL
358 Java_org_oic_simulator_SimulatorResourceModel_dispose
359 (JNIEnv *env, jobject thiz)
361 JSimulatorResourceModel *resourceModel = GetHandle<JSimulatorResourceModel>(env, thiz);
362 delete resourceModel;