user_registry.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <user_registry.h>
  15. #include <user.h>
  16. UserRegistry::UserRegistry() {
  17. }
  18. UserRegistry::~UserRegistry(){
  19. }
  20. void
  21. UserRegistry::addUser(UserPtr& user) {
  22. if (!user) {
  23. isc_throw (UserRegistryError, "UserRegistry cannot add blank user");
  24. }
  25. UserPtr found_user;
  26. if ((found_user = findUser(user->getUserId()))) {
  27. isc_throw (UserRegistryError, "UserRegistry duplicate user: "
  28. << user->getUserId());
  29. }
  30. users_[user->getUserId()] = user;
  31. }
  32. const UserPtr&
  33. UserRegistry::findUser(const UserId& id) const {
  34. static UserPtr empty;
  35. UserMap::const_iterator it = users_.find(id);
  36. if (it != users_.end()) {
  37. const UserPtr tmp = (*it).second;
  38. return ((*it).second);
  39. }
  40. return empty;
  41. }
  42. void
  43. UserRegistry::removeUser(const UserId& id) {
  44. static UserPtr empty;
  45. UserMap::iterator it = users_.find(id);
  46. if (it != users_.end()) {
  47. users_.erase(it);
  48. }
  49. }
  50. const UserPtr&
  51. UserRegistry::findUser(const isc::dhcp::HWAddr& hwaddr) const {
  52. UserId id(UserId::HW_ADDRESS, hwaddr.hwaddr_);
  53. return (findUser(id));
  54. }
  55. const UserPtr&
  56. UserRegistry::findUser(const isc::dhcp::DUID& duid) const {
  57. UserId id(UserId::DUID, duid.getDuid());
  58. return (findUser(id));
  59. }
  60. void UserRegistry::refresh() {
  61. if (!source_) {
  62. isc_throw(UserRegistryError,
  63. "UserRegistry: cannot refresh, no data source");
  64. }
  65. // If the source isn't open, open it.
  66. if (!source_->isOpen()) {
  67. source_->open();
  68. }
  69. // Make a copy in case something goes wrong midstream.
  70. UserMap backup(users_);
  71. // Empty the registry then read users from source until source is empty.
  72. clearall();
  73. try {
  74. UserPtr user;
  75. while ((user = source_->readNextUser())) {
  76. addUser(user);
  77. }
  78. } catch (const std::exception& ex) {
  79. // Source was compromsised so restore registry from backup.
  80. users_ = backup;
  81. // Close the source.
  82. source_->close();
  83. isc_throw (UserRegistryError, "UserRegistry: refresh failed during read"
  84. << ex.what());
  85. }
  86. // Close the source.
  87. source_->close();
  88. }
  89. void UserRegistry::clearall() {
  90. users_.clear();
  91. }
  92. void UserRegistry::setSource(UserDataSourcePtr& source) {
  93. if (!source) {
  94. isc_throw (UserRegistryError,
  95. "UserRegistry: data source cannot be set to null");
  96. }
  97. source_ = source;
  98. }
  99. const UserDataSourcePtr& UserRegistry::getSource() {
  100. return (source_);
  101. }