resource-encapsulation: fix prevent issue for container
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceContainer / src / Configuration.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 "Configuration.h"
22
23 namespace OIC
24 {
25     namespace Service
26     {
27         static inline std::string trim_both(const std::string &str)
28         {
29             int npos = str.find_first_not_of(" \t\v\n\r");
30
31             if (npos == -1)
32             {
33                 return "";
34             }
35
36             unsigned int n = (unsigned int) npos;
37             std::string tempString = n == std::string::npos ? str : str.substr(n, str.length());
38
39             n = tempString.find_last_not_of(" \t\v\n\r");
40
41             return n == std::string::npos ? tempString : tempString.substr(0, n + 1);
42         }
43
44         Configuration::Configuration()
45         {
46             m_loaded = false;
47         }
48
49         Configuration::Configuration(string configFile)
50         {
51             m_loaded = false;
52
53             getCurrentPath(&m_pathConfigFile);
54             m_pathConfigFile.append("/");
55             m_pathConfigFile.append(configFile);
56
57             getConfigDocument(m_pathConfigFile);
58         }
59
60         Configuration::~Configuration()
61         {
62         }
63
64         bool Configuration::isLoaded()
65         {
66             return m_loaded;
67         }
68
69         void Configuration::getConfiguredBundles(configInfo *configOutput)
70         {
71             rapidxml::xml_node< char > *bundle;
72             rapidxml::xml_node< char > *subItem;
73
74             string strKey, strValue;
75
76             if (m_loaded)
77             {
78                 try
79                 {
80                     //cout << "Name of first node is: " << m_xmlDoc.first_node()->name() << endl;
81
82                     for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling())
83                     {
84                         std::map< std::string, std::string > bundleMap;
85                         //cout << "Bundle: " << bundle->name() << endl;
86                         for (subItem = bundle->first_node(); subItem; subItem = subItem->next_sibling())
87                         {
88                             strKey = subItem->name();
89                             strValue = subItem->value();
90
91                             if (strlen(subItem->value()) > 0)
92                             {
93                                 bundleMap.insert(std::make_pair(trim_both(strKey), trim_both(strValue)));
94                                 //cout << strKey << " " << strValue << endl;
95                             }
96                         }
97                         configOutput->push_back(bundleMap);
98                     }
99
100                 }
101                 catch (rapidxml::parse_error &e)
102                 {
103                     cout << "xml parsing failed !!" << endl;
104                     cout << e.what() << endl;
105                 }
106             }
107         }
108
109         void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
110         {
111             rapidxml::xml_node< char > *bundle;
112
113             string strBundleId, strPath, strVersion;
114
115             if (m_loaded)
116             {
117                 try
118                 {
119                     std::map< std::string, std::string > bundleConfigMap;
120
121                     // <bundle>
122                     for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling())
123                     {
124                         // <id>
125                         strBundleId = bundle->first_node("id")->value();
126
127                         if (!strBundleId.compare(bundleId))
128                         {
129                             bundleConfigMap.insert(std::make_pair("id", trim_both(strBundleId)));
130
131                             // <path>
132                             strPath = bundle->first_node("path")->value();
133                             bundleConfigMap.insert(std::make_pair("path", trim_both(strPath)));
134
135                             // <version>
136                             strVersion = bundle->first_node("version")->value();
137                             bundleConfigMap.insert(std::make_pair("version", trim_both(strVersion)));
138
139                             configOutput->push_back(bundleConfigMap);
140
141                             break;
142                         }
143                     }
144                 }
145                 catch (rapidxml::parse_error &e)
146                 {
147                     cout << "xml parsing failed !!" << endl;
148                     cout << e.what() << endl;
149                 }
150             }
151         }
152
153         void Configuration::getResourceConfiguration(std::string bundleId,
154                 std::vector< resourceInfo > *configOutput)
155         {
156             rapidxml::xml_node< char > *bundle;
157             rapidxml::xml_node< char > *resource;
158             rapidxml::xml_node< char > *item, *subItem, *subItem2;
159
160             string strBundleId;
161             string strKey, strValue;
162
163             if (m_loaded)
164             {
165                 try
166                 {
167                     // <bundle>
168                     for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling())
169                     {
170                         // <id>
171                         strBundleId = bundle->first_node("id")->value();
172
173                         if (!strBundleId.compare(bundleId))
174                         {
175                             // <resourceInfo>
176                             for (resource = bundle->first_node("resources")->first_node("resourceInfo"); resource;
177                                  resource = resource->next_sibling())
178                             {
179                                 resourceInfo tempResourceInfo;
180
181                                 for (item = resource->first_node(); item; item = item->next_sibling())
182                                 {
183                                     strKey = item->name();
184                                     strValue = item->value();
185
186                                     if (!strKey.compare("name"))
187                                         tempResourceInfo.name = trim_both(strValue);
188
189                                     else if (!strKey.compare("uri"))
190                                         tempResourceInfo.uri = trim_both(strValue);
191
192                                     else if (!strKey.compare("address"))
193                                         tempResourceInfo.address = trim_both(strValue);
194
195                                     else if (!strKey.compare("resourceType"))
196                                         tempResourceInfo.resourceType = trim_both(strValue);
197
198                                     else
199                                     {
200                                         for (subItem = item->first_node(); subItem; subItem = subItem->next_sibling())
201                                         {
202                                             map< string, string > propertyMap;
203
204                                             strKey = subItem->name();
205
206                                             for (subItem2 = subItem->first_node(); subItem2; subItem2 = subItem2->next_sibling())
207                                             {
208                                                 string newStrKey = subItem2->name();
209                                                 string newStrValue = subItem2->value();
210
211                                                 propertyMap[trim_both(newStrKey)] = trim_both(newStrValue);
212                                             }
213
214                                             tempResourceInfo.resourceProperty[trim_both(strKey)].push_back(propertyMap);
215                                         }
216                                     }
217                                 }
218                                 configOutput->push_back(tempResourceInfo);
219                             }
220                         }
221                     }
222                 }
223                 catch (rapidxml::parse_error &e)
224                 {
225                     std::cout << "xml parsing failed !!" << std::endl;
226                     std::cout << e.what() << std::endl;
227                 }
228             }
229         }
230
231         void Configuration::getConfigDocument(std::string pathConfigFile)
232         {
233             std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
234
235             if (!xmlFile.fail())
236             {
237                 xmlFile.seekg(0, std::ios::end);
238                 unsigned int size = (unsigned int) xmlFile.tellg();
239                 xmlFile.seekg(0);
240
241                 std::vector< char > xmlData(size + 1);
242                 xmlData[size] = 0;
243
244                 xmlFile.read(&xmlData.front(), (std::streamsize) size);
245                 xmlFile.close();
246                 m_strConfigData = std::string(xmlData.data());
247
248                 try
249                 {
250                     m_xmlDoc.parse< 0 >((char *)m_strConfigData.c_str());
251                     m_loaded = true;
252                 }
253                 catch (rapidxml::parse_error &e)
254                 {
255                     std::cout << "xml parsing failed !!" << std::endl;
256                     std::cout << e.what() << std::endl;
257                 }
258             }
259             else
260             {
261                 std::cout << "Configuration File load failed !!" << std::endl;
262             }
263         }
264
265         void Configuration::getCurrentPath(std::string *pPath)
266         {
267             char buffer[2048];
268             char *strPath = NULL;
269
270             int length = readlink("/proc/self/exe", buffer, 2047);
271
272             if (length > 0 && length < 2047)
273             {
274                 buffer[length] = '\0';
275
276                 strPath = strrchr(buffer, '/');
277
278                 *strPath = '\0';
279
280                 pPath->append(buffer);
281             }
282         }
283     }
284 }