Gaudi Framework, version v22r2

Home   Generated: Tue May 10 2011

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::release
00020 // Specialized implementation because the default one is not enough.
00021 unsigned long Service::release()   {
00022   // Avoid to decrement 0
00023   const unsigned long count = (m_refCount) ? --m_refCount : m_refCount;
00024   if( count == 0) {
00025     if (m_svcManager!=0) {
00026       m_svcManager->removeService(this).ignore();
00027     }
00028     delete this;
00029   }
00030   return count;
00031 }
00032 
00033 // IService::sysInitialize
00034 StatusCode Service::sysInitialize() {
00035   StatusCode sc;
00036 
00037   try {
00038     m_targetState = Gaudi::StateMachine::INITIALIZED;
00039     Gaudi::Guards::AuditorGuard guard(this,
00040                                       // check if we want to audit the initialize
00041                                       (m_auditorInitialize) ? auditorSvc().get() : 0,
00042                                       IAuditor::Initialize);
00043     sc = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED
00044     if (sc.isSuccess())
00045       m_state = m_targetState;
00046     return sc;
00047   }
00048   catch ( const GaudiException& Exception )  {
00049     fatal() << "in sysInitialize(): exception with tag=" << Exception.tag()
00050         << " is caught" << endmsg;
00051     error() << Exception  << endmsg;
00052     //    Stat stat( chronoSvc() , Exception.tag() );
00053   }
00054   catch( const std::exception& Exception ) {
00055     fatal() << "in sysInitialize(): standard std::exception is caught" << endmsg;
00056     error() << Exception.what()  << endmsg;
00057     //    Stat stat( chronoSvc() , "*std::exception*" );
00058   }
00059   catch(...) {
00060     fatal() << "in sysInitialize(): UNKNOWN Exception is caught" << endmsg;
00061     //    Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
00062   }
00063 
00064   return StatusCode::FAILURE;
00065 }
00066 
00067 
00068 //--- IService::initialize
00069 StatusCode Service::initialize() {
00070   // Set the Algorithm's properties
00071   StatusCode sc = setProperties();
00072 
00073   debug() <<  "Service base class initialized successfully" << endmsg;
00074   m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::CONFIGURE,m_state);
00075   return sc ;
00076 }
00077 
00078 // IService::sysStart
00079 StatusCode Service::sysStart() {
00080   StatusCode sc;
00081 
00082   try {
00083     m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::START,m_state);
00084     Gaudi::Guards::AuditorGuard guard(this,
00085                                       // check if we want to audit the initialize
00086                                       (m_auditorStart) ? auditorSvc().get() : 0,
00087                                       IAuditor::Start);
00088     sc = start();
00089     if (sc.isSuccess())
00090       m_state = m_targetState;
00091     return sc;
00092   }
00093   catch ( const GaudiException& Exception )  {
00094     fatal() << "in sysStart(): exception with tag=" << Exception.tag()
00095         << " is caught" << endmsg;
00096     error() << Exception  << endmsg;
00097     //    Stat stat( chronoSvc() , Exception.tag() );
00098   }
00099   catch( const std::exception& Exception ) {
00100     fatal() << "in sysStart(): standard std::exception is caught" << endmsg;
00101     fatal() << Exception.what()  << endmsg;
00102     //    Stat stat( chronoSvc() , "*std::exception*" );
00103   }
00104   catch(...) {
00105     fatal() << "in sysStart(): UNKNOWN Exception is caught" << endmsg;
00106     //    Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
00107   }
00108 
00109   return StatusCode::FAILURE;
00110 }
00111 
00112 // IService::sysStop
00113 StatusCode Service::sysStop() {
00114   StatusCode sc;
00115 
00116   try {
00117     m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::STOP,m_state);
00118     Gaudi::Guards::AuditorGuard guard(this,
00119                                       // check if we want to audit the initialize
00120                                       (m_auditorStop) ? auditorSvc().get() : 0,
00121                                       IAuditor::Stop);
00122     sc = stop();
00123     if (sc.isSuccess())
00124       m_state = m_targetState;
00125     return sc;
00126   }
00127   catch ( const GaudiException& Exception )  {
00128     fatal() << "in sysStop(): exception with tag=" << Exception.tag()
00129         << " is caught" << endmsg;
00130     error() << Exception  << endmsg;
00131     //    Stat stat( chronoSvc() , Exception.tag() );
00132   }
00133   catch( const std::exception& Exception ) {
00134     fatal() << "in sysStop(): standard std::exception is caught" << endmsg;
00135     error() << Exception.what()  << endmsg;
00136     //    Stat stat( chronoSvc() , "*std::exception*" );
00137   }
00138   catch(...) {
00139     fatal() << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
00140     //    Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
00141   }
00142 
00143   return StatusCode::FAILURE;
00144 }
00145 
00146 
00147 //--- IService::stop
00148 StatusCode Service::stop() {
00149   // stub implementation
00150   return StatusCode::SUCCESS;
00151 }
00152 
00153 //--- IService::start
00154 StatusCode Service::start() {
00155   // stub implementation
00156   return StatusCode::SUCCESS;
00157 }
00158 
00159 //--- IService::sysFinalize
00160 StatusCode Service::sysFinalize() {
00161 
00162   StatusCode sc(StatusCode::FAILURE);
00163 
00164   try {
00165     m_targetState = Gaudi::StateMachine::OFFLINE;
00166     Gaudi::Guards::AuditorGuard guard(this,
00167                                       // check if we want to audit the initialize
00168                                       (m_auditorFinalize) ? auditorSvc().get() : 0,
00169                                       IAuditor::Finalize);
00170     sc = finalize();
00171     if (sc.isSuccess())
00172       m_state = m_targetState;
00173   }
00174   catch( const GaudiException& Exception ) {
00175     fatal() << " Exception with tag=" << Exception.tag()
00176         << " is caught " << endmsg;
00177     error() << Exception  << endmsg;
00178     //    Stat stat( chronoSvc() , Exception.tag() ) ;
00179   }
00180   catch( const std::exception& Exception ) {
00181     fatal() << " Standard std::exception is caught " << endmsg;
00182     error() << Exception.what()  << endmsg;
00183     //    Stat stat( chronoSvc() , "*std::exception*" ) ;
00184   }
00185   catch( ... ) {
00186     fatal() << "UNKNOWN Exception is caught " << endmsg;
00187     //    Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
00188   }
00189 
00190   m_pAuditorSvc = 0;
00191   return sc;
00192 }
00193 
00194 //--- IService::finalize
00195 StatusCode Service::finalize() {
00196   //MsgStream log(msgSvc(),name());
00197   //m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state);
00198   return StatusCode::SUCCESS;
00199 }
00200 
00201 //--- IService::sysReinitialize
00202 StatusCode Service::sysReinitialize() {
00203 
00204   StatusCode sc;
00205 
00206   // Check that the current status is the correct one.
00207   if ( Gaudi::StateMachine::INITIALIZED != FSMState() ) {
00208     MsgStream log ( msgSvc() , name() );
00209     error()
00210         << "sysReinitialize(): cannot reinitialize service not initialized"
00211         << endmsg;
00212     return StatusCode::FAILURE;
00213   }
00214 
00215   try {
00216 
00217     Gaudi::Guards::AuditorGuard guard(this,
00218                                       // check if we want to audit the initialize
00219                                       (m_auditorReinitialize) ? auditorSvc().get() : 0,
00220                                       IAuditor::ReInitialize);
00221     sc = reinitialize();
00222     return sc;
00223   }
00224   catch( const GaudiException& Exception ) {
00225     MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
00226     fatal() << " Exception with tag=" << Exception.tag()
00227         << " is caught " << endmsg;
00228     error() << Exception  << endmsg;
00229     //    Stat stat( chronoSvc() , Exception.tag() ) ;
00230   }
00231   catch( const std::exception& Exception ) {
00232     MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
00233     fatal() << " Standard std::exception is caught " << endmsg;
00234     error() << Exception.what()  << endmsg;
00235     //    Stat stat( chronoSvc() , "*std::exception*" ) ;
00236   }
00237   catch( ... ) {
00238     MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
00239     fatal() << "UNKNOWN Exception is caught " << endmsg;
00240     //    Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
00241   }
00242   return StatusCode::FAILURE ;
00243 
00244 }
00245 
00246 //--- IService::sysRestart
00247 StatusCode Service::sysRestart() {
00248 
00249   StatusCode sc;
00250 
00251   // Check that the current status is the correct one.
00252   if ( Gaudi::StateMachine::RUNNING != FSMState() ) {
00253     MsgStream log ( msgSvc() , name() );
00254     error()
00255         << "sysRestart(): cannot restart service in state " << FSMState()
00256         << " -- must be RUNNING "
00257         << endmsg;
00258     return StatusCode::FAILURE;
00259   }
00260 
00261   try {
00262 
00263     Gaudi::Guards::AuditorGuard guard(this,
00264                                       // check if we want to audit the initialize
00265                                       (m_auditorRestart) ? auditorSvc().get() : 0,
00266                                       IAuditor::ReStart);
00267     sc = restart();
00268     return sc;
00269   }
00270   catch( const GaudiException& Exception ) {
00271     fatal() << " Exception with tag=" << Exception.tag()
00272         << " is caught " << endmsg;
00273     error() << Exception  << endmsg;
00274     //    Stat stat( chronoSvc() , Exception.tag() ) ;
00275   }
00276   catch( const std::exception& Exception ) {
00277     fatal() << " Standard std::exception is caught " << endmsg;
00278     error() << Exception.what()  << endmsg;
00279     //    Stat stat( chronoSvc() , "*std::exception*" ) ;
00280   }
00281   catch( ... ) {
00282     fatal() << "UNKNOWN Exception is caught " << endmsg;
00283     //    Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
00284   }
00285   return StatusCode::FAILURE ;
00286 
00287 }
00288 
00289 //--- IService::reinitialize
00290 StatusCode Service::reinitialize() {
00291   /* @TODO
00292    * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
00293    *                 is causing too many problems
00294    *
00295   // Default implementation is finalize+initialize
00296   StatusCode sc = finalize();
00297   if (sc.isFailure()) {
00298     error() << "reinitialize(): cannot be finalized" << endmsg;
00299     return sc;
00300   }
00301   sc = initialize();
00302   if (sc.isFailure()) {
00303     error() << "reinitialize(): cannot be initialized" << endmsg;
00304     return sc;
00305   }
00306   */
00307   return StatusCode::SUCCESS;
00308 }
00309 
00310 //--- IService::restart
00311 StatusCode Service::restart() {
00312   // Default implementation is stop+start
00313   StatusCode sc = stop();
00314   if (sc.isFailure()) {
00315     error() << "restart(): cannot be stopped" << endmsg;
00316     return sc;
00317   }
00318   sc = start();
00319   if (sc.isFailure()) {
00320     error() << "restart(): cannot be started" << endmsg;
00321     return sc;
00322   }
00323   return StatusCode::SUCCESS;
00324 }
00325 
00326 //--- IService::getServiceName
00327 const std::string& Service::name()   const {
00328   return m_name;
00329 }
00330 
00331 //--- Retrieve pointer to service locator
00332 SmartIF<ISvcLocator>& Service::serviceLocator() const {
00333   return m_svcLocator;
00334 }
00335 
00336 // IProperty implementation
00337 // Delegate to the Property manager
00338 StatusCode Service::setProperty(const Property& p) {
00339   return m_propertyMgr->setProperty(p);
00340 }
00341 
00342 StatusCode Service::setProperty(const std::string& s) {
00343   return m_propertyMgr->setProperty(s);
00344 }
00345 
00346 StatusCode Service::setProperty(const std::string& n, const std::string& v) {
00347   return m_propertyMgr->setProperty(n,v);
00348 }
00349 
00350 StatusCode Service::getProperty(Property* p) const {
00351   return m_propertyMgr->getProperty(p);
00352 }
00353 
00354 const Property& Service::getProperty(const std::string& n) const {
00355   return m_propertyMgr->getProperty(n);
00356 }
00357 
00358 StatusCode Service::getProperty(const std::string& n, std::string& v ) const {
00359   return m_propertyMgr->getProperty(n,v);
00360 }
00361 
00362 const std::vector<Property*>& Service::getProperties() const {
00363   return m_propertyMgr->getProperties();
00364 }
00365 
00366 // Use the job options service to set declared properties
00367 StatusCode Service::setProperties() {
00368   const bool CREATEIF(true);
00369   SmartIF<IJobOptionsSvc> jos(serviceLocator()->service("JobOptionsSvc", CREATEIF));
00370   if( !jos.isValid() ) {
00371     throw GaudiException("Service [JobOptionsSvc] not found", name(), StatusCode::FAILURE);
00372   }
00373   // set first generic Properties
00374   StatusCode sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this );
00375   if( sc.isFailure() ) return sc;
00376 
00377   // set specific Properties
00378   if (isGaudiThreaded(name())) {
00379     if (jos->setMyProperties( name(), this ).isFailure()) {
00380       return StatusCode::FAILURE;
00381     }
00382   }
00383   return StatusCode::SUCCESS;
00384 }
00385 
00386 
00387 //--- Local methods
00388 // Standard Constructor
00389 Service::Service(const std::string& name, ISvcLocator* svcloc) {
00390   m_name       = name;
00391   m_svcLocator = svcloc;
00392   m_state      = Gaudi::StateMachine::OFFLINE;
00393   m_targetState = Gaudi::StateMachine::OFFLINE;
00394   m_propertyMgr = new PropertyMgr();
00395   m_outputLevel = MSG::NIL;
00396   // Declare common Service properties with their defaults
00397   if ( (name != "MessageSvc") && msgSvc().isValid() )  {
00398     // In genconf a service is instantiated without the ApplicationMgr
00399     m_outputLevel = msgSvc()->outputLevel();
00400   }
00401   declareProperty( "OutputLevel", m_outputLevel);
00402   m_outputLevel.declareUpdateHandler(&Service::initOutputLevel, this);
00403 
00404   // Get the default setting for service auditing from the AppMgr
00405   declareProperty( "AuditServices", m_auditInit=true );
00406 
00407   bool audit(false);
00408   SmartIF<IProperty> appMgr(serviceLocator()->service("ApplicationMgr"));
00409   if (appMgr.isValid()) {
00410     const Property& prop = appMgr->getProperty("AuditServices");
00411     if (m_name != "IncidentSvc") {
00412       setProperty(prop).ignore();
00413       audit = m_auditInit.value();
00414     } else {
00415       audit = false;
00416     }
00417   }
00418 
00419   declareProperty( "AuditInitialize"   , m_auditorInitialize   = audit );
00420   declareProperty( "AuditStart"        , m_auditorStart        = audit );
00421   declareProperty( "AuditStop"         , m_auditorStop         = audit );
00422   declareProperty( "AuditFinalize"     , m_auditorFinalize     = audit );
00423   declareProperty( "AuditReInitialize" , m_auditorReinitialize = audit );
00424   declareProperty( "AuditReStart"      , m_auditorRestart      = audit );
00425 }
00426 
00427 // Callback to set output level
00428 void Service::initOutputLevel(Property& /*prop*/) {
00429   if ( (name() != "MessageSvc") && msgSvc().isValid() ) {
00430     msgSvc()->setOutputLevel( name(), m_outputLevel );
00431   }
00432 }
00433 
00434 // Standard Destructor
00435 Service::~Service() {
00436   delete m_propertyMgr;
00437 }
00438 
00439 SmartIF<IAuditorSvc>& Service::auditorSvc() const {
00440   if ( !m_pAuditorSvc.isValid() ) {
00441     m_pAuditorSvc = serviceLocator()->service("AuditorSvc");
00442     if( !m_pAuditorSvc.isValid() ) {
00443       throw GaudiException("Service [AuditorSvc] not found", name(), StatusCode::FAILURE);
00444     }
00445   }
00446   return m_pAuditorSvc;
00447 }
00448 
00449 void
00450 Service::setServiceManager(ISvcManager *ism) {
00451   m_svcManager = ism;
00452 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Tue May 10 2011 18:53:46 for Gaudi Framework, version v22r2 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004