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,2005 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 
00046 using std::string;
00047 using std::cerr;
00048 using std::endl;
00049 
00053 class NoSuchLibrary : public BESInternalFatalError
00054 {
00055 public:
00056     NoSuchLibrary( const string &msg, const string &file, int line )
00057         : BESInternalFatalError( msg, file, line ) {}
00058 };
00059 
00063 class NoSuchObject : public BESInternalFatalError
00064 {
00065 public:
00066     NoSuchObject( const string &msg, const string &file, int line )
00067         : BESInternalFatalError( msg, file, line ) {}
00068 };
00069 
00090 template<typename M>
00091 class BESPlugin : public BESObj
00092 {
00093 private:
00094     string d_filename;          // Library filename
00095     void *d_lib;                // Open library handle
00096 
00099     BESPlugin()  throw(BESInternalError)
00100     {   
00101         throw BESInternalError( "Unimplemented method", __FILE__, __LINE__ );
00102     }
00103 
00108     BESPlugin(const BESPlugin &p) throw(BESInternalError)
00109     {
00110         throw BESInternalError( "Unimplemented method.", __FILE__, __LINE__ );
00111     }
00112 
00116     BESPlugin &operator=(const BESPlugin &p) throw(BESInternalError)
00117     {
00118         throw BESInternalError( "Unimplemented method.", __FILE__, __LINE__ );
00119     }
00120 
00121     void *get_lib() throw(NoSuchLibrary) {
00122         if (!d_lib) {
00123             d_lib = dlopen(d_filename.c_str(), RTLD_NOW|RTLD_LOCAL);
00124             if (d_lib == NULL) {
00125                 throw NoSuchLibrary( string( dlerror() ), __FILE__, __LINE__ ) ;
00126             }
00127         }
00128 
00129         return d_lib;
00130     }
00131 
00132 public:
00137     BESPlugin(const string &filename) : d_filename(filename), d_lib(0) {}
00138 
00141     virtual ~BESPlugin() {
00142         if (d_lib)
00143             dlclose(d_lib);
00144     }
00145 
00152     M* instantiate() throw(NoSuchLibrary, NoSuchObject) {
00153         void *maker = dlsym(get_lib(), "maker");
00154         if (!maker) {
00155             throw NoSuchObject( string( dlerror() ), __FILE__, __LINE__ ) ;
00156         }
00157     
00158         typedef M *(*maker_func_ptr)();
00159         maker_func_ptr my_maker = *reinterpret_cast<maker_func_ptr*>(&maker);
00160         M *my_M = (my_maker)();
00161 
00162         return my_M;
00163     }
00164 
00165     virtual void dump( ostream &strm ) const
00166     {
00167         strm << "BESPlugin::dump - (" << (void *)this << ")" << endl ;
00168         strm << "    plugin name: " << d_filename << endl ;
00169         strm << "    library handle: " << (void *)d_lib << endl ;
00170     }
00171 };
00172 
00173 #endif // T_BESPlugin_h
00174 

Generated on Fri Apr 11 15:13:27 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.4.7