configuration_loader.h 5.0 KB

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