Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_collection_resource_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_exceptions_jni.h"
22 #include "simulator_resource_utils_jni.h"
23 #include "simulator_utils_jni.h"
24 #include "jni_sharedobject_holder.h"
25 #include "jni_string.h"
26 #include "jni_vector.h"
27
28 #include "simulator_collection_resource.h"
29
30 #define VALIDATE_OBJECT(ENV, OBJECT) if (!OBJECT){ThrowBadObjectException(ENV, "No corresponsing native object!"); return;}
31 #define VALIDATE_OBJECT_RET(ENV, OBJECT, RET) if (!OBJECT){ThrowBadObjectException(ENV, "No corresponsing native object!"); return RET;}
32
33 extern SimulatorResourceSP SimulatorResourceToCpp(JNIEnv *env, jobject object);
34
35 static SimulatorCollectionResourceSP simulatorCollectionResourceToCpp(JNIEnv *env, jobject object)
36 {
37     JniSharedObjectHolder<SimulatorCollectionResource> *jniResource =
38         getHandle<JniSharedObjectHolder<SimulatorCollectionResource>>(env, object);
39     if (jniResource)
40         return jniResource->get();
41     return nullptr;
42 }
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 JNIEXPORT jobject JNICALL
49 Java_org_oic_simulator_server_SimulatorCollectionResource_nativeGetSupportedResources
50 (JNIEnv *env, jobject object)
51 {
52     auto collectionResource = simulatorCollectionResourceToCpp(env, object);
53     VALIDATE_OBJECT_RET(env, collectionResource, nullptr)
54
55     std::vector<std::string> supportedTypes = collectionResource->getSupportedResources();
56     return JniVector(env).toJava(supportedTypes);
57 }
58
59 JNIEXPORT void JNICALL
60 Java_org_oic_simulator_server_SimulatorCollectionResource_nativeAddChildResource
61 (JNIEnv *env, jobject object, jobject resource)
62 {
63     VALIDATE_INPUT(env, !resource, "Child resource is null!")
64
65     auto childResource = SimulatorResourceToCpp(env, resource);
66     VALIDATE_INPUT(env, !childResource, "No corresponding native object of child resource!")
67
68     auto collectionResource = simulatorCollectionResourceToCpp(env, object);
69     VALIDATE_OBJECT(env, collectionResource)
70
71     try
72     {
73         collectionResource->addChildResource(childResource);
74     }
75     catch (InvalidArgsException &e)
76     {
77         ThrowInvalidArgsException(env, e.code(), e.what());
78     }
79     catch (SimulatorException &e)
80     {
81         ThrowSimulatorException(env, e.code(), e.what());
82     }
83 }
84
85 JNIEXPORT void JNICALL
86 Java_org_oic_simulator_server_SimulatorCollectionResource_nativeRemoveChildResource
87 (JNIEnv *env, jobject object, jobject resource)
88 {
89     VALIDATE_INPUT(env, !resource, "Child resource is null!")
90
91     auto childResource = SimulatorResourceToCpp(env, resource);
92     VALIDATE_INPUT(env, !childResource, "No corresponding native object of child resource!")
93
94     auto collectionResource = simulatorCollectionResourceToCpp(env, object);
95     VALIDATE_OBJECT(env, collectionResource)
96
97     try
98     {
99         collectionResource->removeChildResource(childResource);
100     }
101     catch (InvalidArgsException &e)
102     {
103         ThrowInvalidArgsException(env, e.code(), e.what());
104     }
105     catch (SimulatorException &e)
106     {
107         ThrowSimulatorException(env, e.code(), e.what());
108     }
109 }
110
111 JNIEXPORT void JNICALL
112 Java_org_oic_simulator_server_SimulatorCollectionResource_nativeRemoveChildResourceByUri
113 (JNIEnv *env, jobject object, jstring uri)
114 {
115     auto collectionResource = simulatorCollectionResourceToCpp(env, object);
116     VALIDATE_OBJECT(env, collectionResource)
117
118     try
119     {
120         JniString jniUri(env, uri);
121         collectionResource->removeChildResource(jniUri.get());
122     }
123     catch (InvalidArgsException &e)
124     {
125         ThrowInvalidArgsException(env, e.code(), e.what());
126     }
127     catch (SimulatorException &e)
128     {
129         ThrowSimulatorException(env, e.code(), e.what());
130     }
131 }
132
133 JNIEXPORT jobject JNICALL
134 Java_org_oic_simulator_server_SimulatorCollectionResource_nativeGetChildResources
135 (JNIEnv *env, jobject object)
136 {
137     auto collectionResource = simulatorCollectionResourceToCpp(env, object);
138     VALIDATE_OBJECT_RET(env, collectionResource, nullptr)
139
140     std::vector<SimulatorResourceSP> childResources = collectionResource->getChildResources();
141     return CreateSimulatorResourceVector(env,  childResources);
142 }
143
144 JNIEXPORT void JNICALL Java_org_oic_simulator_server_SimulatorCollectionResource_nativeDispose
145 (JNIEnv *env, jobject object)
146 {
147     JniSharedObjectHolder<SimulatorCollectionResource> *resource =
148         getHandle<JniSharedObjectHolder<SimulatorCollectionResource>>(env, object);
149     delete resource;
150 }
151
152 #ifdef __cplusplus
153 }
154 #endif