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 public:
58  NoSuchLibrary(const string &msg, const string &file, int line) :
59  BESInternalFatalError(msg, file, line)
60  {
61  }
62 };
63 
68 public:
69  NoSuchObject(const string &msg, const string &file, int line) :
70  BESInternalFatalError(msg, file, line)
71  {
72  }
73 };
74 
95 template<typename M>
96 class BESPlugin: public BESObj {
97 private:
98  string d_filename; // Library filename
99  void *d_lib; // Open library handle
100 
103  BESPlugin() throw (BESInternalError)
104  {
105  throw BESInternalError("Unimplemented method", __FILE__, __LINE__);
106  }
107 
112  BESPlugin(const BESPlugin &p) throw (BESInternalError)
113  {
114  throw BESInternalError("Unimplemented method.", __FILE__, __LINE__);
115  }
116 
120  BESPlugin &operator=(const BESPlugin &p) throw (BESInternalError)
121  {
122  throw BESInternalError("Unimplemented method.", __FILE__, __LINE__);
123  }
124 
125  void *get_lib() throw (NoSuchLibrary)
126  {
127  if (!d_lib) {
128  d_lib = dlopen(d_filename.c_str(), RTLD_NOW | RTLD_GLOBAL);
129  BESDEBUG( "bes", "BESPlugin: plug in handler:" << d_filename << ", " << d_lib << endl);
130  if (d_lib == NULL) {
131  throw NoSuchLibrary(string(dlerror()), __FILE__, __LINE__);
132  }
133  }
134 
135  return d_lib;
136  }
137 
138 public:
143  BESPlugin(const string &filename) :
144  d_filename(filename), d_lib(0)
145  {
146  }
147 
150  virtual ~BESPlugin()
151  {
152  BESDEBUG( "bes", "BESPlugin: unplugging handler:" << d_filename << ", " << d_lib << endl);
153 #ifdef UNPLUG_HANDLERS
154  if (d_lib) {
155  dlclose(d_lib);
156  d_lib = 0; // Added; paranoia. jhrg
157  }
158 #endif
159  }
160 
168  {
169  void *maker = dlsym(get_lib(), "maker");
170  if (!maker) {
171  throw NoSuchObject(string(dlerror()), __FILE__, __LINE__);
172  }
173 
174  typedef M *(*maker_func_ptr)();
175  maker_func_ptr my_maker = *reinterpret_cast<maker_func_ptr*>(&maker);
176  M *my_M = (my_maker)();
177 
178  return my_M;
179  }
180 
181  virtual void dump(ostream &strm) const
182  {
183  strm << "BESPlugin::dump - (" << (void *) this << ")" << endl;
184  strm << " plugin name: " << d_filename << endl;
185  strm << " library handle: " << (void *) d_lib << endl;
186  }
187 };
188 
189 #endif // T_BESPlugin_h
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:56
Thrown as an exception when BESPlugin cannot find or run the maker() function in a shared library alr...
Definition: BESPlugin.h:67
NoSuchObject(const string &msg, const string &file, int line)
Definition: BESPlugin.h:69
M * instantiate()
Instantiate the object.
Definition: BESPlugin.h:167
Base object for bes objects.
Definition: BESObj.h:52
NoSuchLibrary(const string &msg, const string &file, int line)
Definition: BESPlugin.h:58
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
Definition: BESPlugin.h:181
virtual ~BESPlugin()
The destructor closes the library.
Definition: BESPlugin.h:150
BESAbstractModule * maker()
Definition: DapModule.cc:86
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
BESPlugin(const string &filename)
Create a new BESPlugin.
Definition: BESPlugin.h:143
BESPlugin provides a mechanism that can load C++ classes at runtime.
Definition: BESPlugin.h:96