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 "simulator_resource_model_jni.h"
22 #include "simulator_exceptions_jni.h"
23 #include "simulator_utils_jni.h"
24 #include "jni_sharedobject_holder.h"
25 #include "jni_listener_holder.h"
26 #include "jni_string.h"
27 #include "jni_vector.h"
29 #include "simulator_single_resource.h"
31 #define VALIDATE_OBJECT(ENV, OBJECT) if (!OBJECT){throwBadObjectException(ENV, "No corresponsing native object!"); return;}
32 #define VALIDATE_OBJECT_RET(ENV, OBJECT, RET) if (!OBJECT){throwBadObjectException(ENV, "No corresponsing native object!"); return RET;}
34 SimulatorSingleResourceSP simulatorSingleResourceToCpp(JNIEnv *env, jobject object)
36 JniSharedObjectHolder<SimulatorSingleResource> *jniResource =
37 GetHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);
39 return jniResource->get();
43 static void onResourceModelChange(jobject listener, const std::string &uri,
44 SimulatorResourceModel &resModel)
46 JNIEnv *env = getEnv();
50 jclass listenerCls = env->GetObjectClass(listener);
51 jmethodID listenerMethod = env->GetMethodID(listenerCls, "onResourceModelChanged",
52 "(Ljava/lang/String;Lorg/oic/simulator/SimulatorResourceModel;)V");
54 jobject jResModel = simulatorResourceModelToJava(env, resModel);
55 jstring jUri = env->NewStringUTF(uri.c_str());
56 env->CallVoidMethod(listenerCls, listenerMethod, jUri, jResModel);
60 static void onAutoUpdationComplete(jobject listener, const std::string &uri, const int id)
62 JNIEnv *env = getEnv();
66 jclass listenerCls = env->GetObjectClass(listener);
67 jmethodID listenerMethod = env->GetMethodID(listenerCls, "onUpdateComplete",
68 "(Ljava/lang/String;I)V");
70 jstring jUri = env->NewStringUTF(uri.c_str());
71 env->CallVoidMethod(listener, listenerMethod, jUri, id);
79 JNIEXPORT jobject JNICALL
80 Java_org_oic_simulator_server_SimulatorSingleResource_getResourceModel
81 (JNIEnv *env, jobject object)
83 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
84 VALIDATE_OBJECT_RET(env, singleResource, nullptr)
86 SimulatorResourceModel resModel = singleResource->getResourceModel();
87 return simulatorResourceModelToJava(env, resModel);
90 JNIEXPORT jobject JNICALL
91 Java_org_oic_simulator_server_SimulatorSingleResource_getAttribute
92 (JNIEnv *env, jobject object, jstring attrName)
94 VALIDATE_INPUT_RET(env, !attrName, "Attribute name is null!", nullptr)
96 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
97 VALIDATE_OBJECT_RET(env, singleResource, nullptr)
99 JniString jniAttrName(env, attrName);
100 SimulatorResourceModel::Attribute attribute;
101 if (singleResource->getAttribute(jniAttrName.get(), attribute))
102 return simulatorResourceAttributeToJava(env, attribute);
106 JNIEXPORT void JNICALL
107 Java_org_oic_simulator_server_SimulatorSingleResource_addAttribute
108 (JNIEnv *env, jobject object, jobject resAttribute)
110 VALIDATE_INPUT(env, !resAttribute, "Resource attribute is null!")
112 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
113 VALIDATE_OBJECT(env, singleResource)
117 SimulatorResourceModel::Attribute attribute;
118 if (!simulatorResourceAttributeToCpp(env, resAttribute, attribute))
120 throwSimulatorException(env, SIMULATOR_ERROR,
121 "Failed to covnert SimulatorResourceAttribute java object!");
125 singleResource->addAttribute(attribute);
127 catch (SimulatorException &e)
129 throwSimulatorException(env, e.code(), e.what());
133 JNIEXPORT void JNICALL
134 Java_org_oic_simulator_server_SimulatorSingleResource_updateAttribute
135 (JNIEnv *env, jobject object, jstring attrName, jobject attrValue)
137 VALIDATE_INPUT(env, !attrName, "Attribute name is null!")
138 VALIDATE_INPUT(env, !attrValue, "Attribute value is null!")
140 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
141 VALIDATE_OBJECT(env, singleResource)
143 SimulatorResourceModel::ValueVariant value;
144 if (!AttributeValueToCpp(env, attrValue, value))
146 throwSimulatorException(env, SIMULATOR_ERROR,
147 "Failed to covnert AttributeValue java object!");
151 SimulatorResourceModel::Attribute attribute(JniString(env, attrName).get());
152 attribute.setValue(value);
153 singleResource->updateAttributeValue(attribute);
156 JNIEXPORT void JNICALL
157 Java_org_oic_simulator_server_SimulatorSingleResource_removeAttribute
158 (JNIEnv *env, jobject object, jstring attrName)
160 VALIDATE_INPUT(env, !attrName, "Attribute name is null!")
162 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
163 VALIDATE_OBJECT(env, singleResource)
167 JniString jniAttrName(env, attrName);
168 singleResource->removeAttribute(jniAttrName.get());
170 catch (InvalidArgsException &e)
172 throwInvalidArgsException(env, e.code(), e.what());
176 JNIEXPORT jint JNICALL
177 Java_org_oic_simulator_server_SimulatorSingleResource_startResourceUpdation
178 (JNIEnv *env, jobject object, jint type, jint interval, jobject listener)
180 VALIDATE_CALLBACK_RET(env, listener, -1)
182 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
183 VALIDATE_OBJECT_RET(env, singleResource, -1)
185 jobject listenerRef = env->NewGlobalRef(listener);
186 updateCompleteCallback callback = [listenerRef](const std::string & uri, const int id)
188 onAutoUpdationComplete(listenerRef, uri, id);
193 int id = singleResource->startResourceUpdation((1 == type) ? AutomationType::RECURRENT :
194 AutomationType::NORMAL, interval, callback);
197 catch (InvalidArgsException &e)
199 throwInvalidArgsException(env, e.code(), e.what());
201 catch (SimulatorException &e)
203 throwSimulatorException(env, e.code(), e.what());
209 JNIEXPORT jint JNICALL
210 Java_org_oic_simulator_server_SimulatorSingleResource_startAttributeUpdation
211 (JNIEnv *env, jobject object, jstring attrName, jint type, jint interval, jobject listener)
213 VALIDATE_INPUT_RET(env, !attrName, "Attribute name is null!", -1)
214 VALIDATE_CALLBACK_RET(env, listener, -1)
216 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
217 VALIDATE_OBJECT_RET(env, singleResource, -1)
219 jobject listenerRef = env->NewGlobalRef(listener);
220 updateCompleteCallback callback = [listenerRef](const std::string & uri, const int id)
222 onAutoUpdationComplete(listenerRef, uri, id);
225 JniString jniAttrName(env, attrName);
229 int id = singleResource->startAttributeUpdation(jniAttrName.get(),
230 (1 == type) ? AutomationType::RECURRENT : AutomationType::NORMAL,
234 catch (InvalidArgsException &e)
236 throwInvalidArgsException(env, e.code(), e.what());
238 catch (SimulatorException &e)
240 throwSimulatorException(env, e.code(), e.what());
246 JNIEXPORT void JNICALL
247 Java_org_oic_simulator_server_SimulatorSingleResource_stopUpdation
248 (JNIEnv *env, jobject object, jint id)
250 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
251 VALIDATE_OBJECT(env, singleResource)
253 singleResource->stopUpdation(id);
256 JNIEXPORT void JNICALL
257 Java_org_oic_simulator_server_SimulatorSingleResource_setModelChangeListener
258 (JNIEnv *env, jobject object, jobject listener)
260 VALIDATE_CALLBACK(env, listener)
262 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
263 VALIDATE_OBJECT(env, singleResource)
265 SimulatorResource::ResourceModelChangedCallback callback = std::bind(
266 [](const std::string & uri, SimulatorResourceModel & resModel,
267 const std::shared_ptr<JniListenerHolder> &listenerRef)
269 onResourceModelChange(listenerRef->get(), uri, resModel);
270 }, std::placeholders::_1, std::placeholders::_2, JniListenerHolder::create(env, listener));
274 singleResource->setModelChangeCallback(callback);
276 catch (InvalidArgsException &e)
278 throwInvalidArgsException(env, e.code(), e.what());
282 JNIEXPORT void JNICALL
283 Java_org_oic_simulator_server_SimulatorSingleResource_dispose
284 (JNIEnv *env, jobject object)
286 JniSharedObjectHolder<SimulatorSingleResource> *resource =
287 GetHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);