bes  Updated for version 3.20.6
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 
53 public:
54  NoSuchLibrary(const std::string &msg, const std::string &file, int line) :
55  BESInternalFatalError(msg, file, line)
56  {
57  }
58 };
59 
64 public:
65  NoSuchObject(const std::string &msg, const std::string &file, int line) :
66  BESInternalFatalError(msg, file, line)
67  {
68  }
69 };
70 
91 template<typename M>
92 class BESPlugin: public BESObj {
93 private:
94  std::string d_filename; // Library filename
95  void *d_lib; // Open library handle
96 
99  BESPlugin();
100 #if 0
101  throw (BESInternalError)
102  {
103  throw BESInternalError("Unimplemented method", __FILE__, __LINE__);
104  }
105 #endif
106 
110  BESPlugin(const BESPlugin &p);
111 #if 0
112  throw (BESInternalError)
113  {
114  throw BESInternalError("Unimplemented method.", __FILE__, __LINE__);
115  }
116 #endif
117 
120  BESPlugin &operator=(const BESPlugin &p);
121 #if 0
122  throw (BESInternalError)
123  {
124  throw BESInternalError("Unimplemented method.", __FILE__, __LINE__);
125  }
126 #endif
127  void *get_lib() throw (NoSuchLibrary)
128  {
129  if (!d_lib) {
130  d_lib = dlopen(d_filename.c_str(), RTLD_NOW | RTLD_GLOBAL);
131  BESDEBUG( "bes", "BESPlugin: plug in handler:" << d_filename << ", " << d_lib << std::endl);
132  if (d_lib == NULL) {
133  throw NoSuchLibrary(std::string(dlerror()), __FILE__, __LINE__);
134  }
135  }
136 
137  return d_lib;
138  }
139 
140 public:
145  BESPlugin(const std::string &filename) :
146  d_filename(filename), d_lib(0)
147  {
148  }
149 
152  virtual ~BESPlugin()
153  {
154  BESDEBUG( "bes", "BESPlugin: unplugging handler:" << d_filename << ", " << d_lib << std::endl);
155 #ifdef UNPLUG_HANDLERS
156  if (d_lib) {
157  dlclose(d_lib);
158  d_lib = 0; // Added; paranoia. jhrg
159  }
160 #endif
161  }
162 
170  {
171  void *maker = dlsym(get_lib(), "maker");
172  if (!maker) {
173  throw NoSuchObject(std::string(dlerror()), __FILE__, __LINE__);
174  }
175 
176  typedef M *(*maker_func_ptr)();
177  maker_func_ptr my_maker = *reinterpret_cast<maker_func_ptr*>(&maker);
178  M *my_M = (my_maker)();
179 
180  return my_M;
181  }
182 
183  virtual void dump(std::ostream &strm) const
184  {
185  strm << "BESPlugin::dump - (" << (void *) this << ")" << std::endl;
186  strm << " plugin name: " << d_filename << std::endl;
187  strm << " library handle: " << (void *) d_lib << std::endl;
188  }
189 };
190 
191 #endif // T_BESPlugin_h
BESPlugin::instantiate
M * instantiate()
Definition: BESPlugin.h:169
BESPlugin::BESPlugin
BESPlugin(const std::string &filename)
Definition: BESPlugin.h:145
BESInternalFatalError
exception thrown if an internal error is found and is fatal to the BES
Definition: BESInternalFatalError.h:43
NoSuchLibrary
Definition: BESPlugin.h:52
BESPlugin::~BESPlugin
virtual ~BESPlugin()
Definition: BESPlugin.h:152
BESObj
top level BES object to house generic methods
Definition: BESObj.h:49
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
BESPlugin::dump
virtual void dump(std::ostream &strm) const
dump the contents of this object to the specified ostream
Definition: BESPlugin.h:183
BESPlugin
Definition: BESPlugin.h:92
NoSuchObject
Definition: BESPlugin.h:63