Imported Upstream version 1.0.0
[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 #if 0
58         // Boolean type value conversion
59         jobject operator ()(const bool &value) const
60         {
61             jobject result = m_Env->NewObject(gSimulatorClassRefs.classBoolean,
62                                               gSimulatorClassRefs.classBooleanCtor, value);
63             return result;
64         }
65
66         // SimulatorResourceModel::Attribute type value conversion
67         jobject operator ()(const SimulatorResourceModel::Attribute &value) const
68         {
69             JResourceAttributeConverter converter(value);
70             return converter.toJava();
71         }
72 #endif
73
74     private:
75         JNIEnv *m_Env;
76 };
77
78 jobject JResourceAttributeConverter::toJava(JNIEnv *env)
79 {
80     // Create an object of ResourceAttribute java class
81     jobject jattributeObj = (jobject) env->NewObject(gSimulatorClassRefs.classResourceAttribute,
82                             gSimulatorClassRefs.classResourceAttributeCtor);
83     if (env->ExceptionCheck() || !jattributeObj)
84     {
85         return nullptr;
86     }
87
88     // Set attribute name
89     if (!setName(env, jattributeObj))
90     {
91         return nullptr;
92     }
93
94     // Set types
95     if (!setType(env, jattributeObj))
96     {
97         return nullptr;
98     }
99
100     // Set value
101     if (!setValue(env, jattributeObj))
102     {
103         return nullptr;
104     }
105
106     // Set Range
107     if (!setRange(env, jattributeObj))
108     {
109         return nullptr;
110     }
111
112     // Set Allowed values
113     if (!setAllowedValues(env, jattributeObj))
114     {
115         return nullptr;
116     }
117
118     return jattributeObj;
119 }
120
121 bool JResourceAttributeConverter::setName(JNIEnv *env, jobject &jattributeObj)
122 {
123     // Get field reference to "ResourceAttribute::m_name"
124     static jfieldID fidName = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute, "m_name",
125                               "Ljava/lang/String;");
126     if (!fidName)
127     {
128         return false;
129     }
130
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())
136     {
137         return false;
138     }
139
140     return true;
141 }
142
143 bool JResourceAttributeConverter::setType(JNIEnv *env, jobject &jattributeObj)
144 {
145     // Get class refs to ResourceAttribute::Type class
146     static jclass clsType = env->FindClass("org/oic/simulator/ResourceAttribute$Type");
147     if (!clsType)
148     {
149         return false;
150     }
151
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;");
155     if (!midGetType)
156     {
157         return false;
158     }
159
160     // Get field reference to "ResourceAttribute::m_type"
161     static jfieldID fidType = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute,
162                               "m_type", "Lorg/oic/simulator/ResourceAttribute$Type;");
163     if (!fidType)
164     {
165         return false;
166     }
167
168     int type = static_cast<int>(m_attribute.getValueType());
169     jobject jtype = env->CallStaticObjectMethod(clsType, midGetType, type);
170     if (env->ExceptionCheck())
171     {
172         return false;
173     }
174
175     env->SetObjectField(jattributeObj, fidType, jtype);
176     if (env->ExceptionCheck())
177     {
178         return false;
179     }
180
181     return true;
182 }
183
184 bool JResourceAttributeConverter::setValue(JNIEnv *env, jobject &jattributeObj)
185 {
186     // Get field reference to "ResourceAttribute::m_value"
187     static jfieldID fidValue = env->GetFieldID(gSimulatorClassRefs.classResourceAttribute,
188                                "m_value", "Ljava/lang/Object;");
189     if (!fidValue)
190     {
191         return false;
192     }
193     jobject jvalue = boost::apply_visitor(attribute_value_visitor(env), m_attribute.getValue());
194     env->SetObjectField(jattributeObj, fidValue, jvalue);
195     return true;
196 }
197
198 bool JResourceAttributeConverter::setRange(JNIEnv *env, jobject &jattributeObj)
199 {
200     int min = INT_MIN;
201     int max = INT_MAX;
202     m_attribute.getRange(min, max);
203     if (INT_MIN == min || INT_MAX == max)
204     {
205         return true;
206     }
207     env->CallVoidMethod(jattributeObj, gSimulatorClassRefs.classResourceAttributeSetRange, min, max);
208     if (env->ExceptionCheck())
209     {
210         return false;
211     }
212
213     return true;
214 }
215
216 bool JResourceAttributeConverter::setAllowedValues(JNIEnv *env, jobject &jattributeObj)
217 {
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)
222     {
223         return false;
224     }
225
226     jobjectArray jallowedValues = env->NewObjectArray(m_attribute.getAllowedValuesSize(),
227                                   gSimulatorClassRefs.classObject, NULL);
228     if (!jallowedValues)
229     {
230         return false;
231     }
232
233     int index = 0;
234     for (auto & value : m_attribute.getAllowedValues())
235     {
236         jobject jvalue = boost::apply_visitor(attribute_value_visitor(env), value);
237         env->SetObjectArrayElement(jallowedValues, index++, jvalue);
238     }
239
240     env->SetObjectField(jattributeObj, fidAllowedValues, jallowedValues);
241     if (env->ExceptionCheck())
242     {
243         return false;
244     }
245
246     return true;
247 }