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