bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
BESModuleApp.cc
1
2// BESModuleApp.C
3
4// This file is part of bes, A C++ back-end server implementation framework
5// for the OPeNDAP Data Access Protocol.
6
7// Copyright (c) 2004-2009 University Corporation for Atmospheric Research
8// Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.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
34#include <iostream>
35
36#include "BESModuleApp.h"
37#include "BESError.h"
38#include "BESPluginFactory.h"
39#include "BESAbstractModule.h"
40#include "TheBESKeys.h"
41#include "BESUtil.h"
42
43using namespace std;
44
51 BESApp()
52{
53}
54
55#if 0
61BESModuleApp::~BESModuleApp()
62{
63}
64#endif
65
72int BESModuleApp::initialize(int argC, char **argV)
73{
74 int retVal = BESApp::initialize(argC, argV);
75 if (!retVal) {
76 try {
77 retVal = loadModules();
78 }
79 catch( BESError &e ) {
80 cerr << "Error during module initialization: " << e.get_message() << endl;
81 retVal = 1;
82 }
83 catch( ... ) {
84 cerr << "Error during module initialization: Unknown exception" << endl;
85 retVal = 1;
86 }
87 }
88
89 return retVal;
90}
91
94int BESModuleApp::loadModules()
95{
96 int retVal = 0;
97
98 bool found = false;
99 vector<string> vals;
100 TheBESKeys::TheKeys()->get_values("BES.modules", vals, found);
101 vector<string>::iterator l = vals.begin();
102 vector<string>::iterator le = vals.end();
103
104 // The following code was likely added before we had the 'Include'
105 // directive. Now all modules have a line in their .conf file to
106 // Include dap.conf and that makes this redundant. However, what
107 // was happening was a module named XdapX would match the find()
108 // call below and would wind up being loaded _before_ the dap module.
109 // That led to all sorts of runtime problems. See ticket 2258. Since
110 // we don't need this and it can cause problems, I'm removing it.
111 // jhrg 10/13/14
112#if 0
113 // This is a kludge. But we want to be sure that the dap
114 // modules get loaded first.
115 vector<string> ordered_list;
116 for (; l != le; l++) {
117 string mods = (*l);
118 if (mods != "") {
119 if (mods.find("dap", 0) != string::npos) {
120 ordered_list.insert(ordered_list.begin(), mods);
121 }
122 else {
123 ordered_list.push_back(mods);
124 }
125 }
126 }
127
128 l = ordered_list.begin();
129 le = ordered_list.end();
130#endif
131 for (; l != le; l++) {
132 string mods = (*l);
133 list<string> mod_list;
134 BESUtil::explode(',', mods, mod_list);
135
136 list<string>::iterator i = mod_list.begin();
137 list<string>::iterator e = mod_list.end();
138 for (; i != e; i++) {
139 if (!(*i).empty()) {
140 string key = "BES.module." + (*i);
141 string so;
142 try {
143 TheBESKeys::TheKeys()->get_value(key, so, found);
144 }
145 catch( BESError &e ) {
146 cerr << e.get_message() << endl;
147 return 1;
148 }
149 if (so == "") {
150 cerr << "Couldn't find the module for " << (*i) << endl;
151 return 1;
152 }
153 bes_module new_mod;
154 new_mod._module_name = (*i);
155 new_mod._module_library = so;
156 _module_list.push_back(new_mod);
157 }
158 }
159 }
160
161 list<bes_module>::iterator mi = _module_list.begin();
162 list<bes_module>::iterator me = _module_list.end();
163 for (; mi != me; mi++) {
164 bes_module curr_mod = *mi;
165 _moduleFactory.add_mapping(curr_mod._module_name, curr_mod._module_library);
166 }
167
168 for (mi = _module_list.begin(); mi != me; mi++) {
169 bes_module curr_mod = *mi;
170 try {
171 string modname = curr_mod._module_name;
172 BESAbstractModule *o = _moduleFactory.get(modname);
173 o->initialize(modname);
174 delete o;
175 }
176 catch( BESError &e ) {
177 cerr << "Caught plugin exception during initialization of " << curr_mod._module_name << " module:" << endl
178 << " " << e.get_message() << endl;
179 retVal = 1;
180 break;
181 }
182 catch( ... ) {
183 cerr << "Caught unknown exception during initialization of " << curr_mod._module_name << " module" << endl;
184 retVal = 1;
185 break;
186 }
187 }
188
189 return retVal;
190}
191
201{
202 list<bes_module>::iterator i = _module_list.begin();
203 list<bes_module>::iterator e = _module_list.end();
204 bool done = false;
205 try {
206 // go in the reverse order that the modules were loaded
207 // TODO replace this with a reverse iterator. jhrg 12/21/12
208 while (!done) {
209 if (e == i)
210 done = true;
211 else {
212 e--;
213 bes_module curr_mod = *e;
214 string modname = curr_mod._module_name;
215 BESAbstractModule *o = _moduleFactory.get(modname);
216 if (o) {
217 o->terminate(modname);
218 delete o;
219 }
220 }
221 }
222 }
223 catch( BESError &e ) {
224 cerr << "Caught exception during module termination: " << e.get_message() << endl;
225 }
226 catch( ... ) {
227 cerr << "Caught unknown exception during terminate" << endl;
228 }
229
230 return BESApp::terminate(sig);
231}
232
241void BESModuleApp::dump(ostream &strm) const
242{
243 strm << BESIndent::LMarg << "BESModuleApp::dump - (" << (void *) this << ")" << endl;
244 BESIndent::Indent();
245 if (_module_list.size()) {
246 strm << BESIndent::LMarg << "loaded modules:" << endl;
247 BESIndent::Indent();
248 list<bes_module>::const_iterator i = _module_list.begin();
249 list<bes_module>::const_iterator e = _module_list.end();
250 for (; i != e; i++) {
251 bes_module curr_mod = *i;
252 strm << BESIndent::LMarg << curr_mod._module_name << ": " << curr_mod._module_library << endl;
253 }
254 BESIndent::UnIndent();
255 }
256 else {
257 strm << BESIndent::LMarg << "loaded modules: none" << endl;
258 }
259 BESIndent::UnIndent();
260}
261
virtual int initialize(int argC, char **argV)
Initialize the application using the passed argc and argv values.
Definition BESApp.cc:70
virtual int terminate(int sig=0)
Clean up after the application.
Definition BESApp.cc:100
Base exception class for the BES with basic string message.
Definition BESError.h:66
std::string get_message() const
get the error message for this exception
Definition BESError.h:132
BESModuleApp()
Default constructor.
int terminate(int sig=0) override
clean up after the application
int initialize(int argC, char **argV) override
Load and initialize any BES modules.
void dump(std::ostream &strm) const override
dumps information about this object
static void explode(char delim, const std::string &str, std::list< std::string > &values)
Definition BESUtil.cc:543
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()
Access to the singleton.
Definition TheBESKeys.cc:85
void get_values(const std::string &s, std::vector< std::string > &vals, bool &found)
Retrieve the values of a given key, if set.
STL iterator class.
STL iterator class.
STL iterator class.