Imported Upstream version 1.0.1
[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     if (!jValue)
238     {
239         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Attribute value cannot be null!");
240         return;
241     }
242
243     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
244                                          jobject);
245     if (!resource)
246     {
247         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
248         return;
249     }
250
251     std::string key = env->GetStringUTFChars(jKey, NULL);
252     std::string value = env->GetStringUTFChars(jValue, NULL);
253     SimulatorResourceModel::Attribute att;
254     att.setName(key);
255     att.setValue(value);
256     resource->addAttribute(att);
257 }
258
259 JNIEXPORT void JNICALL
260 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeInteger
261 (JNIEnv *env, jobject jobject, jstring jKey, jint jValue)
262 {
263     if (!jKey)
264     {
265         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
266         return;
267     }
268
269     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
270                                          jobject);
271     if (!resource)
272     {
273         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
274         return;
275     }
276
277     std::string str = env->GetStringUTFChars(jKey, NULL);
278     resource->updateAttributeValue(str, static_cast<int>(jValue));
279 }
280
281 JNIEXPORT void JNICALL
282 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeDouble
283 (JNIEnv *env, jobject jobject, jstring jKey, jdouble jValue)
284 {
285     if (!jKey)
286     {
287         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
288         return;
289     }
290
291     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
292                                          jobject);
293     if (!resource)
294     {
295         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
296         return;
297     }
298
299     std::string str = env->GetStringUTFChars(jKey, NULL);
300     resource->updateAttributeValue(str, static_cast<double>(jValue));
301 }
302
303 JNIEXPORT void JNICALL
304 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeBoolean
305 (JNIEnv *env, jobject jobject, jstring jKey, jboolean jValue)
306 {
307     if (!jKey)
308     {
309         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
310         return;
311     }
312
313     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
314                                          jobject);
315     if (!resource)
316     {
317         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
318         return;
319     }
320
321     std::string str = env->GetStringUTFChars(jKey, NULL);
322     resource->updateAttributeValue(str, static_cast<bool>(jValue));
323 }
324
325 JNIEXPORT void JNICALL
326 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeString
327 (JNIEnv *env, jobject jobject, jstring jKey, jstring jValue)
328 {
329     if (!jKey)
330     {
331         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
332         return;
333     }
334
335     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
336                                          jobject);
337     if (!resource)
338     {
339         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
340         return;
341     }
342
343     std::string key = env->GetStringUTFChars(jKey, NULL);
344     std::string value = env->GetStringUTFChars(jValue, NULL);
345
346     resource->updateAttributeValue(key, value);
347 }
348
349 JNIEXPORT void JNICALL
350 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_updateAttributeFromAllowedValues
351 (JNIEnv *env, jobject object, jstring attrName, jint index)
352 {
353     if (!attrName || index < 0)
354     {
355         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
356         return;
357     }
358
359     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
360     if (!resource)
361     {
362         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
363         return;
364     }
365
366     const char *attrNameCStr = env->GetStringUTFChars(attrName, NULL);
367     if (!attrNameCStr)
368     {
369         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
370         return;
371     }
372
373     resource->updateAttributeValue(attrNameCStr, static_cast<int>(index));
374     env->ReleaseStringUTFChars(attrName, attrNameCStr);
375 }
376
377 JNIEXPORT void JNICALL
378 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setRange
379 (JNIEnv *env, jobject object, jstring attrName, jint min, jint max)
380 {
381     if (!attrName)
382     {
383         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
384         return;
385     }
386
387     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
388     if (!resource)
389     {
390         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
391         return;
392     }
393
394     const char *attrNameCStr = env->GetStringUTFChars(attrName, NULL);
395     if (!attrNameCStr)
396     {
397         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
398         return;
399     }
400
401     resource->setRange(attrNameCStr, static_cast<int>(min), static_cast<int>(max));
402     env->ReleaseStringUTFChars(attrName, attrNameCStr);
403 }
404
405 JNIEXPORT void JNICALL
406 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesInteger
407 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
408 {
409     if (!jKey || !jAllowedValues)
410     {
411         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
412         return;
413     }
414
415     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
416     if (!resource)
417     {
418         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
419         return;
420     }
421
422     const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
423     if (!keyCStr)
424     {
425         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
426         return;
427     }
428
429     resource->setAllowedValues(keyCStr, convertIntegerVector(env, jAllowedValues));
430     env->ReleaseStringUTFChars(jKey, keyCStr);
431 }
432
433 JNIEXPORT void JNICALL
434 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesDouble
435 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
436 {
437     if (!jKey || !jAllowedValues)
438     {
439         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
440         return;
441     }
442
443     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
444     if (!resource)
445     {
446         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
447         return;
448     }
449
450     const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
451     if (!keyCStr)
452     {
453         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
454         return;
455     }
456
457     resource->setAllowedValues(keyCStr, convertDoubleVector(env, jAllowedValues));
458     env->ReleaseStringUTFChars(jKey, keyCStr);
459 }
460
461 JNIEXPORT void JNICALL
462 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setAllowedValuesString
463 (JNIEnv *env, jobject object, jstring jKey, jobject jAllowedValues)
464 {
465     if (!jKey || !jAllowedValues)
466     {
467         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid parameter!");
468         return;
469     }
470
471     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
472     if (!resource)
473     {
474         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
475         return;
476     }
477
478     const char *keyCStr = env->GetStringUTFChars(jKey, NULL);
479     if (!keyCStr)
480     {
481         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
482         return;
483     }
484
485     resource->setAllowedValues(keyCStr, convertStringVector(env, jAllowedValues));
486     env->ReleaseStringUTFChars(jKey, keyCStr);
487 }
488
489 JNIEXPORT jint JNICALL
490 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startResourceAutomation
491 (JNIEnv *env, jobject object, jint automationType, jint updateInterval, jobject listener)
492 {
493     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
494     if (!resource)
495     {
496         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
497         return SIMULATOR_BAD_OBJECT;
498     }
499
500     if (!listener)
501     {
502         throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK,
503                                   "Start Resource Automation failed! Callback not set");
504         return SIMULATOR_INVALID_CALLBACK;
505     }
506
507     jweak jlistenerRef = env->NewWeakGlobalRef(listener);
508     updateCompleteCallback callback =  [jlistenerRef](const std::string & uri, const int automationID)
509     {
510         onAutomationComplete(jlistenerRef, uri, automationID);
511     };
512
513     AutomationType type = AutomationType::NORMAL;
514     if (1 == automationType)
515     {
516         type = AutomationType::RECURRENT;
517     }
518
519     int automationId = -1;
520
521     try
522     {
523         automationId = resource->startUpdateAutomation(type, updateInterval, callback);
524     }
525     catch (InvalidArgsException &e)
526     {
527         throwInvalidArgsException(env, e.code(), e.what());
528     }
529     catch (SimulatorException &e)
530     {
531         throwSimulatorException(env, e.code(), e.what());
532     }
533     catch (...)
534     {
535         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
536     }
537     return automationId;
538 }
539
540 JNIEXPORT jint JNICALL
541 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_startAttributeAutomation
542 (JNIEnv *env, jobject object, jstring attrName, jint automationType, jint updateInterval, jobject listener)
543 {
544     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
545     if (!resource)
546     {
547         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
548         return SIMULATOR_BAD_OBJECT;
549     }
550
551     if (!attrName)
552     {
553         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
554         return SIMULATOR_INVALID_PARAM;
555     }
556
557     if (!listener)
558     {
559         throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK,
560                                   "Start Attribute Automation failed! Callback not set");
561         return SIMULATOR_INVALID_CALLBACK;
562     }
563
564     jweak jlistenerRef = env->NewWeakGlobalRef(listener);
565     updateCompleteCallback callback =  [jlistenerRef](const std::string & uri, const int automationID)
566     {
567         onAutomationComplete(jlistenerRef, uri, automationID);
568     };
569
570     const char *attrNamePtr = env->GetStringUTFChars(attrName, NULL);
571
572     AutomationType type = AutomationType::NORMAL;
573     if (1 == automationType)
574     {
575         type = AutomationType::RECURRENT;
576     }
577
578     int automationId = -1;
579     try
580     {
581         automationId = resource->startUpdateAutomation(attrNamePtr, type, updateInterval, callback);
582     }
583     catch (InvalidArgsException &e)
584     {
585         throwInvalidArgsException(env, e.code(), e.what());
586         return -1;
587     }
588     catch (SimulatorException &e)
589     {
590         throwSimulatorException(env, e.code(), e.what());
591         return -1;
592     }
593     catch (...)
594     {
595         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
596         return -1;
597     }
598     env->ReleaseStringUTFChars(attrName, attrNamePtr);
599     return automationId;
600 }
601
602 JNIEXPORT void JNICALL
603 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_stopAutomation
604 (JNIEnv *env, jobject object, jint automationId)
605 {
606     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
607     if (!resource)
608     {
609         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
610         return;
611     }
612
613     try
614     {
615         resource->stopUpdateAutomation(automationId);
616     }
617     catch (SimulatorException &e)
618     {
619         throwSimulatorException(env, e.code(), e.what());
620     }
621     catch (...)
622     {
623         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
624     }
625
626     SIM_LOG(ILogger::INFO, "Automation has been forcibly stopped.")
627 }
628
629 JNIEXPORT void JNICALL
630 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_removeAttribute
631 (JNIEnv *env, jobject jobject, jstring jKey)
632 {
633     if (!jKey)
634     {
635         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid Attribute name!");
636         return;
637     }
638
639     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env,
640                                          jobject);
641     if (!resource)
642     {
643         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
644         return;
645     }
646
647     std::string str = env->GetStringUTFChars(jKey, NULL);
648     try
649     {
650         resource->removeAttribute(str);
651     }
652     catch (InvalidArgsException &e)
653     {
654         throwInvalidArgsException(env, e.code(), e.what());
655         return;
656     }
657     catch (SimulatorException &e)
658     {
659         throwSimulatorException(env, e.code(), e.what());
660         return;
661     }
662     catch (...)
663     {
664         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
665         return;
666     }
667 }
668
669 JNIEXPORT jobjectArray JNICALL
670 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_getObserversList
671 (JNIEnv *env, jobject object)
672 {
673     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
674     if (!resource)
675     {
676         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
677         return nullptr;
678     }
679
680     std::vector<ObserverInfo> observersList;
681     observersList = resource->getObserversList();
682
683     // Construct the object array and send it java layer
684     jobjectArray jobserversArray = env->NewObjectArray(observersList.size(),
685                                    gSimulatorClassRefs.classObserverInfo, NULL);
686     if (jobserversArray)
687     {
688         for (size_t i = 0; i < observersList.size(); i++)
689         {
690             jstring jaddress = env->NewStringUTF(observersList[i].address.c_str());
691             jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
692                                 gSimulatorClassRefs.classObserverInfoCtor, observersList[i].id,
693                                 jaddress, observersList[i].port);
694
695             env->SetObjectArrayElement(jobserversArray, i, jobserver);
696             env->DeleteLocalRef(jaddress);
697         }
698     }
699
700     return jobserversArray;
701 }
702
703 void onObserverChange(jweak jlistenerRef, const std::string &uri,
704                       ObservationStatus state, const ObserverInfo &observerInfo)
705 {
706     JNIEnv *env = getEnv();
707     if (nullptr == env)
708         return;
709
710     jobject observerChangeListener = env->NewLocalRef(jlistenerRef);
711     if (!observerChangeListener)
712     {
713         releaseEnv();
714         return;
715     }
716
717     jclass observerChangeCls = env->GetObjectClass(observerChangeListener);
718     if (!observerChangeCls)
719     {
720         releaseEnv();
721         return;
722     }
723
724     jmethodID observerChangeMId = env->GetMethodID(observerChangeCls, "onObserverChanged",
725                                   "(Ljava/lang/String;ILorg/oic/simulator/serviceprovider/ObserverInfo;)V");
726     if (!observerChangeMId)
727     {
728         releaseEnv();
729         return;
730     }
731
732     // Convert URI
733     jstring jUri = env->NewStringUTF(uri.c_str());
734
735     // Convert state
736     jint jstate = (state == ObservationStatus::OBSERVE_REGISTER) ? 0 : 1;
737
738     // Construct the java object of observerinfo
739     jstring jaddress = env->NewStringUTF(observerInfo.address.c_str());
740     jobject jobserver = (jobject) env->NewObject(gSimulatorClassRefs.classObserverInfo,
741                         gSimulatorClassRefs.classObserverInfoCtor, observerInfo.id,
742                         jaddress, observerInfo.port);
743
744     env->CallVoidMethod(observerChangeListener, observerChangeMId, jUri, jstate, jobserver);
745     env->DeleteLocalRef(jaddress);
746     if ((env)->ExceptionCheck())
747     {
748         releaseEnv();
749         return;
750     }
751
752     env->DeleteLocalRef(jUri);
753
754     releaseEnv();
755 }
756
757 JNIEXPORT void JNICALL
758 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_setObserverCallback
759 (JNIEnv *env, jobject object, jobject jcallback)
760 {
761     if (!jcallback)
762     {
763         throwInvalidArgsException(env, SIMULATOR_INVALID_CALLBACK, "Callback not set");
764         return;
765     }
766
767     jweak jlistenerRef = env->NewWeakGlobalRef(jcallback);
768     SimulatorResourceServer::ObserverCB callback =  [jlistenerRef](const std::string & uri,
769             ObservationStatus state, const ObserverInfo & observerInfo)
770     {
771         onObserverChange(jlistenerRef, uri, state, observerInfo);
772     };
773
774     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
775     if (!resource)
776     {
777         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
778         return;
779     }
780
781     resource->setObserverCallback(callback);
782 }
783
784 JNIEXPORT void JNICALL
785 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyObserver
786 (JNIEnv *env, jobject object, jint jId)
787 {
788     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
789     if (!resource)
790     {
791         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
792         return;
793     }
794
795     try
796     {
797         resource->notify(jId);
798     }
799     catch (SimulatorException &e)
800     {
801         throwSimulatorException(env, e.code(), e.what());
802     }
803     catch (...)
804     {
805         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
806     }
807 }
808
809 JNIEXPORT void JNICALL
810 Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_notifyAllObservers
811 (JNIEnv *env, jobject object)
812 {
813     SimulatorResourceServerSP resource = JniSimulatorResource::getJniSimulatorResourceSP(env, object);
814     if (!resource)
815     {
816         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "No resource!");
817         return;
818     }
819
820     try
821     {
822         resource->notifyAll();
823     }
824     catch (SimulatorException &e)
825     {
826         throwSimulatorException(env, e.code(), e.what());
827     }
828     catch (...)
829     {
830         throwSimulatorException(env, SIMULATOR_ERROR, "Unknown Exception");
831     }
832 }
833
834 JNIEXPORT void JNICALL Java_org_oic_simulator_serviceprovider_SimulatorResourceServer_dispose
835 (JNIEnv *env, jobject thiz)
836 {
837     JniSimulatorResource *resource = GetHandle<JniSimulatorResource>(env, thiz);
838     delete resource;
839 }
840