configuration_loader.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 "common/logging.h"
  20. #include <fstream>
  21. #include <strings.h>
  22. #include <sstream>
  23. #include <map>
  24. #include <sys/stat.h>
  25. #include <rapidxml/rapidxml.hpp>
  26. #include <rapidxml/rapidxml_utils.hpp>
  27. namespace hdfs {
  28. /*
  29. * ConfigurationLoader class
  30. */
  31. #if defined(WIN32) || defined(_WIN32)
  32. static const char kFileSeparator = '\\';
  33. #else
  34. static const char kFileSeparator = '/';
  35. #endif
  36. static const char kSearchPathSeparator = ':';
  37. bool is_valid_bool(const std::string& raw) {
  38. if (raw.empty()) {
  39. return false;
  40. }
  41. if (!strcasecmp(raw.c_str(), "true")) {
  42. return true;
  43. }
  44. if (!strcasecmp(raw.c_str(), "false")) {
  45. return true;
  46. }
  47. return false;
  48. }
  49. bool str_to_bool(const std::string& raw) {
  50. if (!strcasecmp(raw.c_str(), "true")) {
  51. return true;
  52. }
  53. return false;
  54. }
  55. ConfigurationLoader::ConfigurationLoader() {
  56. //In order to creates a configuration loader with the default search path
  57. //("$HADOOP_CONF_DIR" or "/etc/hadoop/conf") we call SetDefaultSearchPath().
  58. ConfigurationLoader::SetDefaultSearchPath();
  59. }
  60. void ConfigurationLoader::SetDefaultSearchPath() {
  61. // Try (in order, taking the first valid one):
  62. // $HADOOP_CONF_DIR
  63. // /etc/hadoop/conf
  64. const char * hadoop_conf_dir_env = getenv("HADOOP_CONF_DIR");
  65. if (hadoop_conf_dir_env) {
  66. std::stringstream ss(hadoop_conf_dir_env);
  67. std::string path;
  68. while (std::getline(ss, path, kSearchPathSeparator)) {
  69. AddToSearchPath(path);
  70. }
  71. } else {
  72. AddToSearchPath("/etc/hadoop/conf");
  73. }
  74. }
  75. void ConfigurationLoader::ClearSearchPath()
  76. {
  77. search_path_.clear();
  78. }
  79. void ConfigurationLoader::SetSearchPath(const std::string & searchPath)
  80. {
  81. search_path_.clear();
  82. std::vector<std::string> paths;
  83. std::string::size_type start = 0;
  84. std::string::size_type end = searchPath.find(kSearchPathSeparator);
  85. while (end != std::string::npos) {
  86. paths.push_back(searchPath.substr(start, end-start));
  87. start = ++end;
  88. end = searchPath.find(kSearchPathSeparator, start);
  89. }
  90. paths.push_back(searchPath.substr(start, searchPath.length()));
  91. for (auto path: paths) {
  92. AddToSearchPath(path);
  93. }
  94. }
  95. void ConfigurationLoader::AddToSearchPath(const std::string & searchPath)
  96. {
  97. if (searchPath.empty())
  98. return;
  99. if (searchPath.back() != kFileSeparator) {
  100. std::string pathWithSlash(searchPath);
  101. pathWithSlash += kFileSeparator;
  102. search_path_.push_back(pathWithSlash);
  103. } else {
  104. search_path_.push_back(searchPath);
  105. }
  106. }
  107. std::string ConfigurationLoader::GetSearchPath()
  108. {
  109. std::stringstream result;
  110. bool first = true;
  111. for(std::string item: search_path_) {
  112. if (!first) {
  113. result << kSearchPathSeparator;
  114. }
  115. result << item;
  116. first = false;
  117. }
  118. return result.str();
  119. }
  120. Status validateStream(std::istream & stream) {
  121. std::streampos start = stream.tellg();
  122. stream.seekg(0, std::ios::end);
  123. std::streampos end = stream.tellg();
  124. stream.seekg(start, std::ios::beg);
  125. int length = end - start;
  126. if (length <= 0 || start == -1 || end == -1)
  127. return Status::Error("The configuration file is empty");
  128. LOG_DEBUG(kFileSystem, << "validateStream will read a config file of length " << length);
  129. std::vector<char> raw_bytes((int64_t)length + 1);
  130. stream.read(&raw_bytes[0], length);
  131. raw_bytes[length] = 0;
  132. try {
  133. rapidxml::xml_document<> dom;
  134. dom.parse<rapidxml::parse_trim_whitespace|rapidxml::parse_validate_closing_tags>(&raw_bytes[0]);
  135. /* File must contain a single <configuration> stanza */
  136. auto config_node = dom.first_node("configuration", 0, false);
  137. if (!config_node) {
  138. return Status::Error("The configuration file is missing a 'configuration' tag");
  139. }
  140. return Status::OK();
  141. } catch (const rapidxml::parse_error &e) {
  142. size_t location = e.where<char>() - &raw_bytes[0];
  143. std::string msg = "The configuration file has invalid xml around character " + std::to_string(location);
  144. return Status::Error(msg.c_str());
  145. }
  146. }
  147. std::vector<std::pair<std::string, Status> > ConfigurationLoader::ValidateResources(std::vector<std::string> filenames) const
  148. {
  149. std::vector<std::pair<std::string, Status> > stats;
  150. bool found;
  151. for(auto file: filenames) {
  152. found = false;
  153. for(auto dir: search_path_) {
  154. std::ifstream stream(dir + file);
  155. if ( stream.is_open() ) {
  156. found = true;
  157. stats.push_back(std::make_pair(file,validateStream(stream)));
  158. } else {
  159. LOG_DEBUG(kFileSystem, << dir << file << " was not found");
  160. }
  161. }
  162. if(!found) {
  163. std::string msg("No directory in the current search path contains the file [" + file + "]");
  164. stats.push_back(std::make_pair(file,Status::PathNotFound(msg.c_str())));
  165. }
  166. }
  167. return stats;
  168. }
  169. bool ConfigurationLoader::UpdateMapWithFile(ConfigMap & map, const std::string & path) const
  170. {
  171. if (path.front() == kFileSeparator) { // Absolute path
  172. std::ifstream stream(path, std::ifstream::in);
  173. if ( stream.is_open() ) {
  174. return UpdateMapWithStream(map, stream);
  175. } else {
  176. return false;
  177. }
  178. } else { // Use search path
  179. for(auto dir: search_path_) {
  180. std::ifstream stream(dir + path);
  181. if ( stream.is_open() ) {
  182. if (UpdateMapWithStream(map, stream))
  183. return true;
  184. }
  185. }
  186. }
  187. return false;
  188. }
  189. bool ConfigurationLoader::UpdateMapWithStream(ConfigMap & map,
  190. std::istream & stream) {
  191. std::streampos start = stream.tellg();
  192. stream.seekg(0, std::ios::end);
  193. std::streampos end = stream.tellg();
  194. stream.seekg(start, std::ios::beg);
  195. int length = end - start;
  196. if (length <= 0 || start == -1 || end == -1)
  197. return false;
  198. std::vector<char> raw_bytes((int64_t)length + 1);
  199. stream.read(&raw_bytes[0], length);
  200. raw_bytes[length] = 0;
  201. return UpdateMapWithBytes(map, raw_bytes);
  202. }
  203. bool ConfigurationLoader::UpdateMapWithString(ConfigMap & map,
  204. const std::string &xml_data) {
  205. if (xml_data.size() == 0) {
  206. return false;
  207. }
  208. std::vector<char> raw_bytes(xml_data.begin(), xml_data.end());
  209. raw_bytes.push_back('\0');
  210. bool success = UpdateMapWithBytes(map, raw_bytes);
  211. if (success) {
  212. return true;
  213. } else {
  214. return false;
  215. }
  216. }
  217. bool ConfigurationLoader::UpdateMapWithBytes(ConfigMap& map,
  218. std::vector<char>& raw_bytes) {
  219. try {
  220. rapidxml::xml_document<> dom;
  221. dom.parse<rapidxml::parse_trim_whitespace>(&raw_bytes[0]);
  222. /* File must contain a single <configuration> stanza */
  223. auto config_node = dom.first_node("configuration", 0, false);
  224. if (!config_node) {
  225. return false;
  226. }
  227. /* Walk all of the <property> nodes, ignoring the rest */
  228. for (auto property_node = config_node->first_node("property", 0, false);
  229. property_node;
  230. property_node = property_node->next_sibling("property", 0, false)) {
  231. auto name_node = property_node->first_node("name", 0, false);
  232. auto value_node = property_node->first_node("value", 0, false);
  233. if (name_node && value_node) {
  234. std::string final_value;
  235. auto final_node = property_node->first_node("final", 0, false);
  236. if (final_node) {
  237. final_value = final_node->value();
  238. }
  239. UpdateMapWithValue(map, name_node->value(), value_node->value(), final_value);
  240. }
  241. auto name_attr = property_node->first_attribute("name", 0, false);
  242. auto value_attr = property_node->first_attribute("value", 0, false);
  243. if (name_attr && value_attr) {
  244. std::string final_value;
  245. auto final_attr = property_node->first_attribute("final", 0, false);
  246. if (final_attr) {
  247. final_value = final_attr->value();
  248. }
  249. UpdateMapWithValue(map, name_attr->value(), value_attr->value(), final_value);
  250. }
  251. }
  252. return true;
  253. } catch (const rapidxml::parse_error &e) {
  254. // TODO: Capture the result in a Status object
  255. return false;
  256. }
  257. }
  258. bool ConfigurationLoader::UpdateMapWithValue(ConfigMap& map,
  259. const std::string& key, const std::string& value,
  260. const std::string& final_text)
  261. {
  262. std::string caseFixedKey = Configuration::fixCase(key);
  263. auto mapValue = map.find(caseFixedKey);
  264. if (mapValue != map.end() && mapValue->second.final) {
  265. return false;
  266. }
  267. bool final_value = false;
  268. if (is_valid_bool(final_text)) {
  269. final_value = str_to_bool(final_text);
  270. }
  271. map[caseFixedKey].value = value;
  272. map[caseFixedKey].final = final_value;
  273. return true;
  274. }
  275. }