bes  Updated for version 3.17.4
BESPlugin.h
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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();
104 #if 0
105  throw (BESInternalError)
106  {
107  throw BESInternalError("Unimplemented method", __FILE__, __LINE__);
108  }
109 #endif
110 
114  BESPlugin(const BESPlugin &p);
115 #if 0
116  throw (BESInternalError)
117  {
118  throw BESInternalError("Unimplemented method.", __FILE__, __LINE__);
119  }
120 #endif
121 
124  BESPlugin &operator=(const BESPlugin &p);
125 #if 0
126  throw (BESInternalError)
127  {
128  throw BESInternalError("Unimplemented method.", __FILE__, __LINE__);
129  }
130 #endif
131  void *get_lib() throw (NoSuchLibrary)
132  {
133  if (!d_lib) {
134  d_lib = dlopen(d_filename.c_str(), RTLD_NOW | RTLD_GLOBAL);
135  BESDEBUG( "bes", "BESPlugin: plug in handler:" << d_filename << ", " << d_lib << endl);
136  if (d_lib == NULL) {
137  throw NoSuchLibrary(string(dlerror()), __FILE__, __LINE__);
138  }
139  }
140 
141  return d_lib;
142  }
143 
144 public:
149  BESPlugin(const string &filename) :
150  d_filename(filename), d_lib(0)
151  {
152  }
153 
156  virtual ~BESPlugin()
157  {
158  BESDEBUG( "bes", "BESPlugin: unplugging handler:" << d_filename << ", " << d_lib << endl);
159 #ifdef UNPLUG_HANDLERS
160  if (d_lib) {
161  dlclose(d_lib);
162  d_lib = 0; // Added; paranoia. jhrg
163  }
164 #endif
165  }
166 
174  {
175  void *maker = dlsym(get_lib(), "maker");
176  if (!maker) {
177  throw NoSuchObject(string(dlerror()), __FILE__, __LINE__);
178  }
179 
180  typedef M *(*maker_func_ptr)();
181  maker_func_ptr my_maker = *reinterpret_cast<maker_func_ptr*>(&maker);
182  M *my_M = (my_maker)();
183 
184  return my_M;
185  }
186 
187  virtual void dump(ostream &strm) const
188  {
189  strm << "BESPlugin::dump - (" << (void *) this << ")" << endl;
190  strm << " plugin name: " << d_filename << endl;
191  strm << " library handle: " << (void *) d_lib << endl;
192  }
193 };
194 
195 #endif // T_BESPlugin_h
exception thrown if an internal error is found and is fatal to the BES
exception thrown if inernal error encountered
M * instantiate()
Definition: BESPlugin.h:173
Base object for bes objects.
Definition: BESObj.h:52
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
Definition: BESPlugin.h:187
virtual ~BESPlugin()
Definition: BESPlugin.h:156
BESPlugin(const string &filename)
Definition: BESPlugin.h:149