Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / simulator / java / jni / resource_attributes_jni.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "resource_attributes_jni.h"
22 #include "simulator_resource_model.h"
23 #include "simulator_common_jni.h"
24 #include "simulator_error_codes.h"
25 #include <climits>
26
27 extern SimulatorClassRefs gSimulatorClassRefs;
28
29 class attribute_value_visitor : public boost::static_visitor<jobject>
30 {
31     public:
32         attribute_value_visitor(JNIEnv *env) : m_Env(env) {}
33
34         // Integer type value conversion
35         jobject operator ()(const int &value) const
36         {
37             jobject result = m_Env->NewObject(gSimulatorClassRefs.classInteger,
38                                               gSimulatorClassRefs.classIntegerCtor, value);
39             return result;
40         }
41
42         // Double type value conversion
43         jobject operator ()(const double &value) const
44         {
45             jobject result = m_Env->NewObject(gSimulatorClassRefs.classDouble,
46                                               gSimulatorClassRefs.classDoubleCtor, value);
47             return result;
48         }
49
50         // String type value conversion
51         jobject operator ()(const std::string &value) const
52         {
53             jstring str = m_Env->NewStringUTF(value.c_str());
54             return static_cast<jobject>(str);
55         }
56
57         // Boolean type value conversion
58         jobject operator ()(const bool &value) const
59         {
60             jobject result = m_Env->NewObject(gSimulatorClassRefs.classBoolean,
61                                               gSimulatorClassRefs.classBooleanCtor, value);
62             return result;
63         }
64 #if 0
65         // SimulatorResourceModel::Attribute type value conversion
66         jobject operator ()(const SimulatorResourceModel::Attribute &value) const
67         {
68             JResourceAttributeConverter converter(value);
69             return converter.toJava();
70         }
71 #endif
72
73     private:
74         JNIEnv *m_Env;
75 };
76
77 jobject JResourceAttributeConverter::toJava(JNIEnv *env)
78 {
79     // Create an object of ResourceAttribute java class
80     jobject jattributeObj = (jobject) env->NewObject(gSimulatorClassRefs.classResourceAttribute,
81                             gSimulatorClassRefs.classResourceAttributeCtor);
82     if (env->ExceptionCheck() || !jattributeObj)
83     {
84         return nullptr;
85     }
86
87     // Set attribute name
88     if (!setName(env, jattributeObj))
89     {
90         return nullptr;
91     }
92
93     // Set types
94     if (!setType(env, jattributeObj))
95     {
96         return nullptr;
97     }
98
99     // Set value
100     if (!setValue(env, jattributeObj))
101     {
102         return nullptr;
103     }
104
105     // Set Range
106     if (!setRange(env, jattributeObj))
107     {
108         return nullptr;
109     }
110
111     // Set Allowed values
112     if (!setAllowedValues(env, jattributeObj))
113     {
114         return nullptr;
115     }
116
117     return jattributeObj;
118 }
119
120 bool JResourceAttributeConverter::setName(JNIEnv *env, jobject &jattributeObj)
121 {
122     // Get field reference to "ResourceAttribute::m_name"
123     static jfieldID fidName = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute, "m_name",
124                               "Ljava/lang/String;");
125     if (!fidName)
126     {
127         return false;
128     }
129
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())
135     {
136         return false;
137     }
138
139     return true;
140 }
141
142 bool JResourceAttributeConverter::setType(JNIEnv *env, jobject &jattributeObj)
143 {
144     // Get class refs to ResourceAttribute::Type class
145     static jclass clsType = env->FindClass("org/oic/simulator/ResourceAttribute$Type");
146     if (!clsType)
147     {
148         return false;
149     }
150
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;");
154     if (!midGetType)
155     {
156         return false;
157     }
158
159     // Get field reference to "ResourceAttribute::m_type"
160     static jfieldID fidType = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute,
161                               "m_type", "Lorg/oic/simulator/ResourceAttribute$Type;");
162     if (!fidType)
163     {
164         return false;
165     }
166
167     int type = static_cast<int>(m_attribute.getValueType());
168     jobject jtype = env->CallStaticObjectMethod(clsType, midGetType, type);
169     if (env->ExceptionCheck())
170     {
171         return false;
172     }
173
174     env->SetObjectField(jattributeObj, fidType, jtype);
175     if (env->ExceptionCheck())
176     {
177         return false;
178     }
179
180     return true;
181 }
182
183 bool JResourceAttributeConverter::setValue(JNIEnv *env, jobject &jattributeObj)
184 {
185     // Get field reference to "ResourceAttribute::m_value"
186     static jfieldID fidValue = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute,
187                                "m_value", "Ljava/lang/Object;");
188     if (!fidValue)
189     {
190         return false;
191     }
192     jobject jvalue = boost::apply_visitor(attribute_value_visitor(env), m_attribute.getValue());
193     env->SetObjectField(jattributeObj, fidValue, jvalue);
194     return true;
195 }
196
197 bool JResourceAttributeConverter::setRange(JNIEnv *env, jobject &jattributeObj)
198 {
199     int min = INT_MIN;
200     int max = INT_MAX;
201     m_attribute.getRange(min, max);
202     if (INT_MIN == min || INT_MAX == max)
203     {
204         return true;
205     }
206     env->CallVoidMethod(jattributeObj, gSimulatorClassRefs.classResourceAttributeSetRange, min, max);
207     if (env->ExceptionCheck())
208     {
209         return false;
210     }
211
212     return true;
213 }
214
215 bool JResourceAttributeConverter::setAllowedValues(JNIEnv *env, jobject &jattributeObj)
216 {
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)
221     {
222         return false;
223     }
224
225     jobjectArray jallowedValues = env->NewObjectArray(m_attribute.getAllowedValuesSize(),
226                                   gSimulatorClassRefs.classObject, NULL);
227     if (!jallowedValues)
228     {
229         return false;
230     }
231
232     int index = 0;
233     for (auto & value : m_attribute.getAllowedValues())
234     {
235         jobject jvalue = boost::apply_visitor(attribute_value_visitor(env), value);
236         env->SetObjectArrayElement(jallowedValues, index++, jvalue);
237     }
238
239     env->SetObjectField(jattributeObj, fidAllowedValues, jallowedValues);
240     if (env->ExceptionCheck())
241     {
242         return false;
243     }
244
245     return true;
246 }