Service.cpp
Go to the documentation of this file.
1 // Include Files
2 #include "GaudiKernel/Service.h"
3 #include "GaudiKernel/PropertyMgr.h"
4 #include "GaudiKernel/IMessageSvc.h"
5 #include "GaudiKernel/MsgStream.h"
6 #include "GaudiKernel/ISvcLocator.h"
7 #include "GaudiKernel/ISvcManager.h"
8 #include "GaudiKernel/IAuditorSvc.h"
9 #include "GaudiKernel/IJobOptionsSvc.h"
10 #include "GaudiKernel/GaudiException.h"
11 #include "GaudiKernel/ServiceLocatorHelper.h"
12 #include "GaudiKernel/ThreadGaudi.h"
13 #include "GaudiKernel/Guards.h"
14 
15 using std::string;
16 
17 #define ON_DEBUG if (UNLIKELY(outputLevel() <= MSG::DEBUG))
18 #define ON_VERBOSE if (UNLIKELY(outputLevel() <= MSG::VERBOSE))
19 
20 //--- IInterface::release
21 // Specialized implementation because the default one is not enough.
22 unsigned long Service::release() {
23  // Avoid to decrement 0
24  const unsigned long count = (m_refCount) ? --m_refCount : m_refCount;
25  if( count == 0) {
26  if (m_svcManager!=0) {
28  }
29  delete this;
30  }
31  return count;
32 }
33 
34 // IService::sysInitialize
36  StatusCode sc;
37 
38  try {
40  Gaudi::Guards::AuditorGuard guard(this,
41  // check if we want to audit the initialize
42  (m_auditorInitialize) ? auditorSvc().get() : 0,
44  if ((name() != "MessageSvc") && msgSvc().isValid()) // pre-set the outputLevel from the MessageSvc value
46  sc = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED
47  if (sc.isSuccess())
49  return sc;
50  }
51  catch ( const GaudiException& Exception ) {
52  fatal() << "in sysInitialize(): exception with tag=" << Exception.tag()
53  << " is caught" << endmsg;
54  error() << Exception << endmsg;
55  // Stat stat( chronoSvc() , Exception.tag() );
56  }
57  catch( const std::exception& Exception ) {
58  fatal() << "in sysInitialize(): standard std::exception is caught" << endmsg;
59  error() << Exception.what() << endmsg;
60  // Stat stat( chronoSvc() , "*std::exception*" );
61  }
62  catch(...) {
63  fatal() << "in sysInitialize(): UNKNOWN Exception is caught" << endmsg;
64  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
65  }
66 
67  return StatusCode::FAILURE;
68 }
69 
70 
71 //--- IService::initialize
73  // Set the Algorithm's properties
75  ON_DEBUG debug() << "Service base class initialized successfully" << endmsg;
77  return sc ;
78 }
79 
80 // IService::sysStart
82  StatusCode sc;
83 
84  try {
86  Gaudi::Guards::AuditorGuard guard(this,
87  // check if we want to audit the initialize
88  (m_auditorStart) ? auditorSvc().get() : 0,
90  sc = start();
91  if (sc.isSuccess())
93  return sc;
94  }
95  catch ( const GaudiException& Exception ) {
96  fatal() << "in sysStart(): exception with tag=" << Exception.tag()
97  << " is caught" << endmsg;
98  error() << Exception << endmsg;
99  // Stat stat( chronoSvc() , Exception.tag() );
100  }
101  catch( const std::exception& Exception ) {
102  fatal() << "in sysStart(): standard std::exception is caught" << endmsg;
103  fatal() << Exception.what() << endmsg;
104  // Stat stat( chronoSvc() , "*std::exception*" );
105  }
106  catch(...) {
107  fatal() << "in sysStart(): UNKNOWN Exception is caught" << endmsg;
108  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
109  }
110 
111  return StatusCode::FAILURE;
112 }
113 
114 // IService::sysStop
116  StatusCode sc;
117 
118  try {
120  Gaudi::Guards::AuditorGuard guard(this,
121  // check if we want to audit the initialize
122  (m_auditorStop) ? auditorSvc().get() : 0,
124  sc = stop();
125  if (sc.isSuccess())
127  return sc;
128  }
129  catch ( const GaudiException& Exception ) {
130  fatal() << "in sysStop(): exception with tag=" << Exception.tag()
131  << " is caught" << endmsg;
132  error() << Exception << endmsg;
133  // Stat stat( chronoSvc() , Exception.tag() );
134  }
135  catch( const std::exception& Exception ) {
136  fatal() << "in sysStop(): standard std::exception is caught" << endmsg;
137  error() << Exception.what() << endmsg;
138  // Stat stat( chronoSvc() , "*std::exception*" );
139  }
140  catch(...) {
141  fatal() << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
142  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
143  }
144 
145  return StatusCode::FAILURE;
146 }
147 
148 
149 //--- IService::stop
151  // stub implementation
152  return StatusCode::SUCCESS;
153 }
154 
155 //--- IService::start
157  // stub implementation
158  return StatusCode::SUCCESS;
159 }
160 
161 //--- IService::sysFinalize
163 
165 
166  try {
168  Gaudi::Guards::AuditorGuard guard(this,
169  // check if we want to audit the initialize
170  (m_auditorFinalize) ? auditorSvc().get() : 0,
172  sc = finalize();
173  if (sc.isSuccess())
175  }
176  catch( const GaudiException& Exception ) {
177  fatal() << " Exception with tag=" << Exception.tag()
178  << " is caught " << endmsg;
179  error() << Exception << endmsg;
180  // Stat stat( chronoSvc() , Exception.tag() ) ;
181  }
182  catch( const std::exception& Exception ) {
183  fatal() << " Standard std::exception is caught " << endmsg;
184  error() << Exception.what() << endmsg;
185  // Stat stat( chronoSvc() , "*std::exception*" ) ;
186  }
187  catch( ... ) {
188  fatal() << "UNKNOWN Exception is caught " << endmsg;
189  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
190  }
191 
192  m_pAuditorSvc = 0;
193  return sc;
194 }
195 
196 //--- IService::finalize
198  //MsgStream log(msgSvc(),name());
199  //m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state);
200  return StatusCode::SUCCESS;
201 }
202 
203 //--- IService::sysReinitialize
205 
206  StatusCode sc;
207 
208  // Check that the current status is the correct one.
210  MsgStream log ( msgSvc() , name() );
211  error()
212  << "sysReinitialize(): cannot reinitialize service not initialized"
213  << endmsg;
214  return StatusCode::FAILURE;
215  }
216 
217  try {
218 
219  Gaudi::Guards::AuditorGuard guard(this,
220  // check if we want to audit the initialize
221  (m_auditorReinitialize) ? auditorSvc().get() : 0,
223  sc = reinitialize();
224  return sc;
225  }
226  catch( const GaudiException& Exception ) {
227  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
228  fatal() << " Exception with tag=" << Exception.tag()
229  << " is caught " << endmsg;
230  error() << Exception << endmsg;
231  // Stat stat( chronoSvc() , Exception.tag() ) ;
232  }
233  catch( const std::exception& Exception ) {
234  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
235  fatal() << " Standard std::exception is caught " << endmsg;
236  error() << Exception.what() << endmsg;
237  // Stat stat( chronoSvc() , "*std::exception*" ) ;
238  }
239  catch( ... ) {
240  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
241  fatal() << "UNKNOWN Exception is caught " << endmsg;
242  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
243  }
244  return StatusCode::FAILURE ;
245 
246 }
247 
248 //--- IService::sysRestart
250 
251  StatusCode sc;
252 
253  // Check that the current status is the correct one.
255  MsgStream log ( msgSvc() , name() );
256  error()
257  << "sysRestart(): cannot restart service in state " << FSMState()
258  << " -- must be RUNNING "
259  << endmsg;
260  return StatusCode::FAILURE;
261  }
262 
263  try {
264 
265  Gaudi::Guards::AuditorGuard guard(this,
266  // check if we want to audit the initialize
267  (m_auditorRestart) ? auditorSvc().get() : 0,
269  sc = restart();
270  return sc;
271  }
272  catch( const GaudiException& Exception ) {
273  fatal() << " Exception with tag=" << Exception.tag()
274  << " is caught " << endmsg;
275  error() << Exception << endmsg;
276  // Stat stat( chronoSvc() , Exception.tag() ) ;
277  }
278  catch( const std::exception& Exception ) {
279  fatal() << " Standard std::exception is caught " << endmsg;
280  error() << Exception.what() << endmsg;
281  // Stat stat( chronoSvc() , "*std::exception*" ) ;
282  }
283  catch( ... ) {
284  fatal() << "UNKNOWN Exception is caught " << endmsg;
285  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
286  }
287  return StatusCode::FAILURE ;
288 
289 }
290 
291 //--- IService::reinitialize
293  /* @TODO
294  * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
295  * is causing too many problems
296  *
297  // Default implementation is finalize+initialize
298  StatusCode sc = finalize();
299  if (sc.isFailure()) {
300  error() << "reinitialize(): cannot be finalized" << endmsg;
301  return sc;
302  }
303  sc = initialize();
304  if (sc.isFailure()) {
305  error() << "reinitialize(): cannot be initialized" << endmsg;
306  return sc;
307  }
308  */
309  return StatusCode::SUCCESS;
310 }
311 
312 //--- IService::restart
314  // Default implementation is stop+start
315  StatusCode sc = stop();
316  if (sc.isFailure()) {
317  error() << "restart(): cannot be stopped" << endmsg;
318  return sc;
319  }
320  sc = start();
321  if (sc.isFailure()) {
322  error() << "restart(): cannot be started" << endmsg;
323  return sc;
324  }
325  return StatusCode::SUCCESS;
326 }
327 
328 //--- IService::getServiceName
329 const std::string& Service::name() const {
330  return m_name;
331 }
332 
333 //--- Retrieve pointer to service locator
335  return m_svcLocator;
336 }
337 
338 // IProperty implementation
339 // Delegate to the Property manager
341  return m_propertyMgr->setProperty(p);
342 }
343 
344 StatusCode Service::setProperty(const std::string& s) {
345  return m_propertyMgr->setProperty(s);
346 }
347 
348 StatusCode Service::setProperty(const std::string& n, const std::string& v) {
349  return m_propertyMgr->setProperty(n,v);
350 }
351 
353  return m_propertyMgr->getProperty(p);
354 }
355 
356 const Property& Service::getProperty(const std::string& n) const {
357  return m_propertyMgr->getProperty(n);
358 }
359 
360 StatusCode Service::getProperty(const std::string& n, std::string& v ) const {
361  return m_propertyMgr->getProperty(n,v);
362 }
363 
364 const std::vector<Property*>& Service::getProperties() const {
365  return m_propertyMgr->getProperties();
366 }
367 
368 bool Service::hasProperty(const std::string& name) const {
369  return m_propertyMgr->hasProperty(name);
370 }
371 
372 // Use the job options service to set declared properties
374  const bool CREATEIF(true);
375  SmartIF<IJobOptionsSvc> jos(serviceLocator()->service("JobOptionsSvc", CREATEIF));
376  if( !jos.isValid() ) {
377  throw GaudiException("Service [JobOptionsSvc] not found", name(), StatusCode::FAILURE);
378  }
379  // set first generic Properties
381  if( sc.isFailure() ) return sc;
382 
383  // set specific Properties
384  if (isGaudiThreaded(name())) {
385  if (jos->setMyProperties( name(), this ).isFailure()) {
386  return StatusCode::FAILURE;
387  }
388  }
389  return StatusCode::SUCCESS;
390 }
391 
392 
393 //--- Local methods
394 // Standard Constructor
395 Service::Service(const std::string& name, ISvcLocator* svcloc) {
396  m_name = name;
397  m_svcLocator = svcloc;
400  m_propertyMgr = new PropertyMgr();
402  // Declare common Service properties with their defaults
403  if ( (name != "MessageSvc") && msgSvc().isValid() ) {
404  // In genconf a service is instantiated without the ApplicationMgr
406  }
407  declareProperty("OutputLevel", m_outputLevel);
409 
410  // Get the default setting for service auditing from the AppMgr
411  declareProperty("AuditServices", m_auditInit = true);
412 
413  bool audit(false);
414  SmartIF<IProperty> appMgr(serviceLocator()->service("ApplicationMgr"));
415  if (appMgr.isValid()) {
416  const Property& prop = appMgr->getProperty("AuditServices");
417  if (m_name != "IncidentSvc") {
418  setProperty(prop).ignore();
419  audit = m_auditInit.value();
420  } else {
421  audit = false;
422  }
423  }
424 
425  declareProperty( "AuditInitialize" , m_auditorInitialize = audit );
426  declareProperty( "AuditStart" , m_auditorStart = audit );
427  declareProperty( "AuditStop" , m_auditorStop = audit );
428  declareProperty( "AuditFinalize" , m_auditorFinalize = audit );
429  declareProperty( "AuditReInitialize" , m_auditorReinitialize = audit );
430  declareProperty( "AuditReStart" , m_auditorRestart = audit );
431 }
432 
433 // Callback to set output level
435  if ( (name() != "MessageSvc") && msgSvc().isValid() ) {
437  }
439 }
440 
441 // Standard Destructor
443  delete m_propertyMgr;
444 }
445 
447  if ( !m_pAuditorSvc.isValid() ) {
448  m_pAuditorSvc = serviceLocator()->service("AuditorSvc");
449  if( !m_pAuditorSvc.isValid() ) {
450  throw GaudiException("Service [AuditorSvc] not found", name(), StatusCode::FAILURE);
451  }
452  }
453  return m_pAuditorSvc;
454 }
455 
456 void
458  m_svcManager = ism;
459 }
virtual ~Service()
Standard Destructor.
Definition: Service.cpp:442
virtual StatusCode restart()
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Definition: Service.cpp:313
BooleanProperty m_auditInit
Definition: Service.h:266
Gaudi::StateMachine::State m_targetState
Service state.
Definition: Service.h:248
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:258
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:244
StatusCode setProperty(const Property &p)
set the property form another property
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
bool m_auditorInitialize
Definition: Service.h:267
virtual StatusCode getProperty(Property *p) const =0
Get the property by property.
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:246
virtual StatusCode stop()
Stop (from RUNNING to INITIALIZED).
Definition: Service.cpp:150
virtual StatusCode setProperty(const Property &p)
Set the property by property.
Definition: Service.cpp:340
SmartIF< IAuditorSvc > & auditorSvc() const
The standard auditor service.May not be invoked before sysInitialize() has been invoked.
Definition: Service.cpp:446
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
GAUDI_API bool isGaudiThreaded(const std::string &name)
test if current Gaudi object is running /will run in a thread
Definition: ThreadGaudi.cpp:75
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:29
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
State GAUDI_API ChangeState(const Transition transition, const State state)
Function to get the new state according to the required transition, checking if the transition is all...
Definition: StateMachine.cpp:8
virtual StatusCode getProperty(Property *p) const
Get the property by property.
Definition: Service.cpp:352
std::string m_name
Service Name.
Definition: Service.h:255
GAUDI_API std::string getGaudiThreadGenericName(const std::string &name)
helper function to extract Gaudi instance name from thread copy name
Definition: ThreadGaudi.cpp:52
virtual StatusCode sysFinalize()
Finalize Service.
Definition: Service.cpp:162
Property manager helper class.
Definition: PropertyMgr.h:34
bool hasProperty(const std::string &name) const
Return true if we have a property with the given name.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
virtual void declareUpdateHandler(PropertyCallbackFunctor *pf)
set new callback for update
Definition: Property.cpp:141
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:82
virtual int outputLevel() const =0
Retrieve the current output level threshold.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
virtual StatusCode setMyProperties(const std::string &client, IProperty *me)=0
Override default properties of the calling client.
StatusCode getProperty(Property *p) const
get the property
virtual const std::string & tag() const
name tag for the exception, or exception type
virtual StatusCode sysStop()
Initialize Service.
Definition: Service.cpp:115
bool m_auditorFinalize
Definition: Service.h:270
bool m_auditorReinitialize
Definition: Service.h:271
virtual Gaudi::StateMachine::State FSMState() const
Get the current state.
Definition: Service.h:56
const TYPE & value() const
explicit conversion
Definition: Property.h:355
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:373
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:329
virtual StatusCode sysInitialize()
Initialize Service.
Definition: Service.cpp:35
virtual StatusCode reinitialize()
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: Service.cpp:292
#define ON_DEBUG
Definition: Service.cpp:17
const std::vector< Property * > & getProperties() const
get all properties
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:43
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:257
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:72
bool m_auditorStart
Definition: Service.h:268
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
virtual unsigned long release()
Release Interface instance.
Definition: Service.cpp:22
virtual StatusCode sysRestart()
Re-initialize the Service.
Definition: Service.cpp:249
void initOutputLevel(Property &prop)
callback for output level property
Definition: Service.cpp:434
string s
Definition: gaudirun.py:244
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
tuple appMgr
Definition: IOTest.py:83
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:143
virtual bool hasProperty(const std::string &name) const
Return true if we have a property with the given name.
Definition: Service.cpp:368
virtual StatusCode sysReinitialize()
Re-initialize the Service.
Definition: Service.cpp:204
PropertyMgr * m_propertyMgr
Property Manager.
Definition: Service.h:260
virtual StatusCode start()
Start (from INITIALIZED to RUNNING).
Definition: Service.cpp:156
void ignore() const
Definition: StatusCode.h:107
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:212
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way...
Definition: Guards.h:218
virtual StatusCode removeService(IService *svc)=0
Remove a service from the "active" list of services of the factory.
Service(const std::string &name, ISvcLocator *svcloc)
Standard Constructor.
Definition: Service.cpp:395
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:265
virtual StatusCode sysStart()
Initialize Service.
Definition: Service.cpp:81
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:197
bool m_auditorRestart
Definition: Service.h:272
void setServiceManager(ISvcManager *ism)
Definition: Service.cpp:457
bool m_auditorStop
Definition: Service.h:269
virtual void setOutputLevel(int new_level)=0
Set new global output level threshold.
virtual const std::vector< Property * > & getProperties() const
Get list of properties.
Definition: Service.cpp:364
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:334