configuration.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /*
  19. * The following features are not currently implemented
  20. * - Deprecated values
  21. * - Make filename and config file contents unicode-safe
  22. * - Config redirection/environment substitution
  23. *
  24. * - getInts (comma separated))
  25. * - getStrings (comma separated))
  26. * - getIntegerRange
  27. * - getSocketAddr
  28. * - getTimeDuration
  29. * - getBytes (e.g. 1M or 1G)
  30. * - hex values
  31. */
  32. #include "configuration.h"
  33. #include "hdfspp/uri.h"
  34. #include <strings.h>
  35. #include <sstream>
  36. #include <map>
  37. #include <rapidxml/rapidxml.hpp>
  38. #include <rapidxml/rapidxml_utils.hpp>
  39. namespace hdfs {
  40. /*
  41. * Configuration class
  42. */
  43. std::vector<std::string> Configuration::GetDefaultFilenames() {
  44. auto result = std::vector<std::string>();
  45. result.push_back("core-site.xml");
  46. return result;
  47. }
  48. optional<std::string> Configuration::Get(const std::string& key) const {
  49. std::string caseFixedKey = fixCase(key);
  50. auto found = raw_values_.find(caseFixedKey);
  51. if (found != raw_values_.end()) {
  52. return std::experimental::make_optional(found->second.value);
  53. } else {
  54. return optional<std::string>();
  55. }
  56. }
  57. std::string Configuration::GetWithDefault(
  58. const std::string& key, const std::string& default_value) const {
  59. return Get(key).value_or(default_value);
  60. }
  61. optional<int64_t> Configuration::GetInt(const std::string& key) const {
  62. auto raw = Get(key);
  63. if (raw) {
  64. errno = 0;
  65. char* end = nullptr;
  66. auto result =
  67. std::experimental::make_optional(strtol(raw->c_str(), &end, 10));
  68. if (end == raw->c_str()) {
  69. /* strtoll will set end to input if no conversion was done */
  70. return optional<int64_t>();
  71. }
  72. if (errno == ERANGE) {
  73. return optional<int64_t>();
  74. }
  75. return result;
  76. } else {
  77. return optional<int64_t>();
  78. }
  79. }
  80. int64_t Configuration::GetIntWithDefault(const std::string& key,
  81. int64_t default_value) const {
  82. return GetInt(key).value_or(default_value);
  83. }
  84. optional<double> Configuration::GetDouble(const std::string& key) const {
  85. auto raw = Get(key);
  86. if (raw) {
  87. errno = 0;
  88. char* end = nullptr;
  89. auto result = std::experimental::make_optional(strtod(raw->c_str(), &end));
  90. if (end == raw->c_str()) {
  91. /* strtod will set end to input if no conversion was done */
  92. return optional<double>();
  93. }
  94. if (errno == ERANGE) {
  95. return optional<double>();
  96. }
  97. return result;
  98. } else {
  99. return optional<double>();
  100. }
  101. }
  102. double Configuration::GetDoubleWithDefault(const std::string& key,
  103. double default_value) const {
  104. return GetDouble(key).value_or(default_value);
  105. }
  106. optional<bool> Configuration::GetBool(const std::string& key) const {
  107. auto raw = Get(key);
  108. if (!raw) {
  109. return optional<bool>();
  110. }
  111. if (!strcasecmp(raw->c_str(), "true")) {
  112. return std::experimental::make_optional(true);
  113. }
  114. if (!strcasecmp(raw->c_str(), "false")) {
  115. return std::experimental::make_optional(false);
  116. }
  117. return optional<bool>();
  118. }
  119. bool Configuration::GetBoolWithDefault(const std::string& key,
  120. bool default_value) const {
  121. return GetBool(key).value_or(default_value);
  122. }
  123. optional<URI> Configuration::GetUri(const std::string& key) const {
  124. optional<std::string> raw = Get(key);
  125. if (raw) {
  126. try {
  127. return std::experimental::make_optional(URI::parse_from_string(*raw));
  128. } catch (const uri_parse_error& e) {
  129. // Return empty below
  130. }
  131. }
  132. return optional<URI>();
  133. }
  134. URI Configuration::GetUriWithDefault(const std::string& key,
  135. std::string default_value) const {
  136. optional<URI> result = GetUri(key);
  137. if (result) {
  138. return *result;
  139. } else {
  140. try {
  141. return URI::parse_from_string(default_value);
  142. } catch (const uri_parse_error& e) {
  143. return URI();
  144. }
  145. }
  146. }
  147. }