ServiceConfiguration.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "config.h"
00031
00032 #include "ServiceConfiguration.hpp"
00033
00034
00035 #include <Exception.hpp>
00036
00037 #include <XmlCharData.hpp>
00038 #include <XmlElementVector.hpp>
00039
00040 #include <sstream>
00041
00042 using namespace mermaid::support::service;
00043 using mermaid::support::errorhandling::Exception;
00044 using boost::shared_ptr;
00045 using mermaid::support::xml::XmlCharData;
00046 using mermaid::support::xml::XmlElement;
00047 using mermaid::support::xml::XmlElementVector;
00048
00049 #include <iostream>
00050
00051 ServiceConfiguration::ServiceConfiguration (shared_ptr<XmlDocument> serviceConfiguration)
00052 {
00053
00054 shared_ptr<XmlElement> serviceConfigurationElement = serviceConfiguration->getRootElement();
00055 XmlElementVector serviceConfigurationChildren = serviceConfigurationElement->getChildrenElements();
00056 shared_ptr<XmlElement> updateFrequencyElement = serviceConfigurationChildren.getFirstElementWithName ("update-frequency");
00057 shared_ptr<XmlElement> configItemElement = serviceConfigurationChildren.getFirstElementWithName ("config-items");
00058
00059 if (updateFrequencyElement == false) {
00060 throw Exception ("ServiceConfiguration::ServiceConfiguration() : no update-frequency element found in XML");
00061 };
00062
00063
00064 {
00065 shared_ptr<XmlCharData> updateFrequencyChar = updateFrequencyElement->getFirstNonBlankCharData();
00066
00067 if (updateFrequencyChar == false) {
00068 throw Exception ("ServiceConfiguration::ServiceConfiguration() : update-frequency item is empty");
00069 }
00070
00071 std::string updateFrequency = (std::string) ( (*updateFrequencyChar));
00072
00073 std::istringstream iss (updateFrequency);
00074 iss >> serviceUpdateFrequency_;
00075 }
00076
00077
00078 if (configItemElement == false) {
00079 return;
00080 };
00081
00082
00083 {
00084 XmlElementVector configItemsChildren = configItemElement->getChildrenElements();
00085
00086 XmlElementVector::iterator it;
00087
00088 for (it = configItemsChildren.begin(); it != configItemsChildren.end(); it++) {
00089 shared_ptr<XmlElement> e = *it;
00090
00091 if (e->getName() == "item") {
00092 XmlElementVector itemChildren = e->getChildrenElements();
00093 shared_ptr<XmlElement> nameElement = itemChildren.getFirstElementWithName ("name");
00094 shared_ptr<XmlElement> valueElement = itemChildren.getFirstElementWithName ("value");
00095
00096 if (nameElement == false) {
00097 throw Exception ("ServiceConfiguration::ServiceConfiguration() : item's name element is not valid");
00098 }
00099
00100 if (valueElement == false) {
00101 throw Exception ("ServiceConfiguration::ServiceConfiguration() : item's value element is not valid");
00102 }
00103
00104
00105 shared_ptr<XmlCharData> nameChar = nameElement->getFirstNonBlankCharData();
00106 shared_ptr<XmlCharData> valueChar = valueElement->getFirstNonBlankCharData();
00107
00108 if (nameChar == false) {
00109 throw Exception ("ServiceConfiguration::ServiceConfiguration() : item's name element is empty");
00110 }
00111
00112 if (valueChar == false) {
00113 throw Exception ("ServiceConfiguration::ServiceConfiguration() : item's value element is empty");
00114 }
00115
00116 std::string nameString = (std::string) ( (*nameChar));
00117 std::string valueString = (std::string) ( (*valueChar));
00118
00119 valuesMap_[nameString] = valueString;
00120 }
00121 }
00122 }
00123 }
00124
00125
00126
00127
00128
00129
00130 float ServiceConfiguration::getServiceUpdateFrequency()
00131 {
00132 return serviceUpdateFrequency_;
00133 };
00134
00135 std::string ServiceConfiguration::getConfigValue (std::string name)
00136 {
00137 std::map<std::string, std::string>::iterator it = valuesMap_.find (name);
00138
00139 if (it != valuesMap_.end()) {
00140 return (*it).second;
00141 }
00142 else {
00143 throw Exception ("ServiceConfiguration::getConfigValue : unknown config item name: " + name);
00144 }
00145 };
00146