hooks_manager.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // ... and obtain the callout manager for them.
  66. callout_manager_ = lm_collection_->getCalloutManager();
  67. return (status);
  68. }
  69. bool
  70. HooksManager::loadLibraries(const std::vector<std::string>& libraries) {
  71. return (getHooksManager().loadLibrariesInternal(libraries));
  72. }
  73. // Unload the libraries. This just deletes all internal objects which will
  74. // cause the libraries to be unloaded.
  75. void
  76. HooksManager::unloadLibrariesInternal() {
  77. // The order of deletion does not matter here, as each library manager
  78. // holds its own pointer to the callout manager. However, we may as
  79. // well delete the library managers first: if there are no other references
  80. // to the callout manager, the second statement will delete it, which may
  81. // ease debugging.
  82. lm_collection_.reset();
  83. callout_manager_.reset();
  84. }
  85. void HooksManager::unloadLibraries() {
  86. getHooksManager().unloadLibrariesInternal();
  87. }
  88. // Create a callout handle
  89. boost::shared_ptr<CalloutHandle>
  90. HooksManager::createCalloutHandleInternal() {
  91. conditionallyInitialize();
  92. return (boost::shared_ptr<CalloutHandle>(
  93. new CalloutHandle(callout_manager_, lm_collection_)));
  94. }
  95. boost::shared_ptr<CalloutHandle>
  96. HooksManager::createCalloutHandle() {
  97. return (getHooksManager().createCalloutHandleInternal());
  98. }
  99. // Perform conditional initialization if nothing is loaded.
  100. void
  101. HooksManager::performConditionalInitialization() {
  102. // Nothing present, so create the collection with any empty set of
  103. // libraries, and get the CalloutManager.
  104. vector<string> libraries;
  105. lm_collection_.reset(new LibraryManagerCollection(libraries));
  106. lm_collection_->loadLibraries();
  107. callout_manager_ = lm_collection_->getCalloutManager();
  108. }
  109. // Shell around ServerHooks::registerHook()
  110. int
  111. HooksManager::registerHook(const std::string& name) {
  112. return (ServerHooks::getServerHooks().registerHook(name));
  113. }
  114. // Return pre- and post- library handles.
  115. isc::hooks::LibraryHandle&
  116. HooksManager::preCalloutsLibraryHandleInternal() {
  117. conditionallyInitialize();
  118. return (callout_manager_->getPreLibraryHandle());
  119. }
  120. isc::hooks::LibraryHandle&
  121. HooksManager::preCalloutsLibraryHandle() {
  122. return (getHooksManager().preCalloutsLibraryHandleInternal());
  123. }
  124. isc::hooks::LibraryHandle&
  125. HooksManager::postCalloutsLibraryHandleInternal() {
  126. conditionallyInitialize();
  127. return (callout_manager_->getPostLibraryHandle());
  128. }
  129. isc::hooks::LibraryHandle&
  130. HooksManager::postCalloutsLibraryHandle() {
  131. return (getHooksManager().postCalloutsLibraryHandleInternal());
  132. }
  133. } // namespace util
  134. } // namespace isc