00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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 bool any_loaded = false ;
00241 for( ; i != e; i++ )
00242 {
00243 bes_module curr_mod = *i ;
00244 strm << BESIndent::LMarg << curr_mod._module_name << ": "
00245 << curr_mod._module_library << endl ;
00246 any_loaded = true ;
00247 }
00248 BESIndent::UnIndent() ;
00249 }
00250 else
00251 {
00252 strm << BESIndent::LMarg << "loaded modules: none" << endl ;
00253 }
00254 BESIndent::UnIndent() ;
00255 }
00256