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