b9cadf2ffaeeff4f25b33f7461a4de01f417a083
[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     resource->stopUpdateAutomation(automationId);
608     SIM_LOG(ILogger::INFO, "Automation has been forcibly stopped.")
609 }
610
611 JNIEXPORT void JNICALL
612 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_removeAttribute
613 (JNIEnv *env, jobject jobject, jstring jKey)
614 {
615     if (!jKey)
616     {
617         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
618         return;
619     }
620
621     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
622                                          jobject);
623     if (!resource)
624     {
625         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
626         return;
627     }
628
629     std::string str = env->GetStringUTFChars(jKey, NULL);
630     resource->removeAttribute(str);
631 }
632
633 JNIEXPORT jobjectArray JNICALL
634 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_getObserversList
635 (JNIEnv *env, jobject object)
636 {
637     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
638     if (!resource)
639     {
640         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
641         return nullptr;
642     }
643
644     std::vector<ObserverInfo> observersList;
645     observersList = resource->getObserversList();
646
647     // Construct the object array and send it java layer
648     jobjectArray jobserversArray = env->NewObjectArray(observersList.size(),
649                                    gSimulatorClassRefs.classObserverInfo, NULL);
650     if (jobserversArray)
651     {
652         for (size_t i = 0; i < observersList.size(); i++)
653         {
654             jstring jaddress = env->NewStringUTF(observersList[i].address.c_str());
655             jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
656                                 gSimulatorClassRefs.classObserverInfoCtor, observersList[i].id,
657                                 jaddress, observersList[i].port);
658
659             env->SetObjectArrayElement(jobserversArray, i, jobserver);
660             env->DeleteLocalRef(jaddress);
661         }
662     }
663
664     return jobserversArray;
665 }
666
667 void onObserverChange(jweak jlistenerRef, const std::string &uri,
668                       ObservationStatus state, const ObserverInfo &observerInfo)
669 {
670     JNIEnv *env = getEnv();
671     if (nullptr == env)
672         return;
673
674     jobject observerChangeListener = env->NewLocalRef(jlistenerRef);
675     if (!observerChangeListener)
676     {
677         releaseEnv();
678         return;
679     }
680
681     jclass observerChangeCls = env->GetObjectClass(observerChangeListener);
682     if (!observerChangeCls)
683     {
684         releaseEnv();
685         return;
686     }
687
688     jmethodID observerChangeMId = env->GetMethodID(observerChangeCls, "onObserverChanged",
689                                   "(Ljava/lang/String;ILorg/oic/simulator/serviceprovider/ObserverInfo;)V");
690     if (!observerChangeMId)
691     {
692         releaseEnv();
693         return;
694     }
695
696     // Convert URI
697     jstring jUri = env->NewStringUTF(uri.c_str());
698
699     // Convert state
700     jint jstate = (state == ObservationStatus::OBSERVE_REGISTER) ? 0 : 1;
701
702     // Construct the java object of observerinfo
703     jstring jaddress = env->NewStringUTF(observerInfo.address.c_str());
704     jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
705                         gSimulatorClassRefs.classObserverInfoCtor, observerInfo.id,
706                         jaddress, observerInfo.port);
707
708     env->CallVoidMethod(observerChangeListener, observerChangeMId, jUri, jstate, jobserver);
709     env->DeleteLocalRef(jaddress);
710     if ((env)->ExceptionCheck())
711     {
712         releaseEnv();
713         return;
714     }
715
716     env->DeleteLocalRef(jUri);
717
718     releaseEnv();
719 }
720
721 JNIEXPORT void JNICALL
722 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setObserverCallback
723 (JNIEnv *env, jobject object, jobject jcallback)
724 {
725     if (!jcallback)
726     {
727         throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Callback not set");
728         return;
729     }
730
731     jweak jlistenerRef = env->NewWeakGlobalRef(jcallback);
732     SimulatorResourceServer::ObserverCB callback =  [jlistenerRef](const std::string & uri,
733             ObservationStatus state, const ObserverInfo & observerInfo)
734     {
735         onObserverChange(jlistenerRef, uri, state, observerInfo);
736     };
737
738     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
739     if (!resource)
740     {
741         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
742         return;
743     }
744
745     resource->setObserverCallback(callback);
746 }
747
748 JNIEXPORT void JNICALL
749 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyObserver
750 (JNIEnv *env, jobject object, jint jId)
751 {
752     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
753     if (!resource)
754     {
755         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
756         return;
757     }
758
759     try
760     {
761         resource->notify(jId);
762     }
763     catch (SimulatorException &e)
764     {
765         throwSimulatorException(env, e.code(), e.what());
766     }
767     catch (...)
768     {
769         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
770     }
771 }
772
773 JNIEXPORT void JNICALL
774 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyAllObservers
775 (JNIEnv *env, jobject object)
776 {
777     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
778     if (!resource)
779     {
780         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
781         return;
782     }
783
784     try
785     {
786         resource->notifyAll();
787     }
788     catch (SimulatorException &e)
789     {
790         throwSimulatorException(env, e.code(), e.what());
791     }
792     catch (...)
793     {
794         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
795     }
796 }
797
798 JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_dispose
799 (JNIEnv *env, jobject thiz)
800 {
801     JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
802     delete resource;
803 }
804