Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_resource_server_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_server_jni.h"
22 #include "simulator_resource_jni_util.h"
23 #include "simulator_common_jni.h"
24 #include "simulator_resource_model_jni.h"
25 #include "simulator_jni_utils.h"
26 #include "simulator_logger.h"
27 #include "simulator_jni_utils.h"
28
29 extern SimulatorClassRefs gSimulatorClassRefs;
30
31 JniSimulatorResource::JniSimulatorResource(SimulatorResourceServerSP &resource)
32     : m_sharedResource(resource) {}
33
34 SimulatorResourceServerSP JniSimulatorResource::getJniSimulatorResourceSP(JNIEnv *env,
35         jobject thiz)
36 {
37     JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
38     if (env->ExceptionCheck())
39     {
40         return NULL;
41     }
42     return resource->m_sharedResource;
43 }
44
45 jobject JniSimulatorResource::toJava(JNIEnv *env, jlong resource)
46 {
47     jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResource,
48                           gSimulatorClassRefs.classSimulatorResourceCtor, resource);
49     if (NULL == resourceObj)
50     {
51         return NULL;
52     }
53     return resourceObj;
54 }
55
56 void JniSimulatorResource::setResourceInfo(JNIEnv *env, jobject jobj)
57 {
58     if (!env || !jobj)
59         return;
60
61     std::string uri = m_sharedResource->getURI();
62     std::string resourceType = m_sharedResource->getResourceType();
63     std::string name = m_sharedResource->getName();
64     std::string interfaceType = m_sharedResource->getInterfaceType();
65
66     jfieldID fieldID = env->GetFieldID(gSimulatorClassRefs.classSimulatorResource, "resourceURI",
67                                        "Ljava/lang/String;");
68     jstring jUri = env->NewStringUTF(uri.c_str());
69     env->SetObjectField(jobj, fieldID, jUri);
70
71     fieldID = env->GetFieldID(gSimulatorClassRefs.classSimulatorResource, "resourceName",
72                               "Ljava/lang/String;");
73     jstring jName = env->NewStringUTF(name.c_str());
74     env->SetObjectField(jobj, fieldID, jName);
75
76     fieldID = env->GetFieldID(gSimulatorClassRefs.classSimulatorResource, "resourceType",
77                               "Ljava/lang/String;");
78     jstring jResourceType = env->NewStringUTF(resourceType.c_str());
79     env->SetObjectField(jobj, fieldID, jResourceType);
80
81     fieldID = env->GetFieldID(gSimulatorClassRefs.classSimulatorResource, "interfaceType",
82                               "Ljava/lang/String;");
83     jstring jInterfaceType = env->NewStringUTF(interfaceType.c_str());
84     env->SetObjectField(jobj, fieldID, jInterfaceType);
85
86     env->DeleteLocalRef(jUri);
87     env->DeleteLocalRef(jName);
88     env->DeleteLocalRef(jResourceType);
89     env->DeleteLocalRef(jInterfaceType);
90 }
91
92 void onAutomationComplete(jweak jlistenerRef, const std::string &uri,
93                           const int automationID)
94 {
95     JNIEnv *env = getEnv();
96     if (nullptr == env)
97         return;
98
99     jobject autoCompleteListener = env->NewLocalRef(jlistenerRef);
100     if (!autoCompleteListener)
101     {
102         releaseEnv();
103         return;
104     }
105
106     jclass autoCompleteCls = env->GetObjectClass(autoCompleteListener);
107     if (!autoCompleteCls)
108     {
109         releaseEnv();
110         return;
111     }
112
113     jmethodID autoCompleteMId = env->GetMethodID(autoCompleteCls, "onAutomationComplete",
114                                 "(Ljava/lang/String;I)V");
115     if (!autoCompleteMId)
116     {
117         releaseEnv();
118         return;
119     }
120
121     jstring jUri = env->NewStringUTF(uri.c_str());
122
123     env->CallVoidMethod(autoCompleteListener, autoCompleteMId, jUri, automationID);
124     if ((env)->ExceptionCheck())
125     {
126         releaseEnv();
127         return;
128     }
129
130     env->DeleteLocalRef(jUri);
131
132     releaseEnv();
133 }
134
135 JNIEXPORT jobject JNICALL
136 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_getModel
137 (JNIEnv *env, jobject object)
138 {
139     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
140     if (!resource)
141     {
142         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
143         return nullptr;
144     }
145
146     SimulatorResourceModel resModel = resource->getModel();
147     JSimulatorResourceModel *model = new JSimulatorResourceModel(resModel);
148     jobject jModel = JSimulatorResourceModel::toJava(env, reinterpret_cast<jlong>(model));
149     return jModel;
150 }
151
152 JNIEXPORT void JNICALL
153 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeInteger
154 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
155 {
156     if (!jKey)
157     {
158         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
159         return;
160     }
161
162     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
163                                          jobject);
164     if (!resource)
165     {
166         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
167         return;
168     }
169
170     std::string str = env->GetStringUTFChars(jKey, NULL);
171     SimulatorResourceModel::Attribute att;
172     att.setName(str);
173     att.setValue(static_cast<int>(jValue));
174     resource->addAttribute(att);
175 }
176
177 JNIEXPORT void JNICALL
178 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeDouble
179 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
180 {
181     if (!jKey)
182     {
183         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
184         return;
185     }
186
187     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
188                                          jobject);
189     if (!resource)
190     {
191         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
192         return;
193     }
194
195     std::string str = env->GetStringUTFChars(jKey, NULL);
196     SimulatorResourceModel::Attribute att;
197     att.setName(str);
198     att.setValue(static_cast<double>(jValue));
199     resource->addAttribute(att);
200 }
201
202 JNIEXPORT void JNICALL
203 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeBoolean
204 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
205 {
206     if (!jKey)
207     {
208         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
209         return;
210     }
211
212     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
213                                          jobject);
214     if (!resource)
215     {
216         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
217         return;
218     }
219
220     std::string str = env->GetStringUTFChars(jKey, NULL);
221     SimulatorResourceModel::Attribute att;
222     att.setName(str);
223     att.setValue(static_cast<bool>(jValue));
224     resource->addAttribute(att);
225 }
226
227 JNIEXPORT void JNICALL
228 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_addAttributeString
229 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
230 {
231     if (!jKey)
232     {
233         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
234         return;
235     }
236
237     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
238                                          jobject);
239     if (!resource)
240     {
241         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
242         return;
243     }
244
245     std::string key = env->GetStringUTFChars(jKey, NULL);
246     std::string value = env->GetStringUTFChars(jValue, NULL);
247     SimulatorResourceModel::Attribute att;
248     att.setName(key);
249     att.setValue(value);
250     resource->addAttribute(att);
251 }
252
253 JNIEXPORT void JNICALL
254 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeInteger
255 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
256 {
257     if (!jKey)
258     {
259         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
260         return;
261     }
262
263     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
264                                          jobject);
265     if (!resource)
266     {
267         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
268         return;
269     }
270
271     std::string str = env->GetStringUTFChars(jKey, NULL);
272     resource->updateAttributeValue(str, static_cast<int>(jValue));
273 }
274
275 JNIEXPORT void JNICALL
276 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeDouble
277 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
278 {
279     if (!jKey)
280     {
281         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
282         return;
283     }
284
285     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
286                                          jobject);
287     if (!resource)
288     {
289         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
290         return;
291     }
292
293     std::string str = env->GetStringUTFChars(jKey, NULL);
294     resource->updateAttributeValue(str, static_cast<double>(jValue));
295 }
296
297 JNIEXPORT void JNICALL
298 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeBoolean
299 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
300 {
301     if (!jKey)
302     {
303         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
304         return;
305     }
306
307     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
308                                          jobject);
309     if (!resource)
310     {
311         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
312         return;
313     }
314
315     std::string str = env->GetStringUTFChars(jKey, NULL);
316     resource->updateAttributeValue(str, static_cast<bool>(jValue));
317 }
318
319 JNIEXPORT void JNICALL
320 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeString
321 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
322 {
323     if (!jKey)
324     {
325         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
326         return;
327     }
328
329     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
330                                          jobject);
331     if (!resource)
332     {
333         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
334         return;
335     }
336
337     std::string key = env->GetStringUTFChars(jKey, NULL);
338     std::string value = env->GetStringUTFChars(jValue, NULL);
339
340     resource->updateAttributeValue(key, value);
341 }
342
343 JNIEXPORT void JNICALL
344 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeFromAllowedValues
345 (JNIEnv *env, jobject object, jstring attrName, jint index)
346 {
347     if (!attrName || index < 0)
348     {
349         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
350         return;
351     }
352
353     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
354     if (!resource)
355     {
356         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
357         return;
358     }
359
360     const char *attrNameCStr = env->GetStringUTFChars(attrName, NULL);
361     if (!attrNameCStr)
362     {
363         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
364         return;
365     }
366
367     resource->updateAttributeValue(attrNameCStr, static_cast<int>(index));
368     env->ReleaseStringUTFChars(attrName, attrNameCStr);
369 }
370
371 JNIEXPORT void JNICALL
372 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setRange
373 (JNIEnv *env, jobject object, jstring attrName, jint min, jint max)
374 {
375     if (!attrName)
376     {
377         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
378         return;
379     }
380
381     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
382     if (!resource)
383     {
384         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
385         return;
386     }
387
388     const char *attrNameCStr = env->GetStringUTFChars(attrName, NULL);
389     if (!attrNameCStr)
390     {
391         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
392         return;
393     }
394
395     resource->setRange(attrNameCStr, static_cast<int>(min), static_cast<int>(max));
396     env->ReleaseStringUTFChars(attrName, attrNameCStr);
397 }
398
399 JNIEXPORT void JNICALL
400 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesInteger
401 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
402 {
403     if (!jKey || !jAllowedValues)
404     {
405         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
406         return;
407     }
408
409     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
410     if (!resource)
411     {
412         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
413         return;
414     }
415
416     const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
417     if (!keyCStr)
418     {
419         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
420         return;
421     }
422
423     resource->setAllowedValues(keyCStr, convertIntegerVector(env, jAllowedValues));
424     env->ReleaseStringUTFChars(jKey, keyCStr);
425 }
426
427 JNIEXPORT void JNICALL
428 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesDouble
429 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
430 {
431     if (!jKey || !jAllowedValues)
432     {
433         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
434         return;
435     }
436
437     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
438     if (!resource)
439     {
440         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
441         return;
442     }
443
444     const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
445     if (!keyCStr)
446     {
447         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
448         return;
449     }
450
451     resource->setAllowedValues(keyCStr, convertDoubleVector(env, jAllowedValues));
452     env->ReleaseStringUTFChars(jKey, keyCStr);
453 }
454
455 JNIEXPORT void JNICALL
456 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesString
457 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
458 {
459     if (!jKey || !jAllowedValues)
460     {
461         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
462         return;
463     }
464
465     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
466     if (!resource)
467     {
468         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
469         return;
470     }
471
472     const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
473     if (!keyCStr)
474     {
475         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
476         return;
477     }
478
479     resource->setAllowedValues(keyCStr, convertStringVector(env, jAllowedValues));
480     env->ReleaseStringUTFChars(jKey, keyCStr);
481 }
482
483 JNIEXPORT jint JNICALL
484 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAutomation
485 (JNIEnv *env, jobject object, jint automationType, jobject listener)
486 {
487     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
488     if (!resource)
489     {
490         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
491         return SIMULATOR_BAD_OBJECT;
492     }
493
494     if (!listener)
495     {
496         throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK,
497                                   "Start Resource Automation failed! Callback not set");
498         return SIMULATOR_INVALID_CALLBACK;
499     }
500
501     jweak jlistenerRef = env->NewWeakGlobalRef(listener);
502     updateCompleteCallback callback =  [jlistenerRef](const std::string & uri, const int automationID)
503     {
504         onAutomationComplete(jlistenerRef, uri, automationID);
505     };
506
507     AutomationType type = AutomationType::NORMAL;
508     if (1 == automationType)
509     {
510         type = AutomationType::RECURRENT;
511     }
512
513     int automationId = -1;
514
515     try
516     {
517         automationId = resource->startUpdateAutomation(type, callback);
518     }
519     catch (InvalidArgsException &e)
520     {
521         throwInvalidArgsException(env, e.code(), e.what());
522     }
523     catch (SimulatorException &e)
524     {
525         throwSimulatorException(env, e.code(), e.what());
526     }
527     catch (...)
528     {
529         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
530     }
531     return automationId;
532 }
533
534 JNIEXPORT jint JNICALL
535 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startAttributeAutomation
536 (JNIEnv *env, jobject object, jstring attrName, jint automationType, jobject listener)
537 {
538     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
539     if (!resource)
540     {
541         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
542         return SIMULATOR_BAD_OBJECT;
543     }
544
545     if (!attrName)
546     {
547         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
548         return SIMULATOR_INVALID_PARAM;
549     }
550
551     if (!listener)
552     {
553         throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK,
554                                   "Start Attribute Automation failed! Callback not set");
555         return SIMULATOR_INVALID_CALLBACK;
556     }
557
558     jweak jlistenerRef = env->NewWeakGlobalRef(listener);
559     updateCompleteCallback callback =  [jlistenerRef](const std::string & uri, const int automationID)
560     {
561         onAutomationComplete(jlistenerRef, uri, automationID);
562     };
563
564     const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
565
566     AutomationType type = AutomationType::NORMAL;
567     if (1 == automationType)
568     {
569         type = AutomationType::RECURRENT;
570     }
571
572     int automationId = -1;
573     try
574     {
575         automationId = resource->startUpdateAutomation(attrNamePtr, type, callback);
576     }
577     catch (InvalidArgsException &e)
578     {
579         throwInvalidArgsException(env, e.code(), e.what());
580         return -1;
581     }
582     catch (SimulatorException &e)
583     {
584         throwSimulatorException(env, e.code(), e.what());
585         return -1;
586     }
587     catch (...)
588     {
589         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
590         return -1;
591     }
592     env->ReleaseStringUTFChars(attrName, attrNamePtr);
593     return automationId;
594 }
595
596 JNIEXPORT void JNICALL
597 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_stopAutomation
598 (JNIEnv *env, jobject object, jint automationId)
599 {
600     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
601     if (!resource)
602     {
603         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
604         return;
605     }
606
607     try
608     {
609         resource->stopUpdateAutomation(automationId);
610     }
611     catch (SimulatorException &e)
612     {
613         throwSimulatorException(env, e.code(), e.what());
614     }
615     catch (...)
616     {
617         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
618     }
619
620     SIM_LOG(ILogger::INFO, "Automation has been forcibly stopped.")
621 }
622
623 JNIEXPORT void JNICALL
624 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_removeAttribute
625 (JNIEnv *env, jobject jobject, jstring jKey)
626 {
627     if (!jKey)
628     {
629         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
630         return;
631     }
632
633     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
634                                          jobject);
635     if (!resource)
636     {
637         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
638         return;
639     }
640
641     std::string str = env->GetStringUTFChars(jKey, NULL);
642     resource->removeAttribute(str);
643 }
644
645 JNIEXPORT jobjectArray JNICALL
646 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_getObserversList
647 (JNIEnv *env, jobject object)
648 {
649     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
650     if (!resource)
651     {
652         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
653         return nullptr;
654     }
655
656     std::vector<ObserverInfo> observersList;
657     observersList = resource->getObserversList();
658
659     // Construct the object array and send it java layer
660     jobjectArray jobserversArray = env->NewObjectArray(observersList.size(),
661                                    gSimulatorClassRefs.classObserverInfo, NULL);
662     if (jobserversArray)
663     {
664         for (size_t i = 0; i < observersList.size(); i++)
665         {
666             jstring jaddress = env->NewStringUTF(observersList[i].address.c_str());
667             jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
668                                 gSimulatorClassRefs.classObserverInfoCtor, observersList[i].id,
669                                 jaddress, observersList[i].port);
670
671             env->SetObjectArrayElement(jobserversArray, i, jobserver);
672             env->DeleteLocalRef(jaddress);
673         }
674     }
675
676     return jobserversArray;
677 }
678
679 void onObserverChange(jweak jlistenerRef, const std::string &uri,
680                       ObservationStatus state, const ObserverInfo &observerInfo)
681 {
682     JNIEnv *env = getEnv();
683     if (nullptr == env)
684         return;
685
686     jobject observerChangeListener = env->NewLocalRef(jlistenerRef);
687     if (!observerChangeListener)
688     {
689         releaseEnv();
690         return;
691     }
692
693     jclass observerChangeCls = env->GetObjectClass(observerChangeListener);
694     if (!observerChangeCls)
695     {
696         releaseEnv();
697         return;
698     }
699
700     jmethodID observerChangeMId = env->GetMethodID(observerChangeCls, "onObserverChanged",
701                                   "(Ljava/lang/String;ILorg/oic/simulator/serviceprovider/ObserverInfo;)V");
702     if (!observerChangeMId)
703     {
704         releaseEnv();
705         return;
706     }
707
708     // Convert URI
709     jstring jUri = env->NewStringUTF(uri.c_str());
710
711     // Convert state
712     jint jstate = (state == ObservationStatus::OBSERVE_REGISTER) ? 0 : 1;
713
714     // Construct the java object of observerinfo
715     jstring jaddress = env->NewStringUTF(observerInfo.address.c_str());
716     jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
717                         gSimulatorClassRefs.classObserverInfoCtor, observerInfo.id,
718                         jaddress, observerInfo.port);
719
720     env->CallVoidMethod(observerChangeListener, observerChangeMId, jUri, jstate, jobserver);
721     env->DeleteLocalRef(jaddress);
722     if ((env)->ExceptionCheck())
723     {
724         releaseEnv();
725         return;
726     }
727
728     env->DeleteLocalRef(jUri);
729
730     releaseEnv();
731 }
732
733 JNIEXPORT void JNICALL
734 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setObserverCallback
735 (JNIEnv *env, jobject object, jobject jcallback)
736 {
737     if (!jcallback)
738     {
739         throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Callback not set");
740         return;
741     }
742
743     jweak jlistenerRef = env->NewWeakGlobalRef(jcallback);
744     SimulatorResourceServer::ObserverCB callback =  [jlistenerRef](const std::string & uri,
745             ObservationStatus state, const ObserverInfo & observerInfo)
746     {
747         onObserverChange(jlistenerRef, uri, state, observerInfo);
748     };
749
750     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
751     if (!resource)
752     {
753         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
754         return;
755     }
756
757     resource->setObserverCallback(callback);
758 }
759
760 JNIEXPORT void JNICALL
761 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyObserver
762 (JNIEnv *env, jobject object, jint jId)
763 {
764     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
765     if (!resource)
766     {
767         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
768         return;
769     }
770
771     try
772     {
773         resource->notify(jId);
774     }
775     catch (SimulatorException &e)
776     {
777         throwSimulatorException(env, e.code(), e.what());
778     }
779     catch (...)
780     {
781         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
782     }
783 }
784
785 JNIEXPORT void JNICALL
786 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyAllObservers
787 (JNIEnv *env, jobject object)
788 {
789     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
790     if (!resource)
791     {
792         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
793         return;
794     }
795
796     try
797     {
798         resource->notifyAll();
799     }
800     catch (SimulatorException &e)
801     {
802         throwSimulatorException(env, e.code(), e.what());
803     }
804     catch (...)
805     {
806         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
807     }
808 }
809
810 JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_dispose
811 (JNIEnv *env, jobject thiz)
812 {
813     JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
814     delete resource;
815 }
816