bes Updated for version 3.20.13
BESModuleApp.cc
1// BESModuleApp.C
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//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact University Corporation for Atmospheric Research at
24// 3080 Center Green Drive, Boulder, CO 80301
25
26// (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27// Please read the full copyright statement in the file COPYRIGHT_UCAR.
28//
29// Authors:
30// pwest Patrick West <pwest@ucar.edu>
31// jgarcia Jose Garcia <jgarcia@ucar.edu>
32
33#include <iostream>
34
35#include "BESModuleApp.h"
36#include "BESError.h"
37#include "BESPluginFactory.h"
38#include "BESAbstractModule.h"
39#include "TheBESKeys.h"
40#include "BESUtil.h"
41
42using namespace std;
43
50 BESApp()
51{
52}
53
54#if 0
60BESModuleApp::~BESModuleApp()
61{
62}
63#endif
64
71int BESModuleApp::initialize(int argC, char **argV)
72{
73 int retVal = BESApp::initialize(argC, argV);
74 if (!retVal) {
75 try {
76 retVal = loadModules();
77 }
78 catch( BESError &e ) {
79 cerr << "Error during module initialization: " << e.get_message() << endl;
80 retVal = 1;
81 }
82 catch( ... ) {
83 cerr << "Error during module initialization: Unknown exception" << endl;
84 retVal = 1;
85 }
86 }
87
88 return retVal;
89}
90
93int BESModuleApp::loadModules()
94{
95 int retVal = 0;
96
97 bool found = false;
98 vector<string> vals;
99 TheBESKeys::TheKeys()->get_values("BES.modules", vals, found);
100 vector<string>::iterator l = vals.begin();
101 vector<string>::iterator le = vals.end();
102
103 // The following code was likely added before we had the 'Include'
104 // directive. Now all modules have a line in their .conf file to
105 // Include dap.conf and that makes this redundant. However, what
106 // was happening was a module named XdapX would match the find()
107 // call below and would wind up being loaded _before_ the dap module.
108 // That led to all sorts of runtime problems. See ticket 2258. Since
109 // we don't need this and it can cause problems, I'm removing it.
110 // jhrg 10/13/14
111#if 0
112 // This is a kludge. But we want to be sure that the dap
113 // modules get loaded first.
114 vector<string> ordered_list;
115 for (; l != le; l++) {
116 string mods = (*l);
117 if (mods != "") {
118 if (mods.find("dap", 0) != string::npos) {
119 ordered_list.insert(ordered_list.begin(), mods);
120 }
121 else {
122 ordered_list.push_back(mods);
123 }
124 }
125 }
126
127 l = ordered_list.begin();
128 le = ordered_list.end();
129#endif
130 for (; l != le; l++) {
131 string mods = (*l);
132 list<string> mod_list;
133 BESUtil::explode(',', mods, mod_list);
134
135 list<string>::iterator i = mod_list.begin();
136 list<string>::iterator e = mod_list.end();
137 for (; i != e; i++) {
138 if (!(*i).empty()) {
139 string key = "BES.module." + (*i);
140 string so;
141 try {
142 TheBESKeys::TheKeys()->get_value(key, so, found);
143 }
144 catch( BESError &e ) {
145 cerr << e.get_message() << endl;
146 return 1;
147 }
148 if (so == "") {
149 cerr << "Couldn't find the module for " << (*i) << endl;
150 return 1;
151 }
152 bes_module new_mod;
153 new_mod._module_name = (*i);
154 new_mod._module_library = so;
155 _module_list.push_back(new_mod);
156 }
157 }
158 }
159
160 list<bes_module>::iterator mi = _module_list.begin();
161 list<bes_module>::iterator me = _module_list.end();
162 for (; mi != me; mi++) {
163 bes_module curr_mod = *mi;
164 _moduleFactory.add_mapping(curr_mod._module_name, curr_mod._module_library);
165 }
166
167 for (mi = _module_list.begin(); mi != me; mi++) {
168 bes_module curr_mod = *mi;
169 try {
170 string modname = curr_mod._module_name;
171 BESAbstractModule *o = _moduleFactory.get(modname);
172 o->initialize(modname);
173 delete o;
174 }
175 catch( BESError &e ) {
176 cerr << "Caught plugin exception during initialization of " << curr_mod._module_name << " module:" << endl
177 << " " << e.get_message() << endl;
178 retVal = 1;
179 break;
180 }
181 catch( ... ) {
182 cerr << "Caught unknown exception during initialization of " << curr_mod._module_name << " module" << endl;
183 retVal = 1;
184 break;
185 }
186 }
187
188 return retVal;
189}
190
200{
201 list<bes_module>::iterator i = _module_list.begin();
202 list<bes_module>::iterator e = _module_list.end();
203 bool done = false;
204 try {
205 // go in the reverse order that the modules were loaded
206 // TODO replace this with a reverse iterator. jhrg 12/21/12
207 while (!done) {
208 if (e == i)
209 done = true;
210 else {
211 e--;
212 bes_module curr_mod = *e;
213 string modname = curr_mod._module_name;
214 BESAbstractModule *o = _moduleFactory.get(modname);
215 if (o) {
216 o->terminate(modname);
217 delete o;
218 }
219 }
220 }
221 }
222 catch( BESError &e ) {
223 cerr << "Caught exception during module termination: " << e.get_message() << endl;
224 }
225 catch( ... ) {
226 cerr << "Caught unknown exception during terminate" << endl;
227 }
228
229 return BESApp::terminate(sig);
230}
231
240void BESModuleApp::dump(ostream &strm) const
241{
242 strm << BESIndent::LMarg << "BESModuleApp::dump - (" << (void *) this << ")" << endl;
243 BESIndent::Indent();
244 if (_module_list.size()) {
245 strm << BESIndent::LMarg << "loaded modules:" << endl;
246 BESIndent::Indent();
247 list<bes_module>::const_iterator i = _module_list.begin();
248 list<bes_module>::const_iterator e = _module_list.end();
249 for (; i != e; i++) {
250 bes_module curr_mod = *i;
251 strm << BESIndent::LMarg << curr_mod._module_name << ": " << curr_mod._module_library << endl;
252 }
253 BESIndent::UnIndent();
254 }
255 else {
256 strm << BESIndent::LMarg << "loaded modules: none" << endl;
257 }
258 BESIndent::UnIndent();
259}
260
Application class for BES applications.
Definition: BESApp.h:56
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:59
std::string get_message() const
get the error message for this exception
Definition: BESError.h:111
BESModuleApp()
Default constructor.
Definition: BESModuleApp.cc:49
int terminate(int sig=0) override
clean up after the application
int initialize(int argC, char **argV) override
Load and initialize any BES modules.
Definition: BESModuleApp.cc:71
void dump(std::ostream &strm) const override
dumps information about this object
void add_mapping(const std::string &name, const std::string &library_name)
C * get(const std::string &name)
static void explode(char delim, const std::string &str, std::list< std::string > &values)
Definition: BESUtil.cc:540
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: TheBESKeys.cc:340
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:71
void get_values(const std::string &s, std::vector< std::string > &vals, bool &found)
Retrieve the values of a given key, if set.
Definition: TheBESKeys.cc:371