BESModuleApp.cc
Go to the documentation of this file.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 "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 string mods = TheBESKeys::TheKeys()->get_key( "BES.modules", found ) ;
00109 if( mods != "" )
00110 {
00111 list<string> mod_list ;
00112 BESUtil::explode( ',', mods, mod_list ) ;
00113
00114 list<string>::iterator i = mod_list.begin() ;
00115 list<string>::iterator e = mod_list.end() ;
00116 for( ; i != e; i++ )
00117 {
00118 string key = "BES.module." + (*i) ;
00119 string so = TheBESKeys::TheKeys()->get_key( key, found ) ;
00120 if( so == "" )
00121 {
00122 cerr << "couldn't find the module for " << (*i) << endl ;
00123 return 1 ;
00124 }
00125 bes_module new_mod ;
00126 new_mod._module_name = (*i) ;
00127 new_mod._module_library = so ;
00128 _module_list.push_back( new_mod ) ;
00129 }
00130
00131 list< bes_module >::iterator mi = _module_list.begin() ;
00132 list< bes_module >::iterator me = _module_list.end() ;
00133 for( ; mi != me; mi++ )
00134 {
00135 bes_module curr_mod = *mi ;
00136 _moduleFactory.add_mapping( curr_mod._module_name, curr_mod._module_library ) ;
00137 }
00138
00139 for( mi = _module_list.begin(); mi != me; mi++ )
00140 {
00141 bes_module curr_mod = *mi ;
00142 try
00143 {
00144 string modname = curr_mod._module_name ;
00145 BESAbstractModule *o = _moduleFactory.get( modname ) ;
00146 o->initialize( modname ) ;
00147 delete o ;
00148 }
00149 catch( BESError &e )
00150 {
00151 cerr << "Caught plugin exception during initialization of "
00152 << curr_mod._module_name << " module:" << endl << " "
00153 << e.get_message() << endl ;
00154 retVal = 1 ;
00155 break ;
00156 }
00157 catch( ... )
00158 {
00159 cerr << "Caught unknown exception during initialization of "
00160 << curr_mod._module_name << " module" << endl ;
00161 retVal = 1 ;
00162 break ;
00163 }
00164 }
00165 }
00166
00167 return retVal ;
00168 }
00169
00178 int
00179 BESModuleApp::terminate( int sig )
00180 {
00181 list< bes_module >::iterator i = _module_list.begin() ;
00182 list< bes_module >::iterator e = _module_list.end() ;
00183 try
00184 {
00185 for( i = _module_list.begin(); i != e; i++ )
00186 {
00187 bes_module curr_mod = *i ;
00188 string modname = curr_mod._module_name ;
00189 BESAbstractModule *o = _moduleFactory.get( modname ) ;
00190 if( o )
00191 {
00192 o->terminate( modname ) ;
00193 delete o ;
00194 }
00195 }
00196 }
00197 catch( BESError &e )
00198 {
00199 cerr << "Caught exception during module termination: "
00200 << e.get_message() << endl ;
00201 }
00202 catch( ... )
00203 {
00204 cerr << "Caught unknown exception during terminate" << endl ;
00205 }
00206
00207 return BESBaseApp::terminate( sig ) ;
00208 }
00209
00218 void
00219 BESModuleApp::dump( ostream &strm ) const
00220 {
00221 strm << BESIndent::LMarg << "BESModuleApp::dump - ("
00222 << (void *)this << ")" << endl ;
00223 BESIndent::Indent() ;
00224 if( _module_list.size() )
00225 {
00226 strm << BESIndent::LMarg << "loaded modules:" << endl ;
00227 BESIndent::Indent() ;
00228 list< bes_module >::const_iterator i = _module_list.begin() ;
00229 list< bes_module >::const_iterator e = _module_list.end() ;
00230 for( ; i != e; i++ )
00231 {
00232 bes_module curr_mod = *i ;
00233 strm << BESIndent::LMarg << curr_mod._module_name << ": "
00234 << curr_mod._module_library << endl ;
00235 }
00236 BESIndent::UnIndent() ;
00237 }
00238 else
00239 {
00240 strm << BESIndent::LMarg << "loaded modules: none" << endl ;
00241 }
00242 BESIndent::UnIndent() ;
00243 }
00244