hooks_manager.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (C) 2013 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 <hooks/callout_handle.h>
  15. #include <hooks/callout_manager.h>
  16. #include <hooks/callout_manager.h>
  17. #include <hooks/library_handle.h>
  18. #include <hooks/library_manager_collection.h>
  19. #include <hooks/hooks_manager.h>
  20. #include <hooks/server_hooks.h>
  21. #include <boost/shared_ptr.hpp>
  22. #include <string>
  23. #include <vector>
  24. using namespace std;
  25. namespace isc {
  26. namespace hooks {
  27. // Constructor
  28. HooksManager::HooksManager() {
  29. }
  30. // Return reference to singleton hooks manager.
  31. HooksManager&
  32. HooksManager::getHooksManager() {
  33. static HooksManager manager;
  34. return (manager);
  35. }
  36. // Are callouts present?
  37. bool
  38. HooksManager::calloutsPresentInternal(int index) {
  39. conditionallyInitialize();
  40. return (callout_manager_->calloutsPresent(index));
  41. }
  42. bool
  43. HooksManager::calloutsPresent(int index) {
  44. return (getHooksManager().calloutsPresentInternal(index));
  45. }
  46. // Call the callouts
  47. void
  48. HooksManager::callCalloutsInternal(int index, CalloutHandle& handle) {
  49. conditionallyInitialize();
  50. return (callout_manager_->callCallouts(index, handle));
  51. }
  52. void
  53. HooksManager::callCallouts(int index, CalloutHandle& handle) {
  54. return (getHooksManager().callCalloutsInternal(index, handle));
  55. }
  56. // Load the libraries. This will delete the previously-loaded libraries
  57. // (if present) and load new ones.
  58. bool
  59. HooksManager::loadLibrariesInternal(const std::vector<std::string>& libraries) {
  60. // Unload current set of libraries (if any are loaded).
  61. unloadLibrariesInternal();
  62. // Create the library manager and load the libraries.
  63. lm_collection_.reset(new LibraryManagerCollection(libraries));
  64. bool status = lm_collection_->loadLibraries();
  65. if (status) {
  66. // ... and obtain the callout manager for them if successful.
  67. callout_manager_ = lm_collection_->getCalloutManager();
  68. } else {
  69. // Unable to load libraries, reset to state before this function was
  70. // called.
  71. lm_collection_.reset();
  72. callout_manager_.reset();
  73. }
  74. return (status);
  75. }
  76. bool
  77. HooksManager::loadLibraries(const std::vector<std::string>& libraries) {
  78. return (getHooksManager().loadLibrariesInternal(libraries));
  79. }
  80. // Unload the libraries. This just deletes all internal objects which will
  81. // cause the libraries to be unloaded.
  82. void
  83. HooksManager::unloadLibrariesInternal() {
  84. // The order of deletion does not matter here, as each library manager
  85. // holds its own pointer to the callout manager. However, we may as
  86. // well delete the library managers first: if there are no other references
  87. // to the callout manager, the second statement will delete it, which may
  88. // ease debugging.
  89. lm_collection_.reset();
  90. callout_manager_.reset();
  91. }
  92. void HooksManager::unloadLibraries() {
  93. getHooksManager().unloadLibrariesInternal();
  94. }
  95. // Create a callout handle
  96. boost::shared_ptr<CalloutHandle>
  97. HooksManager::createCalloutHandleInternal() {
  98. conditionallyInitialize();
  99. return (boost::shared_ptr<CalloutHandle>(
  100. new CalloutHandle(callout_manager_, lm_collection_)));
  101. }
  102. boost::shared_ptr<CalloutHandle>
  103. HooksManager::createCalloutHandle() {
  104. return (getHooksManager().createCalloutHandleInternal());
  105. }
  106. // Get the list of the names of loaded libraries.
  107. std::vector<std::string>
  108. HooksManager::getLibraryNamesInternal() const {
  109. return (lm_collection_ ? lm_collection_->getLibraryNames()
  110. : std::vector<std::string>());
  111. }
  112. std::vector<std::string>
  113. HooksManager::getLibraryNames() {
  114. return (getHooksManager().getLibraryNamesInternal());
  115. }
  116. // Perform conditional initialization if nothing is loaded.
  117. void
  118. HooksManager::performConditionalInitialization() {
  119. // Nothing present, so create the collection with any empty set of
  120. // libraries, and get the CalloutManager.
  121. vector<string> libraries;
  122. lm_collection_.reset(new LibraryManagerCollection(libraries));
  123. lm_collection_->loadLibraries();
  124. callout_manager_ = lm_collection_->getCalloutManager();
  125. }
  126. // Shell around ServerHooks::registerHook()
  127. int
  128. HooksManager::registerHook(const std::string& name) {
  129. return (ServerHooks::getServerHooks().registerHook(name));
  130. }
  131. // Return pre- and post- library handles.
  132. isc::hooks::LibraryHandle&
  133. HooksManager::preCalloutsLibraryHandleInternal() {
  134. conditionallyInitialize();
  135. return (callout_manager_->getPreLibraryHandle());
  136. }
  137. isc::hooks::LibraryHandle&
  138. HooksManager::preCalloutsLibraryHandle() {
  139. return (getHooksManager().preCalloutsLibraryHandleInternal());
  140. }
  141. isc::hooks::LibraryHandle&
  142. HooksManager::postCalloutsLibraryHandleInternal() {
  143. conditionallyInitialize();
  144. return (callout_manager_->getPostLibraryHandle());
  145. }
  146. isc::hooks::LibraryHandle&
  147. HooksManager::postCalloutsLibraryHandle() {
  148. return (getHooksManager().postCalloutsLibraryHandleInternal());
  149. }
  150. // Validate libraries
  151. std::string
  152. HooksManager::validateLibraries(const std::vector<std::string>& libraries) {
  153. return (LibraryManagerCollection::validateLibraries(libraries));
  154. }
  155. } // namespace util
  156. } // namespace isc