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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 
52 {
53 }
54 
62 {
63 }
64 
71 int
72 BESModuleApp::initialize(int argC, char **argV)
73 {
74  int retVal = BESBaseApp::initialize( argC, argV ) ;
75  if( !retVal )
76  {
77  try
78  {
79  retVal = loadModules() ;
80  }
81  catch( BESError &e )
82  {
83  string newerr = "Error during module initialization: " ;
84  newerr += e.get_message() ;
85  cerr << newerr << endl ;
86  retVal = 1 ;
87  }
88  catch( ... )
89  {
90  string newerr = "Error during module initialization: " ;
91  newerr += "caught unknown exception" ;
92  cerr << newerr << endl ;
93  retVal = 1 ;
94  }
95  }
96 
97  return retVal ;
98 }
99 
102 int
103 BESModuleApp::loadModules()
104 {
105  int retVal = 0 ;
106 
107  bool found = false ;
108  vector<string> vals ;
109  TheBESKeys::TheKeys()->get_values( "BES.modules", vals, found ) ;
110  vector<string>::iterator l = vals.begin() ;
111  vector<string>::iterator le = vals.end() ;
112 
113  // FIXME: 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  {
118  string mods = (*l) ;
119  if( mods != "" )
120  {
121  if( mods.find( "dap", 0 ) != string::npos )
122  {
123  ordered_list.insert( ordered_list.begin(), mods ) ;
124  }
125  else
126  {
127  ordered_list.push_back( mods ) ;
128  }
129  }
130  }
131 
132  l = ordered_list.begin() ;
133  le = ordered_list.end() ;
134  for( ; l != le; l++ )
135  {
136  string mods = (*l) ;
137  list<string> mod_list ;
138  BESUtil::explode( ',', mods, mod_list ) ;
139 
140  list<string>::iterator i = mod_list.begin() ;
141  list<string>::iterator e = mod_list.end() ;
142  for( ; i != e; i++ )
143  {
144  string key = "BES.module." + (*i) ;
145  string so ;
146  try
147  {
148  TheBESKeys::TheKeys()->get_value( key, so, found ) ;
149  }
150  catch( BESError &e )
151  {
152  cerr << e.get_message() << endl ;
153  return 1 ;
154  }
155  if( so == "" )
156  {
157  cerr << "couldn't find the module for " << (*i) << endl ;
158  return 1 ;
159  }
160  bes_module new_mod ;
161  new_mod._module_name = (*i) ;
162  new_mod._module_library = so ;
163  _module_list.push_back( new_mod ) ;
164  }
165  }
166 
167  list< bes_module >::iterator mi = _module_list.begin() ;
168  list< bes_module >::iterator me = _module_list.end() ;
169  for( ; mi != me; mi++ )
170  {
171  bes_module curr_mod = *mi ;
172  _moduleFactory.add_mapping( curr_mod._module_name, curr_mod._module_library ) ;
173  }
174 
175  for( mi = _module_list.begin(); mi != me; mi++ )
176  {
177  bes_module curr_mod = *mi ;
178  try
179  {
180  string modname = curr_mod._module_name ;
181  BESAbstractModule *o = _moduleFactory.get( modname ) ;
182  o->initialize( modname ) ;
183  delete o ;
184  }
185  catch( BESError &e )
186  {
187  cerr << "Caught plugin exception during initialization of "
188  << curr_mod._module_name << " module:" << endl << " "
189  << e.get_message() << endl ;
190  retVal = 1 ;
191  break ;
192  }
193  catch( ... )
194  {
195  cerr << "Caught unknown exception during initialization of "
196  << curr_mod._module_name << " module" << endl ;
197  retVal = 1 ;
198  break ;
199  }
200  }
201 
202  return retVal ;
203 }
204 
213 int
215 {
216  list< bes_module >::iterator i = _module_list.begin() ;
217  list< bes_module >::iterator e = _module_list.end() ;
218  try
219  {
220  for( i = _module_list.begin(); i != e; i++ )
221  {
222  bes_module curr_mod = *i ;
223  string modname = curr_mod._module_name ;
224  BESAbstractModule *o = _moduleFactory.get( modname ) ;
225  if( o )
226  {
227  o->terminate( modname ) ;
228  delete o ;
229  }
230  }
231  }
232  catch( BESError &e )
233  {
234  cerr << "Caught exception during module termination: "
235  << e.get_message() << endl ;
236  }
237  catch( ... )
238  {
239  cerr << "Caught unknown exception during terminate" << endl ;
240  }
241 
242  return BESBaseApp::terminate( sig ) ;
243 }
244 
253 void
254 BESModuleApp::dump( ostream &strm ) const
255 {
256  strm << BESIndent::LMarg << "BESModuleApp::dump - ("
257  << (void *)this << ")" << endl ;
259  if( _module_list.size() )
260  {
261  strm << BESIndent::LMarg << "loaded modules:" << endl ;
263  list< bes_module >::const_iterator i = _module_list.begin() ;
264  list< bes_module >::const_iterator e = _module_list.end() ;
265  for( ; i != e; i++ )
266  {
267  bes_module curr_mod = *i ;
268  strm << BESIndent::LMarg << curr_mod._module_name << ": "
269  << curr_mod._module_library << endl ;
270  }
272  }
273  else
274  {
275  strm << BESIndent::LMarg << "loaded modules: none" << endl ;
276  }
278 }
279 
virtual ~BESModuleApp(void)
Default destructor.
Definition: BESModuleApp.cc:61
virtual int initialize(int argC, char **argV)
initialize the BES application
Definition: BESBaseApp.cc:99
Base application object for all BES applications.
Definition: BESBaseApp.h:54
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:91
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:513
virtual void dump(ostream &strm) const
dumps information about this object
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:72
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:466
virtual int terminate(int sig=0)
clean up after the application
Definition: BESBaseApp.cc:154
BESModuleApp(void)
Default constructor.
Definition: BESModuleApp.cc:51
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:503
static void UnIndent()
Definition: BESIndent.cc:44
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:46