![]() |
|
|
Generated: 18 Jul 2008 |
00001 // $Id: Service.cpp,v 1.34 2008/07/15 11:57:09 marcocle Exp $ 00002 00003 // Include Files 00004 #include "GaudiKernel/Service.h" 00005 #include "GaudiKernel/PropertyMgr.h" 00006 #include "GaudiKernel/IMessageSvc.h" 00007 #include "GaudiKernel/MsgStream.h" 00008 #include "GaudiKernel/ISvcLocator.h" 00009 #include "GaudiKernel/ISvcManager.h" 00010 #include "GaudiKernel/IAuditorSvc.h" 00011 #include "GaudiKernel/IJobOptionsSvc.h" 00012 #include "GaudiKernel/GaudiException.h" 00013 #include "GaudiKernel/ServiceLocatorHelper.h" 00014 #include "GaudiKernel/ThreadGaudi.h" 00015 #include "GaudiKernel/Guards.h" 00016 00017 using std::string; 00018 00019 //--- IInterface::addRef 00020 unsigned long Service::addRef() { 00021 m_refCount++; 00022 return m_refCount; 00023 } 00024 00025 //--- IInterface::release 00026 unsigned long Service::release() { 00027 unsigned long count = --m_refCount; 00028 if( count == 0) { 00029 if (m_svcManager!=0) { 00030 m_svcManager->removeService(this).ignore(); 00031 } 00032 delete this; 00033 } 00034 return count; 00035 } 00036 00037 //--- IInterface::queryInterface 00038 StatusCode Service::queryInterface 00039 ( const InterfaceID& riid, 00040 void** ppvi ) 00041 { 00042 if ( 0 == ppvi ) { return StatusCode::FAILURE ; } // RETURN 00043 // 00044 if ( IService ::interfaceID() . versionMatch ( riid ) ) 00045 { *ppvi = static_cast<IService*> ( this ) ; } 00046 else if ( IProperty ::interfaceID() . versionMatch ( riid ) ) 00047 { *ppvi = static_cast<IProperty*> ( this ) ; } 00048 else if ( IStateful ::interfaceID() . versionMatch ( riid ) ) 00049 { *ppvi = static_cast<IStateful*> ( this ) ; } 00050 else if ( INamedInterface ::interfaceID() . versionMatch ( riid ) ) 00051 { *ppvi = static_cast<INamedInterface*> ( this ) ; } 00052 else if ( IInterface ::interfaceID() . versionMatch ( riid ) ) 00053 { *ppvi = static_cast<IInterface*> ( this ) ; } 00054 else { *ppvi = 0 ; return NO_INTERFACE ; } // RETURN 00055 // increment reference counter 00056 addRef(); 00057 // 00058 return SUCCESS; 00059 } 00060 00061 // IService::sysInitialize 00062 StatusCode Service::sysInitialize() { 00063 StatusCode sc; 00064 00065 try { 00066 m_targetState = Gaudi::StateMachine::INITIALIZED; 00067 Gaudi::Guards::AuditorGuard guard(this, 00068 // check if we want to audit the initialize 00069 (m_auditorInitialize) ? auditorSvc() : 0, 00070 IAuditor::Initialize); 00071 sc = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED 00072 if (sc.isSuccess()) 00073 m_state = m_targetState; 00074 return sc; 00075 } 00076 catch ( const GaudiException& Exception ) { 00077 MsgStream log ( msgSvc() , name() ); 00078 log << MSG::FATAL << "in sysInitialize(): exception with tag=" << Exception.tag() 00079 << " is caught" << endreq; 00080 log << MSG::ERROR << Exception << endreq; 00081 // Stat stat( chronoSvc() , Exception.tag() ); 00082 } 00083 catch( const std::exception& Exception ) { 00084 MsgStream log ( msgSvc() , name() ); 00085 log << MSG::FATAL << "in sysInitialize(): standard std::exception is caught" << endreq; 00086 log << MSG::ERROR << Exception.what() << endreq; 00087 // Stat stat( chronoSvc() , "*std::exception*" ); 00088 } 00089 catch(...) { 00090 MsgStream log ( msgSvc() , name() ); 00091 log << MSG::FATAL << "in sysInitialize(): UNKNOWN Exception is caught" << endreq; 00092 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00093 } 00094 00095 return StatusCode::FAILURE; 00096 } 00097 00098 00099 //--- IService::initialize 00100 StatusCode Service::initialize() { 00101 MsgStream log(msgSvc(),name()); 00102 00103 // Set the Algorithm's properties 00104 StatusCode sc = setProperties(); 00105 00106 log << MSG::DEBUG << "Service base class initialized successfully" << endmsg; 00107 m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::CONFIGURE,m_state); 00108 return sc ; 00109 } 00110 00111 // IService::sysStart 00112 StatusCode Service::sysStart() { 00113 StatusCode sc; 00114 00115 try { 00116 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::START,m_state); 00117 Gaudi::Guards::AuditorGuard guard(this, 00118 // check if we want to audit the initialize 00119 (m_auditorStart) ? auditorSvc() : 0, 00120 IAuditor::Start); 00121 sc = start(); 00122 if (sc.isSuccess()) 00123 m_state = m_targetState; 00124 return sc; 00125 } 00126 catch ( const GaudiException& Exception ) { 00127 MsgStream log ( msgSvc() , name() ); 00128 log << MSG::FATAL << "in sysStart(): exception with tag=" << Exception.tag() 00129 << " is caught" << endreq; 00130 log << MSG::ERROR << Exception << endreq; 00131 // Stat stat( chronoSvc() , Exception.tag() ); 00132 } 00133 catch( const std::exception& Exception ) { 00134 MsgStream log ( msgSvc() , name() ); 00135 log << MSG::FATAL << "in sysStart(): standard std::exception is caught" << endreq; 00136 log << MSG::ERROR << Exception.what() << endreq; 00137 // Stat stat( chronoSvc() , "*std::exception*" ); 00138 } 00139 catch(...) { 00140 MsgStream log ( msgSvc() , name() ); 00141 log << MSG::FATAL << "in sysStart(): UNKNOWN Exception is caught" << endreq; 00142 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00143 } 00144 00145 return StatusCode::FAILURE; 00146 } 00147 00148 // IService::sysStop 00149 StatusCode Service::sysStop() { 00150 StatusCode sc; 00151 00152 try { 00153 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::STOP,m_state); 00154 Gaudi::Guards::AuditorGuard guard(this, 00155 // check if we want to audit the initialize 00156 (m_auditorStop) ? auditorSvc() : 0, 00157 IAuditor::Stop); 00158 sc = stop(); 00159 if (sc.isSuccess()) 00160 m_state = m_targetState; 00161 return sc; 00162 } 00163 catch ( const GaudiException& Exception ) { 00164 MsgStream log ( msgSvc() , name() ); 00165 log << MSG::FATAL << "in sysStop(): exception with tag=" << Exception.tag() 00166 << " is caught" << endreq; 00167 log << MSG::ERROR << Exception << endreq; 00168 // Stat stat( chronoSvc() , Exception.tag() ); 00169 } 00170 catch( const std::exception& Exception ) { 00171 MsgStream log ( msgSvc() , name() ); 00172 log << MSG::FATAL << "in sysStop(): standard std::exception is caught" << endreq; 00173 log << MSG::ERROR << Exception.what() << endreq; 00174 // Stat stat( chronoSvc() , "*std::exception*" ); 00175 } 00176 catch(...) { 00177 MsgStream log ( msgSvc() , name() ); 00178 log << MSG::FATAL << "in sysStop(): UNKNOWN Exception is caught" << endreq; 00179 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00180 } 00181 00182 return StatusCode::FAILURE; 00183 } 00184 00185 00186 //--- IService::stop 00187 StatusCode Service::stop() { 00188 // stub implementation 00189 return StatusCode::SUCCESS; 00190 } 00191 00192 //--- IService::start 00193 StatusCode Service::start() { 00194 // stub implementation 00195 return StatusCode::SUCCESS; 00196 } 00197 00198 //--- IService::sysFinalize 00199 StatusCode Service::sysFinalize() { 00200 00201 StatusCode sc(StatusCode::FAILURE); 00202 00203 try { 00204 m_targetState = Gaudi::StateMachine::OFFLINE; 00205 Gaudi::Guards::AuditorGuard guard(this, 00206 // check if we want to audit the initialize 00207 (m_auditorFinalize) ? auditorSvc() : 0, 00208 IAuditor::Finalize); 00209 sc = finalize(); 00210 if (sc.isSuccess()) 00211 m_state = m_targetState; 00212 } 00213 catch( const GaudiException& Exception ) { 00214 MsgStream log ( msgSvc() , name() + ".sysFinalize()" ); 00215 log << MSG::FATAL << " Exception with tag=" << Exception.tag() 00216 << " is caught " << endreq; 00217 log << MSG::ERROR << Exception << endreq; 00218 // Stat stat( chronoSvc() , Exception.tag() ) ; 00219 } 00220 catch( const std::exception& Exception ) { 00221 MsgStream log ( msgSvc() , name() + ".sysFinalize()" ); 00222 log << MSG::FATAL << " Standard std::exception is caught " << endreq; 00223 log << MSG::ERROR << Exception.what() << endreq; 00224 // Stat stat( chronoSvc() , "*std::exception*" ) ; 00225 } 00226 catch( ... ) { 00227 MsgStream log ( msgSvc() , name() + ".sysFinalize()" ); 00228 log << MSG::FATAL << "UNKNOWN Exception is caught " << endreq; 00229 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00230 } 00231 00232 if( m_messageSvc ) { 00233 m_messageSvc->release(); 00234 m_messageSvc = 0; 00235 } 00236 if( m_pAuditorSvc ) { 00237 m_pAuditorSvc->release(); 00238 m_pAuditorSvc = 0; 00239 } 00240 00241 return sc ; 00242 00243 } 00244 00245 //--- IService::finalize 00246 StatusCode Service::finalize() { 00247 //MsgStream log(msgSvc(),name()); 00248 //m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state); 00249 return StatusCode::SUCCESS; 00250 } 00251 00252 //--- IService::sysReinitialize 00253 StatusCode Service::sysReinitialize() { 00254 00255 StatusCode sc; 00256 00257 // Check that the current status is the correct one. 00258 if ( Gaudi::StateMachine::INITIALIZED != FSMState() ) { 00259 MsgStream log ( msgSvc() , name() ); 00260 log << MSG::ERROR 00261 << "sysReinitialize(): cannot reinitialize service not initialized" 00262 << endreq; 00263 return StatusCode::FAILURE; 00264 } 00265 00266 try { 00267 00268 Gaudi::Guards::AuditorGuard guard(this, 00269 // check if we want to audit the initialize 00270 (m_auditorReinitialize) ? auditorSvc() : 0, 00271 IAuditor::ReInitialize); 00272 sc = reinitialize(); 00273 return sc; 00274 } 00275 catch( const GaudiException& Exception ) { 00276 MsgStream log ( msgSvc() , name() + ".sysReinitialize()" ); 00277 log << MSG::FATAL << " Exception with tag=" << Exception.tag() 00278 << " is caught " << endreq; 00279 log << MSG::ERROR << Exception << endreq; 00280 // Stat stat( chronoSvc() , Exception.tag() ) ; 00281 } 00282 catch( const std::exception& Exception ) { 00283 MsgStream log ( msgSvc() , name() + ".sysReinitialize()" ); 00284 log << MSG::FATAL << " Standard std::exception is caught " << endreq; 00285 log << MSG::ERROR << Exception.what() << endreq; 00286 // Stat stat( chronoSvc() , "*std::exception*" ) ; 00287 } 00288 catch( ... ) { 00289 MsgStream log ( msgSvc() , name() + ".sysReinitialize()" ); 00290 log << MSG::FATAL << "UNKNOWN Exception is caught " << endreq; 00291 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00292 } 00293 return StatusCode::FAILURE ; 00294 00295 } 00296 00297 //--- IService::sysRestart 00298 StatusCode Service::sysRestart() { 00299 00300 StatusCode sc; 00301 00302 // Check that the current status is the correct one. 00303 if ( Gaudi::StateMachine::RUNNING != FSMState() ) { 00304 MsgStream log ( msgSvc() , name() ); 00305 log << MSG::ERROR 00306 << "sysRestart(): cannot restart service in state " << FSMState() 00307 << " -- must be RUNNING " 00308 << endreq; 00309 return StatusCode::FAILURE; 00310 } 00311 00312 try { 00313 00314 Gaudi::Guards::AuditorGuard guard(this, 00315 // check if we want to audit the initialize 00316 (m_auditorRestart) ? auditorSvc() : 0, 00317 IAuditor::ReStart); 00318 sc = restart(); 00319 return sc; 00320 } 00321 catch( const GaudiException& Exception ) { 00322 MsgStream log ( msgSvc() , name() + ".sysRestart()" ); 00323 log << MSG::FATAL << " Exception with tag=" << Exception.tag() 00324 << " is caught " << endreq; 00325 log << MSG::ERROR << Exception << endreq; 00326 // Stat stat( chronoSvc() , Exception.tag() ) ; 00327 } 00328 catch( const std::exception& Exception ) { 00329 MsgStream log ( msgSvc() , name() + ".sysRestart()" ); 00330 log << MSG::FATAL << " Standard std::exception is caught " << endreq; 00331 log << MSG::ERROR << Exception.what() << endreq; 00332 // Stat stat( chronoSvc() , "*std::exception*" ) ; 00333 } 00334 catch( ... ) { 00335 MsgStream log ( msgSvc() , name() + ".sysRestart()" ); 00336 log << MSG::FATAL << "UNKNOWN Exception is caught " << endreq; 00337 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00338 } 00339 return StatusCode::FAILURE ; 00340 00341 } 00342 00343 //--- IService::reinitialize 00344 StatusCode Service::reinitialize() { 00345 // Default implementation is finalize+initialize 00346 StatusCode sc = finalize(); 00347 if (sc.isFailure()) { 00348 MsgStream log ( msgSvc() , name() ); 00349 log << MSG::ERROR << "reinitialize(): cannot be finalized" << endreq; 00350 return sc; 00351 } 00352 sc = initialize(); 00353 if (sc.isFailure()) { 00354 MsgStream log ( msgSvc() , name() ); 00355 log << MSG::ERROR << "reinitialize(): cannot be initialized" << endreq; 00356 return sc; 00357 } 00358 return StatusCode::SUCCESS; 00359 } 00360 00361 //--- IService::restart 00362 StatusCode Service::restart() { 00363 // Default implementation is stop+start 00364 StatusCode sc = stop(); 00365 if (sc.isFailure()) { 00366 MsgStream log ( msgSvc() , name() ); 00367 log << MSG::ERROR << "restart(): cannot be stopped" << endreq; 00368 return sc; 00369 } 00370 sc = start(); 00371 if (sc.isFailure()) { 00372 MsgStream log ( msgSvc() , name() ); 00373 log << MSG::ERROR << "restart(): cannot be started" << endreq; 00374 return sc; 00375 } 00376 return StatusCode::SUCCESS; 00377 } 00378 00379 //--- IService::getServiceName 00380 const std::string& Service::name() const { 00381 return m_name; 00382 } 00383 00384 //--- IService::getSeriviceType 00385 const InterfaceID& Service::type() const { 00386 return IID_IService; 00387 } 00388 00389 //--- Retrieve pointer to service locator 00390 ISvcLocator* Service::serviceLocator() const { 00391 return m_svcLocator; 00392 } 00393 00394 //--- Retrieve pointer to message service 00395 IMessageSvc* Service::msgSvc() { 00396 if ( 0 == m_messageSvc ) { 00397 StatusCode sc = serviceLocator()->service( "MessageSvc", m_messageSvc, true ); 00398 if( sc.isFailure() ) { 00399 throw GaudiException("Service [MessageSvc] not found", name(), sc); 00400 } 00401 } 00402 return m_messageSvc; 00403 } 00404 00405 IMessageSvc* Service::msgSvc() const { 00406 if ( 0 == m_messageSvc ) { 00407 StatusCode sc = serviceLocator()->service( "MessageSvc", m_messageSvc, true ); 00408 if( sc.isFailure() ) { 00409 throw GaudiException("Service [MessageSvc] not found", name(), sc); 00410 } 00411 } 00412 return m_messageSvc; 00413 } 00414 // Obsoleted name, kept due to the backwards compatibility 00415 IMessageSvc* Service::messageService() { 00416 return msgSvc(); 00417 } 00418 IMessageSvc* Service::messageService() const { 00419 return msgSvc(); 00420 } 00421 00422 // IProperty implementation 00423 // Delegate to the Property manager 00424 StatusCode Service::setProperty(const Property& p) { 00425 return m_propertyMgr->setProperty(p); 00426 } 00427 00428 StatusCode Service::setProperty(const std::string& s) { 00429 return m_propertyMgr->setProperty(s); 00430 } 00431 00432 StatusCode Service::setProperty(const std::string& n, const std::string& v) { 00433 return m_propertyMgr->setProperty(n,v); 00434 } 00435 00436 StatusCode Service::getProperty(Property* p) const { 00437 return m_propertyMgr->getProperty(p); 00438 } 00439 00440 const Property& Service::getProperty(const std::string& n) const { 00441 return m_propertyMgr->getProperty(n); 00442 } 00443 00444 StatusCode Service::getProperty(const std::string& n, std::string& v ) const { 00445 return m_propertyMgr->getProperty(n,v); 00446 } 00447 00448 const std::vector<Property*>& Service::getProperties() const { 00449 return m_propertyMgr->getProperties(); 00450 } 00451 00452 // Use the job options service to set declared properties 00453 StatusCode Service::setProperties() { 00454 IJobOptionsSvc* jos; 00455 const bool CREATEIF(true); 00456 StatusCode sc = serviceLocator()->service( "JobOptionsSvc", jos, CREATEIF ); 00457 if( sc.isFailure() ) { 00458 throw GaudiException("Service [JobOptionsSvc] not found", name(), sc); 00459 } 00460 // set first generic Properties 00461 sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this ); 00462 if( sc.isFailure() ) return StatusCode::FAILURE; 00463 00464 // set specific Properties 00465 if (isGaudiThreaded(name())) { 00466 if (jos->setMyProperties( name(), this ).isFailure()) { 00467 return StatusCode::FAILURE; 00468 } 00469 } 00470 jos->release(); 00471 return StatusCode::SUCCESS; 00472 } 00473 00474 00475 //--- Local methods 00476 // Standard Constructor 00477 Service::Service( const std::string& name, ISvcLocator* svcloc) { 00478 m_name = name; 00479 m_refCount = 0; 00480 m_svcLocator = svcloc; 00481 m_svcManager = 0; 00482 m_messageSvc = 0; 00483 m_state = Gaudi::StateMachine::OFFLINE; 00484 m_targetState = Gaudi::StateMachine::OFFLINE; 00485 m_propertyMgr = new PropertyMgr(); 00486 m_pAuditorSvc = 0; 00487 m_outputLevel = MSG::NIL; 00488 // Declare common Service properties with their defaults 00489 if ( name != "MessageSvc" ) { 00490 m_outputLevel = msgSvc()->outputLevel(); 00491 } 00492 declareProperty( "OutputLevel", m_outputLevel); 00493 m_outputLevel.declareUpdateHandler(&Service::initOutputLevel, this); 00494 00495 // Get the default setting for service auditing from the AppMgr 00496 declareProperty( "AuditServices", m_auditInit=true ); 00497 00498 IProperty *appMgr; 00499 bool audit(false); 00500 if (m_svcLocator->service("ApplicationMgr", appMgr, false).isSuccess()) { 00501 const Property& prop = appMgr->getProperty("AuditServices"); 00502 Property &pr = const_cast<Property&>(prop); 00503 if (m_name != "IncidentSvc") { 00504 setProperty( pr ).ignore(); 00505 audit = m_auditInit.value(); 00506 } else { 00507 audit = false; 00508 } 00509 } 00510 00511 declareProperty( "AuditInitialize" , m_auditorInitialize = audit ); 00512 declareProperty( "AuditStart" , m_auditorStart = audit ); 00513 declareProperty( "AuditStop" , m_auditorStop = audit ); 00514 declareProperty( "AuditFinalize" , m_auditorFinalize = audit ); 00515 declareProperty( "AuditReInitialize" , m_auditorReinitialize = audit ); 00516 declareProperty( "AuditReStart" , m_auditorRestart = audit ); 00517 00518 } 00519 00520 // Callback to set output level 00521 void Service::initOutputLevel(Property& /*prop*/) { 00522 if ( name() != "MessageSvc" ) { 00523 msgSvc()->setOutputLevel( name(), m_outputLevel ); 00524 } 00525 } 00526 00527 // Standard Destructor 00528 Service::~Service() { 00529 delete m_propertyMgr; 00530 } 00531 00532 00533 StatusCode 00534 Service::service_i(const std::string& svcName, bool createIf, 00535 const InterfaceID& iid, void** ppSvc) const { 00536 MsgStream log(msgSvc(), name()); 00537 ServiceLocatorHelper helper(*serviceLocator(), log, name()); 00538 return helper.getService(svcName, createIf, iid, ppSvc); 00539 } 00540 00541 StatusCode 00542 Service::service_i(const std::string& svcType, 00543 const std::string& svcName, 00544 const InterfaceID& iid, 00545 void** ppSvc) const { 00546 00547 MsgStream log(msgSvc(), name()); 00548 ServiceLocatorHelper helper(*serviceLocator(), log, name()); 00549 return helper.createService(svcType, svcName, iid, ppSvc); 00550 } 00551 00552 IAuditorSvc* Service::auditorSvc() const { 00553 if ( 0 == m_pAuditorSvc ) { 00554 StatusCode sc = service( "AuditorSvc", m_pAuditorSvc, true ); 00555 if( sc.isFailure() ) { 00556 throw GaudiException("Service [AuditorSvc] not found", name(), sc); 00557 } 00558 } 00559 return m_pAuditorSvc; 00560 } 00561 00562 void 00563 Service::setServiceManager(ISvcManager *ism) { 00564 m_svcManager = ism; 00565 }