Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-container / src / BaseActivator.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 #include "ResourceContainerImpl.h"
21 #if(JAVA_SUPPORT)
22 #include <jni.h>
23 #include "org_iotivity_resourcecontainer_bundle_api_BaseActivator.h"
24 #include "JavaBundleResource.h"
25
26 using namespace OIC::Service;
27
28 std::map< string, BundleResource::Ptr > java_resources;
29
30 /*
31  * Class:     org_iotivity_resourcecontainer_bundle_api_BaseActivator
32  * Method:    registerJavaResource
33  * Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
34  */
35 JNIEXPORT jint JNICALL
36 Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_registerJavaResource
37 (JNIEnv *env, jobject obj, jobject bundleResource, jobjectArray attributes, jstring bundleId,
38  jstring uri, jstring resourceType, jstring res_name)
39 {
40     const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
41     const char *str_uri = env->GetStringUTFChars(uri, 0);
42     const char *str_resourceType = env->GetStringUTFChars(resourceType, 0);
43     const char *str_res_name = env->GetStringUTFChars(res_name, 0);
44     int ret;
45
46     BundleResource::Ptr javaBundleResource = std::make_shared< JavaBundleResource >
47             (env, obj, bundleResource, str_bundleId, attributes);
48     ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
49
50     javaBundleResource->m_uri = string(str_uri, strlen(str_uri));
51     javaBundleResource->m_resourceType = string(str_resourceType, strlen(str_resourceType));
52     javaBundleResource->m_name = string(str_res_name, strlen(str_res_name));
53     ret = container->registerResource(javaBundleResource);
54
55     java_resources[str_uri] = javaBundleResource;
56
57     return ret;
58 }
59
60 /*
61  * Class:     org_iotivity_resourcecontainer_bundle_api_BaseActivator
62  * Method:    unregisterJavaResource
63  * Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;)V
64  */
65 JNIEXPORT void JNICALL
66 Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_unregisterJavaResource
67 (JNIEnv *env, jobject obj, jobject bundleResource, jstring uri)
68 {
69     (void)obj;
70     (void)bundleResource;
71     const char *str_uri = env->GetStringUTFChars(uri, 0);
72
73     if (java_resources[str_uri] != NULL)
74     {
75         ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
76         container->unregisterResource(java_resources[str_uri]);
77         java_resources.erase(str_uri);
78     }
79 }
80
81 /*
82  * Class:     org_iotivity_resourcecontainer_bundle_api_BaseActivator
83  * Method:    getNumberOfConfiguredResources
84  * Signature: (Ljava/lang/String;)I
85  */
86 JNIEXPORT jint JNICALL
87 Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_getNumberOfConfiguredResources(
88     JNIEnv *env, jobject obj, jstring bundleId)
89 {
90     (void)obj;
91     const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
92
93     ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
94     vector< resourceInfo > resourceConfig;
95     container->getResourceConfiguration(str_bundleId, &resourceConfig);
96
97     return resourceConfig.size();
98 }
99
100 /*
101  * Class:     org_iotivity_resourcecontainer_bundle_api_BaseActivator
102  * Method:    getConfiguredResourceParams
103  * Signature: (Ljava/lang/String;I)[Ljava/lang/String;
104  */
105 JNIEXPORT jobjectArray JNICALL
106 Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_getConfiguredResourceParams(
107     JNIEnv *env, jobject obj, jstring bundleId, jint resourceId)
108 {
109     (void)obj;
110     jobjectArray ret;
111     const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
112
113     ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
114     vector< resourceInfo > resourceConfig;
115     container->getResourceConfiguration(str_bundleId, &resourceConfig);
116     resourceInfo conf = resourceConfig[resourceId];
117     ret = (jobjectArray) env->NewObjectArray(4, env->FindClass("java/lang/String"),
118             env->NewStringUTF(""));
119
120     env->SetObjectArrayElement(ret, 0, env->NewStringUTF(conf.name.c_str()));
121     env->SetObjectArrayElement(ret, 1, env->NewStringUTF(conf.uri.c_str()));
122     env->SetObjectArrayElement(ret, 2, env->NewStringUTF(conf.resourceType.c_str()));
123     env->SetObjectArrayElement(ret, 3, env->NewStringUTF(conf.address.c_str()));
124     return ret;
125 }
126 #endif