Imported Upstream version 1.0.0
[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 "JavaLocalRef.h"
24 #include "JNIEnvWrapper.h"
25 #include "Log.h"
26 #include "Verify.h"
27 #include "JniRcsObject.h"
28
29 #include "RCSResourceContainer.h"
30 #include "RCSBundleInfo.h"
31
32 #define LOG_TAG "JNI-RCSResourceContainer"
33
34 using namespace OIC::Service;
35
36 #define CLS_NAME_BUNDLE_INFO PACKAGE_NAME "/resourcecontainer/RcsBundleInfo"
37
38 #include <android/log.h>
39
40
41
42 namespace
43 {
44     jclass g_cls_RCSBundleInfo;
45
46     jmethodID g_ctor_RCSBundleInfo;
47
48     std::map< std::string, std::string > convertJavaMapToParamsMap(JNIEnvWrapper *env,
49             jobject mapObj)
50     {
51         EXPECT_RET_DEF(mapObj, "map is null");
52
53         auto setObj = invoke_Map_entrySet(env, mapObj);
54         auto iterObj = invoke_Set_iterator(env, setObj);
55
56         std::map< std::string, std::string > ret;
57
58         while (invoke_Iterator_hasNext(env, iterObj))
59         {
60             JavaLocalObject entryObj { env, invoke_Iterator_next(env, iterObj) };
61
62             JavaLocalString keyObj { env,
63                                      static_cast< jstring >(invoke_MapEntry_getKey(env, entryObj))
64                                    };
65             JavaLocalString valueObj { env,
66                                        static_cast< jstring >(invoke_MapEntry_getValue(env, entryObj))
67                                      };
68
69             ret.emplace(toStdString(env, keyObj), toStdString(env, valueObj));
70         }
71
72         return ret;
73     }
74 }
75
76 void initRCSResourceContainer(JNIEnvWrapper *env)
77 {
78     g_cls_RCSBundleInfo = env->FindClassAsGlobalRef(CLS_NAME_BUNDLE_INFO);
79
80     g_ctor_RCSBundleInfo = env->GetConstructorID(g_cls_RCSBundleInfo, "()V");
81 }
82
83 void clearRCSResourceContainer(JNIEnvWrapper *env)
84 {
85     env->DeleteGlobalRef(g_cls_RCSBundleInfo);
86 }
87
88 JNIEXPORT void JNICALL
89 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStartContainer
90 (JNIEnv *env, jobject, jstring configFileObj)
91 {
92     LOGD("nativeStartContainer");
93
94     EXPECT(configFileObj, "ConfigFile is null.");
95
96     auto configFile = toStdString(env, configFileObj);
97     //  std::string nativeFilePath = env->GetStringUTFChars(configFile, NULL);
98     VERIFY_NO_EXC(env);
99
100     RCSResourceContainer::getInstance()->startContainer(configFile);
101 }
102
103 JNIEXPORT void JNICALL
104 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStopContainer
105 (JNIEnv *env, jobject)
106 {
107     LOGD("nativeStopContainers");
108
109     RCSResourceContainer::getInstance()->stopContainer();
110 }
111
112 JNIEXPORT void JNICALL
113 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeAddBundle
114 (JNIEnv *env, jobject, jstring idObj, jstring uriObj, jstring pathObj, jstring activatorObj,
115  jobject paramsObj)
116 {
117     LOGD("nativeAddBundle");
118
119     EXPECT(idObj, "BundleId is null.");
120     EXPECT(pathObj, "BundlePath is null.");
121     EXPECT(activatorObj, "Activator is null.");
122
123     JNIEnvWrapper envWrapper(env);
124
125     try
126     {
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));
132
133         LOGD("nativeAddBundle after calling native");
134     }
135     catch (const JavaException &)
136     {
137         LOGE("Failed to add bundle.");
138     }
139 }
140
141 JNIEXPORT void JNICALL
142 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeRemoveBundle
143 (JNIEnv *env, jobject, jstring idObj)
144 {
145     LOGD("nativeRemoveBundle");
146
147     EXPECT(idObj, "BundleId is null.");
148
149     auto id = toStdString(env, idObj);
150     VERIFY_NO_EXC(env);
151
152     RCSResourceContainer::getInstance()->removeBundle(id);
153 }
154
155
156 JNIEXPORT jobject JNICALL
157 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeListBundles
158 (JNIEnv *env, jobject)
159 {
160     LOGD("nativeListBundles");
161
162     JNIEnvWrapper envWrapper(env);
163
164     try
165     {
166         auto listObj = newArrayList(&envWrapper);
167
168         for (auto& uniqeBundleInfo : RCSResourceContainer::getInstance()->listBundles())
169         {
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.
173
174             JavaLocalObject bundleInfoObj { &envWrapper,
175                 envWrapper.NewObject(g_cls_RCSBundleInfo, g_ctor_RCSBundleInfo) };
176
177             setSafeNativeHandle< RCSBundleInfo* >(&envWrapper, bundleInfoObj, bundleInfo);
178
179             invoke_Collection_add(&envWrapper, listObj, bundleInfoObj);
180         }
181         return listObj;
182     }
183     catch (const JavaException &)
184     {
185         LOGE("Failed to convert bundle info list.");
186     }
187     return nullptr;
188 }
189
190 JNIEXPORT void JNICALL
191 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStartBundle
192 (JNIEnv *env, jobject, jstring idObj)
193 {
194     LOGD("nativeStartBundle");
195
196     EXPECT(idObj, "BundleId is null.");
197
198     auto id = env->GetStringUTFChars(idObj, NULL);
199     VERIFY_NO_EXC(env);
200
201     RCSResourceContainer::getInstance()->startBundle(id);
202 }
203 JNICALL
204 JNIEXPORT void
205 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeStopBundle
206 (JNIEnv *env, jobject, jstring idObj)
207 {
208     LOGD("nativeStopBundle");
209
210     EXPECT(idObj, "BundleId is null.");
211
212     auto id = env->GetStringUTFChars(idObj, NULL);
213     VERIFY_NO_EXC(env);
214
215     RCSResourceContainer::getInstance()->stopBundle(id);
216 }
217
218 JNIEXPORT void JNICALL
219 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeAddResourceConfig
220 (JNIEnv *env, jobject, jstring idObj, jstring uriObj, jobject paramsObj)
221 {
222     LOGD("nativeAddResourceConfig");
223
224     EXPECT(idObj, "BundleId is null.");
225     EXPECT(uriObj, "BundleUri is null.");
226     EXPECT(paramsObj, "Params is null.");
227
228     JNIEnvWrapper envWrapper(env);
229
230     try
231     {
232         RCSResourceContainer::getInstance()->addResourceConfig(toStdString(&envWrapper, idObj),
233                 toStdString(&envWrapper, uriObj), convertJavaMapToParamsMap(&envWrapper, paramsObj));
234     }
235     catch (const JavaException &)
236     {
237         LOGE("Failed to add bundle.");
238     }
239 }
240
241 JNIEXPORT void JNICALL
242 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeRemoveResourceConfig
243 (JNIEnv *env, jobject, jstring idObj, jstring uriObj)
244 {
245     LOGD("nativeRemoveResourceConfig");
246
247     EXPECT(idObj, "BundleId is null.");
248     EXPECT(uriObj, "BundleUri is null.");
249
250     auto id = toStdString(env, idObj);
251     VERIFY_NO_EXC(env);
252     auto uri = toStdString(env, uriObj);
253     VERIFY_NO_EXC(env);
254
255     RCSResourceContainer::getInstance()->removeResourceConfig(id, uri);
256 }
257
258 JNIEXPORT jobject JNICALL
259 Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeListBundleResources
260 (JNIEnv *env, jobject, jstring idObj)
261 {
262     LOGD("nativeListBundleResources");
263
264     EXPECT_RET_DEF(idObj, "BundleId is null.");
265
266     JNIEnvWrapper envWrapper(env);
267
268     try
269     {
270         auto id = toStdString(&envWrapper, idObj);
271
272         auto listObj = newArrayList(&envWrapper);
273
274         for (const auto & s : RCSResourceContainer::getInstance()->listBundleResources(id))
275         {
276             JavaLocalString strObj { &envWrapper, newStringObject(&envWrapper, s) };
277
278             invoke_Collection_add(&envWrapper, listObj, strObj);
279         }
280
281         return listObj;
282     }
283     catch (const JavaException &)
284     {
285         LOGE("Failed to convert bundle info list.");
286     }
287
288     return nullptr;
289 }
290