configuration_loader.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "configuration_loader.h"
  19. #include <fstream>
  20. #include <strings.h>
  21. #include <sstream>
  22. #include <map>
  23. #include <sys/stat.h>
  24. #include <rapidxml/rapidxml.hpp>
  25. #include <rapidxml/rapidxml_utils.hpp>
  26. namespace hdfs {
  27. /*
  28. * ConfigurationLoader class
  29. */
  30. #if defined(WIN32) || defined(_WIN32)
  31. static const char kFileSeparator = '\\';
  32. #else
  33. static const char kFileSeparator = '/';
  34. #endif
  35. static const char kSearchPathSeparator = ':';
  36. bool is_valid_bool(const std::string& raw) {
  37. if (raw.empty()) {
  38. return false;
  39. }
  40. if (!strcasecmp(raw.c_str(), "true")) {
  41. return true;
  42. }
  43. if (!strcasecmp(raw.c_str(), "false")) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. bool str_to_bool(const std::string& raw) {
  49. if (!strcasecmp(raw.c_str(), "true")) {
  50. return true;
  51. }
  52. return false;
  53. }
  54. ConfigurationLoader::ConfigurationLoader() {
  55. //In order to creates a configuration loader with the default search path
  56. //("$HADOOP_CONF_DIR" or "/etc/hadoop/conf") we call SetDefaultSearchPath().
  57. ConfigurationLoader::SetDefaultSearchPath();
  58. }
  59. void ConfigurationLoader::SetDefaultSearchPath() {
  60. // Try (in order, taking the first valid one):
  61. // $HADOOP_CONF_DIR
  62. // /etc/hadoop/conf
  63. const char * hadoop_conf_dir_env = getenv("HADOOP_CONF_DIR");
  64. if (hadoop_conf_dir_env) {
  65. AddToSearchPath(hadoop_conf_dir_env);
  66. } else {
  67. AddToSearchPath("/etc/hadoop/conf");
  68. }
  69. }
  70. void ConfigurationLoader::ClearSearchPath()
  71. {
  72. search_path_.clear();
  73. }
  74. void ConfigurationLoader::SetSearchPath(const std::string & searchPath)
  75. {
  76. search_path_.clear();
  77. std::vector<std::string> paths;
  78. std::string::size_type start = 0;
  79. std::string::size_type end = searchPath.find(kSearchPathSeparator);
  80. while (end != std::string::npos) {
  81. paths.push_back(searchPath.substr(start, end-start));
  82. start = ++end;
  83. end = searchPath.find(kSearchPathSeparator, start);
  84. }
  85. paths.push_back(searchPath.substr(start, searchPath.length()));
  86. for (auto path: paths) {
  87. AddToSearchPath(path);
  88. }
  89. }
  90. void ConfigurationLoader::AddToSearchPath(const std::string & searchPath)
  91. {
  92. if (searchPath.empty())
  93. return;
  94. if (searchPath.back() != kFileSeparator) {
  95. std::string pathWithSlash(searchPath);
  96. pathWithSlash += kFileSeparator;
  97. search_path_.push_back(pathWithSlash);
  98. } else {
  99. search_path_.push_back(searchPath);
  100. }
  101. }
  102. std::string ConfigurationLoader::GetSearchPath()
  103. {
  104. std::stringstream result;
  105. bool first = true;
  106. for(std::string item: search_path_) {
  107. if (!first) {
  108. result << kSearchPathSeparator;
  109. }
  110. result << item;
  111. first = false;
  112. }
  113. return result.str();
  114. }
  115. bool ConfigurationLoader::UpdateMapWithFile(ConfigMap & map, const std::string & path) const
  116. {
  117. if (path.front() == kFileSeparator) { // Absolute path
  118. std::ifstream stream(path, std::ifstream::in);
  119. if ( stream.is_open() ) {
  120. return UpdateMapWithStream(map, stream);
  121. } else {
  122. return false;
  123. }
  124. } else { // Use search path
  125. for(auto dir: search_path_) {
  126. std::ifstream stream(dir + path);
  127. if ( stream.is_open() ) {
  128. if (UpdateMapWithStream(map, stream))
  129. return true;
  130. }
  131. }
  132. }
  133. return false;
  134. }
  135. bool ConfigurationLoader::UpdateMapWithStream(ConfigMap & map,
  136. std::istream & stream) {
  137. std::streampos start = stream.tellg();
  138. stream.seekg(0, std::ios::end);
  139. std::streampos end = stream.tellg();
  140. stream.seekg(start, std::ios::beg);
  141. int length = end - start;
  142. if (length <= 0 || start == -1 || end == -1)
  143. return false;
  144. std::vector<char> raw_bytes((int64_t)length + 1);
  145. stream.read(&raw_bytes[0], length);
  146. raw_bytes[length] = 0;
  147. return UpdateMapWithBytes(map, raw_bytes);
  148. }
  149. bool ConfigurationLoader::UpdateMapWithString(ConfigMap & map,
  150. const std::string &xml_data) {
  151. if (xml_data.size() == 0) {
  152. return false;
  153. }
  154. std::vector<char> raw_bytes(xml_data.begin(), xml_data.end());
  155. raw_bytes.push_back('\0');
  156. bool success = UpdateMapWithBytes(map, raw_bytes);
  157. if (success) {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. bool ConfigurationLoader::UpdateMapWithBytes(ConfigMap& map,
  164. std::vector<char>& raw_bytes) {
  165. try {
  166. rapidxml::xml_document<> dom;
  167. dom.parse<rapidxml::parse_trim_whitespace>(&raw_bytes[0]);
  168. /* File must contain a single <configuration> stanza */
  169. auto config_node = dom.first_node("configuration", 0, false);
  170. if (!config_node) {
  171. return false;
  172. }
  173. /* Walk all of the <property> nodes, ignoring the rest */
  174. for (auto property_node = config_node->first_node("property", 0, false);
  175. property_node;
  176. property_node = property_node->next_sibling("property", 0, false)) {
  177. auto name_node = property_node->first_node("name", 0, false);
  178. auto value_node = property_node->first_node("value", 0, false);
  179. if (name_node && value_node) {
  180. std::string final_value;
  181. auto final_node = property_node->first_node("final", 0, false);
  182. if (final_node) {
  183. final_value = final_node->value();
  184. }
  185. UpdateMapWithValue(map, name_node->value(), value_node->value(), final_value);
  186. }
  187. auto name_attr = property_node->first_attribute("name", 0, false);
  188. auto value_attr = property_node->first_attribute("value", 0, false);
  189. if (name_attr && value_attr) {
  190. std::string final_value;
  191. auto final_attr = property_node->first_attribute("final", 0, false);
  192. if (final_attr) {
  193. final_value = final_attr->value();
  194. }
  195. UpdateMapWithValue(map, name_attr->value(), value_attr->value(), final_value);
  196. }
  197. }
  198. return true;
  199. } catch (const rapidxml::parse_error &e) {
  200. // TODO: Capture the result in a Status object
  201. return false;
  202. }
  203. }
  204. bool ConfigurationLoader::UpdateMapWithValue(ConfigMap& map,
  205. const std::string& key, const std::string& value,
  206. const std::string& final_text)
  207. {
  208. std::string caseFixedKey = Configuration::fixCase(key);
  209. auto mapValue = map.find(caseFixedKey);
  210. if (mapValue != map.end() && mapValue->second.final) {
  211. return false;
  212. }
  213. bool final_value = false;
  214. if (is_valid_bool(final_text)) {
  215. final_value = str_to_bool(final_text);
  216. }
  217. map[caseFixedKey].value = value;
  218. map[caseFixedKey].final = final_value;
  219. return true;
  220. }
  221. }