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 "JavaLocalRef.h"
24 #include "JNIEnvWrapper.h"
27 #include "JniRcsObject.h"
29 #include "RCSResourceContainer.h"
30 #include "RCSBundleInfo.h"
32 #define LOG_TAG "JNI-RCSResourceContainer"
34 using namespace OIC::Service;
36 #define CLS_NAME_BUNDLE_INFO PACKAGE_NAME "/resourcecontainer/RcsBundleInfo"
38 #include <android/log.h>
44 jclass g_cls_RCSBundleInfo;
46 jmethodID g_ctor_RCSBundleInfo;
48 std::map< std::string, std::string > convertJavaMapToParamsMap(JNIEnvWrapper *env,
51 EXPECT_RET_DEF(mapObj, "map is null");
53 auto setObj = invoke_Map_entrySet(env, mapObj);
54 auto iterObj = invoke_Set_iterator(env, setObj);
56 std::map< std::string, std::string > ret;
58 while (invoke_Iterator_hasNext(env, iterObj))
60 JavaLocalObject entryObj { env, invoke_Iterator_next(env, iterObj) };
62 JavaLocalString keyObj { env,
63 static_cast< jstring >(invoke_MapEntry_getKey(env, entryObj))
65 JavaLocalString valueObj { env,
66 static_cast< jstring >(invoke_MapEntry_getValue(env, entryObj))
69 ret.emplace(toStdString(env, keyObj), toStdString(env, valueObj));
76 void initRCSResourceContainer(JNIEnvWrapper *env)
78 g_cls_RCSBundleInfo = env->FindClassAsGlobalRef(CLS_NAME_BUNDLE_INFO);
80 g_ctor_RCSBundleInfo = env->GetConstructorID(g_cls_RCSBundleInfo, "()V");
83 void clearRCSResourceContainer(JNIEnvWrapper *env)
85 env->DeleteGlobalRef(g_cls_RCSBundleInfo);
88 JNIEXPORT void JNICALL
89 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStartContainer
90 (JNIEnv *env, jobject, jstring configFileObj)
92 LOGD("nativeStartContainer");
94 EXPECT(configFileObj, "ConfigFile is null.");
96 auto configFile = toStdString(env, configFileObj);
97 // std::string nativeFilePath = env->GetStringUTFChars(configFile, NULL);
100 RCSResourceContainer::getInstance()->startContainer(configFile);
103 JNIEXPORT void JNICALL
104 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStopContainer
105 (JNIEnv *env, jobject)
107 LOGD("nativeStopContainers");
109 RCSResourceContainer::getInstance()->stopContainer();
112 JNIEXPORT void JNICALL
113 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeAddBundle
114 (JNIEnv *env, jobject, jstring idObj, jstring uriObj, jstring pathObj, jstring activatorObj,
117 LOGD("nativeAddBundle");
119 EXPECT(idObj, "BundleId is null.");
120 EXPECT(pathObj, "BundlePath is null.");
121 EXPECT(activatorObj, "Activator is null.");
123 JNIEnvWrapper envWrapper(env);
127 LOGD("nativeAddBundle before calling native");
128 RCSResourceContainer::getInstance()->addBundle(toStdString(&envWrapper, idObj),
129 toStdString(&envWrapper, uriObj), toStdString(&envWrapper, pathObj),
130 toStdString(&envWrapper, activatorObj),
131 convertJavaMapToParamsMap(&envWrapper, paramsObj));
133 LOGD("nativeAddBundle after calling native");
135 catch (const JavaException &)
137 LOGE("Failed to add bundle.");
141 JNIEXPORT void JNICALL
142 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeRemoveBundle
143 (JNIEnv *env, jobject, jstring idObj)
145 LOGD("nativeRemoveBundle");
147 EXPECT(idObj, "BundleId is null.");
149 auto id = toStdString(env, idObj);
152 RCSResourceContainer::getInstance()->removeBundle(id);
156 JNIEXPORT jobject JNICALL
157 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeListBundles
158 (JNIEnv *env, jobject)
160 LOGD("nativeListBundles");
162 JNIEnvWrapper envWrapper(env);
166 auto listObj = newArrayList(&envWrapper);
168 for (auto& uniqeBundleInfo : RCSResourceContainer::getInstance()->listBundles())
170 RCSBundleInfo* bundleInfo = uniqeBundleInfo.release();
171 // FIXME we need a safe way to keep bundle info in java obj!!
172 // because we currently put raw pointer in the java obj.
174 JavaLocalObject bundleInfoObj { &envWrapper,
175 envWrapper.NewObject(g_cls_RCSBundleInfo, g_ctor_RCSBundleInfo) };
177 setSafeNativeHandle< RCSBundleInfo* >(&envWrapper, bundleInfoObj, bundleInfo);
179 invoke_Collection_add(&envWrapper, listObj, bundleInfoObj);
183 catch (const JavaException &)
185 LOGE("Failed to convert bundle info list.");
190 JNIEXPORT void JNICALL
191 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStartBundle
192 (JNIEnv *env, jobject, jstring idObj)
194 LOGD("nativeStartBundle");
196 EXPECT(idObj, "BundleId is null.");
198 auto id = env->GetStringUTFChars(idObj, NULL);
201 RCSResourceContainer::getInstance()->startBundle(id);
205 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStopBundle
206 (JNIEnv *env, jobject, jstring idObj)
208 LOGD("nativeStopBundle");
210 EXPECT(idObj, "BundleId is null.");
212 auto id = env->GetStringUTFChars(idObj, NULL);
215 RCSResourceContainer::getInstance()->stopBundle(id);
218 JNIEXPORT void JNICALL
219 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeAddResourceConfig
220 (JNIEnv *env, jobject, jstring idObj, jstring uriObj, jobject paramsObj)
222 LOGD("nativeAddResourceConfig");
224 EXPECT(idObj, "BundleId is null.");
225 EXPECT(uriObj, "BundleUri is null.");
226 EXPECT(paramsObj, "Params is null.");
228 JNIEnvWrapper envWrapper(env);
232 RCSResourceContainer::getInstance()->addResourceConfig(toStdString(&envWrapper, idObj),
233 toStdString(&envWrapper, uriObj), convertJavaMapToParamsMap(&envWrapper, paramsObj));
235 catch (const JavaException &)
237 LOGE("Failed to add bundle.");
241 JNIEXPORT void JNICALL
242 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeRemoveResourceConfig
243 (JNIEnv *env, jobject, jstring idObj, jstring uriObj)
245 LOGD("nativeRemoveResourceConfig");
247 EXPECT(idObj, "BundleId is null.");
248 EXPECT(uriObj, "BundleUri is null.");
250 auto id = toStdString(env, idObj);
252 auto uri = toStdString(env, uriObj);
255 RCSResourceContainer::getInstance()->removeResourceConfig(id, uri);
258 JNIEXPORT jobject JNICALL
259 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeListBundleResources
260 (JNIEnv *env, jobject, jstring idObj)
262 LOGD("nativeListBundleResources");
264 EXPECT_RET_DEF(idObj, "BundleId is null.");
266 JNIEnvWrapper envWrapper(env);
270 auto id = toStdString(&envWrapper, idObj);
272 auto listObj = newArrayList(&envWrapper);
274 for (const auto & s : RCSResourceContainer::getInstance()->listBundleResources(id))
276 JavaLocalString strObj { &envWrapper, newStringObject(&envWrapper, s) };
278 invoke_Collection_add(&envWrapper, listObj, strObj);
283 catch (const JavaException &)
285 LOGE("Failed to convert bundle info list.");