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             string key = "BES.module." + (*i) ;
00145             string so ;
00146             try
00147             {
00148                 TheBESKeys::TheKeys()->get_value( key, so, found ) ;
00149             }
00150             catch( BESError &e )
00151             {
00152                 cerr << e.get_message() << endl ;
00153                 return 1 ;
00154             }
00155             if( so == "" )
00156             {
00157                 cerr << "couldn't find the module for " << (*i) << endl ;
00158                 return 1 ;
00159             }
00160             bes_module new_mod ;
00161             new_mod._module_name = (*i) ;
00162             new_mod._module_library = so ;
00163             _module_list.push_back( new_mod ) ;
00164         }
00165     }
00166 
00167     list< bes_module >::iterator mi = _module_list.begin() ;
00168     list< bes_module >::iterator me = _module_list.end() ;
00169     for( ; mi != me; mi++ )
00170     {
00171         bes_module curr_mod = *mi ;
00172         _moduleFactory.add_mapping( curr_mod._module_name, curr_mod._module_library ) ;
00173     }
00174 
00175     for( mi = _module_list.begin(); mi != me; mi++ )
00176     {
00177         bes_module curr_mod = *mi ;
00178         try
00179         {
00180             string modname = curr_mod._module_name ;
00181             BESAbstractModule *o = _moduleFactory.get( modname ) ;
00182             o->initialize( modname ) ;
00183             delete o ;
00184         }
00185         catch( BESError &e )
00186         {
00187             cerr << "Caught plugin exception during initialization of "
00188                  << curr_mod._module_name << " module:" << endl << "    "
00189                  << e.get_message() << endl ;
00190             retVal = 1 ;
00191             break ;
00192         }
00193         catch( ... )
00194         {
00195             cerr << "Caught unknown exception during initialization of "
00196                  << curr_mod._module_name << " module" << endl ;
00197             retVal = 1 ;
00198             break ;
00199         }
00200     }
00201 
00202     return retVal ;
00203 }
00204 
00213 int
00214 BESModuleApp::terminate( int sig )
00215 {
00216     list< bes_module >::iterator i = _module_list.begin() ;
00217     list< bes_module >::iterator e = _module_list.end() ;
00218     try
00219     {
00220         for( i = _module_list.begin(); i != e; i++ )
00221         {
00222             bes_module curr_mod = *i ;
00223             string modname = curr_mod._module_name ;
00224             BESAbstractModule *o = _moduleFactory.get( modname ) ;
00225             if( o )
00226             {
00227                 o->terminate( modname ) ;
00228                 delete o ;
00229             }
00230         }
00231     }
00232     catch( BESError &e )
00233     {
00234         cerr << "Caught exception during module termination: "
00235              << e.get_message() << endl ;
00236     }
00237     catch( ... )
00238     {
00239         cerr << "Caught unknown exception during terminate" << endl ;
00240     }
00241 
00242     return BESBaseApp::terminate( sig ) ;
00243 }
00244 
00253 void
00254 BESModuleApp::dump( ostream &strm ) const
00255 {
00256     strm << BESIndent::LMarg << "BESModuleApp::dump - ("
00257                              << (void *)this << ")" << endl ;
00258     BESIndent::Indent() ;
00259     if( _module_list.size() )
00260     {
00261         strm << BESIndent::LMarg << "loaded modules:" << endl ;
00262         BESIndent::Indent() ;
00263         list< bes_module >::const_iterator i = _module_list.begin() ;
00264         list< bes_module >::const_iterator e = _module_list.end() ;
00265         for( ; i != e; i++ )
00266         {
00267             bes_module curr_mod = *i ;
00268             strm << BESIndent::LMarg << curr_mod._module_name << ": "
00269                  << curr_mod._module_library << endl ;
00270         }
00271         BESIndent::UnIndent() ;
00272     }
00273     else
00274     {
00275         strm << BESIndent::LMarg << "loaded modules: none" << endl ;
00276     }
00277     BESIndent::UnIndent() ;
00278 }
00279