1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 "Configuration.h"
25 #include "InternalTypes.h"
28 #define CONTAINER_TAG "RESOURCE_CONTAINER"
29 #define DISCOVER_TAG "DISCOVER_RESOURCE_UNIT"
35 static inline std::string trim_both(const std::string &str)
37 size_t npos = str.find_first_not_of(" \t\v\n\r");
39 if (npos == std::string::npos)
44 std::string tempString = str.substr(npos, str.length());
45 npos = tempString.find_last_not_of(" \t\v\n\r");
47 return npos == std::string::npos ? tempString : tempString.substr(0, npos + 1);
50 Configuration::Configuration()
55 Configuration::Configuration(string configFile)
59 m_pathConfigFile.append(configFile);
61 getConfigDocument(m_pathConfigFile);
64 Configuration::~Configuration()
68 bool Configuration::isLoaded() const
73 bool Configuration::isHasInput(std::string &bundleId) const
78 OIC_LOG_V(INFO, CONTAINER_TAG, "isHasInput: (%d) %s",m_mapisHasInput.at(bundleId), bundleId.c_str() );
79 return m_mapisHasInput.at(bundleId);
81 catch (std::out_of_range &e)
83 OIC_LOG_V(INFO, CONTAINER_TAG, "isHasInput out of range %s.", bundleId.c_str());
88 void Configuration::getConfiguredBundles(configInfo *configOutput)
90 rapidxml::xml_node< char > *bundle;
91 rapidxml::xml_node< char > *subItem;
93 string strKey, strValue;
99 bundle = m_xmlDoc.first_node();
102 for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
103 bundle->next_sibling())
105 std::map< std::string, std::string > bundleMap;
106 for (subItem = bundle->first_node(); subItem;
107 subItem = subItem->next_sibling())
109 strKey = subItem->name();
110 strValue = subItem->value();
112 if (strlen(subItem->value()) > 0)
115 std::make_pair(trim_both(strKey), trim_both(strValue)));
118 configOutput->push_back(bundleMap);
123 catch (rapidxml::parse_error &e)
125 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
126 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception : (%s)", e.what());
131 void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
133 rapidxml::xml_node< char > *bundle;
135 string strBundleId, strPath, strVersion;
141 std::map< std::string, std::string > bundleConfigMap;
144 bundle = m_xmlDoc.first_node();
147 for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
148 bundle->next_sibling())
151 if (bundle->first_node(BUNDLE_ID))
153 strBundleId = bundle->first_node(BUNDLE_ID)->value();
160 if (!strBundleId.compare(bundleId))
162 bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
165 if (bundle->first_node(BUNDLE_PATH))
167 strPath = bundle->first_node(BUNDLE_PATH)->value();
173 bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
176 if (bundle->first_node(BUNDLE_VERSION))
178 strVersion = bundle->first_node(BUNDLE_VERSION)->value();
184 bundleConfigMap.insert(
185 std::make_pair(BUNDLE_VERSION, trim_both(strVersion)));
187 configOutput->push_back(bundleConfigMap);
194 catch (rapidxml::parse_error &e)
196 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
197 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
202 void Configuration::getResourceConfiguration(std::string bundleId, std::string resourceUri,
203 resourceInfo *resourceInfoOut)
205 rapidxml::xml_node< char > *bundle;
206 rapidxml::xml_node< char > *resource;
207 rapidxml::xml_node< char > *item, *subItem, *subItem2;
210 string strKey, strValue;
211 OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource configuration for %s %s!",
212 bundleId.c_str(), resourceUri.c_str());
219 bundle = m_xmlDoc.first_node();
222 for (bundle = bundle->first_node(BUNDLE_TAG); bundle;
223 bundle = bundle->next_sibling())
226 strBundleId = bundle->first_node(BUNDLE_ID)->value();
228 OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle id %s - %s !",
229 strBundleId.c_str(), bundleId.c_str());
231 if (!strBundleId.compare(bundleId))
233 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
235 bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
238 for (resource = bundle->first_node(OUTPUT_RESOURCE_INFO);
239 resource; resource = resource->next_sibling())
242 for (item = resource->first_node(); item; item =
243 item->next_sibling())
245 strKey = item->name();
246 strValue = item->value();
248 if (!strKey.compare(OUTPUT_RESOURCE_NAME))
251 resourceInfoOut->name = trim_both(strValue);
254 else if (!strKey.compare(OUTPUT_RESOURCE_URI))
256 if (trim_both(strValue).compare(resourceUri) != 0)
260 resourceInfoOut->uri = trim_both(strValue);
263 else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
265 resourceInfoOut->address = trim_both(strValue);
268 else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
270 resourceInfoOut->resourceType = trim_both(strValue);
275 for (subItem = item->first_node(); subItem; subItem =
276 subItem->next_sibling())
278 map< string, string > propertyMap;
280 strKey = subItem->name();
282 if (strKey.compare(INPUT_RESOURCE))
284 m_mapisHasInput[strBundleId] = true;
285 OIC_LOG_V(INFO, CONTAINER_TAG,
286 "Bundle has input (%s)",
287 strBundleId.c_str());
290 for (subItem2 = subItem->first_node(); subItem2;
291 subItem2 = subItem2->next_sibling())
293 string newStrKey = subItem2->name();
294 string newStrValue = subItem2->value();
295 OIC_LOG_V(INFO, CONTAINER_TAG,
297 newStrKey.c_str(), newStrValue.c_str());
299 propertyMap[trim_both(newStrKey)] =
300 trim_both(newStrValue);
303 resourceInfoOut->resourceProperty[trim_both(strKey)].push_back(
315 catch (rapidxml::parse_error &e)
317 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
318 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
323 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
327 void Configuration::getResourceConfiguration(std::string bundleId,
328 std::vector< resourceInfo > *configOutput)
330 rapidxml::xml_node< char > *bundle;
331 rapidxml::xml_node< char > *resource;
332 rapidxml::xml_node< char > *item, *subItem, *subItem2;
335 string strKey, strValue;
336 OIC_LOG(INFO, CONTAINER_TAG, "Loading resource configuration!");
343 bundle = m_xmlDoc.first_node();
346 for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
347 bundle->next_sibling())
350 strBundleId = bundle->first_node(BUNDLE_ID)->value();
352 OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle ids %s - %s !",
353 strBundleId.c_str(), bundleId.c_str());
355 if (!strBundleId.compare(bundleId))
357 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
359 bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
362 for (resource = bundle->
363 first_node(OUTPUT_RESOURCE_INFO);
364 resource; resource = resource->next_sibling())
366 resourceInfo tempResourceInfo;
368 for (item = resource->first_node(); item; item =
369 item->next_sibling())
371 strKey = item->name();
372 strValue = item->value();
374 if (!strKey.compare(OUTPUT_RESOURCE_NAME))
376 tempResourceInfo.name = trim_both(strValue);
379 else if (!strKey.compare(OUTPUT_RESOURCE_URI))
381 tempResourceInfo.uri = trim_both(strValue);
384 else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
386 tempResourceInfo.address = trim_both(strValue);
389 else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
391 tempResourceInfo.resourceType = trim_both(strValue);
396 for (subItem = item->first_node(); subItem; subItem =
397 subItem->next_sibling())
399 map< string, string > propertyMap;
401 strKey = subItem->name();
403 if (strKey.compare(INPUT_RESOURCE))
405 m_mapisHasInput[strBundleId] = true;
406 OIC_LOG_V(INFO, CONTAINER_TAG,
407 "Bundle has input (%s)",
408 strBundleId.c_str());
411 for (subItem2 = subItem->first_node(); subItem2;
412 subItem2 = subItem2->next_sibling())
414 string newStrKey = subItem2->name();
415 string newStrValue = subItem2->value();
416 OIC_LOG_V(INFO, CONTAINER_TAG,
419 newStrValue.c_str());
421 propertyMap[trim_both(newStrKey)] =
422 trim_both(newStrValue);
425 tempResourceInfo.resourceProperty[trim_both(strKey)].
426 push_back(propertyMap);
430 configOutput->push_back(tempResourceInfo);
437 catch (rapidxml::parse_error &e)
439 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
440 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
445 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
449 void Configuration::getConfigDocument(std::string pathConfigFile)
451 std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
455 xmlFile.seekg(0, std::ios::end);
456 unsigned int size = (unsigned int) xmlFile.tellg();
459 std::vector< char > xmlData(size + 1);
462 xmlFile.read(&xmlData.front(), (std::streamsize) size);
464 m_strConfigData = std::string(xmlData.data());
468 m_xmlDoc.parse< 0 >((char *) m_strConfigData.c_str());
471 catch (rapidxml::parse_error &e)
473 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
474 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
479 OIC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");