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 
00031 
00032 
00033 #include "mmstools/mmsshlhandler.h"
00034 #include "mmstools/mmsfile.h"
00035 #ifdef __HAVE_DL__
00036 #include <dlfcn.h>
00037 #endif
00038 
00039 MMSShlHandler::~MMSShlHandler() {
00040 #ifndef __HAVE_DL__
00041     throw MMSShlError(0, "Compile with use_dl (libdl support)");
00042 #else
00043 
00044     if(this->isloaded) {
00045         dlclose(this->handle);
00046     }
00047 #endif
00048 }
00049 
00050 void MMSShlHandler::close() {
00051 #ifndef __HAVE_DL__
00052     throw MMSShlError(0, "Compile with use_dl (libdl support)");
00053 #else
00054 
00055     if(this->isloaded) {
00056         dlclose(this->handle);
00057         this->isloaded = false;
00058     } else {
00059         throw MMSShlError(0,"shared library " + this->name + " is not loaded");
00060     }
00061 #endif
00062 }
00063 
00064 void MMSShlHandler::open() {
00065 #ifndef __HAVE_DL__
00066     throw MMSShlError(0, "Compile with use_dl (libdl support)");
00067 #else
00068 
00069     if(this->isloaded) {
00070         throw MMSShlError(0,"shared library " + this->name + " is alread loaded");
00071     }
00072     else {
00073         this->handle = dlopen(this->name.c_str(),RTLD_NOW);
00074 
00075         if(this->handle == NULL)
00076             throw MMSShlError(0,"shared library " + this->name + " cannot be loaded: " + dlerror());
00077 
00078         this->isloaded = true;
00079     }
00080 #endif
00081 }
00082 
00083 void MMSShlHandler::setName(string name) {
00084 #ifndef __HAVE_DL__
00085     throw MMSShlError(0, "Compile with use_dl (libdl support)");
00086 #else
00087 
00088     this->name = name;
00089 #endif
00090 }
00091 
00092 string MMSShlHandler::getName() {
00093 #ifndef __HAVE_DL__
00094     throw MMSShlError(0, "Compile with use_dl (libdl support)");
00095 #else
00096 
00097     return this->name;
00098 #endif
00099 }
00100 
00101 void *MMSShlHandler::getFunction(string name) {
00102 #ifndef __HAVE_DL__
00103     throw MMSShlError(0, "Compile with use_dl (libdl support)");
00104 #else
00105 
00106     void *ret = NULL;
00107 
00108     if(this->isloaded) {
00109         ret = dlsym(this->handle,name.c_str());
00110         if(ret == NULL)
00111             throw MMSShlError(0,"symbol " + name + " cannot be retrieved: " + dlerror());
00112     } else {
00113         throw MMSShlError(0,"shared library " + this->name + " is not loaded");
00114     }
00115 
00116     return ret;
00117 #endif
00118 }
00119 
00120 bool MMSShlHandler::isLoaded() {
00121 #ifndef __HAVE_DL__
00122     throw MMSShlError(0, "Compile with use_dl (libdl support)");
00123 #else
00124     return this->isloaded;
00125 #endif
00126 
00127 }