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,2005 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 "BESException.h"
00040 #include "BESPluginFactory.h"
00041 #include "BESAbstractModule.h"
00042 #include "TheBESKeys.h"
00043 
00049 BESModuleApp::
00050 BESModuleApp(void) : BESBaseApp()
00051 {
00052 }
00053 
00059 BESModuleApp::
00060 ~BESModuleApp(void)
00061 {
00062 }
00063 
00070 int BESModuleApp::
00071 initialize(int argC, char **argV)
00072 {
00073     int retVal = BESBaseApp::initialize( argC, argV ) ;
00074     if( !retVal )
00075     {
00076         try
00077         {
00078             retVal = loadModules() ;
00079         }
00080         catch( BESException &e )
00081         {
00082             string newerr = "Error during module initialization: " ;
00083             newerr += e.get_message() ;
00084             cerr << newerr << endl ;
00085             retVal = 1 ;
00086         }
00087         catch( ... )
00088         {
00089             string newerr = "Error during module initialization: " ;
00090             newerr += "caught unknown exception" ;
00091             cerr << newerr << endl ;
00092             retVal = 1 ;
00093         }
00094     }
00095 
00096     return retVal ;
00097 }
00098 
00101 int
00102 BESModuleApp::loadModules()
00103 {
00104     int retVal = 0 ;
00105 
00106     bool found = false ;
00107     string mods = TheBESKeys::TheKeys()->get_key( "BES.modules", found ) ;
00108     if( mods != "" )
00109     {
00110         std::string::size_type start = 0 ;
00111         std::string::size_type comma = 0 ;
00112         bool done = false ;
00113         while( !done )
00114         {
00115             string mod ;
00116             comma = mods.find( ',', start ) ;
00117             if( comma == string::npos )
00118             {
00119                 mod = mods.substr( start, mods.length() - start ) ;
00120                 done = true ;
00121             }
00122             else
00123             {
00124                 mod = mods.substr( start, comma - start ) ;
00125             }
00126             string key = "BES.module." + mod ;
00127             string so = TheBESKeys::TheKeys()->get_key( key, found ) ;
00128             if( so == "" )
00129             {
00130                 cerr << "couldn't find the module for " << mod << endl ;
00131                 return 1 ;
00132             }
00133             bes_module new_mod ;
00134             new_mod._module_name = mod ;
00135             new_mod._module_library = so ;
00136             _module_list.push_back( new_mod ) ;
00137 
00138             start = comma + 1 ;
00139         }
00140 
00141         list< bes_module >::iterator i = _module_list.begin() ;
00142         list< bes_module >::iterator e = _module_list.end() ;
00143         for( ; i != e; i++ )
00144         {
00145             bes_module curr_mod = *i ;
00146             _moduleFactory.add_mapping( curr_mod._module_name, curr_mod._module_library ) ;
00147         }
00148 
00149         for( i = _module_list.begin(); i != e; i++ )
00150         {
00151             bes_module curr_mod = *i ;
00152             try
00153             {
00154                 string modname = curr_mod._module_name ;
00155                 BESAbstractModule *o = _moduleFactory.get( modname ) ;
00156                 o->initialize( modname ) ;
00157                 delete o ;
00158             }
00159             catch( BESException &e )
00160             {
00161                 cerr << "Caught plugin exception during initialization of "
00162                      << curr_mod._module_name << " module:" << endl << "    "
00163                      << e.get_message() << endl ;
00164                 retVal = 1 ;
00165                 break ;
00166             }
00167             catch( ... )
00168             {
00169                 cerr << "Caught unknown exception during initialization of "
00170                      << curr_mod._module_name << " module" << endl ;
00171                 retVal = 1 ;
00172                 break ;
00173             }
00174         }
00175     }
00176 
00177     return retVal ;
00178 }
00179 
00188 int BESModuleApp::
00189 terminate( int sig )
00190 {
00191     list< bes_module >::iterator i = _module_list.begin() ;
00192     list< bes_module >::iterator e = _module_list.end() ;
00193     try
00194     {
00195         for( i = _module_list.begin(); i != e; i++ )
00196         {
00197             bes_module curr_mod = *i ;
00198             string modname = curr_mod._module_name ;
00199             BESAbstractModule *o = _moduleFactory.get( modname ) ;
00200             if( o )
00201             {
00202                 o->terminate( modname ) ;
00203                 delete o ;
00204             }
00205         }
00206     }
00207     catch( BESException &e )
00208     {
00209         cerr << "Caught exception during module termination: "
00210              << e.get_message() << endl ;
00211     }
00212     catch( ... )
00213     {
00214         cerr << "Caught unknown exception during terminate" << endl ;
00215     }
00216 
00217     return BESBaseApp::terminate( sig ) ;
00218 }
00219 
00228 void BESModuleApp::
00229 dump( ostream &strm ) const
00230 {
00231     strm << BESIndent::LMarg << "BESModuleApp::dump - ("
00232                              << (void *)this << ")" << endl ;
00233     BESIndent::Indent() ;
00234     if( _module_list.size() )
00235     {
00236         strm << BESIndent::LMarg << "loaded modules:" << endl ;
00237         BESIndent::Indent() ;
00238         list< bes_module >::const_iterator i = _module_list.begin() ;
00239         list< bes_module >::const_iterator e = _module_list.end() ;
00240         for( ; i != e; i++ )
00241         {
00242             bes_module curr_mod = *i ;
00243             strm << BESIndent::LMarg << curr_mod._module_name << ": "
00244                  << curr_mod._module_library << endl ;
00245         }
00246         BESIndent::UnIndent() ;
00247     }
00248     else
00249     {
00250         strm << BESIndent::LMarg << "loaded modules: none" << endl ;
00251     }
00252     BESIndent::UnIndent() ;
00253 }
00254 

Generated on Wed Jan 2 06:01:18 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.4