OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESModuleApp.cc
Go to the documentation of this file.
00001 // BESModuleApp.C
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00032 
00033 #include <iostream>
00034 
00035 using std::cerr ;
00036 using std::endl ;
00037 
00038 #include "BESModuleApp.h"
00039 #include "BESError.h"
00040 #include "BESPluginFactory.h"
00041 #include "BESAbstractModule.h"
00042 #include "TheBESKeys.h"
00043 #include "BESUtil.h"
00044 
00050 BESModuleApp::
00051 BESModuleApp(void) : BESBaseApp()
00052 {
00053 }
00054 
00060 BESModuleApp::
00061 ~BESModuleApp(void)
00062 {
00063 }
00064 
00071 int
00072 BESModuleApp::initialize(int argC, char **argV)
00073 {
00074     int retVal = BESBaseApp::initialize( argC, argV ) ;
00075     if( !retVal )
00076     {
00077         try
00078         {
00079             retVal = loadModules() ;
00080         }
00081         catch( BESError &e )
00082         {
00083             string newerr = "Error during module initialization: " ;
00084             newerr += e.get_message() ;
00085             cerr << newerr << endl ;
00086             retVal = 1 ;
00087         }
00088         catch( ... )
00089         {
00090             string newerr = "Error during module initialization: " ;
00091             newerr += "caught unknown exception" ;
00092             cerr << newerr << endl ;
00093             retVal = 1 ;
00094         }
00095     }
00096 
00097     return retVal ;
00098 }
00099 
00102 int
00103 BESModuleApp::loadModules()
00104 {
00105     int retVal = 0 ;
00106 
00107     bool found = false ;
00108     vector<string> vals ;
00109     TheBESKeys::TheKeys()->get_values( "BES.modules", vals, found ) ;
00110     vector<string>::iterator l = vals.begin() ;
00111     vector<string>::iterator le = vals.end() ;
00112 
00113     // FIXME: This is a kludge. But we want to be sure that the dap
00114     // modules get loaded first.
00115     vector<string> ordered_list ;
00116     for( ; l != le; l++ )
00117     {
00118         string mods = (*l) ;
00119         if( mods != "" )
00120         {
00121             if( mods.find( "dap", 0 ) != string::npos )
00122             {
00123                 ordered_list.insert( ordered_list.begin(), mods ) ;
00124             }
00125             else
00126             {
00127                 ordered_list.push_back( mods ) ;
00128             }
00129         }
00130     }
00131 
00132     l = ordered_list.begin() ;
00133     le = ordered_list.end() ;
00134     for( ; l != le; l++ )
00135     {
00136         string mods = (*l) ;
00137         list<string> mod_list ;
00138         BESUtil::explode( ',', mods, mod_list ) ;
00139 
00140         list<string>::iterator i = mod_list.begin() ;
00141         list<string>::iterator e = mod_list.end() ;
00142         for( ; i != e; i++ )
00143         {
00144             if( !(*i).empty() )
00145             {
00146                 string key = "BES.module." + (*i) ;
00147                 string so ;
00148                 try
00149                 {
00150                     TheBESKeys::TheKeys()->get_value( key, so, found ) ;
00151                 }
00152                 catch( BESError &e )
00153                 {
00154                     cerr << e.get_message() << endl ;
00155                     return 1 ;
00156                 }
00157                 if( so == "" )
00158                 {
00159                     cerr << "couldn't find the module for " << (*i) << endl ;
00160                     return 1 ;
00161                 }
00162                 bes_module new_mod ;
00163                 new_mod._module_name = (*i) ;
00164                 new_mod._module_library = so ;
00165                 _module_list.push_back( new_mod ) ;
00166             }
00167         }
00168     }
00169 
00170     list< bes_module >::iterator mi = _module_list.begin() ;
00171     list< bes_module >::iterator me = _module_list.end() ;
00172     for( ; mi != me; mi++ )
00173     {
00174         bes_module curr_mod = *mi ;
00175         _moduleFactory.add_mapping( curr_mod._module_name, curr_mod._module_library ) ;
00176     }
00177 
00178     for( mi = _module_list.begin(); mi != me; mi++ )
00179     {
00180         bes_module curr_mod = *mi ;
00181         try
00182         {
00183             string modname = curr_mod._module_name ;
00184             BESAbstractModule *o = _moduleFactory.get( modname ) ;
00185             o->initialize( modname ) ;
00186             delete o ;
00187         }
00188         catch( BESError &e )
00189         {
00190             cerr << "Caught plugin exception during initialization of "
00191                  << curr_mod._module_name << " module:" << endl << "    "
00192                  << e.get_message() << endl ;
00193             retVal = 1 ;
00194             break ;
00195         }
00196         catch( ... )
00197         {
00198             cerr << "Caught unknown exception during initialization of "
00199                  << curr_mod._module_name << " module" << endl ;
00200             retVal = 1 ;
00201             break ;
00202         }
00203     }
00204 
00205     return retVal ;
00206 }
00207 
00216 int
00217 BESModuleApp::terminate( int sig )
00218 {
00219     list< bes_module >::iterator i = _module_list.begin() ;
00220     list< bes_module >::iterator e = _module_list.end() ;
00221     bool done = false ;
00222     try
00223     {
00224         // go in the reverse order that the modules were loaded
00225         while( !done )
00226         {
00227             if( e == i ) done = true ;
00228             else
00229             {
00230                 e-- ;
00231                 bes_module curr_mod = *e ;
00232                 string modname = curr_mod._module_name ;
00233                 BESAbstractModule *o = _moduleFactory.get( modname ) ;
00234                 if( o )
00235                 {
00236                     o->terminate( modname ) ;
00237                     delete o ;
00238                 }
00239             }
00240         }
00241     }
00242     catch( BESError &e )
00243     {
00244         cerr << "Caught exception during module termination: "
00245              << e.get_message() << endl ;
00246     }
00247     catch( ... )
00248     {
00249         cerr << "Caught unknown exception during terminate" << endl ;
00250     }
00251 
00252     return BESBaseApp::terminate( sig ) ;
00253 }
00254 
00263 void
00264 BESModuleApp::dump( ostream &strm ) const
00265 {
00266     strm << BESIndent::LMarg << "BESModuleApp::dump - ("
00267                              << (void *)this << ")" << endl ;
00268     BESIndent::Indent() ;
00269     if( _module_list.size() )
00270     {
00271         strm << BESIndent::LMarg << "loaded modules:" << endl ;
00272         BESIndent::Indent() ;
00273         list< bes_module >::const_iterator i = _module_list.begin() ;
00274         list< bes_module >::const_iterator e = _module_list.end() ;
00275         for( ; i != e; i++ )
00276         {
00277             bes_module curr_mod = *i ;
00278             strm << BESIndent::LMarg << curr_mod._module_name << ": "
00279                  << curr_mod._module_library << endl ;
00280         }
00281         BESIndent::UnIndent() ;
00282     }
00283     else
00284     {
00285         strm << BESIndent::LMarg << "loaded modules: none" << endl ;
00286     }
00287     BESIndent::UnIndent() ;
00288 }
00289