configuration_loader.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef COMMON_CONFIGURATION_BUILDER_H_
  19. #define COMMON_CONFIGURATION_BUILDER_H_
  20. #include "configuration.h"
  21. #include "hdfspp/status.h"
  22. namespace hdfs {
  23. class ConfigurationLoader {
  24. public:
  25. // Creates a new, empty Configuration object
  26. // T must be Configuration or a subclass
  27. template<class T>
  28. T NewConfig();
  29. /****************************************************************************
  30. * LOADING CONFIG FILES
  31. ***************************************************************************/
  32. // Loads Configuration XML contained in a string/stream/file and returns a parsed
  33. // Configuration object.
  34. // T must be Configuration or a subclass
  35. template<class T>
  36. optional<T> Load(const std::string &xml_data);
  37. // Streams must be seekable
  38. template<class T>
  39. optional<T> LoadFromStream(std::istream & stream);
  40. // The ConfigurationBuilder's search path will be searched for the filename
  41. // unless it is an absolute path
  42. template<class T>
  43. optional<T> LoadFromFile(const std::string &filename);
  44. // Loads Configuration XML contained in a string and produces a new copy that
  45. // is the union of the src and xml_data
  46. // Any parameters from src will be overwritten by the xml_data unless they
  47. // are marked as "final" in src.
  48. // T must be Configuration or a subclass
  49. template<class T>
  50. optional<T> OverlayResourceString(const T &src, const std::string &xml_data) const;
  51. // Streams must be seekable
  52. template<class T>
  53. optional<T> OverlayResourceStream(const T &src, std::istream &stream) const;
  54. // The ConfigurationBuilder's search path will be searched for the filename
  55. // unless it is an absolute path
  56. template<class T>
  57. optional<T> OverlayResourceFile(const T &src, const std::string &path) const;
  58. // Attempts to update the map. If the update failed (because there was
  59. // an existing final value, for example), returns the original map
  60. template<class T>
  61. optional<T> OverlayValue(const T &src, const std::string &key, const std::string &value) const;
  62. // Returns an instance of the Configuration with all of the default resource
  63. // files loaded.
  64. // T must be Configuration or a subclass
  65. template<class T>
  66. optional<T> LoadDefaultResources();
  67. // Returns a vector of filenames and the corresponding status when validation is attempted.
  68. // If the files can be successfully validated, then the status returned for that file is Status::OK
  69. // The files that are validated are those returned by T::GetDefaultFilenames().
  70. // T must be Configuration or a subclass
  71. template<class T>
  72. std::vector<std::pair<std::string, Status>> ValidateDefaultResources() const;
  73. /****************************************************************************
  74. * SEARCH PATH METHODS
  75. ***************************************************************************/
  76. //Creates a configuration loader with the default search path ("$HADOOP_CONF_DIR" or "/etc/hadoop/conf").
  77. //If you want to explicitly set the entire search path, call ClearSearchPath() first
  78. ConfigurationLoader();
  79. // Sets the search path to the default search path (namely, "$HADOOP_CONF_DIR" or "/etc/hadoop/conf")
  80. void SetDefaultSearchPath();
  81. // Clears out the search path
  82. void ClearSearchPath();
  83. // Sets the search path to ":"-delimited paths
  84. void SetSearchPath(const std::string & searchPath);
  85. // Adds an element to the search path
  86. void AddToSearchPath(const std::string & searchPath);
  87. // Returns the search path in ":"-delmited form
  88. std::string GetSearchPath();
  89. protected:
  90. using ConfigMap = Configuration::ConfigMap;
  91. std::vector<std::pair<std::string, Status>> ValidateResources(std::vector<std::string> filenames) const;
  92. // Updates the src map with data from the XML in the path
  93. // The search path will be searched for the filename
  94. bool UpdateMapWithFile(ConfigMap & map, const std::string & path) const;
  95. // Updates the src map with data from the XML in the stream
  96. // The stream must be seekable
  97. static bool UpdateMapWithStream(ConfigMap & map,
  98. std::istream & stream);
  99. // Updates the src map with data from the XML
  100. static bool UpdateMapWithString(Configuration::ConfigMap & src,
  101. const std::string &xml_data);
  102. // Updates the src map with data from the XML
  103. static bool UpdateMapWithBytes(Configuration::ConfigMap &map,
  104. std::vector<char> &raw_bytes);
  105. // Attempts to update the map. If the update failed (because there was
  106. // an existing final value, for example), returns false
  107. static bool UpdateMapWithValue(ConfigMap& map,
  108. const std::string& key, const std::string& value, const std::string& final_text);
  109. std::vector<std::string> search_path_;
  110. };
  111. }
  112. #include "configuration_loader_impl.h"
  113. #endif