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