Gaudi Framework, version v23r4

Home   Generated: Mon Sep 17 2012

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

Generated at Mon Sep 17 2012 13:49:34 for Gaudi Framework, version v23r4 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004