c2aafbdfa082db8cd1a1f1a8aad1410dbbc8b446
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_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 "simulator_resource_attributes_jni.h"
22 #include "simulator_resource_model.h"
23 #include "simulator_common_jni.h"
24 #include "simulator_error_codes.h"
25
26 extern SimulatorClassRefs gSimulatorClassRefs;
27
28 jobject JniSimulatorResourceAttribute::toJava(JNIEnv *env, jlong resource)
29 {
30     jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResourceAttribute,
31                           gSimulatorClassRefs.classSimulatorResourceAttributeCtor, resource);
32     if (NULL == resourceObj)
33     {
34         return NULL;
35     }
36
37     return resourceObj;
38 }
39
40 class attribute_value_visitor : public boost::static_visitor<jobject>
41 {
42     public:
43         attribute_value_visitor(JNIEnv *env) : m_Env(env) {}
44
45         jobject operator ()(const int &value) const
46         {
47             jobject result = m_Env->NewObject(gSimulatorClassRefs.classInteger,
48                                               gSimulatorClassRefs.classIntegerCtor, value);
49             return result;
50         }
51
52         jobject operator ()(const double &value) const
53         {
54             jobject result = m_Env->NewObject(gSimulatorClassRefs.classDouble,
55                                               gSimulatorClassRefs.classDoubleCtor, value);
56             return result;
57         }
58
59         jobject operator ()(const std::string &value) const
60         {
61             jstring str = m_Env->NewStringUTF(value.c_str());
62             return static_cast<jobject>(str);
63         }
64
65     private:
66         JNIEnv *m_Env;
67 };
68
69 JNIEXPORT void JNICALL
70 Java_org_iotivity_simulator_SimulatorResourceAttribute_create
71 (JNIEnv *env, jobject object, jstring attrName)
72 {
73     if (!attrName)
74     {
75         std::cout << "SimulatorResourceAttribute_create: AttributeName is Empty";
76         return;
77     }
78
79     const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
80     if (!attrNamePtr)
81         return;
82
83     std::string attrNameStr(attrNamePtr);
84     SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute(attrNameStr);
85     SetHandle<SimulatorResourceModel::Attribute>(env, object, attribute);
86     if (env->ExceptionCheck())
87     {
88         delete attribute;
89     }
90
91     env->ReleaseStringUTFChars(attrName, attrNamePtr);
92 }
93
94 JNIEXPORT void JNICALL
95 Java_org_iotivity_simulator_SimulatorResourceAttribute_dispose
96 (JNIEnv *env, jobject object)
97 {
98     SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
99             object);
100     delete attribute;
101 }
102
103 JNIEXPORT int JNICALL
104 Java_org_iotivity_simulator_SimulatorResourceAttribute_allowedValuesSize
105 (JNIEnv *env, jobject object)
106 {
107     SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
108             object);
109     if (env->ExceptionCheck() || !attribute)
110     {
111         return SIMULATOR_ERROR;
112     }
113
114     return attribute->getAllowedValuesSize();
115 }
116
117 JNIEXPORT jstring JNICALL
118 Java_org_iotivity_simulator_SimulatorResourceAttribute_valueToString
119 (JNIEnv *env, jobject object)
120 {
121     SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
122             object);
123     if (env->ExceptionCheck() || !attribute)
124     {
125         return NULL;
126     }
127
128     std::string str = attribute->valueToString();
129     return env->NewStringUTF(str.c_str());
130 }
131
132 JNIEXPORT jstring JNICALL
133 Java_org_iotivity_simulator_SimulatorResourceAttribute_allowedValuesToString
134 (JNIEnv *env, jobject object)
135 {
136     SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
137             object);
138     if (env->ExceptionCheck() || !attribute)
139     {
140         return NULL;
141     }
142
143     std::string str = attribute->allowedValuesToString();
144     return env->NewStringUTF(str.c_str());
145 }
146
147 JNIEXPORT jstring JNICALL
148 Java_org_iotivity_simulator_SimulatorResourceAttribute_getName
149 (JNIEnv *env, jobject object)
150 {
151     SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
152             object);
153     if (env->ExceptionCheck() || !attribute)
154     {
155         return NULL;
156     }
157
158     std::string str = attribute->getName();
159     return env->NewStringUTF(str.c_str());
160 }
161
162 JNIEXPORT jobject JNICALL
163 Java_org_iotivity_simulator_SimulatorResourceAttribute_getValue
164 (JNIEnv *env, jobject object)
165 {
166     SimulatorResourceModel::Attribute *attribute = GetHandle<SimulatorResourceModel::Attribute>(env,
167             object);
168     if (env->ExceptionCheck() || !attribute)
169     {
170         return NULL;
171     }
172
173     return boost::apply_visitor(attribute_value_visitor(env), attribute->getValue());
174 }