bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
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
52class NoSuchLibrary: public BESInternalFatalError {
53public:
54 NoSuchLibrary(const std::string &msg, const std::string &file, int line) :
55 BESInternalFatalError(msg, file, line)
56 {
57 }
58};
59
63class NoSuchObject: public BESInternalFatalError {
64public:
65 NoSuchObject(const std::string &msg, const std::string &file, int line) :
66 BESInternalFatalError(msg, file, line)
67 {
68 }
69};
70
90
91template<typename M>
92class BESPlugin: public BESObj {
93private:
94 std::string d_filename; // Library filename
95 void *d_lib = nullptr; // Open library handle
96
99 void *get_lib() {
100 if (!d_lib) {
101 d_lib = dlopen(d_filename.c_str(), RTLD_LAZY /*RTLD_NOW*/ | RTLD_GLOBAL);
102 BESDEBUG( "bes", "BESPlugin: plug in handler:" << d_filename << ", " << d_lib << std::endl);
103 if (d_lib == nullptr) {
104 throw NoSuchLibrary(std::string(dlerror()), __FILE__, __LINE__);
105 }
106 }
107
108 return d_lib;
109 }
110
111public:
112 BESPlugin() = delete;
113 BESPlugin(const BESPlugin &p) = delete;
114
119 explicit BESPlugin(const std::string &filename) : d_filename(filename) { }
120
121 ~BESPlugin() override = default;
122
123 BESPlugin &operator=(const BESPlugin &p) = delete;
124
131 void *maker = dlsym(get_lib(), "maker");
132 if (!maker) {
133 throw NoSuchObject(std::string(dlerror()), __FILE__, __LINE__);
134 }
135
136 typedef M *(*maker_func_ptr)();
137 maker_func_ptr my_maker = *reinterpret_cast<maker_func_ptr*>(&maker);
138 M *my_M = my_maker();
139
140 return my_M;
141 }
142
143 void dump(std::ostream &strm) const override {
144 strm << "BESPlugin::dump - (" << std::ios::hex << this << ")" << std::endl;
145 strm << " plugin name: " << d_filename << std::endl;
146 strm << " library handle: " << std::ios::hex << d_lib << std::endl;
147 }
148};
149
150#endif // T_BESPlugin_h
top level BES object to house generic methods
Definition BESObj.h:54
void dump(std::ostream &strm) const override
dump the contents of this object to the specified ostream
Definition BESPlugin.h:143
BESPlugin(const std::string &filename)
Definition BESPlugin.h:119
M * instantiate()
Definition BESPlugin.h:130