OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESPlugin.h
Go to the documentation of this file.
00001 // BESPlugin.h
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 // and James Gallagher <jgallagher@gso.uri.edu>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 // 
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 // 
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact University Corporation for Atmospheric Research at
00025 // 3080 Center Green Drive, Boulder, CO 80301
00026  
00027 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00028 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00029 //
00030 // Authors:
00031 //      pwest       Patrick West <pwest@ucar.edu>
00032 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00033 //      jimg        James Gallagher <jgallagher@gso.uri.edu>
00034 
00035 #ifndef T_BESPlugin_h
00036 #define T_BESPlugin_h
00037 
00038 #include <dlfcn.h>
00039 #include <string>
00040 #include <iostream>
00041 
00042 #include "BESObj.h"
00043 #include "BESInternalFatalError.h"
00044 #include "BESInternalError.h"
00045 #include "BESDebug.h"
00046 
00047 #undef UNPLUG_HANDLERS
00048 
00049 using std::string;
00050 using std::cerr;
00051 using std::endl;
00052 
00056 class NoSuchLibrary : public BESInternalFatalError
00057 {
00058 public:
00059     NoSuchLibrary( const string &msg, const string &file, int line )
00060         : BESInternalFatalError( msg, file, line ) {}
00061 };
00062 
00066 class NoSuchObject : public BESInternalFatalError
00067 {
00068 public:
00069     NoSuchObject( const string &msg, const string &file, int line )
00070         : BESInternalFatalError( msg, file, line ) {}
00071 };
00072 
00093 template<typename M>
00094 class BESPlugin : public BESObj
00095 {
00096 private:
00097     string d_filename;          // Library filename
00098     void *d_lib;                // Open library handle
00099 
00102     BESPlugin()  throw(BESInternalError)
00103     {   
00104         throw BESInternalError( "Unimplemented method", __FILE__, __LINE__ );
00105     }
00106 
00111     BESPlugin(const BESPlugin &p) throw(BESInternalError)
00112     {
00113         throw BESInternalError( "Unimplemented method.", __FILE__, __LINE__ );
00114     }
00115 
00119     BESPlugin &operator=(const BESPlugin &p) throw(BESInternalError)
00120     {
00121         throw BESInternalError( "Unimplemented method.", __FILE__, __LINE__ );
00122     }
00123 
00124     void *get_lib() throw(NoSuchLibrary) {
00125         if (!d_lib) {
00126             d_lib = dlopen(d_filename.c_str(), RTLD_NOW|RTLD_LOCAL);
00127             BESDEBUG( "bes", "BESPlugin: plug in handler:" << d_filename << ", " << d_lib << endl);
00128             if (d_lib == NULL) {
00129                 throw NoSuchLibrary( string( dlerror() ), __FILE__, __LINE__ ) ;
00130             }
00131         }
00132 
00133         return d_lib;
00134     }
00135 
00136 public:
00141     BESPlugin(const string &filename) : d_filename(filename), d_lib(0) {}
00142 
00145     virtual ~BESPlugin() {
00146         BESDEBUG( "bes", "BESPlugin: unplugging handler:" << d_filename << ", " << d_lib << endl);
00147 #ifdef UNPLUG_HANDLERS
00148         if (d_lib) {
00149             dlclose(d_lib);
00150             d_lib = 0;  // Added; paranoia. jhrg
00151         }
00152 #endif
00153     }
00154 
00161     M* instantiate() throw(NoSuchLibrary, NoSuchObject) {
00162         void *maker = dlsym(get_lib(), "maker");
00163         if (!maker) {
00164             throw NoSuchObject( string( dlerror() ), __FILE__, __LINE__ ) ;
00165         }
00166     
00167         typedef M *(*maker_func_ptr)();
00168         maker_func_ptr my_maker = *reinterpret_cast<maker_func_ptr*>(&maker);
00169         M *my_M = (my_maker)();
00170 
00171         return my_M;
00172     }
00173 
00174     virtual void dump( ostream &strm ) const
00175     {
00176         strm << "BESPlugin::dump - (" << (void *)this << ")" << endl ;
00177         strm << "    plugin name: " << d_filename << endl ;
00178         strm << "    library handle: " << (void *)d_lib << endl ;
00179     }
00180 };
00181 
00182 #endif // T_BESPlugin_h
00183