configuration_loader.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 New();
  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. // Sets the search path to the default search path (namely, "$HADOOP_CONF_DIR" or "/etc/hadoop/conf")
  70. void SetDefaultSearchPath();
  71. // Clears out the search path
  72. void ClearSearchPath();
  73. // Sets the search path to ":"-delimited paths
  74. void SetSearchPath(const std::string & searchPath);
  75. // Adds an element to the search path
  76. void AddToSearchPath(const std::string & searchPath);
  77. // Returns the search path in ":"-delmited form
  78. std::string GetSearchPath();
  79. protected:
  80. using ConfigMap = Configuration::ConfigMap;
  81. // Updates the src map with data from the XML in the path
  82. // The search path will be searched for the filename
  83. bool UpdateMapWithFile(ConfigMap & map, const std::string & path) const;
  84. // Updates the src map with data from the XML in the stream
  85. // The stream must be seekable
  86. static bool UpdateMapWithStream(ConfigMap & map,
  87. std::istream & stream);
  88. // Updates the src map with data from the XML
  89. static bool UpdateMapWithString(Configuration::ConfigMap & src,
  90. const std::string &xml_data);
  91. // Updates the src map with data from the XML
  92. static bool UpdateMapWithBytes(Configuration::ConfigMap &map,
  93. std::vector<char> &raw_bytes);
  94. // Attempts to update the map. If the update failed (because there was
  95. // an existing final value, for example), returns false
  96. static bool UpdateMapWithValue(ConfigMap& map,
  97. const std::string& key, const std::string& value, const std::string& final_text);
  98. std::vector<std::string> search_path_;
  99. };
  100. }
  101. #include "configuration_loader_impl.h"
  102. #endif