factory.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2011 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 "factory.h"
  15. #include "data_source.h"
  16. #include "database.h"
  17. #include "sqlite3_accessor.h"
  18. #include "memory_datasrc.h"
  19. #include <dlfcn.h>
  20. using namespace isc::data;
  21. using namespace isc::datasrc;
  22. namespace isc {
  23. namespace datasrc {
  24. LibraryContainer::LibraryContainer(const std::string& name) {
  25. ds_lib_ = dlopen(name.c_str(), RTLD_NOW | RTLD_LOCAL);
  26. if (ds_lib_ == NULL) {
  27. isc_throw(DataSourceLibraryError, dlerror());
  28. }
  29. }
  30. LibraryContainer::~LibraryContainer() {
  31. dlclose(ds_lib_);
  32. }
  33. void*
  34. LibraryContainer::getSym(const char* name) {
  35. // Since dlsym can return NULL on success, we check for errors by
  36. // first clearing any existing errors with dlerror(), then calling dlsym,
  37. // and finally checking for errors with dlerror()
  38. dlerror();
  39. void *sym = dlsym(ds_lib_, name);
  40. const char* dlsym_error = dlerror();
  41. if (dlsym_error != NULL) {
  42. isc_throw(DataSourceLibraryError, dlsym_error);
  43. }
  44. return (sym);
  45. }
  46. DataSourceClientContainer::DataSourceClientContainer(const std::string& type,
  47. ConstElementPtr config)
  48. : ds_lib_(type + "_ds.so")
  49. {
  50. ds_creator* ds_create = (ds_creator*)ds_lib_.getSym("createInstance");
  51. destructor_ = (ds_destructor*)ds_lib_.getSym("destroyInstance");
  52. instance_ = ds_create(config);
  53. }
  54. DataSourceClientContainer::~DataSourceClientContainer() {
  55. destructor_(instance_);
  56. }
  57. } // end namespace datasrc
  58. } // end namespace isc