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_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"
28 #include "simulator_collection_resource.h"
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;}
33 extern SimulatorResourceSP SimulatorResourceToCpp(JNIEnv *env, jobject object);
35 static SimulatorCollectionResourceSP simulatorCollectionResourceToCpp(JNIEnv *env, jobject object)
37 JniSharedObjectHolder<SimulatorCollectionResource> *jniResource =
38 getHandle<JniSharedObjectHolder<SimulatorCollectionResource>>(env, object);
40 return jniResource->get();
48 JNIEXPORT jobject JNICALL
49 Java_org_oic_simulator_server_SimulatorCollectionResource_nativeGetSupportedResources
50 (JNIEnv *env, jobject object)
52 auto collectionResource = simulatorCollectionResourceToCpp(env, object);
53 VALIDATE_OBJECT_RET(env, collectionResource, nullptr)
55 std::vector<std::string> supportedTypes = collectionResource->getSupportedResources();
56 return JniVector(env).toJava(supportedTypes);
59 JNIEXPORT void JNICALL
60 Java_org_oic_simulator_server_SimulatorCollectionResource_nativeAddChildResource
61 (JNIEnv *env, jobject object, jobject resource)
63 VALIDATE_INPUT(env, !resource, "Child resource is null!")
65 auto childResource = SimulatorResourceToCpp(env, resource);
66 VALIDATE_INPUT(env, !childResource, "No corresponding native object of child resource!")
68 auto collectionResource = simulatorCollectionResourceToCpp(env, object);
69 VALIDATE_OBJECT(env, collectionResource)
73 collectionResource->addChildResource(childResource);
75 catch (InvalidArgsException &e)
77 ThrowInvalidArgsException(env, e.code(), e.what());
79 catch (SimulatorException &e)
81 ThrowSimulatorException(env, e.code(), e.what());
85 JNIEXPORT void JNICALL
86 Java_org_oic_simulator_server_SimulatorCollectionResource_nativeRemoveChildResource
87 (JNIEnv *env, jobject object, jobject resource)
89 VALIDATE_INPUT(env, !resource, "Child resource is null!")
91 auto childResource = SimulatorResourceToCpp(env, resource);
92 VALIDATE_INPUT(env, !childResource, "No corresponding native object of child resource!")
94 auto collectionResource = simulatorCollectionResourceToCpp(env, object);
95 VALIDATE_OBJECT(env, collectionResource)
99 collectionResource->removeChildResource(childResource);
101 catch (InvalidArgsException &e)
103 ThrowInvalidArgsException(env, e.code(), e.what());
105 catch (SimulatorException &e)
107 ThrowSimulatorException(env, e.code(), e.what());
111 JNIEXPORT void JNICALL
112 Java_org_oic_simulator_server_SimulatorCollectionResource_nativeRemoveChildResourceByUri
113 (JNIEnv *env, jobject object, jstring uri)
115 auto collectionResource = simulatorCollectionResourceToCpp(env, object);
116 VALIDATE_OBJECT(env, collectionResource)
120 JniString jniUri(env, uri);
121 collectionResource->removeChildResource(jniUri.get());
123 catch (InvalidArgsException &e)
125 ThrowInvalidArgsException(env, e.code(), e.what());
127 catch (SimulatorException &e)
129 ThrowSimulatorException(env, e.code(), e.what());
133 JNIEXPORT jobject JNICALL
134 Java_org_oic_simulator_server_SimulatorCollectionResource_nativeGetChildResources
135 (JNIEnv *env, jobject object)
137 auto collectionResource = simulatorCollectionResourceToCpp(env, object);
138 VALIDATE_OBJECT_RET(env, collectionResource, nullptr)
140 std::vector<SimulatorResourceSP> childResources = collectionResource->getChildResources();
141 return CreateSimulatorResourceVector(env, childResources);
144 JNIEXPORT void JNICALL Java_org_oic_simulator_server_SimulatorCollectionResource_nativeDispose
145 (JNIEnv *env, jobject object)
147 JniSharedObjectHolder<SimulatorCollectionResource> *resource =
148 getHandle<JniSharedObjectHolder<SimulatorCollectionResource>>(env, object);