| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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"
5 #include "GaudiKernel/PropertyMgr.h"
6 #include "GaudiKernel/IMessageSvc.h"
7 #include "GaudiKernel/MsgStream.h"
8 #include "GaudiKernel/ISvcLocator.h"
9 #include "GaudiKernel/ISvcManager.h"
10 #include "GaudiKernel/IAuditorSvc.h"
11 #include "GaudiKernel/IJobOptionsSvc.h"
12 #include "GaudiKernel/GaudiException.h"
13 #include "GaudiKernel/ServiceLocatorHelper.h"
14 #include "GaudiKernel/ThreadGaudi.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
37 StatusCode Service::sysInitialize() {
38 StatusCode sc;
39
40 try {
41 m_targetState = Gaudi::StateMachine::INITIALIZED;
42 Gaudi::Guards::AuditorGuard guard(this,
43 // check if we want to audit the initialize
44 (m_auditorInitialize) ? auditorSvc().get() : 0,
45 IAuditor::Initialize);
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())
50 m_state = m_targetState;
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
74 StatusCode Service::initialize() {
75 // Set the Algorithm's properties
76 StatusCode sc = setProperties();
77 ON_DEBUG debug() << "Service base class initialized successfully" << endmsg;
78 m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::CONFIGURE,m_state);
79 return sc ;
80 }
81
82 // IService::sysStart
83 StatusCode Service::sysStart() {
84 StatusCode sc;
85
86 try {
87 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::START,m_state);
88 Gaudi::Guards::AuditorGuard guard(this,
89 // check if we want to audit the initialize
90 (m_auditorStart) ? auditorSvc().get() : 0,
91 IAuditor::Start);
92 sc = start();
93 if (sc.isSuccess())
94 m_state = m_targetState;
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
117 StatusCode Service::sysStop() {
118 StatusCode sc;
119
120 try {
121 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::STOP,m_state);
122 Gaudi::Guards::AuditorGuard guard(this,
123 // check if we want to audit the initialize
124 (m_auditorStop) ? auditorSvc().get() : 0,
125 IAuditor::Stop);
126 sc = stop();
127 if (sc.isSuccess())
128 m_state = m_targetState;
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
152 StatusCode Service::stop() {
153 // stub implementation
154 return StatusCode::SUCCESS;
155 }
156
157 //--- IService::start
158 StatusCode Service::start() {
159 // stub implementation
160 return StatusCode::SUCCESS;
161 }
162
163 //--- IService::sysFinalize
164 StatusCode Service::sysFinalize() {
165
166 StatusCode sc(StatusCode::FAILURE);
167
168 try {
169 m_targetState = Gaudi::StateMachine::OFFLINE;
170 Gaudi::Guards::AuditorGuard guard(this,
171 // check if we want to audit the initialize
172 (m_auditorFinalize) ? auditorSvc().get() : 0,
173 IAuditor::Finalize);
174 sc = finalize();
175 if (sc.isSuccess())
176 m_state = m_targetState;
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
199 StatusCode Service::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
206 StatusCode Service::sysReinitialize() {
207
208 StatusCode sc;
209
210 // Check that the current status is the correct one.
211 if ( Gaudi::StateMachine::INITIALIZED != FSMState() ) {
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,
224 IAuditor::ReInitialize);
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
251 StatusCode Service::sysRestart() {
252
253 StatusCode sc;
254
255 // Check that the current status is the correct one.
256 if ( Gaudi::StateMachine::RUNNING != FSMState() ) {
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,
270 IAuditor::ReStart);
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
294 StatusCode Service::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
315 StatusCode Service::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
336 SmartIF<ISvcLocator>& Service::serviceLocator() const {
337 return m_svcLocator;
338 }
339
340 // IProperty implementation
341 // Delegate to the Property manager
342 StatusCode Service::setProperty(const Property& p) {
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
354 StatusCode Service::getProperty(Property* p) const {
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
371 StatusCode Service::setProperties() {
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;
396 m_state = Gaudi::StateMachine::OFFLINE;
397 m_targetState = Gaudi::StateMachine::OFFLINE;
398 m_propertyMgr = new PropertyMgr();
399 m_outputLevel = MSG::NIL;
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);
406 m_outputLevel.declareUpdateHandler(&Service::initOutputLevel, this);
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
432 void Service::initOutputLevel(Property& /*prop*/) {
433 if ( (name() != "MessageSvc") && msgSvc().isValid() ) {
434 msgSvc()->setOutputLevel( name(), m_outputLevel );
435 }
436 updateMsgStreamOutputLevel(m_outputLevel);
437 }
438
439 // Standard Destructor
440 Service::~Service() {
441 delete m_propertyMgr;
442 }
443
444 SmartIF<IAuditorSvc>& Service::auditorSvc() const {
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
455 Service::setServiceManager(ISvcManager *ism) {
456 m_svcManager = ism;
457 }