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 "JniRcsResourceContainer.h"
23 #include "JavaClasses.h"
24 #include "JavaLocalRef.h"
25 #include "JNIEnvWrapper.h"
29 #include "ResourceContainerBundleAPI.h"
30 #include "AndroidResource.h"
31 #include "RCSResourceContainer.h"
35 #define LOG_TAG "JNI-RCSResourceContainer"
37 using namespace OIC::Service;
39 #define CLS_NAME_BUNDLE_INFO "org/iotivity/service/resourcecontainer/RcsBundleInfo"
41 std::map< string, BundleResource::Ptr > android_resources;
45 jclass g_cls_RCSBundleInfo;
46 jfieldID g_field_mNativeHandle;
48 jmethodID g_ctor_RCSBundleInfo;
50 std::map< std::string, std::string > convertJavaMapToParamsMap(JNIEnvWrapper *env,
53 EXPECT_RET_DEF(mapObj, "map is null");
55 auto setObj = invoke_Map_entrySet(env, mapObj);
56 auto iterObj = invoke_Set_iterator(env, setObj);
58 std::map< std::string, std::string > ret;
60 while (invoke_Iterator_hasNext(env, iterObj))
62 JavaLocalObject entryObj { env, invoke_Iterator_next(env, iterObj) };
64 JavaLocalString keyObj { env,
65 static_cast< jstring >(invoke_MapEntry_getKey(env, entryObj)) };
67 JavaLocalString valueObj { env,
68 static_cast< jstring >(invoke_MapEntry_getValue(env, entryObj)) };
70 ret.emplace(toStdString(env, keyObj), toStdString(env, valueObj));
76 jobject newBundleInfoObj(JNIEnvWrapper *env, const std::unique_ptr< RCSBundleInfo > &bundleInfo)
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()) };
86 return env->NewObject(g_cls_RCSBundleInfo, g_ctor_RCSBundleInfo,
87 id.get(), path.get(), activatorName.get(), libraryPath.get(), version.get());
91 void initRCSResourceContainer(JNIEnvWrapper *env)
93 g_cls_RCSBundleInfo = env->FindClassAsGlobalRef(CLS_NAME_BUNDLE_INFO);
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)
103 auto clsAndroidBundleResource = env->FindClass(PACKAGE_NAME "/AndroidBundleResource");
105 g_field_mNativeHandle = env->GetFieldID(clsAndroidBundleResource, "mNativeHandle", "J");
108 void clearRCSResourceContainer(JNIEnvWrapper *env)
110 env->DeleteGlobalRef(g_cls_RCSBundleInfo);
113 JNIEXPORT void JNICALL
114 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStartContainer
115 (JNIEnv *env, jobject, jstring configFileObj)
117 LOGD("nativeStartContainer");
119 EXPECT(configFileObj, "ConfigFile is null.");
121 auto configFile = toStdString(env, configFileObj);
122 // std::string nativeFilePath = env->GetStringUTFChars(configFile, NULL);
125 RCSResourceContainer::getInstance()->startContainer(configFile);
128 JNIEXPORT void JNICALL
129 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStopContainer
130 (JNIEnv *env, jobject)
132 LOGD("nativeStopContainers");
134 RCSResourceContainer::getInstance()->stopContainer();
137 JNIEXPORT void JNICALL
138 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeAddBundle
139 (JNIEnv *env, jobject, jstring idObj, jstring uriObj, jstring pathObj, jstring activatorObj,
142 LOGD("nativeAddBundle");
144 EXPECT(idObj, "BundleId is null.");
145 EXPECT(pathObj, "BundlePath is null.");
146 EXPECT(activatorObj, "Activator is null.");
148 JNIEnvWrapper envWrapper(env);
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));
158 LOGD("nativeAddBundle after calling native");
160 catch (const JavaException &)
162 LOGE("Failed to add bundle.");
166 JNIEXPORT void JNICALL
167 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeRemoveBundle
168 (JNIEnv *env, jobject, jstring idObj)
170 LOGD("nativeRemoveBundle");
172 EXPECT(idObj, "BundleId is null.");
174 auto id = toStdString(env, idObj);
177 RCSResourceContainer::getInstance()->removeBundle(id);
181 JNIEXPORT jobject JNICALL
182 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeListBundles
183 (JNIEnv *env, jobject)
185 LOGD("nativeListBundles");
187 JNIEnvWrapper envWrapper(env);
191 auto listObj = newArrayList(&envWrapper);
193 for (const auto& bundleInfo : RCSResourceContainer::getInstance()->listBundles())
195 JavaLocalObject bundleInfoObj{ &envWrapper, newBundleInfoObj(&envWrapper, bundleInfo) };
196 invoke_Collection_add(&envWrapper, listObj, bundleInfoObj);
200 catch (const JavaException &)
202 LOGE("Failed to convert bundle info list.");
207 JNIEXPORT void JNICALL
208 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStartBundle
209 (JNIEnv *env, jobject, jstring idObj)
211 LOGD("nativeStartBundle2");
213 EXPECT(idObj, "BundleId is null.");
215 auto id = env->GetStringUTFChars(idObj, NULL);
218 RCSResourceContainer::getInstance()->startBundle(id);
222 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStopBundle
223 (JNIEnv *env, jobject, jstring idObj)
225 LOGD("nativeStopBundle");
227 EXPECT(idObj, "BundleId is null.");
229 auto id = env->GetStringUTFChars(idObj, NULL);
232 RCSResourceContainer::getInstance()->stopBundle(id);
235 JNIEXPORT void JNICALL
236 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeAddResourceConfig
237 (JNIEnv *env, jobject, jstring idObj, jstring uriObj, jobject paramsObj)
239 LOGD("nativeAddResourceConfig");
241 EXPECT(idObj, "BundleId is null.");
242 EXPECT(uriObj, "BundleUri is null.");
243 EXPECT(paramsObj, "Params is null.");
245 JNIEnvWrapper envWrapper(env);
249 RCSResourceContainer::getInstance()->addResourceConfig(toStdString(&envWrapper, idObj),
250 toStdString(&envWrapper, uriObj), convertJavaMapToParamsMap(&envWrapper, paramsObj));
252 catch (const JavaException &)
254 LOGE("Failed to add bundle.");
258 JNIEXPORT void JNICALL
259 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeRemoveResourceConfig
260 (JNIEnv *env, jobject, jstring idObj, jstring uriObj)
262 LOGD("nativeRemoveResourceConfig");
264 EXPECT(idObj, "BundleId is null.");
265 EXPECT(uriObj, "BundleUri is null.");
267 auto id = toStdString(env, idObj);
269 auto uri = toStdString(env, uriObj);
272 RCSResourceContainer::getInstance()->removeResourceConfig(id, uri);
275 JNIEXPORT jobject JNICALL
276 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeListBundleResources
277 (JNIEnv *env, jobject, jstring idObj)
279 LOGD("nativeListBundleResources");
281 EXPECT_RET_DEF(idObj, "BundleId is null.");
283 JNIEnvWrapper envWrapper(env);
287 auto id = toStdString(&envWrapper, idObj);
289 auto listObj = newArrayList(&envWrapper);
291 for (const auto& s : RCSResourceContainer::getInstance()->listBundleResources(id))
293 JavaLocalString strObj{ &envWrapper, newStringObject(&envWrapper, s) };
295 invoke_Collection_add(&envWrapper, listObj, strObj);
300 catch (const JavaException &)
302 LOGE("Failed to convert bundle info list.");
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)
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.");
326 BundleResource::Ptr androidResource = std::make_shared< AndroidResource >
327 (env, obj, bundleResource, str_bundle_id, attributes);
328 ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
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;
335 // link java resource instance to c++ resource instance
336 env->SetLongField(bundleResource, g_field_mNativeHandle, reinterpret_cast< jlong >(androidResource.get()));
338 container->registerResource(androidResource);
340 android_resources[str_uri] = androidResource;
344 * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
345 * Method: unregisterJavaResource
346 * Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;)V
348 JNIEXPORT void JNICALL
349 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeUnregisterAndroidResource
350 (JNIEnv *env, jobject obj, jobject bundleResource, jstring uri)
353 (void)bundleResource;
354 const char *str_uri = env->GetStringUTFChars(uri, 0);
356 if (android_resources[str_uri] != NULL)
358 ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
359 container->unregisterResource(android_resources[str_uri]);
360 android_resources.erase(str_uri);
365 * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
366 * Method: getNumberOfConfiguredResources
367 * Signature: (Ljava/lang/String;)I
369 JNIEXPORT jint JNICALL
370 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeGetNumberOfConfiguredResources(
371 JNIEnv *env, jobject obj, jstring bundleId)
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);
382 return resourceConfig.size();
386 * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
387 * Method: getConfiguredResourceParams
388 * Signature: (Ljava/lang/String;I)[Ljava/lang/String;
390 JNIEXPORT jobjectArray JNICALL
391 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeGetConfiguredResourceParams(
392 JNIEnv *env, jobject obj, jstring bundleId, jint resourceId)
396 const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
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(""));
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()));