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 "simulator_resource_attributes_jni.h"
24 #include "simulator_error_codes.h"
28 extern SimulatorClassRefs gSimulatorClassRefs;
30 JniSimulatorResourceModel::JniSimulatorResourceModel(SimulatorResourceModel resourceModel)
31 : m_resourceModel(resourceModel)
34 bool JniSimulatorResourceModel::getResourceModel(JNIEnv *env, jobject thiz,
35 SimulatorResourceModel &resourceModel)
37 JniSimulatorResourceModel *resource = GetHandle<JniSimulatorResourceModel>(env, thiz);
38 if (env->ExceptionCheck())
40 cout << "Exception while converting the nativeHandle to JniSimulatorResourceModel" << endl;
43 resourceModel = resource->m_resourceModel;
47 jobject JniSimulatorResourceModel::toJava(JNIEnv *env, jlong resource)
49 jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResourceModel,
50 gSimulatorClassRefs.classSimulatorResourceModelCtor, resource);
58 static jobject createHashMap(JNIEnv *env)
60 jobject mapobj = env->NewObject(gSimulatorClassRefs.classHashMap,
61 gSimulatorClassRefs.classHashMapCtor);
65 static void addEntryToHashMap(JNIEnv *env, jobject mapobj, jobject key, jobject value)
67 if (!mapobj || !key || !value)
72 env->CallObjectMethod(mapobj, gSimulatorClassRefs.classHashMapPut, key, value);
75 JNIEXPORT jint JNICALL
76 Java_org_iotivity_simulator_SimulatorResourceModel_size
77 (JNIEnv *env, jobject thiz)
79 SimulatorResourceModel resourceModel;
80 bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
83 return SIMULATOR_ERROR;
86 return resourceModel.size();
89 JNIEXPORT jobject JNICALL
90 Java_org_iotivity_simulator_SimulatorResourceModel_getAttributes
91 (JNIEnv *env, jobject thiz)
93 SimulatorResourceModel resourceModel;
94 bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
100 map<string, SimulatorResourceModel::Attribute> attributesMap = resourceModel.getAttributes();
102 // Create Java HashMap object
103 jobject jHashMap = NULL;
104 jHashMap = createHashMap(env);
110 for (auto & attributeEntry : attributesMap)
113 // Create JniSimulatorResourceAttribute object and put the attribute.second into it
114 SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute(
115 attributeEntry.second);
117 // Create a java object for SimulatorResourceAttribute
118 jobject jAttribute = JniSimulatorResourceAttribute::toJava(env, reinterpret_cast<jlong>(attribute));
120 // Add an entry with attribute.first and javaSimualatorResourceAttribute to the HashMap
121 jstring jAttrName = env->NewStringUTF((attributeEntry.first).c_str());
122 addEntryToHashMap(env, jHashMap, static_cast<jobject>(jAttrName), jAttribute);
123 env->DeleteLocalRef(jAttrName);
128 JNIEXPORT jobject JNICALL
129 Java_org_iotivity_simulator_SimulatorResourceModel_getAttribute
130 (JNIEnv *env, jobject thiz, jstring jAttrName)
134 std::cout << "getAttribute: AttributeName is Empty";
138 const char *attrName = env->GetStringUTFChars(jAttrName, NULL);
141 std::cout << "getAttribute: Failed to convert jstring to char string!";
145 SimulatorResourceModel resourceModel;
146 bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
149 std::cout << "getAttribute: getResourceModel failed!";
150 env->ReleaseStringUTFChars(jAttrName, attrName);
154 SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute();
155 bool found = resourceModel.getAttribute(attrName, *attribute);
158 std::cout << "getAttribute: Attribute not found in ResourceModel!";
159 env->ReleaseStringUTFChars(jAttrName, attrName);
164 env->ReleaseStringUTFChars(jAttrName, attrName);
166 // Create a java object for SimulatorResourceAttribute
167 jobject jsimulatorResourceAttribute = JniSimulatorResourceAttribute::toJava(env,
168 reinterpret_cast<jlong>(attribute));
169 return jsimulatorResourceAttribute;
172 JNIEXPORT jobjectArray JNICALL
173 Java_org_iotivity_simulator_SimulatorResourceModel_getAllowedValues
174 (JNIEnv *env, jobject thiz, jstring jAttrName)
178 std::cout << "getAllowedValues: AttributeName is Empty";
182 const char *attrName = env->GetStringUTFChars(jAttrName, NULL);
185 std::cout << "getAllowedValues: Failed to convert jstring to char string!";
186 env->ReleaseStringUTFChars(jAttrName, attrName);
190 SimulatorResourceModel resourceModel;
191 bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
194 std::cout << "getAllowedValues: getResourceModel failed!";
195 env->ReleaseStringUTFChars(jAttrName, attrName);
199 SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute();
200 bool found = resourceModel.getAttribute(attrName, *attribute);
203 std::cout << "getAllowedValues: Attribute not found in ResourceModel!";
204 env->ReleaseStringUTFChars(jAttrName, attrName);
209 env->ReleaseStringUTFChars(jAttrName, attrName);
211 std::vector<std::string> values = attribute->allowedValuesToVectorString();
213 int size = attribute->getAllowedValuesSize();
215 // Create a jObjectArray for AllowedValues vector.
216 jobjectArray allowedValuesArr = env->NewObjectArray(size, env->FindClass("java/lang/String"),
217 env->NewStringUTF(""));
220 for (std::vector<std::string>::iterator it = values.begin(); it != values.end(); ++it, i++)
222 env->SetObjectArrayElement(allowedValuesArr, i, env->NewStringUTF((*it).c_str()));
224 return allowedValuesArr;
227 JNIEXPORT void JNICALL
228 Java_org_iotivity_simulator_SimulatorResourceModel_dispose
229 (JNIEnv *env, jobject thiz)
231 JniSimulatorResourceModel *resourceModel = GetHandle<JniSimulatorResourceModel>(env, thiz);
232 delete resourceModel;