00001
00002 #include "GaudiKernel/AlgTool.h"
00003 #include "GaudiKernel/IMessageSvc.h"
00004 #include "GaudiKernel/ISvcLocator.h"
00005 #include "GaudiKernel/IJobOptionsSvc.h"
00006
00007 #include "GaudiKernel/Algorithm.h"
00008 #include "GaudiKernel/Service.h"
00009 #include "GaudiKernel/Auditor.h"
00010 #include "GaudiKernel/System.h"
00011 #include "GaudiKernel/GaudiException.h"
00012 #include "GaudiKernel/ServiceLocatorHelper.h"
00013 #include "GaudiKernel/ThreadGaudi.h"
00014 #include "GaudiKernel/Guards.h"
00015
00016
00017 StatusCode AlgTool::queryInterface
00018 ( const InterfaceID& riid ,
00019 void** ppvi )
00020
00021 {
00022 if ( 0 == ppvi ) { return StatusCode::FAILURE ; }
00023 StatusCode sc = base_class::queryInterface(riid,ppvi);
00024 if (sc.isSuccess()) {
00025 return sc;
00026 }
00027 else {
00028 for ( InterfaceList::iterator it = m_interfaceList.begin() ;
00029 m_interfaceList.end() != it ; ++it )
00030 {
00031 if ( !it->first.versionMatch ( riid ) ) { continue ; }
00032
00033 *ppvi = it->second ;
00034 addRef() ;
00035 return SUCCESS ;
00036 }
00037 *ppvi = 0 ;
00038 return NO_INTERFACE ;
00039 }
00040
00041 }
00042
00043 void AlgTool::declInterface( const InterfaceID& iid, void* ii)
00044
00045 {
00046 m_interfaceList.push_back(std::make_pair(iid, ii));
00047 }
00048
00049
00050
00051 const std::string& AlgTool::name() const
00052
00053 {
00054 return m_name;
00055 }
00056
00057
00058 const std::string& AlgTool::type() const
00059
00060 {
00061 return m_type;
00062 }
00063
00064
00065 const IInterface* AlgTool::parent() const
00066
00067 {
00068 return m_parent;
00069 }
00070
00071
00072 ISvcLocator* AlgTool::serviceLocator() const
00073
00074 {
00075 return m_svcLocator;
00076 }
00077
00078
00079 IMessageSvc* AlgTool::msgSvc() const
00080
00081 {
00082 return m_messageSvc;
00083 }
00084
00085
00086 IToolSvc* AlgTool::toolSvc() const
00087
00088 {
00089 if ( 0 == m_ptoolSvc ) {
00090 StatusCode sc = service( "ToolSvc", m_ptoolSvc, true );
00091 if( sc.isFailure() ) {
00092 throw GaudiException("Service [ToolSvc] not found", name(), sc);
00093 }
00094 }
00095 return m_ptoolSvc;
00096 }
00097
00098
00099 StatusCode AlgTool::setProperty(const Property& p)
00100
00101 {
00102 return m_propertyMgr->setProperty(p);
00103 }
00104
00105
00106 StatusCode AlgTool::setProperty(const std::string& s)
00107
00108 {
00109 return m_propertyMgr->setProperty(s);
00110 }
00111
00112
00113 StatusCode AlgTool::setProperty(const std::string& n, const std::string& v)
00114
00115 {
00116 return m_propertyMgr->setProperty(n,v);
00117 }
00118
00119
00120 StatusCode AlgTool::getProperty(Property* p) const
00121
00122 {
00123 return m_propertyMgr->getProperty(p);
00124 }
00125
00126
00127 const Property& AlgTool::getProperty(const std::string& n) const
00128 {
00129 return m_propertyMgr->getProperty(n);
00130 }
00131
00132
00133 StatusCode AlgTool::getProperty(const std::string& n, std::string& v ) const
00134
00135 {
00136 return m_propertyMgr->getProperty(n,v);
00137 }
00138
00139
00140 const std::vector<Property*>& AlgTool::getProperties() const
00141
00142 {
00143 return m_propertyMgr->getProperties();
00144 }
00145
00146
00147 StatusCode AlgTool::setProperties()
00148
00149 {
00150 if( m_svcLocator == 0) {
00151 return StatusCode::FAILURE;
00152 }
00153 SmartIF<IJobOptionsSvc> jos(m_svcLocator->service("JobOptionsSvc"));
00154 if( !jos.isValid() ) return StatusCode::FAILURE;
00155
00156
00157 StatusCode sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this );
00158 if( sc.isFailure() ) return StatusCode::FAILURE;
00159
00160
00161 if (isGaudiThreaded(name())) {
00162 if(jos->setMyProperties( name(), this ).isFailure()) {
00163 return StatusCode::FAILURE;
00164 }
00165 }
00166
00167
00168 if ( 0 != m_messageSvc )
00169 {
00170 if ( MSG::NIL != m_outputLevel )
00171 { m_messageSvc -> setOutputLevel ( name () , m_outputLevel ) ; }
00172 m_outputLevel = m_messageSvc -> outputLevel ( name () ) ;
00173 }
00174
00175 return StatusCode::SUCCESS;
00176 }
00177
00178
00179 AlgTool::AlgTool( const std::string& type,
00180 const std::string& name,
00181 const IInterface* parent)
00182
00183 : m_outputLevel ( MSG::NIL )
00184 , m_type ( type )
00185 , m_name ( name )
00186 , m_parent ( parent )
00187 , m_svcLocator ( 0 )
00188 , m_messageSvc ( 0 )
00189 , m_ptoolSvc ( 0 )
00190 , m_pMonitorSvc ( NULL )
00191 , m_propertyMgr ( new PropertyMgr() )
00192 , m_interfaceList ( )
00193 , m_threadID ( )
00194 , m_pAuditorSvc ( 0 )
00195 , m_auditInit ( false )
00196 , m_state ( Gaudi::StateMachine::CONFIGURED )
00197 , m_targetState ( Gaudi::StateMachine::CONFIGURED )
00198 {
00199 addRef();
00200
00201 declareProperty( "MonitorService", m_monitorSvcName = "MonitorSvc" );
00202
00203 {
00204 const Property* _p = Gaudi::Utils::getProperty ( parent , "OutputLevel") ;
00205 if ( 0 != _p ) { m_outputLevel.assign( *_p ) ; }
00206 declareProperty ( "OutputLevel" , m_outputLevel ) ;
00207 m_outputLevel.declareUpdateHandler(&AlgTool::initOutputLevel, this);
00208 }
00209
00210 IInterface* _p = const_cast<IInterface*> ( parent ) ;
00211
00212 if ( Algorithm* _alg = dynamic_cast<Algorithm*> ( _p ) )
00213 {
00214 m_svcLocator = _alg -> serviceLocator () ;
00215 m_messageSvc = _alg -> msgSvc () ;
00216 m_threadID = getGaudiThreadIDfromName ( _alg -> name() ) ;
00217 }
00218 else if ( Service* _svc = dynamic_cast<Service*> ( _p ) )
00219 {
00220 m_svcLocator = _svc -> serviceLocator () ;
00221 m_messageSvc = _svc -> msgSvc () ;
00222 m_threadID = getGaudiThreadIDfromName ( _svc -> name() ) ;
00223 }
00224 else if ( AlgTool* _too = dynamic_cast<AlgTool*> ( _p ) )
00225 {
00226 m_svcLocator = _too -> m_svcLocator;
00227 m_messageSvc = _too -> m_messageSvc;
00228 m_threadID = getGaudiThreadIDfromName ( _too ->m_threadID ) ;
00229 }
00230 else if ( Auditor* _aud = dynamic_cast<Auditor*> ( _p ) )
00231 {
00232 m_svcLocator = _aud -> serviceLocator() ;
00233 m_messageSvc = _aud -> msgSvc() ;
00234 m_threadID = getGaudiThreadIDfromName ( _aud -> name() ) ;
00235 }
00236 else
00237 {
00238 throw GaudiException
00239 ( "Failure to create tool '"
00240 + type + "/" + name + "': illegal parent type '"
00241 + System::typeinfoName(typeid(*_p)) + "'", "AlgTool", 0 );
00242 }
00243
00244
00245 {
00246 SmartIF<IProperty> appMgr(m_svcLocator->service("ApplicationMgr"));
00247 if ( !appMgr.isValid() ) {
00248 throw GaudiException("Could not locate ApplicationMgr","AlgTool",0);
00249 }
00250 const Property* p = Gaudi::Utils::getProperty( appMgr , "AuditTools");
00251 if ( 0 != p ) { m_auditInit.assign ( *p ) ; }
00252 declareProperty ( "AuditTools", m_auditInit );
00253 bool audit = m_auditInit.value();
00254
00255 declareProperty ( "AuditInitialize" , m_auditorInitialize = audit ) ;
00256 declareProperty ( "AuditStart" , m_auditorStart = audit ) ;
00257 declareProperty ( "AuditStop" , m_auditorStop = audit ) ;
00258 declareProperty ( "AuditFinalize" , m_auditorFinalize = audit ) ;
00259 }
00260
00261
00262 if ( m_threadID.empty() )
00263 { m_threadID = getGaudiThreadIDfromName ( AlgTool::name() ) ; }
00264 }
00265
00266
00267 StatusCode AlgTool::sysInitialize() {
00268
00269 StatusCode sc;
00270
00271 try {
00272 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::INITIALIZE,m_state);
00273 Gaudi::Guards::AuditorGuard guard(this,
00274
00275 (m_auditorInitialize) ? auditorSvc() : 0,
00276 IAuditor::Initialize);
00277 sc = initialize();
00278 if (sc.isSuccess())
00279 m_state = m_targetState;
00280 return sc;
00281 }
00282 catch( const GaudiException& Exception ) {
00283 MsgStream log ( msgSvc() , name() + ".sysInitialize()" );
00284 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
00285 << " is caught " << endmsg;
00286 log << MSG::ERROR << Exception << endmsg;
00287 }
00288 catch( const std::exception& Exception ) {
00289 MsgStream log ( msgSvc() , name() + ".sysInitialize()" );
00290 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
00291 log << MSG::ERROR << Exception.what() << endmsg;
00292 }
00293 catch( ... ) {
00294 MsgStream log ( msgSvc() , name() + ".sysInitialize()" );
00295 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
00296 }
00297 return StatusCode::FAILURE ;
00298
00299 }
00300
00301
00302 StatusCode AlgTool::initialize()
00303
00304 {
00305
00306
00307
00308 return StatusCode::SUCCESS;
00309 }
00310
00311
00312 StatusCode AlgTool::sysStart() {
00313
00314 StatusCode sc;
00315
00316 try {
00317 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::START,m_state);
00318 Gaudi::Guards::AuditorGuard guard(this,
00319
00320 (m_auditorStart) ? auditorSvc() : 0,
00321 IAuditor::Start);
00322 sc = start();
00323 if (sc.isSuccess())
00324 m_state = m_targetState;
00325 return sc;
00326 }
00327 catch( const GaudiException& Exception ) {
00328 MsgStream log ( msgSvc() , name() + ".sysStart()" );
00329 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
00330 << " is caught " << endmsg;
00331 log << MSG::ERROR << Exception << endmsg;
00332 }
00333 catch( const std::exception& Exception ) {
00334 MsgStream log ( msgSvc() , name() + ".sysStart()" );
00335 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
00336 log << MSG::ERROR << Exception.what() << endmsg;
00337 }
00338 catch( ... ) {
00339 MsgStream log ( msgSvc() , name() + ".sysStart()" );
00340 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
00341 }
00342 return StatusCode::FAILURE ;
00343
00344 }
00345
00346
00347 StatusCode AlgTool::start()
00348
00349 {
00350
00351 return StatusCode::SUCCESS;
00352 }
00353
00354
00355 StatusCode AlgTool::sysStop() {
00356
00357 StatusCode sc;
00358
00359 try {
00360 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::STOP,m_state);
00361 Gaudi::Guards::AuditorGuard guard(this,
00362
00363 (m_auditorStop) ? auditorSvc() : 0,
00364 IAuditor::Stop);
00365 sc = stop();
00366 if (sc.isSuccess())
00367 m_state = m_targetState;
00368 return sc;
00369 }
00370 catch( const GaudiException& Exception ) {
00371 MsgStream log ( msgSvc() , name() + ".sysStop()" );
00372 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
00373 << " is caught " << endmsg;
00374 log << MSG::ERROR << Exception << endmsg;
00375 }
00376 catch( const std::exception& Exception ) {
00377 MsgStream log ( msgSvc() , name() + ".sysStop()" );
00378 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
00379 log << MSG::ERROR << Exception.what() << endmsg;
00380 }
00381 catch( ... ) {
00382 MsgStream log ( msgSvc() , name() + ".sysStop()" );
00383 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
00384 }
00385 return StatusCode::FAILURE ;
00386
00387 }
00388
00389
00390 StatusCode AlgTool::stop()
00391
00392 {
00393
00394 return StatusCode::SUCCESS;
00395 }
00396
00397
00398 StatusCode AlgTool::sysFinalize() {
00399
00400
00401 StatusCode sc;
00402
00403 try {
00404 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::FINALIZE,m_state);
00405 Gaudi::Guards::AuditorGuard guard(this,
00406
00407 (m_auditorFinalize) ? auditorSvc() : 0,
00408 IAuditor::Finalize);
00409 sc = finalize();
00410 if (sc.isSuccess())
00411 m_state = m_targetState;
00412 return sc;
00413 }
00414 catch( const GaudiException& Exception ) {
00415 MsgStream log ( msgSvc() , name() + ".sysFinalize()" );
00416 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
00417 << " is caught " << endmsg;
00418 log << MSG::ERROR << Exception << endmsg;
00419 }
00420 catch( const std::exception& Exception ) {
00421 MsgStream log ( msgSvc() , name() + ".sysFinalize()" );
00422 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
00423 log << MSG::ERROR << Exception.what() << endmsg;
00424 }
00425 catch( ... ) {
00426 MsgStream log ( msgSvc() , name() + ".sysFinalize()" );
00427 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
00428 }
00429 return StatusCode::FAILURE;
00430
00431 }
00432
00433 StatusCode AlgTool::finalize()
00434
00435 {
00436
00437 return StatusCode::SUCCESS;
00438 }
00439
00440
00441 StatusCode AlgTool::sysReinitialize() {
00442
00443 StatusCode sc;
00444
00445
00446 if ( Gaudi::StateMachine::INITIALIZED != FSMState() ) {
00447 MsgStream log ( msgSvc() , name() );
00448 log << MSG::ERROR
00449 << "sysReinitialize(): cannot reinitialize tool not initialized"
00450 << endmsg;
00451 return StatusCode::FAILURE;
00452 }
00453
00454 try {
00455 Gaudi::Guards::AuditorGuard guard(this,
00456
00457 (m_auditorReinitialize) ? auditorSvc() : 0,
00458 IAuditor::ReInitialize);
00459 sc = reinitialize();
00460 return sc;
00461 }
00462 catch( const GaudiException& Exception ) {
00463 MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
00464 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
00465 << " is caught" << endmsg;
00466 log << MSG::ERROR << Exception << endmsg;
00467 }
00468 catch( const std::exception& Exception ) {
00469 MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
00470 log << MSG::FATAL << " Standard std::exception is caught" << endmsg;
00471 log << MSG::ERROR << Exception.what() << endmsg;
00472 }
00473 catch( ... ) {
00474 MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
00475 log << MSG::FATAL << "UNKNOWN Exception is caught" << endmsg;
00476 }
00477 return StatusCode::FAILURE ;
00478
00479 }
00480
00481
00482 StatusCode AlgTool::reinitialize()
00483
00484 {
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503 return StatusCode::SUCCESS;
00504 }
00505
00506
00507 StatusCode AlgTool::sysRestart() {
00508
00509 StatusCode sc;
00510
00511
00512 if ( Gaudi::StateMachine::RUNNING != FSMState() ) {
00513 MsgStream log ( msgSvc() , name() );
00514 log << MSG::ERROR
00515 << "sysRestart(): cannot reinitialize tool not started"
00516 << endmsg;
00517 return StatusCode::FAILURE;
00518 }
00519
00520 try {
00521 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::START,m_state);
00522 Gaudi::Guards::AuditorGuard guard(this,
00523
00524 (m_auditorRestart) ? auditorSvc() : 0,
00525 IAuditor::ReStart);
00526 sc = restart();
00527 return sc;
00528 }
00529 catch( const GaudiException& Exception ) {
00530 MsgStream log ( msgSvc() , name() + ".sysRestart()" );
00531 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
00532 << " is caught" << endmsg;
00533 log << MSG::ERROR << Exception << endmsg;
00534 }
00535 catch( const std::exception& Exception ) {
00536 MsgStream log ( msgSvc() , name() + ".sysRestart()" );
00537 log << MSG::FATAL << " Standard std::exception is caught" << endmsg;
00538 log << MSG::ERROR << Exception.what() << endmsg;
00539 }
00540 catch( ... ) {
00541 MsgStream log ( msgSvc() , name() + ".sysRestart()" );
00542 log << MSG::FATAL << "UNKNOWN Exception is caught" << endmsg;
00543 }
00544 return StatusCode::FAILURE ;
00545
00546 }
00547
00548
00549 StatusCode AlgTool::restart()
00550
00551 {
00552
00553 StatusCode sc = stop();
00554 if (sc.isFailure()) {
00555 MsgStream log ( msgSvc() , name() );
00556 log << MSG::ERROR << "restart(): cannot be stopped" << endmsg;
00557 return sc;
00558 }
00559 sc = start();
00560 if (sc.isFailure()) {
00561 MsgStream log ( msgSvc() , name() );
00562 log << MSG::ERROR << "restart(): cannot be started" << endmsg;
00563 return sc;
00564 }
00565 return StatusCode::SUCCESS;
00566 }
00567
00568
00569 AlgTool::~AlgTool()
00570
00571 {
00572 delete m_propertyMgr;
00573 if( m_ptoolSvc ) m_ptoolSvc->release();
00574 if( m_pAuditorSvc ) m_pAuditorSvc->release();
00575 if ( m_pMonitorSvc ) { m_pMonitorSvc->undeclareAll(this); m_pMonitorSvc->release(); }
00576 }
00577
00578
00580 StatusCode
00581 AlgTool::service_i(const std::string& svcName,
00582 bool createIf,
00583 const InterfaceID& iid,
00584 void** ppSvc) const {
00585 const ServiceLocatorHelper helper(*serviceLocator(), *this);
00586 return helper.getService(svcName, createIf, iid, ppSvc);
00587 }
00588
00589
00590 StatusCode
00591 AlgTool::service_i(const std::string& svcType,
00592 const std::string& svcName,
00593 const InterfaceID& iid,
00594 void** ppSvc) const {
00595 const ServiceLocatorHelper helper(*serviceLocator(), *this);
00596 return helper.createService(svcType, svcName, iid, ppSvc);
00597 }
00598
00599 SmartIF<IService> AlgTool::service(const std::string& name, const bool createIf, const bool quiet) const {
00600 const ServiceLocatorHelper helper(*serviceLocator(), *this);
00601 return helper.service(name, quiet, createIf);
00602 }
00603
00604
00605 IAuditorSvc* AlgTool::auditorSvc() const {
00606
00607 if ( 0 == m_pAuditorSvc ) {
00608 StatusCode sc = service( "AuditorSvc", m_pAuditorSvc, true );
00609 if( sc.isFailure() ) {
00610 throw GaudiException("Service [AuditorSvc] not found", name(), sc);
00611 }
00612 }
00613 return m_pAuditorSvc;
00614 }
00615
00616
00617
00618 void AlgTool::initOutputLevel(Property& ) {
00619
00620
00621 }
00622