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 onAutoUpdationComplete(jobject listener, const std::string &uri, const int id)
45 JNIEnv *env = getEnv();
49 jclass listenerCls = env->GetObjectClass(listener);
50 jmethodID listenerMethod = env->GetMethodID(listenerCls, "onUpdateComplete",
51 "(Ljava/lang/String;I)V");
53 jstring jUri = env->NewStringUTF(uri.c_str());
54 env->CallVoidMethod(listener, listenerMethod, jUri, id);
62 JNIEXPORT jobject JNICALL
63 Java_org_oic_simulator_server_SimulatorSingleResource_getAttribute
64 (JNIEnv *env, jobject object, jstring attrName)
66 VALIDATE_INPUT_RET(env, !attrName, "Attribute name is null!", nullptr)
68 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
69 VALIDATE_OBJECT_RET(env, singleResource, nullptr)
71 JniString jniAttrName(env, attrName);
72 SimulatorResourceModel::Attribute attribute;
73 if (singleResource->getAttribute(jniAttrName.get(), attribute))
74 return simulatorResourceAttributeToJava(env, attribute);
78 JNIEXPORT void JNICALL
79 Java_org_oic_simulator_server_SimulatorSingleResource_addAttribute
80 (JNIEnv *env, jobject object, jobject resAttribute)
82 VALIDATE_INPUT(env, !resAttribute, "Resource attribute is null!")
84 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
85 VALIDATE_OBJECT(env, singleResource)
89 SimulatorResourceModel::Attribute attribute;
90 if (!simulatorResourceAttributeToCpp(env, resAttribute, attribute))
92 throwSimulatorException(env, SIMULATOR_ERROR,
93 "Failed to covnert SimulatorResourceAttribute java object!");
97 singleResource->addAttribute(attribute);
99 catch (SimulatorException &e)
101 throwSimulatorException(env, e.code(), e.what());
105 JNIEXPORT void JNICALL
106 Java_org_oic_simulator_server_SimulatorSingleResource_updateAttribute
107 (JNIEnv *env, jobject object, jstring attrName, jobject attrValue)
109 VALIDATE_INPUT(env, !attrName, "Attribute name is null!")
110 VALIDATE_INPUT(env, !attrValue, "Attribute value is null!")
112 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
113 VALIDATE_OBJECT(env, singleResource)
115 SimulatorResourceModel::ValueVariant value;
116 if (!AttributeValueToCpp(env, attrValue, value))
118 throwSimulatorException(env, SIMULATOR_ERROR,
119 "Failed to covnert AttributeValue java object!");
123 SimulatorResourceModel::Attribute attribute(JniString(env, attrName).get());
124 attribute.setValue(value);
125 singleResource->updateAttributeValue(attribute);
128 JNIEXPORT void JNICALL
129 Java_org_oic_simulator_server_SimulatorSingleResource_removeAttribute
130 (JNIEnv *env, jobject object, jstring attrName)
132 VALIDATE_INPUT(env, !attrName, "Attribute name is null!")
134 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
135 VALIDATE_OBJECT(env, singleResource)
139 JniString jniAttrName(env, attrName);
140 singleResource->removeAttribute(jniAttrName.get());
142 catch (InvalidArgsException &e)
144 throwInvalidArgsException(env, e.code(), e.what());
148 JNIEXPORT jint JNICALL
149 Java_org_oic_simulator_server_SimulatorSingleResource_startResourceUpdation
150 (JNIEnv *env, jobject object, jint type, jint interval, jobject listener)
152 VALIDATE_CALLBACK_RET(env, listener, -1)
154 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
155 VALIDATE_OBJECT_RET(env, singleResource, -1)
157 jobject listenerRef = env->NewGlobalRef(listener);
158 updateCompleteCallback callback = [listenerRef](const std::string & uri, const int id)
160 onAutoUpdationComplete(listenerRef, uri, id);
165 int id = singleResource->startResourceUpdation((1 == type) ? AutomationType::RECURRENT :
166 AutomationType::NORMAL, interval, callback);
169 catch (InvalidArgsException &e)
171 throwInvalidArgsException(env, e.code(), e.what());
173 catch (SimulatorException &e)
175 throwSimulatorException(env, e.code(), e.what());
181 JNIEXPORT jint JNICALL
182 Java_org_oic_simulator_server_SimulatorSingleResource_startAttributeUpdation
183 (JNIEnv *env, jobject object, jstring attrName, jint type, jint interval, jobject listener)
185 VALIDATE_INPUT_RET(env, !attrName, "Attribute name is null!", -1)
186 VALIDATE_CALLBACK_RET(env, listener, -1)
188 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
189 VALIDATE_OBJECT_RET(env, singleResource, -1)
191 jobject listenerRef = env->NewGlobalRef(listener);
192 updateCompleteCallback callback = [listenerRef](const std::string & uri, const int id)
194 onAutoUpdationComplete(listenerRef, uri, id);
197 JniString jniAttrName(env, attrName);
201 int id = singleResource->startAttributeUpdation(jniAttrName.get(),
202 (1 == type) ? AutomationType::RECURRENT : AutomationType::NORMAL,
206 catch (InvalidArgsException &e)
208 throwInvalidArgsException(env, e.code(), e.what());
210 catch (SimulatorException &e)
212 throwSimulatorException(env, e.code(), e.what());
218 JNIEXPORT void JNICALL
219 Java_org_oic_simulator_server_SimulatorSingleResource_stopUpdation
220 (JNIEnv *env, jobject object, jint id)
222 SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
223 VALIDATE_OBJECT(env, singleResource)
225 singleResource->stopUpdation(id);
228 JNIEXPORT void JNICALL
229 Java_org_oic_simulator_server_SimulatorSingleResource_dispose
230 (JNIEnv *env, jobject object)
232 JniSharedObjectHolder<SimulatorSingleResource> *resource =
233 GetHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);