OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESPlugin.h
Go to the documentation of this file.
1 // BESPlugin.h
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 // and James Gallagher <jgallagher@gso.uri.edu>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //
24 // You can contact University Corporation for Atmospheric Research at
25 // 3080 Center Green Drive, Boulder, CO 80301
26 
27 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
28 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
29 //
30 // Authors:
31 // pwest Patrick West <pwest@ucar.edu>
32 // jgarcia Jose Garcia <jgarcia@ucar.edu>
33 // jimg James Gallagher <jgallagher@gso.uri.edu>
34 
35 #ifndef T_BESPlugin_h
36 #define T_BESPlugin_h
37 
38 #include <dlfcn.h>
39 #include <string>
40 #include <iostream>
41 
42 #include "BESObj.h"
43 #include "BESInternalFatalError.h"
44 #include "BESInternalError.h"
45 #include "BESDebug.h"
46 
47 #undef UNPLUG_HANDLERS
48 
49 using std::string;
50 using std::cerr;
51 using std::endl;
52 
57 {
58 public:
59  NoSuchLibrary( const string &msg, const string &file, int line )
60  : BESInternalFatalError( msg, file, line ) {}
61 };
62 
67 {
68 public:
69  NoSuchObject( const string &msg, const string &file, int line )
70  : BESInternalFatalError( msg, file, line ) {}
71 };
72 
93 template<typename M>
94 class BESPlugin : public BESObj
95 {
96 private:
97  string d_filename; // Library filename
98  void *d_lib; // Open library handle
99 
102  BESPlugin() throw(BESInternalError)
103  {
104  throw BESInternalError( "Unimplemented method", __FILE__, __LINE__ );
105  }
106 
111  BESPlugin(const BESPlugin &p) throw(BESInternalError)
112  {
113  throw BESInternalError( "Unimplemented method.", __FILE__, __LINE__ );
114  }
115 
119  BESPlugin &operator=(const BESPlugin &p) throw(BESInternalError)
120  {
121  throw BESInternalError( "Unimplemented method.", __FILE__, __LINE__ );
122  }
123 
124  void *get_lib() throw(NoSuchLibrary) {
125  if (!d_lib) {
126  d_lib = dlopen(d_filename.c_str(), RTLD_NOW|RTLD_LOCAL);
127  BESDEBUG( "bes", "BESPlugin: plug in handler:" << d_filename << ", " << d_lib << endl);
128  if (d_lib == NULL) {
129  throw NoSuchLibrary( string( dlerror() ), __FILE__, __LINE__ ) ;
130  }
131  }
132 
133  return d_lib;
134  }
135 
136 public:
141  BESPlugin(const string &filename) : d_filename(filename), d_lib(0) {}
142 
145  virtual ~BESPlugin() {
146  BESDEBUG( "bes", "BESPlugin: unplugging handler:" << d_filename << ", " << d_lib << endl);
147 #ifdef UNPLUG_HANDLERS
148  if (d_lib) {
149  dlclose(d_lib);
150  d_lib = 0; // Added; paranoia. jhrg
151  }
152 #endif
153  }
154 
162  void *maker = dlsym(get_lib(), "maker");
163  if (!maker) {
164  throw NoSuchObject( string( dlerror() ), __FILE__, __LINE__ ) ;
165  }
166 
167  typedef M *(*maker_func_ptr)();
168  maker_func_ptr my_maker = *reinterpret_cast<maker_func_ptr*>(&maker);
169  M *my_M = (my_maker)();
170 
171  return my_M;
172  }
173 
174  virtual void dump( ostream &strm ) const
175  {
176  strm << "BESPlugin::dump - (" << (void *)this << ")" << endl ;
177  strm << " plugin name: " << d_filename << endl ;
178  strm << " library handle: " << (void *)d_lib << endl ;
179  }
180 };
181 
182 #endif // T_BESPlugin_h
183