c207cde2a1935e9712a62ce51d835c9bd2eff797
[platform/upstream/iotivity.git] / service / resource-container / android / resource-container / src / main / jni / JniRcsResourceContainer.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 "JniRcsResourceContainer.h"
22
23 #include "JavaClasses.h"
24 #include "JavaLocalRef.h"
25 #include "JNIEnvWrapper.h"
26 #include "Log.h"
27 #include "Verify.h"
28
29 #include "ResourceContainerBundleAPI.h"
30 #include "AndroidResource.h"
31 #include "RCSResourceContainer.h"
32
33
34
35 #define LOG_TAG "JNI-RCSResourceContainer"
36
37 using namespace OIC::Service;
38
39 #define CLS_NAME_BUNDLE_INFO "org/iotivity/service/resourcecontainer/RcsBundleInfo"
40
41 std::map< string, BundleResource::Ptr > android_resources;
42
43 namespace
44 {
45     jclass g_cls_RCSBundleInfo;
46     jfieldID g_field_mNativeHandle;
47
48     jmethodID g_ctor_RCSBundleInfo;
49
50     std::map< std::string, std::string > convertJavaMapToParamsMap(JNIEnvWrapper *env,
51             jobject mapObj)
52     {
53         EXPECT_RET_DEF(mapObj, "map is null");
54
55         auto setObj = invoke_Map_entrySet(env, mapObj);
56         auto iterObj = invoke_Set_iterator(env, setObj);
57
58         std::map< std::string, std::string > ret;
59
60         while (invoke_Iterator_hasNext(env, iterObj))
61         {
62             JavaLocalObject entryObj { env, invoke_Iterator_next(env, iterObj) };
63
64             JavaLocalString keyObj { env,
65                 static_cast< jstring >(invoke_MapEntry_getKey(env, entryObj)) };
66
67             JavaLocalString valueObj { env,
68                 static_cast< jstring >(invoke_MapEntry_getValue(env, entryObj)) };
69
70             ret.emplace(toStdString(env, keyObj), toStdString(env, valueObj));
71         }
72
73         return ret;
74     }
75
76     jobject newBundleInfoObj(JNIEnvWrapper *env, const std::unique_ptr< RCSBundleInfo > &bundleInfo)
77     {
78         LOGD("new bundle info");
79         __android_log_print(ANDROID_LOG_DEBUG, "CONTAINER", "newBundleInfoObj %s",bundleInfo->getActivatorName().c_str());
80         JavaLocalString id{env, newStringObject(env, bundleInfo->getID()) };
81         JavaLocalString path{env, newStringObject(env, bundleInfo->getPath()) };
82         JavaLocalString activatorName{env, newStringObject(env, bundleInfo->getActivatorName()) };
83         JavaLocalString libraryPath{env, newStringObject(env, bundleInfo->getLibraryPath()) };
84         JavaLocalString version{env, newStringObject(env, bundleInfo->getVersion()) };
85
86         return env->NewObject(g_cls_RCSBundleInfo, g_ctor_RCSBundleInfo,
87                 id.get(), path.get(), activatorName.get(), libraryPath.get(), version.get());
88     }
89 }
90
91 void initRCSResourceContainer(JNIEnvWrapper *env)
92 {
93     g_cls_RCSBundleInfo = env->FindClassAsGlobalRef(CLS_NAME_BUNDLE_INFO);
94
95     g_ctor_RCSBundleInfo = env->GetConstructorID(g_cls_RCSBundleInfo, "("
96             AS_SIG(CLS_NAME_STRING)
97             AS_SIG(CLS_NAME_STRING)
98             AS_SIG(CLS_NAME_STRING)
99             AS_SIG(CLS_NAME_STRING)
100             AS_SIG(CLS_NAME_STRING)
101             ")V");
102
103     auto clsAndroidBundleResource = env->FindClass(PACKAGE_NAME "/AndroidBundleResource");
104
105     g_field_mNativeHandle = env->GetFieldID(clsAndroidBundleResource, "mNativeHandle", "J");
106 }
107
108 void clearRCSResourceContainer(JNIEnvWrapper *env)
109 {
110     env->DeleteGlobalRef(g_cls_RCSBundleInfo);
111 }
112
113 JNIEXPORT void JNICALL
114 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStartContainer
115 (JNIEnv *env, jobject, jstring configFileObj)
116 {
117     LOGD("nativeStartContainer");
118
119     EXPECT(configFileObj, "ConfigFile is null.");
120
121     auto configFile = toStdString(env, configFileObj);
122     //  std::string nativeFilePath = env->GetStringUTFChars(configFile, NULL);
123     VERIFY_NO_EXC(env);
124
125     RCSResourceContainer::getInstance()->startContainer(configFile);
126 }
127
128 JNIEXPORT void JNICALL
129 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStopContainer
130 (JNIEnv *env, jobject)
131 {
132     LOGD("nativeStopContainers");
133
134     RCSResourceContainer::getInstance()->stopContainer();
135 }
136
137 JNIEXPORT void JNICALL
138 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeAddBundle
139 (JNIEnv *env, jobject, jstring idObj, jstring uriObj, jstring pathObj, jstring activatorObj,
140  jobject paramsObj)
141 {
142     LOGD("nativeAddBundle");
143
144     EXPECT(idObj, "BundleId is null.");
145     EXPECT(pathObj, "BundlePath is null.");
146     EXPECT(activatorObj, "Activator is null.");
147
148     JNIEnvWrapper envWrapper(env);
149
150     try
151     {
152         LOGD("nativeAddBundle before calling native");
153         RCSResourceContainer::getInstance()->addBundle(toStdString(&envWrapper, idObj),
154                 toStdString(&envWrapper, uriObj), toStdString(&envWrapper, pathObj),
155                 toStdString(&envWrapper, activatorObj),
156                 convertJavaMapToParamsMap(&envWrapper, paramsObj));
157
158         LOGD("nativeAddBundle after calling native");
159     }
160     catch (const JavaException &)
161     {
162         LOGE("Failed to add bundle.");
163     }
164 }
165
166 JNIEXPORT void JNICALL
167 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeRemoveBundle
168 (JNIEnv *env, jobject, jstring idObj)
169 {
170     LOGD("nativeRemoveBundle");
171
172     EXPECT(idObj, "BundleId is null.");
173
174     auto id = toStdString(env, idObj);
175     VERIFY_NO_EXC(env);
176
177     RCSResourceContainer::getInstance()->removeBundle(id);
178 }
179
180
181 JNIEXPORT jobject JNICALL
182 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeListBundles
183 (JNIEnv *env, jobject)
184 {
185     LOGD("nativeListBundles");
186
187     JNIEnvWrapper envWrapper(env);
188
189     try
190     {
191         auto listObj = newArrayList(&envWrapper);
192
193         for (const auto& bundleInfo : RCSResourceContainer::getInstance()->listBundles())
194         {
195             JavaLocalObject bundleInfoObj{ &envWrapper, newBundleInfoObj(&envWrapper, bundleInfo) };
196             invoke_Collection_add(&envWrapper, listObj, bundleInfoObj);
197         }
198         return listObj;
199     }
200     catch (const JavaException &)
201     {
202         LOGE("Failed to convert bundle info list.");
203     }
204     return nullptr;
205 }
206
207 JNIEXPORT void JNICALL
208 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStartBundle
209 (JNIEnv *env, jobject, jstring idObj)
210 {
211     LOGD("nativeStartBundle2");
212
213     EXPECT(idObj, "BundleId is null.");
214
215     auto id = env->GetStringUTFChars(idObj, NULL);
216     VERIFY_NO_EXC(env);
217
218     RCSResourceContainer::getInstance()->startBundle(id);
219 }
220 JNICALL
221 JNIEXPORT void
222 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStopBundle
223 (JNIEnv *env, jobject, jstring idObj)
224 {
225     LOGD("nativeStopBundle");
226
227     EXPECT(idObj, "BundleId is null.");
228
229     auto id = env->GetStringUTFChars(idObj, NULL);
230     VERIFY_NO_EXC(env);
231
232     RCSResourceContainer::getInstance()->stopBundle(id);
233 }
234
235 JNIEXPORT void JNICALL
236 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeAddResourceConfig
237 (JNIEnv *env, jobject, jstring idObj, jstring uriObj, jobject paramsObj)
238 {
239     LOGD("nativeAddResourceConfig");
240
241     EXPECT(idObj, "BundleId is null.");
242     EXPECT(uriObj, "BundleUri is null.");
243     EXPECT(paramsObj, "Params is null.");
244
245     JNIEnvWrapper envWrapper(env);
246
247     try
248     {
249         RCSResourceContainer::getInstance()->addResourceConfig(toStdString(&envWrapper, idObj),
250                 toStdString(&envWrapper, uriObj), convertJavaMapToParamsMap(&envWrapper, paramsObj));
251     }
252     catch (const JavaException &)
253     {
254         LOGE("Failed to add bundle.");
255     }
256 }
257
258 JNIEXPORT void JNICALL
259 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeRemoveResourceConfig
260 (JNIEnv *env, jobject, jstring idObj, jstring uriObj)
261 {
262     LOGD("nativeRemoveResourceConfig");
263
264     EXPECT(idObj, "BundleId is null.");
265     EXPECT(uriObj, "BundleUri is null.");
266
267     auto id = toStdString(env, idObj);
268     VERIFY_NO_EXC(env);
269     auto uri = toStdString(env, uriObj);
270     VERIFY_NO_EXC(env);
271
272     RCSResourceContainer::getInstance()->removeResourceConfig(id, uri);
273 }
274
275 JNIEXPORT jobject JNICALL
276 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeListBundleResources
277 (JNIEnv *env, jobject, jstring idObj)
278 {
279     LOGD("nativeListBundleResources");
280
281     EXPECT_RET_DEF(idObj, "BundleId is null.");
282
283     JNIEnvWrapper envWrapper(env);
284
285     try
286     {
287         auto id = toStdString(&envWrapper, idObj);
288
289         auto listObj = newArrayList(&envWrapper);
290
291         for (const auto& s : RCSResourceContainer::getInstance()->listBundleResources(id))
292         {
293             JavaLocalString strObj{ &envWrapper, newStringObject(&envWrapper, s) };
294
295             invoke_Collection_add(&envWrapper, listObj, strObj);
296         }
297
298         return listObj;
299     }
300     catch (const JavaException &)
301     {
302         LOGE("Failed to convert bundle info list.");
303     }
304
305     return nullptr;
306 }
307
308
309 JNIEXPORT void JNICALL
310 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeRegisterAndroidResource
311 (JNIEnv *env, jobject obj, jobject bundleResource, jobjectArray attributes, jstring bundleId,
312  jstring uri, jstring resourceType, jstring res_name)
313 {
314     JNIEnvWrapper envWrapper(env);
315     LOGD("nativeRegisterAndroidResource");
316     auto str_bundle_id = toStdString(&envWrapper, bundleId);
317     __android_log_print(ANDROID_LOG_DEBUG, "JNI-RCSResourceContainer", "retrieved bundle id: %s.", str_bundle_id.c_str());
318     auto str_uri = toStdString(&envWrapper, uri);
319     __android_log_print(ANDROID_LOG_DEBUG, "JNI-RCSResourceContainer", "retrieved uri: %s.", str_uri.c_str());
320     auto str_resourceType = toStdString(&envWrapper, resourceType);
321     __android_log_print(ANDROID_LOG_DEBUG, "JNI-RCSResourceContainer", "retrieved resource type: %s.", str_resourceType.c_str());
322     auto str_res_name = toStdString(&envWrapper, res_name);
323     LOGD("retrieved res name.");
324     AndroidResource res;
325
326     BundleResource::Ptr androidResource = std::make_shared< AndroidResource >
327             (env, obj, bundleResource, str_bundle_id, attributes);
328     ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
329
330     androidResource->m_uri = str_uri;
331     androidResource->m_resourceType = str_resourceType;
332     androidResource->m_name = str_res_name;
333     androidResource->m_bundleId = str_bundle_id;
334
335     // link java resource instance to c++ resource instance
336     env->SetLongField(bundleResource, g_field_mNativeHandle, reinterpret_cast< jlong >(androidResource.get()));
337
338     container->registerResource(androidResource);
339
340     android_resources[str_uri] = androidResource;
341 }
342
343 /*
344  * Class:     org_iotivity_resourcecontainer_bundle_api_BaseActivator
345  * Method:    unregisterJavaResource
346  * Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;)V
347  */
348 JNIEXPORT void JNICALL
349 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeUnregisterAndroidResource
350 (JNIEnv *env, jobject obj, jobject bundleResource, jstring uri)
351 {
352     (void)obj;
353     (void)bundleResource;
354     const char *str_uri = env->GetStringUTFChars(uri, 0);
355
356     if (android_resources[str_uri] != NULL)
357     {
358         ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
359         container->unregisterResource(android_resources[str_uri]);
360         android_resources.erase(str_uri);
361     }
362 }
363
364 /*
365  * Class:     org_iotivity_resourcecontainer_bundle_api_BaseActivator
366  * Method:    getNumberOfConfiguredResources
367  * Signature: (Ljava/lang/String;)I
368  */
369 JNIEXPORT jint JNICALL
370 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeGetNumberOfConfiguredResources(
371     JNIEnv *env, jobject obj, jstring bundleId)
372 {
373     (void)obj;
374     LOGD("nativeGetNumberOfConfiguredResources");
375     const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
376     LOGD("retrieved bundle id");
377     __android_log_print(ANDROID_LOG_DEBUG, "CONTAINER", "getNumberOfConfiguredResources %s",str_bundleId);
378     ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
379     vector< resourceInfo > resourceConfig;
380     container->getResourceConfiguration(str_bundleId, &resourceConfig);
381
382     return resourceConfig.size();
383 }
384
385 /*
386  * Class:     org_iotivity_resourcecontainer_bundle_api_BaseActivator
387  * Method:    getConfiguredResourceParams
388  * Signature: (Ljava/lang/String;I)[Ljava/lang/String;
389  */
390 JNIEXPORT jobjectArray JNICALL
391 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeGetConfiguredResourceParams(
392     JNIEnv *env, jobject obj, jstring bundleId, jint resourceId)
393 {
394     (void)obj;
395     jobjectArray ret;
396     const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
397
398     ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
399     vector< resourceInfo > resourceConfig;
400     container->getResourceConfiguration(str_bundleId, &resourceConfig);
401     resourceInfo conf = resourceConfig[resourceId];
402     ret = (jobjectArray) env->NewObjectArray(4, env->FindClass("java/lang/String"),
403             env->NewStringUTF(""));
404
405     env->SetObjectArrayElement(ret, 0, env->NewStringUTF(conf.name.c_str()));
406     env->SetObjectArrayElement(ret, 1, env->NewStringUTF(conf.uri.c_str()));
407     env->SetObjectArrayElement(ret, 2, env->NewStringUTF(conf.resourceType.c_str()));
408     env->SetObjectArrayElement(ret, 3, env->NewStringUTF(conf.address.c_str()));
409     return ret;
410 }
411