Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "class_loader/class_loader.hpp"
00031
00032 #include <string>
00033
00034 namespace class_loader
00035 {
00036
00037 bool ClassLoader::has_unmananged_instance_been_created_ = false;
00038
00039 bool ClassLoader::hasUnmanagedInstanceBeenCreated()
00040 {
00041 return ClassLoader::has_unmananged_instance_been_created_;
00042 }
00043
00044 std::string systemLibrarySuffix()
00045 {
00046 return Poco::SharedLibrary::suffix();
00047 }
00048
00049 ClassLoader::ClassLoader(const std::string & library_path, bool ondemand_load_unload)
00050 : ondemand_load_unload_(ondemand_load_unload),
00051 library_path_(library_path),
00052 load_ref_count_(0),
00053 plugin_ref_count_(0)
00054 {
00055 CONSOLE_BRIDGE_logDebug(
00056 "class_loader.ClassLoader: "
00057 "Constructing new ClassLoader (%p) bound to library %s.",
00058 this, library_path.c_str());
00059 if (!isOnDemandLoadUnloadEnabled()) {
00060 loadLibrary();
00061 }
00062 }
00063
00064 ClassLoader::~ClassLoader()
00065 {
00066 CONSOLE_BRIDGE_logDebug("%s",
00067 "class_loader.ClassLoader: "
00068 "Destroying class loader, unloading associated library...\n");
00069 unloadLibrary();
00070 }
00071
00072 bool ClassLoader::isLibraryLoaded()
00073 {
00074 return class_loader::class_loader_private::isLibraryLoaded(getLibraryPath(), this);
00075 }
00076
00077 bool ClassLoader::isLibraryLoadedByAnyClassloader()
00078 {
00079 return class_loader::class_loader_private::isLibraryLoadedByAnybody(getLibraryPath());
00080 }
00081
00082 void ClassLoader::loadLibrary()
00083 {
00084 boost::recursive_mutex::scoped_lock lock(load_ref_count_mutex_);
00085 load_ref_count_ = load_ref_count_ + 1;
00086 class_loader::class_loader_private::loadLibrary(getLibraryPath(), this);
00087 }
00088
00089 int ClassLoader::unloadLibrary()
00090 {
00091 return unloadLibraryInternal(true);
00092 }
00093
00094 int ClassLoader::unloadLibraryInternal(bool lock_plugin_ref_count)
00095 {
00096 boost::recursive_mutex::scoped_lock load_ref_lock(load_ref_count_mutex_);
00097 boost::recursive_mutex::scoped_lock plugin_ref_lock;
00098 if (lock_plugin_ref_count) {
00099 plugin_ref_lock = boost::recursive_mutex::scoped_lock(plugin_ref_count_mutex_);
00100 }
00101
00102 if (plugin_ref_count_ > 0) {
00103 CONSOLE_BRIDGE_logWarn("%s",
00104 "class_loader.ClassLoader: "
00105 "SEVERE WARNING!!! Attempting to unload library while objects created by this loader "
00106 "exist in the heap! "
00107 "You should delete your objects before attempting to unload the library or "
00108 "destroying the ClassLoader. The library will NOT be unloaded.");
00109 } else {
00110 load_ref_count_ = load_ref_count_ - 1;
00111 if (0 == load_ref_count_) {
00112 class_loader::class_loader_private::unloadLibrary(getLibraryPath(), this);
00113 } else if (load_ref_count_ < 0) {
00114 load_ref_count_ = 0;
00115 }
00116 }
00117 return load_ref_count_;
00118 }
00119
00120 }