Displaying and editing the complex value types for attributes.
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_manager_jni.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 "simulator_resource_model_jni.h"
22 #include "simulator_device_info_jni.h"
23 #include "simulator_platform_info_jni.h"
24 #include "simulator_exceptions_jni.h"
25 #include "simulator_resource_utils_jni.h"
26 #include "simulator_utils_jni.h"
27 #include "jni_sharedobject_holder.h"
28 #include "jni_listener_holder.h"
29 #include "jni_string.h"
30
31 #include "simulator_manager.h"
32
33 class JNILogger : public ILogger
34 {
35     public:
36         void setJavaLogger(JNIEnv *env, jobject logger)
37         {
38             m_logger = env->NewWeakGlobalRef(logger);
39         }
40
41         void write(std::string time, ILogger::Level level, std::string message)
42         {
43             JNIEnv *env = getEnv();
44             if (nullptr == env)
45                 return;
46
47             jobject logger = env->NewLocalRef(m_logger);
48             if (!logger)
49             {
50                 releaseEnv();
51                 return;
52             }
53
54             jclass loggerCls = env->GetObjectClass(logger);
55             if (!loggerCls)
56             {
57                 releaseEnv();
58                 return;
59             }
60
61             jmethodID writeMId = env->GetMethodID(loggerCls, "write",
62                                                   "(Ljava/lang/String;ILjava/lang/String;)V");
63             if (!writeMId)
64             {
65                 releaseEnv();
66                 return;
67             }
68
69             jstring msg = env->NewStringUTF(message.c_str());
70             jstring timeStr = env->NewStringUTF(time.c_str());
71             env->CallVoidMethod(logger, writeMId, timeStr, static_cast<jint>(level), msg);
72             env->DeleteLocalRef(msg);
73             env->DeleteLocalRef(timeStr);
74             releaseEnv();
75         }
76
77     private:
78         jweak m_logger;
79 };
80
81 void onResourceFound(jobject listener, std::shared_ptr<SimulatorRemoteResource> remoteResource)
82 {
83     JNIEnv *env = getEnv();
84     if (!env)
85         return;
86
87     jclass listenerCls = env->GetObjectClass(listener);
88     jmethodID callbackMethod = env->GetMethodID(listenerCls, "onResourceFound",
89                                "(Lorg/oic/simulator/client/SimulatorRemoteResource;)V");
90
91     jobject resource = createSimulatorRemoteResource(env, remoteResource);
92     if (!resource)
93         return;
94
95     env->CallVoidMethod(listener, callbackMethod, resource);
96     releaseEnv();
97 }
98
99 #ifdef __cplusplus
100 extern "C" {
101 #endif
102
103 JNIEXPORT jobject JNICALL
104 Java_org_oic_simulator_SimulatorManager_createResource
105 (JNIEnv *env, jclass object, jstring configPath)
106 {
107     VALIDATE_INPUT_RET(env, !configPath, "Path is null!", nullptr)
108
109     try
110     {
111         JniString jniPath(env, configPath);
112         SimulatorResourceSP resource = SimulatorManager::getInstance()->createResource(
113                                            jniPath.get());
114         return createSimulatorResource(env, resource);
115     }
116     catch (InvalidArgsException &e)
117     {
118         throwInvalidArgsException(env, e.code(), e.what());
119     }
120     catch (SimulatorException &e)
121     {
122         throwSimulatorException(env, e.code(), e.what());
123     }
124
125     return nullptr;
126 }
127
128 JNIEXPORT jobject JNICALL
129 Java_org_oic_simulator_SimulatorManager_createResources
130 (JNIEnv *env, jclass object, jstring configPath, jint count)
131 {
132     VALIDATE_INPUT_RET(env, !configPath, "Path is null!", nullptr)
133     VALIDATE_INPUT_RET(env, !count || count < 0, "Invalid count value!", nullptr)
134
135     try
136     {
137         JniString jniPath(env, configPath);
138         std::vector<SimulatorResourceSP> resources =
139             SimulatorManager::getInstance()->createResource(jniPath.get(), count);
140         return createSimulatorResourceVector(env, resources);
141     }
142     catch (InvalidArgsException &e)
143     {
144         throwInvalidArgsException(env, e.code(), e.what());
145     }
146     catch (SimulatorException &e)
147     {
148         throwSimulatorException(env, e.code(), e.what());
149     }
150
151     return nullptr;
152 }
153
154 JNIEXPORT jobject JNICALL
155 Java_org_oic_simulator_SimulatorManager_createSingleResource
156 (JNIEnv *env, jclass object, jstring name, jstring uri, jstring resourceType)
157 {
158     VALIDATE_INPUT_RET(env, !name, "Name is null!", nullptr)
159     VALIDATE_INPUT_RET(env, !uri, "URI is null!", nullptr)
160     VALIDATE_INPUT_RET(env, !resourceType, "Resource type is null!", nullptr)
161
162     try
163     {
164         JniString jniName(env, name);
165         JniString jniUri(env, uri);
166         JniString jniResourceType(env, resourceType);
167
168         SimulatorSingleResourceSP resource = SimulatorManager::getInstance()->createSingleResource(
169                 jniName.get(), jniUri.get(), jniResourceType.get());
170         return createSimulatorResource(env, std::dynamic_pointer_cast<SimulatorResource>(resource));
171     }
172     catch (InvalidArgsException &e)
173     {
174         throwInvalidArgsException(env, e.code(), e.what());
175     }
176     catch (SimulatorException &e)
177     {
178         throwSimulatorException(env, e.code(), e.what());
179     }
180
181     return nullptr;
182 }
183
184 JNIEXPORT jobject JNICALL
185 Java_org_oic_simulator_SimulatorManager_createCollectionResource
186 (JNIEnv *env, jclass object, jstring name, jstring uri, jstring resourceType)
187 {
188     VALIDATE_INPUT_RET(env, !name, "Name is null!", nullptr)
189     VALIDATE_INPUT_RET(env, !uri, "URI is null!", nullptr)
190     VALIDATE_INPUT_RET(env, !resourceType, "Resource type is null!", nullptr)
191
192     try
193     {
194         JniString jniName(env, name);
195         JniString jniUri(env, uri);
196         JniString jniResourceType(env, resourceType);
197
198         SimulatorCollectionResourceSP resource = SimulatorManager::getInstance()->createCollectionResource(
199                     jniName.get(), jniUri.get(), jniResourceType.get());
200         return createSimulatorResource(env, std::dynamic_pointer_cast<SimulatorResource>(resource));
201     }
202     catch (InvalidArgsException &e)
203     {
204         throwInvalidArgsException(env, e.code(), e.what());
205     }
206     catch (SimulatorException &e)
207     {
208         throwSimulatorException(env, e.code(), e.what());
209     }
210
211     return nullptr;
212 }
213
214 JNIEXPORT void JNICALL
215 Java_org_oic_simulator_SimulatorManager_searchResource
216 (JNIEnv *env, jobject object, jstring resourceType, jobject listener)
217 {
218     VALIDATE_CALLBACK(env, listener)
219
220     ResourceFindCallback callback =  std::bind([](
221                                          std::shared_ptr<SimulatorRemoteResource> resource,
222                                          const std::shared_ptr<JniListenerHolder> &listenerRef)
223     {
224         onResourceFound(listenerRef->get(), resource);
225     }, std::placeholders::_1, JniListenerHolder::create(env, listener));
226
227     try
228     {
229         if (!resourceType)
230         {
231             SimulatorManager::getInstance()->findResource(callback);
232         }
233         else
234         {
235             JniString type(env, resourceType);
236             SimulatorManager::getInstance()->findResource(type.get(), callback);
237         }
238
239     }
240     catch (InvalidArgsException &e)
241     {
242         throwInvalidArgsException(env, e.code(), e.what());
243     }
244     catch (SimulatorException &e)
245     {
246         throwSimulatorException(env, e.code(), e.what());
247     }
248 }
249
250 JNIEXPORT void JNICALL
251 Java_org_oic_simulator_SimulatorManager_setDeviceInfo
252 (JNIEnv *env, jobject object, jstring deviceName)
253 {
254     VALIDATE_INPUT(env, !deviceName, "Device name is null!")
255
256     try
257     {
258         JniString jniDeviceName(env, deviceName);
259         SimulatorManager::getInstance()->setDeviceInfo(jniDeviceName.get());
260     }
261     catch (InvalidArgsException &e)
262     {
263         throwInvalidArgsException(env, e.code(), e.what());
264     }
265     catch (SimulatorException &e)
266     {
267         throwSimulatorException(env, e.code(), e.what());
268     }
269 }
270
271 JNIEXPORT void JNICALL
272 Java_org_oic_simulator_SimulatorManager_findDevices
273 (JNIEnv *env, jobject object, jstring hostUri, jobject listener)
274 {
275     VALIDATE_CALLBACK(env, listener)
276
277     DeviceInfoCallback callback =  std::bind([](DeviceInfo & deviceInfo,
278                                    const std::shared_ptr<JniListenerHolder> &listenerRef)
279     {
280         onDeviceInfoReceived(listenerRef->get(), deviceInfo);
281     }, std::placeholders::_1, JniListenerHolder::create(env, listener));
282
283     try
284     {
285         JniString jniHostUri(env, hostUri);
286         SimulatorManager::getInstance()->getDeviceInfo(jniHostUri.get(), callback);
287     }
288     catch (InvalidArgsException &e)
289     {
290         throwInvalidArgsException(env, e.code(), e.what());
291     }
292     catch (SimulatorException &e)
293     {
294         throwSimulatorException(env, e.code(), e.what());
295     }
296 }
297
298 JNIEXPORT void JNICALL
299 Java_org_oic_simulator_SimulatorManager_setPlatformInfo
300 (JNIEnv *env, jobject object, jobject platformInfo)
301 {
302     VALIDATE_INPUT(env, !platformInfo, "Platform info is null!")
303
304     try
305     {
306         JniPlatformInfo jniPlatformInfo(env);
307         PlatformInfo info = jniPlatformInfo.toCpp(platformInfo);
308         SimulatorManager::getInstance()->setPlatformInfo(info);
309     }
310     catch (SimulatorException &e)
311     {
312         throwSimulatorException(env, e.code(), e.what());
313     }
314 }
315
316 JNIEXPORT void JNICALL
317 Java_org_oic_simulator_SimulatorManager_getPlatformInformation
318 (JNIEnv *env, jobject object, jstring hostUri, jobject listener)
319 {
320     VALIDATE_CALLBACK(env, listener)
321
322     PlatformInfoCallback callback =  std::bind([](PlatformInfo & platformInfo,
323                                      const std::shared_ptr<JniListenerHolder> &listenerRef)
324     {
325         onPlatformInfoReceived(listenerRef->get(), platformInfo);
326     }, std::placeholders::_1, JniListenerHolder::create(env, listener));
327
328     try
329     {
330         JniString jniHostUri(env, hostUri);
331         SimulatorManager::getInstance()->getPlatformInfo(jniHostUri.get(), callback);
332     }
333     catch (InvalidArgsException &e)
334     {
335         throwInvalidArgsException(env, e.code(), e.what());
336     }
337     catch (SimulatorException &e)
338     {
339         throwSimulatorException(env, e.code(), e.what());
340     }
341 }
342
343 JNIEXPORT void JNICALL
344 Java_org_oic_simulator_SimulatorManager_setLogger
345 (JNIEnv *env, jobject object, jobject logger)
346 {
347     static std::shared_ptr<JNILogger> target(new JNILogger());
348     target->setJavaLogger(env, logger);
349     SimulatorManager::getInstance()->setLogger(target);
350 }
351
352 #ifdef __cplusplus
353 }
354 #endif