config.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <string>
  15. #include <exceptions/exceptions.h>
  16. #include <cc/data.h>
  17. #ifndef __CONFIG_H
  18. #define __CONFIG_H 1
  19. class AuthSrv;
  20. /// An exception that is thrown if an error occurs while configuring an
  21. /// \c AuthSrv object.
  22. class AuthConfigError : public isc::Exception {
  23. public:
  24. AuthConfigError(const char* file, size_t line, const char* what) :
  25. isc::Exception(file, line, what) {}
  26. };
  27. /// The abstract base class that represents a single configuration identifier
  28. /// for an \c AuthSrv object.
  29. ///
  30. /// In general, each top level configuration identifier for \c AuthSrv is
  31. /// expected to have its own derived class of this base class.
  32. /// For example, for the following configuration:
  33. /// \code { "param1": 10, "param2": { "subparam1": "foo", "subparam2": [] } }
  34. /// \endcode
  35. /// "param1" and "param2" are top level identifiers, and would correspond to
  36. /// derived \c AuthConfigParser classes.
  37. /// "subparam1" and/or "subparam2" may also have dedicated derived classes.
  38. ///
  39. /// These derived classes are hidden inside the implementation; applications
  40. /// are not expected to (and in fact cannot) instantiate them directly.
  41. ///
  42. /// Each derived class is generally expected to be constructed with an
  43. /// \c AuthSrv object to be configured and hold a reference to the server
  44. /// throughout the configuration process.
  45. /// For each derived class, the \c build() method parses the configuration
  46. /// value for the corresponding identifier and prepares new configuration
  47. /// value(s) to be applied to the server. This method may throw an exception
  48. /// when it encounters an error.
  49. /// The \c commit() method actually applies the new configuration value
  50. /// to the server. It's basically not expected to throw an exception;
  51. /// any configuration operations that can fail (such as ones involving
  52. /// resource allocation) should be done in \c build().
  53. ///
  54. /// When the destructor is called before \c commit(), the destructor is
  55. /// supposed to make sure the state of the \c AuthSrv object is the same
  56. /// as that before it starts building the configuration value.
  57. /// If \c build() doesn't change the server state (which is recommended)
  58. /// the destructor doesn't have to do anything special in this regard.
  59. /// This is a key to ensure the strong exception guarantee (see also
  60. /// the description of \c configureAuthServer()).
  61. class AuthConfigParser {
  62. ///
  63. /// \name Constructors and Destructor
  64. ///
  65. /// Note: The copy constructor and the assignment operator are
  66. /// intentionally defined as private to make it explicit that this is a
  67. /// pure base class.
  68. //@{
  69. private:
  70. AuthConfigParser(const AuthConfigParser& source);
  71. AuthConfigParser& operator=(const AuthConfigParser& source);
  72. protected:
  73. /// \brief The default constructor.
  74. ///
  75. /// This is intentionally defined as \c protected as this base class should
  76. /// never be instantiated (except as part of a derived class).
  77. AuthConfigParser() {}
  78. public:
  79. /// The destructor.
  80. virtual ~AuthConfigParser() {}
  81. //@}
  82. /// Prepare configuration value.
  83. ///
  84. /// This method parses the "value part" of the configuration identifier
  85. /// that corresponds to this derived class and prepares a new value to
  86. /// apply to the server.
  87. /// In the above example, the derived class for the identifier "param1"
  88. /// would be passed an data \c Element storing an integer whose value
  89. /// is 10, and would record that value internally;
  90. /// the derived class for the identifier "param2" would be passed a
  91. /// map element and (after parsing) convert it into some internal
  92. /// data structure.
  93. ///
  94. /// This method must validate the given value both in terms of syntax
  95. /// and semantics of the configuration, so that the server will be
  96. /// validly configured at the time of \c commit(). Note: the given
  97. /// configuration value is normally syntactically validated, but the
  98. /// \c build() implementation must also expect invalid input. If it
  99. /// detects an error it may throw an exception of a derived class
  100. /// of \c isc::Exception.
  101. ///
  102. /// Preparing a configuration value will often require resource
  103. /// allocation. If it fails, it may throw a corresponding standard
  104. /// exception.
  105. ///
  106. /// This method is not expected to be called more than once. Although
  107. /// multiple calls are not prohibited by the interface, the behavior
  108. /// is undefined.
  109. ///
  110. /// \param config_value The configuration value for the identifier
  111. /// corresponding to the derived class.
  112. virtual void build(isc::data::ConstElementPtr config_value) = 0;
  113. /// Apply the prepared configuration value to the server.
  114. ///
  115. /// This method is expected to be exception free, and, as a consequence,
  116. /// it should normally not involve resource allocation.
  117. /// Typically it would simply perform exception free assignment or swap
  118. /// operation on the value prepared in \c build().
  119. /// In some cases, however, it may be very difficult to meet this
  120. /// condition in a realistic way, while the failure case should really
  121. /// be very rare. In such a case it may throw, and, if the parser is
  122. /// called via \c configureAuthServer(), the caller will convert the
  123. /// exception as a fatal error.
  124. ///
  125. /// This method is expected to be called after \c build(), and only once.
  126. /// The result is undefined otherwise.
  127. virtual void commit() = 0;
  128. };
  129. /// Configure an \c AuthSrv object with a set of configuration values.
  130. ///
  131. /// This function parses configuration information stored in \c config_set
  132. /// and configures the \c server by applying the configuration to it.
  133. /// It provides the strong exception guarantee as long as the underlying
  134. /// derived class implementations of \c AuthConfigParser meet the assumption,
  135. /// that is, it ensures that either configuration is fully applied or the
  136. /// state of the server is intact.
  137. ///
  138. /// If a syntax or semantics level error happens during the configuration
  139. /// (such as malformed configuration or invalid configuration parameter),
  140. /// this function throws an exception of class \c AuthConfigError.
  141. /// If the given configuration requires resource allocation and it fails,
  142. /// a corresponding standard exception will be thrown.
  143. /// Other exceptions may also be thrown, depending on the implementation of
  144. /// the underlying derived class of \c AuthConfigError.
  145. /// In any case the strong guarantee is provided as described above except
  146. /// in the very rare cases where the \c commit() method of a parser throws
  147. /// an exception. If that happens this function converts the exception
  148. /// into a \c FatalError exception and rethrows it. This exception is
  149. /// expected to be caught at the highest level of the application to terminate
  150. /// the program gracefully.
  151. ///
  152. /// \param server The \c AuthSrv object to be configured.
  153. /// \param config_set A JSON style configuration to apply to \c server.
  154. void configureAuthServer(AuthSrv& server,
  155. isc::data::ConstElementPtr config_set);
  156. /// Create a new \c AuthConfigParser object for a given configuration
  157. /// identifier.
  158. ///
  159. /// It internally identifies an appropriate derived class for the given
  160. /// identifier and creates a new instance of that class. The caller can
  161. /// then configure the \c server regarding the identifier by calling
  162. /// the \c build() and \c commit() methods of the returned object.
  163. ///
  164. /// In practice, this function is only expected to be used as a backend of
  165. /// \c configureAuthServer() and is not supposed to be called directly
  166. /// by applications. It is publicly available mainly for testing purposes.
  167. /// When called directly, the created object must be deleted by the caller.
  168. /// Note: this means if this module and the caller use incompatible sets of
  169. /// new/delete, it may cause unexpected strange failure. We could avoid that
  170. /// by providing a separate deallocation function or by using a smart pointer,
  171. /// but since the expected usage of this function is very limited (i.e. for
  172. /// our own testing purposes) it would be an overkilling. We therefore prefer
  173. /// simplicity and keeping the interface intuitive.
  174. ///
  175. /// If the resource allocation for the new object fails, a corresponding
  176. /// standard exception will be thrown. Otherwise this function is not
  177. /// expected to throw an exception, unless the constructor of the underlying
  178. /// derived class implementation (unexpectedly) throws.
  179. ///
  180. /// \param server The \c AuthSrv object to be configured.
  181. /// \param config_id The configuration identifier for which a parser object
  182. /// is to be created.
  183. /// \return A pointer to an \c AuthConfigParser object.
  184. AuthConfigParser* createAuthConfigParser(AuthSrv& server,
  185. const std::string& config_id);
  186. #endif // __CONFIG_H
  187. // Local Variables:
  188. // mode: c++
  189. // End: