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 
46 using std::string;
47 using std::cerr;
48 using std::endl;
49 
54 {
55 public:
56  NoSuchLibrary( const string &msg, const string &file, int line )
57  : BESInternalFatalError( msg, file, line ) {}
58 };
59 
64 {
65 public:
66  NoSuchObject( const string &msg, const string &file, int line )
67  : BESInternalFatalError( msg, file, line ) {}
68 };
69 
90 template<typename M>
91 class BESPlugin : public BESObj
92 {
93 private:
94  string d_filename; // Library filename
95  void *d_lib; // Open library handle
96 
100  {
101  throw BESInternalError( "Unimplemented method", __FILE__, __LINE__ );
102  }
103 
108  BESPlugin(const BESPlugin &p) throw(BESInternalError)
109  {
110  throw BESInternalError( "Unimplemented method.", __FILE__, __LINE__ );
111  }
112 
116  BESPlugin &operator=(const BESPlugin &p) throw(BESInternalError)
117  {
118  throw BESInternalError( "Unimplemented method.", __FILE__, __LINE__ );
119  }
120 
121  void *get_lib() throw(NoSuchLibrary) {
122  if (!d_lib) {
123  d_lib = dlopen(d_filename.c_str(), RTLD_NOW|RTLD_LOCAL);
124  if (d_lib == NULL) {
125  throw NoSuchLibrary( string( dlerror() ), __FILE__, __LINE__ ) ;
126  }
127  }
128 
129  return d_lib;
130  }
131 
132 public:
137  BESPlugin(const string &filename) : d_filename(filename), d_lib(0) {}
138 
141  virtual ~BESPlugin() {
142  if (d_lib)
143  dlclose(d_lib);
144  }
145 
153  void *maker = dlsym(get_lib(), "maker");
154  if (!maker) {
155  throw NoSuchObject( string( dlerror() ), __FILE__, __LINE__ ) ;
156  }
157 
158  typedef M *(*maker_func_ptr)();
159  maker_func_ptr my_maker = *reinterpret_cast<maker_func_ptr*>(&maker);
160  M *my_M = (my_maker)();
161 
162  return my_M;
163  }
164 
165  virtual void dump( ostream &strm ) const
166  {
167  strm << "BESPlugin::dump - (" << (void *)this << ")" << endl ;
168  strm << " plugin name: " << d_filename << endl ;
169  strm << " library handle: " << (void *)d_lib << endl ;
170  }
171 };
172 
173 #endif // T_BESPlugin_h
174 
exception thrown if an internal error is found and is fatal to the BES
exception thrown if inernal error encountered
Thrown as an exception when BESPlugin cannot find the named shareable library.
Definition: BESPlugin.h:53
Thrown as an exception when BESPlugin cannot find or run the maker() function in a shared library alr...
Definition: BESPlugin.h:63
BESAbstractModule * maker()
NoSuchObject(const string &msg, const string &file, int line)
Definition: BESPlugin.h:66
M * instantiate()
Instantiate the object.
Definition: BESPlugin.h:152
Base object for bes objects.
Definition: BESObj.h:52
NoSuchLibrary(const string &msg, const string &file, int line)
Definition: BESPlugin.h:56
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
Definition: BESPlugin.h:165
virtual ~BESPlugin()
The destructor closes the library.
Definition: BESPlugin.h:141
BESPlugin(const string &filename)
Create a new BESPlugin.
Definition: BESPlugin.h:137
BESPlugin provides a mechanism that can load C++ classes at runtime.
Definition: BESPlugin.h:91