OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESModuleApp.cc
Go to the documentation of this file.
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  // FIXME: This is a kludge. But we want to be sure that the dap
107  // modules get loaded first.
108  vector<string> ordered_list;
109  for (; l != le; l++) {
110  string mods = (*l);
111  if (mods != "") {
112  if (mods.find("dap", 0) != string::npos) {
113  ordered_list.insert(ordered_list.begin(), mods);
114  }
115  else {
116  ordered_list.push_back(mods);
117  }
118  }
119  }
120 
121  l = ordered_list.begin();
122  le = ordered_list.end();
123  for (; l != le; l++) {
124  string mods = (*l);
125  list<string> mod_list;
126  BESUtil::explode(',', mods, mod_list);
127 
128  list<string>::iterator i = mod_list.begin();
129  list<string>::iterator e = mod_list.end();
130  for (; i != e; i++) {
131  if (!(*i).empty()) {
132  string key = "BES.module." + (*i);
133  string so;
134  try {
135  TheBESKeys::TheKeys()->get_value(key, so, found);
136  }
137  catch( BESError &e ) {
138  cerr << e.get_message() << endl;
139  return 1;
140  }
141  if (so == "") {
142  cerr << "couldn't find the module for " << (*i) << endl;
143  return 1;
144  }
145  bes_module new_mod;
146  new_mod._module_name = (*i);
147  new_mod._module_library = so;
148  _module_list.push_back(new_mod);
149  }
150  }
151  }
152 
153  list<bes_module>::iterator mi = _module_list.begin();
154  list<bes_module>::iterator me = _module_list.end();
155  for (; mi != me; mi++) {
156  bes_module curr_mod = *mi;
157  _moduleFactory.add_mapping(curr_mod._module_name, curr_mod._module_library);
158  }
159 
160  for (mi = _module_list.begin(); mi != me; mi++) {
161  bes_module curr_mod = *mi;
162  try {
163  string modname = curr_mod._module_name;
164  BESAbstractModule *o = _moduleFactory.get(modname);
165  o->initialize(modname);
166  delete o;
167  }
168  catch( BESError &e ) {
169  cerr << "Caught plugin exception during initialization of " << curr_mod._module_name << " module:" << endl
170  << " " << e.get_message() << endl;
171  retVal = 1;
172  break;
173  }
174  catch( ... ) {
175  cerr << "Caught unknown exception during initialization of " << curr_mod._module_name << " module" << endl;
176  retVal = 1;
177  break;
178  }
179  }
180 
181  return retVal;
182 }
183 
193 {
194  list<bes_module>::iterator i = _module_list.begin();
195  list<bes_module>::iterator e = _module_list.end();
196  bool done = false;
197  try {
198  // go in the reverse order that the modules were loaded
199  // TODO replace this with a reverse iterator. jhrg 12/21/12
200  while (!done) {
201  if (e == i)
202  done = true;
203  else {
204  e--;
205  bes_module curr_mod = *e;
206  string modname = curr_mod._module_name;
207  BESAbstractModule *o = _moduleFactory.get(modname);
208  if (o) {
209  o->terminate(modname);
210  delete o;
211  }
212  }
213  }
214  }
215  catch( BESError &e ) {
216  cerr << "Caught exception during module termination: " << e.get_message() << endl;
217  }
218  catch( ... ) {
219  cerr << "Caught unknown exception during terminate" << endl;
220  }
221 
222  return BESBaseApp::terminate(sig);
223 }
224 
233 void BESModuleApp::dump(ostream &strm) const
234 {
235  strm << BESIndent::LMarg << "BESModuleApp::dump - (" << (void *) this << ")" << endl;
237  if (_module_list.size()) {
238  strm << BESIndent::LMarg << "loaded modules:" << endl;
240  list<bes_module>::const_iterator i = _module_list.begin();
241  list<bes_module>::const_iterator e = _module_list.end();
242  for (; i != e; i++) {
243  bes_module curr_mod = *i;
244  strm << BESIndent::LMarg << curr_mod._module_name << ": " << curr_mod._module_library << endl;
245  }
247  }
248  else {
249  strm << BESIndent::LMarg << "loaded modules: none" << endl;
250  }
252 }
253 
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 void dump(ostream &strm) const
dumps information about this object
virtual int terminate(int sig=0)
clean up after the application
static void Indent()
Definition: BESIndent.cc:38
virtual void terminate(const string &modname)=0
virtual string get_message()
get the error message for this exception
Definition: BESError.h:94
virtual void initialize(const string &modname)=0
Abstract exception class for the BES with basic string message.
Definition: BESError.h:51
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
static void explode(char delim, const string &str, list< string > &values)
explode a string into an array given a delimiter
Definition: BESUtil.cc:457
C * get(const string &name)
Use the BESPlugingFactory to get an instance of the class C matched to 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:453
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)
Add a mapping of name to library_name to the BESPluginFactory.
void get_values(const string &s, vector< string > &vals, bool &found)
Retrieve the values of a given key, if set.
Definition: BESKeys.cc:488
static void UnIndent()
Definition: BESIndent.cc:44
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:48