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  if( !(*i).empty() )
145  {
146  string key = "BES.module." + (*i) ;
147  string so ;
148  try
149  {
150  TheBESKeys::TheKeys()->get_value( key, so, found ) ;
151  }
152  catch( BESError &e )
153  {
154  cerr << e.get_message() << endl ;
155  return 1 ;
156  }
157  if( so == "" )
158  {
159  cerr << "couldn't find the module for " << (*i) << endl ;
160  return 1 ;
161  }
162  bes_module new_mod ;
163  new_mod._module_name = (*i) ;
164  new_mod._module_library = so ;
165  _module_list.push_back( new_mod ) ;
166  }
167  }
168  }
169 
170  list< bes_module >::iterator mi = _module_list.begin() ;
171  list< bes_module >::iterator me = _module_list.end() ;
172  for( ; mi != me; mi++ )
173  {
174  bes_module curr_mod = *mi ;
175  _moduleFactory.add_mapping( curr_mod._module_name, curr_mod._module_library ) ;
176  }
177 
178  for( mi = _module_list.begin(); mi != me; mi++ )
179  {
180  bes_module curr_mod = *mi ;
181  try
182  {
183  string modname = curr_mod._module_name ;
184  BESAbstractModule *o = _moduleFactory.get( modname ) ;
185  o->initialize( modname ) ;
186  delete o ;
187  }
188  catch( BESError &e )
189  {
190  cerr << "Caught plugin exception during initialization of "
191  << curr_mod._module_name << " module:" << endl << " "
192  << e.get_message() << endl ;
193  retVal = 1 ;
194  break ;
195  }
196  catch( ... )
197  {
198  cerr << "Caught unknown exception during initialization of "
199  << curr_mod._module_name << " module" << endl ;
200  retVal = 1 ;
201  break ;
202  }
203  }
204 
205  return retVal ;
206 }
207 
216 int
218 {
219  list< bes_module >::iterator i = _module_list.begin() ;
220  list< bes_module >::iterator e = _module_list.end() ;
221  bool done = false ;
222  try
223  {
224  // go in the reverse order that the modules were loaded
225  while( !done )
226  {
227  if( e == i ) done = true ;
228  else
229  {
230  e-- ;
231  bes_module curr_mod = *e ;
232  string modname = curr_mod._module_name ;
233  BESAbstractModule *o = _moduleFactory.get( modname ) ;
234  if( o )
235  {
236  o->terminate( modname ) ;
237  delete o ;
238  }
239  }
240  }
241  }
242  catch( BESError &e )
243  {
244  cerr << "Caught exception during module termination: "
245  << e.get_message() << endl ;
246  }
247  catch( ... )
248  {
249  cerr << "Caught unknown exception during terminate" << endl ;
250  }
251 
252  return BESBaseApp::terminate( sig ) ;
253 }
254 
263 void
264 BESModuleApp::dump( ostream &strm ) const
265 {
266  strm << BESIndent::LMarg << "BESModuleApp::dump - ("
267  << (void *)this << ")" << endl ;
269  if( _module_list.size() )
270  {
271  strm << BESIndent::LMarg << "loaded modules:" << endl ;
272  BESIndent::Indent() ;
273  list< bes_module >::const_iterator i = _module_list.begin() ;
274  list< bes_module >::const_iterator e = _module_list.end() ;
275  for( ; i != e; i++ )
276  {
277  bes_module curr_mod = *i ;
278  strm << BESIndent::LMarg << curr_mod._module_name << ": "
279  << curr_mod._module_library << endl ;
280  }
282  }
283  else
284  {
285  strm << BESIndent::LMarg << "loaded modules: none" << endl ;
286  }
288 }
289