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