00001 #include "OBTPlugin.h"
00002 #include "OBTPluginInformation.h"
00003 #include "OBT_ASSERT.h"
00004
00005 #include <sys/stat.h>
00006 #ifndef _MSC_VER
00007 #include "dlfcn.h"
00008 #endif
00009
00010 using namespace OBT ;
00011
00012
00013
00014
00015 Plugin::Plugin( const char* path, const char* name, const char* configurationFile )
00016 :
00017 _pluginHandle( 0 ),
00018 _initialised( false )
00019 {
00020 OBT_ASSERT( path != NULL ) ;
00021 _path = path ;
00022 if( name )
00023 {
00024 #ifdef _MSC_VER
00025 # ifndef NDEBUG
00026
00027 _path += "/" + std::string( name ) + "_d.dll" ;
00028 struct stat buffer ;
00029 if( stat( _path.c_str(), &buffer ) )
00030 {
00031 _path = std::string( path ) + "/debug/" + std::string( name ) + "_d.dll" ;
00032 }
00033 # else
00034
00035 _path += "/" + std::string( name ) + ".dll" ;
00036 struct stat buffer ;
00037 if( stat( _path.c_str(), &buffer ) )
00038 {
00039 _path = std::string( path ) + "/release/" + std::string( name ) + ".dll" ;
00040 }
00041 # endif
00042 #else
00043
00044 _path += ( ( "" == _path ) ? "lib" : "/lib" ) + std::string( name ) + ".so" ;
00045 #endif
00046 }
00047
00048 if ( configurationFile != NULL )
00049 {
00050 _configurationFile = configurationFile ;
00051 }
00052 else
00053 {
00054 _configurationFile = "" ;
00055 }
00056 }
00057
00058
00059
00060
00061 Plugin::~Plugin()
00062 {
00063 unload( ) ;
00064 }
00065
00066
00067
00068
00069 bool
00070 Plugin::load()
00071 {
00072 if ( _pluginHandle == NULL )
00073 {
00074
00075 #ifdef _MSC_VER
00076 _pluginHandle = LoadLibrary( _path.c_str() ) ;
00077 #else
00078 _pluginHandle = dlopen( _path.c_str(), RTLD_LAZY | RTLD_GLOBAL ) ;
00079 #endif
00080 if ( _pluginHandle == NULL )
00081 {
00082 TRACE_ERROR( "Unable to load plugin: " << _path.c_str() ) ;
00083 #ifndef _MSC_VER
00084 char *s = dlerror() ;
00085 if (s)
00086 {
00087 TRACE_ERROR( "Error message from plugin loading: " << s ) ;
00088 free( s ) ;
00089 }
00090 #endif
00091 _initialised = false ;
00092 }
00093
00094 else
00095 {
00096 _initialised = true ;
00097 void* functionPointer( NULL ) ;
00098 #ifdef _MSC_VER
00099 functionPointer = GetProcAddress( _pluginHandle, "initPlugin" ) ;
00100 #else
00101 functionPointer = dlsym( _pluginHandle, "initPlugin" ) ;
00102 #endif
00103 InitFunc initFunc( reinterpret_cast<InitFunc>( functionPointer ) ) ;
00104 if( initFunc == NULL )
00105 {
00106 TRACE_ERROR( "Unable to call the initPlugin function of the plugin: " << _path.c_str() ) ;
00107 _initialised = false ;
00108 }
00109 else
00110 {
00111 _initialised = initFunc( getConfigurationFile() ) ;
00112 }
00113 }
00114 }
00115
00116 return _initialised ;
00117 }
00118
00119
00120
00121
00122 bool
00123 Plugin::unload()
00124 {
00125 bool returnCode( true ) ;
00126 if( _pluginHandle != NULL )
00127 {
00128
00129 void* functionPointer( NULL ) ;
00130 #ifdef _MSC_VER
00131 functionPointer = GetProcAddress( _pluginHandle, "finishPlugin" ) ;
00132 #else
00133 functionPointer = dlsym( _pluginHandle, "finishPlugin" ) ;
00134 #endif
00135 DeInitFunc deInitFunc( reinterpret_cast<DeInitFunc>( functionPointer ) ) ;
00136 if( deInitFunc == NULL )
00137 {
00138 TRACE_ERROR( "Unable to call the finishPlugin function of the plugin: " << _path.c_str() ) ;
00139 returnCode = false ;
00140 }
00141 else
00142 {
00143 returnCode = deInitFunc() ;
00144 }
00145
00146
00147 #ifdef _MSC_VER
00148 FreeLibrary( _pluginHandle ) ;
00149 #else
00150 dlclose( _pluginHandle ) ;
00151 #endif
00152 _pluginHandle = NULL ;
00153 _path = "" ;
00154 }
00155 _initialised = false ;
00156
00157 return returnCode ;
00158 }
00159
00160
00161
00162
00163 const PluginInformation&
00164 Plugin::getInformation() const
00165 {
00166 if( _pluginHandle != NULL )
00167 {
00168 void* functionPointer( NULL ) ;
00169 #ifdef _MSC_VER
00170 functionPointer = GetProcAddress( _pluginHandle, "getPluginInformation" ) ;
00171 #else
00172 functionPointer = dlsym( _pluginHandle, "getPluginInformation" ) ;
00173 #endif
00174 PluginInformationFunc pluginInformationFunc( reinterpret_cast<PluginInformationFunc>( functionPointer ) ) ;
00175 if( pluginInformationFunc != NULL )
00176 {
00177 return pluginInformationFunc() ;
00178 }
00179 else
00180 {
00181 TRACE_ERROR( "Unable to call the getPluginInformation function of the plugin: " << _path.c_str() ) ;
00182 }
00183 }
00184
00185 return PluginInformation::Unknown ;
00186 }
00187