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