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 "resource_attributes_jni.h"
22 #include "simulator_resource_model.h"
23 #include "simulator_common_jni.h"
24 #include "simulator_error_codes.h"
27 extern SimulatorClassRefs gSimulatorClassRefs;
29 class attribute_value_visitor : public boost::static_visitor<jobject>
32 attribute_value_visitor(JNIEnv *env) : m_Env(env) {}
34 // Integer type value conversion
35 jobject operator ()(const int &value) const
37 jobject result = m_Env->NewObject(gSimulatorClassRefs.classInteger,
38 gSimulatorClassRefs.classIntegerCtor, value);
42 // Double type value conversion
43 jobject operator ()(const double &value) const
45 jobject result = m_Env->NewObject(gSimulatorClassRefs.classDouble,
46 gSimulatorClassRefs.classDoubleCtor, value);
50 // String type value conversion
51 jobject operator ()(const std::string &value) const
53 jstring str = m_Env->NewStringUTF(value.c_str());
54 return static_cast<jobject>(str);
58 // Boolean type value conversion
59 jobject operator ()(const bool &value) const
61 jobject result = m_Env->NewObject(gSimulatorClassRefs.classBoolean,
62 gSimulatorClassRefs.classBooleanCtor, value);
66 // SimulatorResourceModel::Attribute type value conversion
67 jobject operator ()(const SimulatorResourceModel::Attribute &value) const
69 JResourceAttributeConverter converter(value);
70 return converter.toJava();
78 jobject JResourceAttributeConverter::toJava(JNIEnv *env)
80 // Create an object of ResourceAttribute java class
81 jobject jattributeObj = (jobject) env->NewObject(gSimulatorClassRefs.classResourceAttribute,
82 gSimulatorClassRefs.classResourceAttributeCtor);
83 if (env->ExceptionCheck() || !jattributeObj)
89 if (!setName(env, jattributeObj))
95 if (!setType(env, jattributeObj))
101 if (!setValue(env, jattributeObj))
107 if (!setRange(env, jattributeObj))
112 // Set Allowed values
113 if (!setAllowedValues(env, jattributeObj))
118 return jattributeObj;
121 bool JResourceAttributeConverter::setName(JNIEnv *env, jobject &jattributeObj)
123 // Get field reference to "ResourceAttribute::m_name"
124 static jfieldID fidName = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute, "m_name",
125 "Ljava/lang/String;");
131 // Set the attribute name
132 std::string name = m_attribute.getName();
133 jstring jname = env->NewStringUTF(name.c_str());
134 env->SetObjectField(jattributeObj, fidName, jname);
135 if (env->ExceptionCheck())
143 bool JResourceAttributeConverter::setType(JNIEnv *env, jobject &jattributeObj)
145 // Get class refs to ResourceAttribute::Type class
146 static jclass clsType = env->FindClass("org/oic/simulator/ResourceAttribute$Type");
152 // Get method ref to static method to ResourceAttribute::Type::getType
153 static jmethodID midGetType = env->GetStaticMethodID(clsType, "getType",
154 "(I)Lorg/oic/simulator/ResourceAttribute$Type;");
160 // Get field reference to "ResourceAttribute::m_type"
161 static jfieldID fidType = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute,
162 "m_type", "Lorg/oic/simulator/ResourceAttribute$Type;");
168 int type = static_cast<int>(m_attribute.getValueType());
169 jobject jtype = env->CallStaticObjectMethod(clsType, midGetType, type);
170 if (env->ExceptionCheck())
175 env->SetObjectField(jattributeObj, fidType, jtype);
176 if (env->ExceptionCheck())
184 bool JResourceAttributeConverter::setValue(JNIEnv *env, jobject &jattributeObj)
186 // Get field reference to "ResourceAttribute::m_value"
187 static jfieldID fidValue = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute,
188 "m_value", "Ljava/lang/Object;");
193 jobject jvalue = boost::apply_visitor(attribute_value_visitor(env), m_attribute.getValue());
194 env->SetObjectField(jattributeObj, fidValue, jvalue);
198 bool JResourceAttributeConverter::setRange(JNIEnv *env, jobject &jattributeObj)
202 m_attribute.getRange(min, max);
203 if (INT_MIN == min || INT_MAX == max)
207 env->CallVoidMethod(jattributeObj, gSimulatorClassRefs.classResourceAttributeSetRange, min, max);
208 if (env->ExceptionCheck())
216 bool JResourceAttributeConverter::setAllowedValues(JNIEnv *env, jobject &jattributeObj)
218 // Get field reference to "ResourceAttribute::m_AllowedValues"
219 static jfieldID fidAllowedValues = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute,
220 "m_AllowedValues", "Ljava/lang/Object;");
221 if (!fidAllowedValues)
226 jobjectArray jallowedValues = env->NewObjectArray(m_attribute.getAllowedValuesSize(),
227 gSimulatorClassRefs.classObject, NULL);
234 for (auto & value : m_attribute.getAllowedValues())
236 jobject jvalue = boost::apply_visitor(attribute_value_visitor(env), value);
237 env->SetObjectArrayElement(jallowedValues, index++, jvalue);
240 env->SetObjectField(jattributeObj, fidAllowedValues, jallowedValues);
241 if (env->ExceptionCheck())