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);
57 // Boolean type value conversion
58 jobject operator ()(const bool &value) const
60 jobject result = m_Env->NewObject(gSimulatorClassRefs.classBoolean,
61 gSimulatorClassRefs.classBooleanCtor, value);
65 // SimulatorResourceModel::Attribute type value conversion
66 jobject operator ()(const SimulatorResourceModel::Attribute &value) const
68 JResourceAttributeConverter converter(value);
69 return converter.toJava();
77 jobject JResourceAttributeConverter::toJava(JNIEnv *env)
79 // Create an object of ResourceAttribute java class
80 jobject jattributeObj = (jobject) env->NewObject(gSimulatorClassRefs.classResourceAttribute,
81 gSimulatorClassRefs.classResourceAttributeCtor);
82 if (env->ExceptionCheck() || !jattributeObj)
88 if (!setName(env, jattributeObj))
94 if (!setType(env, jattributeObj))
100 if (!setValue(env, jattributeObj))
106 if (!setRange(env, jattributeObj))
111 // Set Allowed values
112 if (!setAllowedValues(env, jattributeObj))
117 return jattributeObj;
120 bool JResourceAttributeConverter::setName(JNIEnv *env, jobject &jattributeObj)
122 // Get field reference to "ResourceAttribute::m_name"
123 static jfieldID fidName = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute, "m_name",
124 "Ljava/lang/String;");
130 // Set the attribute name
131 std::string name = m_attribute.getName();
132 jstring jname = env->NewStringUTF(name.c_str());
133 env->SetObjectField(jattributeObj, fidName, jname);
134 if (env->ExceptionCheck())
142 bool JResourceAttributeConverter::setType(JNIEnv *env, jobject &jattributeObj)
144 // Get class refs to ResourceAttribute::Type class
145 static jclass clsType = env->FindClass("org/oic/simulator/ResourceAttribute$Type");
151 // Get method ref to static method to ResourceAttribute::Type::getType
152 static jmethodID midGetType = env->GetStaticMethodID(clsType, "getType",
153 "(I)Lorg/oic/simulator/ResourceAttribute$Type;");
159 // Get field reference to "ResourceAttribute::m_type"
160 static jfieldID fidType = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute,
161 "m_type", "Lorg/oic/simulator/ResourceAttribute$Type;");
167 int type = static_cast<int>(m_attribute.getValueType());
168 jobject jtype = env->CallStaticObjectMethod(clsType, midGetType, type);
169 if (env->ExceptionCheck())
174 env->SetObjectField(jattributeObj, fidType, jtype);
175 if (env->ExceptionCheck())
183 bool JResourceAttributeConverter::setValue(JNIEnv *env, jobject &jattributeObj)
185 // Get field reference to "ResourceAttribute::m_value"
186 static jfieldID fidValue = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute,
187 "m_value", "Ljava/lang/Object;");
192 jobject jvalue = boost::apply_visitor(attribute_value_visitor(env), m_attribute.getValue());
193 env->SetObjectField(jattributeObj, fidValue, jvalue);
197 bool JResourceAttributeConverter::setRange(JNIEnv *env, jobject &jattributeObj)
201 m_attribute.getRange(min, max);
202 if (INT_MIN == min || INT_MAX == max)
206 env->CallVoidMethod(jattributeObj, gSimulatorClassRefs.classResourceAttributeSetRange, min, max);
207 if (env->ExceptionCheck())
215 bool JResourceAttributeConverter::setAllowedValues(JNIEnv *env, jobject &jattributeObj)
217 // Get field reference to "ResourceAttribute::m_AllowedValues"
218 static jfieldID fidAllowedValues = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute,
219 "m_AllowedValues", "Ljava/lang/Object;");
220 if (!fidAllowedValues)
225 jobjectArray jallowedValues = env->NewObjectArray(m_attribute.getAllowedValuesSize(),
226 gSimulatorClassRefs.classObject, NULL);
233 for (auto & value : m_attribute.getAllowedValues())
235 jobject jvalue = boost::apply_visitor(attribute_value_visitor(env), value);
236 env->SetObjectArrayElement(jallowedValues, index++, jvalue);
239 env->SetObjectField(jattributeObj, fidAllowedValues, jallowedValues);
240 if (env->ExceptionCheck())