Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / jni / tm / src / jni_things_manager_util.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 #include "jni_things_manager_util.h"
21 #include "JniOcResource.h"
22
23 std::vector<std::string> convertStringVector(JNIEnv *env, jobject jVectorString)
24 {
25     std::vector<std::string> vectorString;
26
27     jclass vectorClass = env->FindClass(TM_JAVA_VECTOR_CLASS_PATH);
28     if (!vectorClass)
29     {
30         LOGE("convertStringVector: Not able to find class %s", TM_JAVA_VECTOR_CLASS_PATH);
31         return vectorString;
32     }
33
34     jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
35     if (NULL == size)
36     {
37         LOGE("convertStringVector: Failed to get method id for size");
38         return vectorString;
39     }
40
41     jmethodID get = env->GetMethodID(vectorClass, "get", "(I)""Ljava/lang/Object;");
42     if (NULL == get)
43     {
44         LOGE("convertStringVector: Failed to get method id for get");
45         return vectorString;
46     }
47
48     jint jSize = env->CallIntMethod(jVectorString, size);
49     int sizeOfVector = jSize;
50
51     for (int index = 0; index < sizeOfVector; index++)
52     {
53         jint jIndex = index;
54         jstring jContactInfoObj = (jstring)env->CallObjectMethod(jVectorString, get, jIndex);
55         if (jContactInfoObj == NULL)
56         {
57             LOGE("convertStringVector: Failed on CallObjectMethod");
58             return vectorString;
59         }
60         const char *buff = env->GetStringUTFChars(jContactInfoObj, 0);
61         if (NULL != buff)
62         {
63             std::string tempString = buff;
64             vectorString.push_back(tempString);
65         }
66
67         env->ReleaseStringUTFChars(jContactInfoObj, buff);
68     }
69
70     return vectorString;
71 }
72
73 std::map<std::string, std::string> convertStringMap(JNIEnv *env, jobject jMapString)
74 {
75     // Get reference to java/util/Map class
76     jclass mapClass = env->GetObjectClass(jMapString);
77     jmethodID sizeMethodId = env->GetMethodID(mapClass, "size", "()I");
78     jmethodID entrySetMethodId = env->GetMethodID(mapClass, "entrySet", "()Ljava/util/Set;");
79
80     // Get reference to java/util/Set class
81     jclass setClass = env->FindClass("java/util/Set");
82     jmethodID iteratorMethodId = env->GetMethodID(setClass, "iterator", "()Ljava/util/Iterator;");
83
84     // Get reference to java/util/Iterator class
85     jclass iteratorClass = env->FindClass("java/util/Iterator");
86     jmethodID hasNextMethodId = env->GetMethodID(iteratorClass, "hasNext", "()Z");
87     jmethodID nextMethodId = env->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;");
88
89     // Get reference to java/util/Map/Entry class
90     jclass mapEntryClass = env->FindClass("java/util/Map$Entry");
91     jmethodID getKeyMethodId = env->GetMethodID(mapEntryClass, "getKey", "()Ljava/lang/Object;");
92     jmethodID getValueMethodId = env->GetMethodID(mapEntryClass, "getValue", "()Ljava/lang/Object;");
93
94     std::map<std::string, std::string> mapString;
95     // Get entry set from map
96     jobject entrySet = env->CallObjectMethod(jMapString, entrySetMethodId);
97     if (!entrySet)
98     {
99         LOGE("convertStringMap: Failed to get entrySet");
100         return mapString;
101     }
102
103     // Get iterator from set
104     jobject iterator = env->CallObjectMethod(entrySet, iteratorMethodId);
105     if (!iterator)
106     {
107         LOGE("convertStringMap: Failed to get iterator from set");
108         return mapString;
109     }
110
111     if (JNI_TRUE == env->CallBooleanMethod(iterator, hasNextMethodId))
112     {
113         jobject entry = env->CallObjectMethod(iterator, nextMethodId);
114         jstring jMapKeyStr = (jstring)env->CallObjectMethod(entry, getKeyMethodId);
115         jstring jMapValueStr = (jstring)env->CallObjectMethod(entry, getValueMethodId);
116
117         const char *key = env->GetStringUTFChars(jMapKeyStr, 0);
118         const char *value = env->GetStringUTFChars(jMapValueStr, 0);
119
120         if ((NULL != key) && (NULL != value))
121         {
122             std::string keyStr = key;
123             std::string valueStr = value;
124             mapString.insert(std::make_pair(keyStr, valueStr));
125         }
126         env->ReleaseStringUTFChars(jMapKeyStr, key);
127         env->ReleaseStringUTFChars(jMapValueStr, value);
128     }
129
130     return mapString;
131 }