Gaudi Framework, version v20r4

Generated: 8 Jan 2009

Service.cpp

Go to the documentation of this file.
00001 // $Id: Service.cpp,v 1.35 2008/10/23 15:57:37 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   /* @TODO
00346    * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
00347    *                 is causing too many problems
00348    *
00349   // Default implementation is finalize+initialize
00350   StatusCode sc = finalize();
00351   if (sc.isFailure()) {
00352     MsgStream log ( msgSvc() , name() );
00353     log << MSG::ERROR << "reinitialize(): cannot be finalized" << endreq;
00354     return sc;
00355   }
00356   sc = initialize();
00357   if (sc.isFailure()) {
00358     MsgStream log ( msgSvc() , name() );
00359     log << MSG::ERROR << "reinitialize(): cannot be initialized" << endreq;
00360     return sc;
00361   }
00362   */
00363   return StatusCode::SUCCESS;
00364 }
00365 
00366 //--- IService::restart
00367 StatusCode Service::restart() {
00368   // Default implementation is stop+start
00369   StatusCode sc = stop();
00370   if (sc.isFailure()) {
00371     MsgStream log ( msgSvc() , name() );
00372     log << MSG::ERROR << "restart(): cannot be stopped" << endreq;
00373     return sc;
00374   }
00375   sc = start();
00376   if (sc.isFailure()) {
00377     MsgStream log ( msgSvc() , name() );
00378     log << MSG::ERROR << "restart(): cannot be started" << endreq;
00379     return sc;
00380   }
00381   return StatusCode::SUCCESS;
00382 }
00383 
00384 //--- IService::getServiceName
00385 const std::string& Service::name()   const {
00386   return m_name;
00387 }
00388 
00389 //--- IService::getSeriviceType
00390 const InterfaceID& Service::type()  const {
00391   return IID_IService;
00392 }
00393 
00394 //--- Retrieve pointer to service locator
00395 ISvcLocator* Service::serviceLocator() const {
00396   return m_svcLocator;
00397 }
00398 
00399 //--- Retrieve pointer to message service
00400 IMessageSvc* Service::msgSvc()    {
00401   if ( 0 == m_messageSvc ) {
00402     StatusCode sc = serviceLocator()->service( "MessageSvc", m_messageSvc, true );
00403     if( sc.isFailure() ) {
00404       throw GaudiException("Service [MessageSvc] not found", name(), sc);
00405     }
00406   }
00407   return m_messageSvc;
00408 }
00409 
00410 IMessageSvc* Service::msgSvc()  const  {
00411   if ( 0 == m_messageSvc ) {
00412     StatusCode sc = serviceLocator()->service( "MessageSvc", m_messageSvc, true );
00413     if( sc.isFailure() ) {
00414       throw GaudiException("Service [MessageSvc] not found", name(), sc);
00415     }
00416   }
00417   return m_messageSvc;
00418 }
00419 // Obsoleted name, kept due to the backwards compatibility
00420 IMessageSvc* Service::messageService()    {
00421   return msgSvc();
00422 }
00423 IMessageSvc* Service::messageService()  const  {
00424   return msgSvc();
00425 }
00426 
00427 // IProperty implementation
00428 // Delegate to the Property manager
00429 StatusCode Service::setProperty(const Property& p) {
00430   return m_propertyMgr->setProperty(p);
00431 }
00432 
00433 StatusCode Service::setProperty(const std::string& s) {
00434   return m_propertyMgr->setProperty(s);
00435 }
00436 
00437 StatusCode Service::setProperty(const std::string& n, const std::string& v) {
00438   return m_propertyMgr->setProperty(n,v);
00439 }
00440 
00441 StatusCode Service::getProperty(Property* p) const {
00442   return m_propertyMgr->getProperty(p);
00443 }
00444 
00445 const Property& Service::getProperty(const std::string& n) const {
00446   return m_propertyMgr->getProperty(n);
00447 }
00448 
00449 StatusCode Service::getProperty(const std::string& n, std::string& v ) const {
00450   return m_propertyMgr->getProperty(n,v);
00451 }
00452 
00453 const std::vector<Property*>& Service::getProperties() const {
00454   return m_propertyMgr->getProperties();
00455 }
00456 
00457 // Use the job options service to set declared properties
00458 StatusCode Service::setProperties() {
00459   IJobOptionsSvc* jos;
00460   const bool CREATEIF(true);
00461   StatusCode sc = serviceLocator()->service( "JobOptionsSvc", jos, CREATEIF );
00462   if( sc.isFailure() ) {
00463     throw GaudiException("Service [JobOptionsSvc] not found", name(), sc);
00464   }
00465   // set first generic Properties
00466   sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this );
00467   if( sc.isFailure() ) return StatusCode::FAILURE;
00468 
00469   // set specific Properties
00470   if (isGaudiThreaded(name())) {
00471     if (jos->setMyProperties( name(), this ).isFailure()) {
00472       return StatusCode::FAILURE;
00473     }
00474   }
00475   jos->release();
00476   return StatusCode::SUCCESS;
00477 }
00478 
00479 
00480 //--- Local methods
00481 // Standard Constructor
00482 Service::Service( const std::string& name, ISvcLocator* svcloc) {
00483   m_name       = name;
00484   m_refCount   = 0;
00485   m_svcLocator = svcloc;
00486   m_svcManager = 0;
00487   m_messageSvc = 0;
00488   m_state      = Gaudi::StateMachine::OFFLINE;
00489   m_targetState = Gaudi::StateMachine::OFFLINE;
00490   m_propertyMgr = new PropertyMgr();
00491   m_pAuditorSvc = 0;
00492   m_outputLevel = MSG::NIL;
00493   // Declare common Service properties with their defaults
00494   if ( name != "MessageSvc" )  {
00495     m_outputLevel = msgSvc()->outputLevel();
00496   }
00497   declareProperty( "OutputLevel", m_outputLevel);
00498   m_outputLevel.declareUpdateHandler(&Service::initOutputLevel, this);
00499 
00500   // Get the default setting for service auditing from the AppMgr
00501   declareProperty( "AuditServices", m_auditInit=true );
00502 
00503   IProperty *appMgr;
00504   bool audit(false);
00505   if (m_svcLocator->service("ApplicationMgr", appMgr, false).isSuccess()) {
00506     const Property& prop = appMgr->getProperty("AuditServices");
00507     Property &pr = const_cast<Property&>(prop);
00508     if (m_name != "IncidentSvc") {
00509       setProperty( pr ).ignore();
00510       audit = m_auditInit.value();
00511     } else {
00512       audit = false;
00513     }
00514   }
00515 
00516   declareProperty( "AuditInitialize"   , m_auditorInitialize   = audit );
00517   declareProperty( "AuditStart"        , m_auditorStart        = audit );
00518   declareProperty( "AuditStop"         , m_auditorStop         = audit );
00519   declareProperty( "AuditFinalize"     , m_auditorFinalize     = audit );
00520   declareProperty( "AuditReInitialize" , m_auditorReinitialize = audit );
00521   declareProperty( "AuditReStart"      , m_auditorRestart      = audit );
00522 
00523 }
00524 
00525 // Callback to set output level
00526 void Service::initOutputLevel(Property& /*prop*/) {
00527   if ( name() != "MessageSvc" ) {
00528     msgSvc()->setOutputLevel( name(), m_outputLevel );
00529   }
00530 }
00531 
00532 // Standard Destructor
00533 Service::~Service() {
00534   delete m_propertyMgr;
00535 }
00536 
00537 
00538 StatusCode
00539 Service::service_i(const std::string& svcName, bool createIf,
00540                    const InterfaceID& iid, void** ppSvc) const {
00541   MsgStream log(msgSvc(), name());
00542   ServiceLocatorHelper helper(*serviceLocator(), log, name());
00543   return helper.getService(svcName, createIf, iid, ppSvc);
00544 }
00545 
00546 StatusCode
00547 Service::service_i(const std::string& svcType,
00548                    const std::string& svcName,
00549                    const InterfaceID& iid,
00550                    void** ppSvc) const {
00551 
00552   MsgStream log(msgSvc(), name());
00553   ServiceLocatorHelper helper(*serviceLocator(), log, name());
00554   return  helper.createService(svcType, svcName, iid, ppSvc);
00555 }
00556 
00557 IAuditorSvc* Service::auditorSvc() const {
00558   if ( 0 == m_pAuditorSvc ) {
00559     StatusCode sc = service( "AuditorSvc", m_pAuditorSvc, true );
00560     if( sc.isFailure() ) {
00561       throw GaudiException("Service [AuditorSvc] not found", name(), sc);
00562     }
00563   }
00564   return m_pAuditorSvc;
00565 }
00566 
00567 void
00568 Service::setServiceManager(ISvcManager *ism) {
00569   m_svcManager = ism;
00570 }

Generated at Thu Jan 8 17:44:21 2009 for Gaudi Framework, version v20r4 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004