Gaudi Framework, version v23r9

Home   Generated: Thu Jul 18 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AlgTool.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/AlgTool.h"
6 
8 #include "GaudiKernel/Service.h"
9 #include "GaudiKernel/Auditor.h"
10 #include "GaudiKernel/System.h"
14 #include "GaudiKernel/Guards.h"
15 
16 //------------------------------------------------------------------------------
18 ( const InterfaceID& riid ,
19  void** ppvi )
20  //------------------------------------------------------------------------------
21 {
22  if ( 0 == ppvi ) { return StatusCode::FAILURE ; } // RETURN
23  StatusCode sc = base_class::queryInterface(riid,ppvi);
24  if (sc.isSuccess()) {
25  return sc;
26  }
27  else {
28  for ( InterfaceList::iterator it = m_interfaceList.begin() ;
29  m_interfaceList.end() != it ; ++it )
30  {
31  if ( !it->first.versionMatch ( riid ) ) { continue ; }
32  // OK
33  *ppvi = it->second ;
34  addRef() ;
35  return SUCCESS ; // RETURN
36  }
37  *ppvi = 0 ;
38  return NO_INTERFACE ; // RETURN
39  }
40  // cannot reach this point
41 }
42 //------------------------------------------------------------------------------
43 void AlgTool::declInterface( const InterfaceID& iid, void* ii)
44  //------------------------------------------------------------------------------
45 {
47 }
48 
49 
50 //------------------------------------------------------------------------------
51 const std::string& AlgTool::name() const
52  //------------------------------------------------------------------------------
53 {
54  return m_name;
55 }
56 
57 //------------------------------------------------------------------------------
58 const std::string& AlgTool::type() const
59  //------------------------------------------------------------------------------
60 {
61  return m_type;
62 }
63 
64 //------------------------------------------------------------------------------
66  //------------------------------------------------------------------------------
67 {
68  return m_parent;
69 }
70 
71 //------------------------------------------------------------------------------
73  //------------------------------------------------------------------------------
74 {
75  return m_svcLocator;
76 }
77 
78 //------------------------------------------------------------------------------
80  //------------------------------------------------------------------------------
81 {
82  return m_messageSvc;
83 }
84 
85 //------------------------------------------------------------------------------
87  //------------------------------------------------------------------------------
88 {
89  if ( 0 == m_ptoolSvc ) {
90  StatusCode sc = service( "ToolSvc", m_ptoolSvc, true );
91  if( sc.isFailure() ) {
92  throw GaudiException("Service [ToolSvc] not found", name(), sc);
93  }
94  }
95  return m_ptoolSvc;
96 }
97 
98 //------------------------------------------------------------------------------
100  //------------------------------------------------------------------------------
101 {
102  return m_propertyMgr->setProperty(p);
103 }
104 
105 //------------------------------------------------------------------------------
107  //------------------------------------------------------------------------------
108 {
109  return m_propertyMgr->setProperty(s);
110 }
111 
112 //------------------------------------------------------------------------------
114  //------------------------------------------------------------------------------
115 {
116  return m_propertyMgr->setProperty(n,v);
117 }
118 
119 //------------------------------------------------------------------------------
121  //------------------------------------------------------------------------------
122 {
123  return m_propertyMgr->getProperty(p);
124 }
125 
126 //------------------------------------------------------------------------------
128 {
129  return m_propertyMgr->getProperty(n);
130 }
131 
132 //------------------------------------------------------------------------------
134  //------------------------------------------------------------------------------
135 {
136  return m_propertyMgr->getProperty(n,v);
137 }
138 
139 //------------------------------------------------------------------------------
141  //------------------------------------------------------------------------------
142 {
143  return m_propertyMgr->getProperties();
144 }
145 
146 //------------------------------------------------------------------------------
148  //------------------------------------------------------------------------------
149 {
150  if( m_svcLocator == 0) {
151  return StatusCode::FAILURE;
152  }
153  SmartIF<IJobOptionsSvc> jos(m_svcLocator->service("JobOptionsSvc"));
154  if( !jos.isValid() ) return StatusCode::FAILURE;
155 
156  // set first generic Properties
157  StatusCode sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this );
158  if( sc.isFailure() ) return StatusCode::FAILURE;
159 
160  // set specific Properties
161  if (isGaudiThreaded(name())) {
162  if(jos->setMyProperties( name(), this ).isFailure()) {
163  return StatusCode::FAILURE;
164  }
165  }
166 
167  // Change my own outputlevel
168  if ( 0 != m_messageSvc )
169  {
170  if ( MSG::NIL != m_outputLevel )
171  { m_messageSvc -> setOutputLevel ( name () , m_outputLevel ) ; }
173  }
174 
175  return StatusCode::SUCCESS;
176 }
177 
178 //------------------------------------------------------------------------------
180  const std::string& name,
181  const IInterface* parent)
182  //------------------------------------------------------------------------------
183  : m_outputLevel ( MSG::NIL )
184  , m_type ( type )
185  , m_name ( name )
186  , m_parent ( parent )
187  , m_svcLocator ( 0 )
188  , m_messageSvc ( 0 )
189  , m_ptoolSvc ( 0 )
190  , m_pMonitorSvc ( NULL )
191  , m_propertyMgr ( new PropertyMgr() )
192  , m_interfaceList ( )
193  , m_threadID ( )
194  , m_pAuditorSvc ( 0 )
195  , m_auditInit ( false )
196  , m_state ( Gaudi::StateMachine::CONFIGURED )
197  , m_targetState ( Gaudi::StateMachine::CONFIGURED )
198 {
199  addRef(); // Initial count set to 1
200 
201  declareProperty( "MonitorService", m_monitorSvcName = "MonitorSvc" );
202 
203  { // get the "OutputLevel" property from parent
204  const Property* _p = Gaudi::Utils::getProperty ( parent , "OutputLevel") ;
205  if ( 0 != _p ) { m_outputLevel.assign( *_p ) ; }
206  declareProperty ( "OutputLevel" , m_outputLevel ) ;
208  }
209 
210  IInterface* _p = const_cast<IInterface*> ( parent ) ;
211 
212  if ( Algorithm* _alg = dynamic_cast<Algorithm*> ( _p ) )
213  {
214  m_svcLocator = _alg -> serviceLocator () ;
215  m_messageSvc = _alg -> msgSvc () ;
216  m_threadID = getGaudiThreadIDfromName ( _alg -> name() ) ;
217  }
218  else if ( Service* _svc = dynamic_cast<Service*> ( _p ) )
219  {
220  m_svcLocator = _svc -> serviceLocator () ;
221  m_messageSvc = _svc -> msgSvc () ;
222  m_threadID = getGaudiThreadIDfromName ( _svc -> name() ) ;
223  }
224  else if ( AlgTool* _too = dynamic_cast<AlgTool*> ( _p ) )
225  {
226  m_svcLocator = _too -> m_svcLocator;
227  m_messageSvc = _too -> m_messageSvc;
229  }
230  else if ( Auditor* _aud = dynamic_cast<Auditor*> ( _p ) )
231  {
232  m_svcLocator = _aud -> serviceLocator() ;
233  m_messageSvc = _aud -> msgSvc() ;
234  m_threadID = getGaudiThreadIDfromName ( _aud -> name() ) ;
235  }
236  else
237  {
238  throw GaudiException
239  ( "Failure to create tool '"
240  + type + "/" + name + "': illegal parent type '"
241  + System::typeinfoName(typeid(*_p)) + "'", "AlgTool", 0 );
242  }
243 
244 
245  { // audit tools
246  SmartIF<IProperty> appMgr(m_svcLocator->service("ApplicationMgr"));
247  if ( !appMgr.isValid() ) {
248  throw GaudiException("Could not locate ApplicationMgr","AlgTool",0);
249  }
250  const Property* p = Gaudi::Utils::getProperty( appMgr , "AuditTools");
251  if ( 0 != p ) { m_auditInit.assign ( *p ) ; }
252  declareProperty ( "AuditTools", m_auditInit );
253  bool audit = m_auditInit.value();
254  // Declare common AlgTool properties with their defaults
255  declareProperty ( "AuditInitialize" , m_auditorInitialize = audit ) ;
256  declareProperty ( "AuditStart" , m_auditorStart = audit ) ;
257  declareProperty ( "AuditStop" , m_auditorStop = audit ) ;
258  declareProperty ( "AuditFinalize" , m_auditorFinalize = audit ) ;
259  }
260 
261  // check thread ID and try if tool name indicates thread ID
262  if ( m_threadID.empty() )
264 }
265 
266 //-----------------------------------------------------------------------------
268  //-----------------------------------------------------------------------------
269  StatusCode sc;
270 
271  try {
273  Gaudi::Guards::AuditorGuard guard(this,
274  // check if we want to audit the initialize
277  sc = initialize();
278  if (sc.isSuccess())
280  return sc;
281  }
282  catch( const GaudiException& Exception ) {
283  MsgStream log ( msgSvc() , name() + ".sysInitialize()" );
284  log << MSG::FATAL << " Exception with tag=" << Exception.tag()
285  << " is caught " << endmsg;
286  log << MSG::ERROR << Exception << endmsg;
287  }
288  catch( const std::exception& Exception ) {
289  MsgStream log ( msgSvc() , name() + ".sysInitialize()" );
290  log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
291  log << MSG::ERROR << Exception.what() << endmsg;
292  }
293  catch( ... ) {
294  MsgStream log ( msgSvc() , name() + ".sysInitialize()" );
295  log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
296  }
297  return StatusCode::FAILURE ;
298 
299 }
300 
301 //------------------------------------------------------------------------------
303 //------------------------------------------------------------------------------
304 {
305  // For the time being there is nothing to be done here.
306  // Setting the properties is done by the ToolSvc calling setProperties()
307  // explicitly.
308  return StatusCode::SUCCESS;
309 }
310 
311 //-----------------------------------------------------------------------------
313  //-----------------------------------------------------------------------------
314  StatusCode sc;
315 
316  try {
318  Gaudi::Guards::AuditorGuard guard(this,
319  // check if we want to audit the initialize
320  (m_auditorStart) ? auditorSvc() : 0,
322  sc = start();
323  if (sc.isSuccess())
325  return sc;
326  }
327  catch( const GaudiException& Exception ) {
328  MsgStream log ( msgSvc() , name() + ".sysStart()" );
329  log << MSG::FATAL << " Exception with tag=" << Exception.tag()
330  << " is caught " << endmsg;
331  log << MSG::ERROR << Exception << endmsg;
332  }
333  catch( const std::exception& Exception ) {
334  MsgStream log ( msgSvc() , name() + ".sysStart()" );
335  log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
336  log << MSG::ERROR << Exception.what() << endmsg;
337  }
338  catch( ... ) {
339  MsgStream log ( msgSvc() , name() + ".sysStart()" );
340  log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
341  }
342  return StatusCode::FAILURE ;
343 
344 }
345 
346 //------------------------------------------------------------------------------
348  //------------------------------------------------------------------------------
349 {
350  // For the time being there is nothing to be done here.
351  return StatusCode::SUCCESS;
352 }
353 
354 //-----------------------------------------------------------------------------
356  //-----------------------------------------------------------------------------
357  StatusCode sc;
358 
359  try {
361  Gaudi::Guards::AuditorGuard guard(this,
362  // check if we want to audit the initialize
363  (m_auditorStop) ? auditorSvc() : 0,
365  sc = stop();
366  if (sc.isSuccess())
368  return sc;
369  }
370  catch( const GaudiException& Exception ) {
371  MsgStream log ( msgSvc() , name() + ".sysStop()" );
372  log << MSG::FATAL << " Exception with tag=" << Exception.tag()
373  << " is caught " << endmsg;
374  log << MSG::ERROR << Exception << endmsg;
375  }
376  catch( const std::exception& Exception ) {
377  MsgStream log ( msgSvc() , name() + ".sysStop()" );
378  log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
379  log << MSG::ERROR << Exception.what() << endmsg;
380  }
381  catch( ... ) {
382  MsgStream log ( msgSvc() , name() + ".sysStop()" );
383  log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
384  }
385  return StatusCode::FAILURE ;
386 
387 }
388 
389 //------------------------------------------------------------------------------
391  //------------------------------------------------------------------------------
392 {
393  // For the time being there is nothing to be done here.
394  return StatusCode::SUCCESS;
395 }
396 
397 //-----------------------------------------------------------------------------
399  //-----------------------------------------------------------------------------
400 
401  StatusCode sc;
402 
403  try {
405  Gaudi::Guards::AuditorGuard guard(this,
406  // check if we want to audit the initialize
407  (m_auditorFinalize) ? auditorSvc() : 0,
409  sc = finalize();
410  if (sc.isSuccess())
412  return sc;
413  }
414  catch( const GaudiException& Exception ) {
415  MsgStream log ( msgSvc() , name() + ".sysFinalize()" );
416  log << MSG::FATAL << " Exception with tag=" << Exception.tag()
417  << " is caught " << endmsg;
418  log << MSG::ERROR << Exception << endmsg;
419  }
420  catch( const std::exception& Exception ) {
421  MsgStream log ( msgSvc() , name() + ".sysFinalize()" );
422  log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
423  log << MSG::ERROR << Exception.what() << endmsg;
424  }
425  catch( ... ) {
426  MsgStream log ( msgSvc() , name() + ".sysFinalize()" );
427  log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
428  }
429  return StatusCode::FAILURE;
430 
431 }
432 //------------------------------------------------------------------------------
434  //------------------------------------------------------------------------------
435 {
436  // For the time being there is nothing to be done here.
437  return StatusCode::SUCCESS;
438 }
439 
440 //-----------------------------------------------------------------------------
442  //-----------------------------------------------------------------------------
443  StatusCode sc;
444 
445  // Check that the current status is the correct one.
447  MsgStream log ( msgSvc() , name() );
448  log << MSG::ERROR
449  << "sysReinitialize(): cannot reinitialize tool not initialized"
450  << endmsg;
451  return StatusCode::FAILURE;
452  }
453 
454  try {
455  Gaudi::Guards::AuditorGuard guard(this,
456  // check if we want to audit the initialize
459  sc = reinitialize();
460  return sc;
461  }
462  catch( const GaudiException& Exception ) {
463  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
464  log << MSG::FATAL << " Exception with tag=" << Exception.tag()
465  << " is caught" << endmsg;
466  log << MSG::ERROR << Exception << endmsg;
467  }
468  catch( const std::exception& Exception ) {
469  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
470  log << MSG::FATAL << " Standard std::exception is caught" << endmsg;
471  log << MSG::ERROR << Exception.what() << endmsg;
472  }
473  catch( ... ) {
474  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
475  log << MSG::FATAL << "UNKNOWN Exception is caught" << endmsg;
476  }
477  return StatusCode::FAILURE ;
478 
479 }
480 
481 //------------------------------------------------------------------------------
483 //------------------------------------------------------------------------------
484 {
485  /* @TODO
486  * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
487  * is causing too many problems
488  *
489  // Default implementation is finalize+initialize
490  StatusCode sc = finalize();
491  if (sc.isFailure()) {
492  MsgStream log ( msgSvc() , name() );
493  log << MSG::ERROR << "reinitialize(): cannot be finalized" << endmsg;
494  return sc;
495  }
496  sc = initialize();
497  if (sc.isFailure()) {
498  MsgStream log ( msgSvc() , name() );
499  log << MSG::ERROR << "reinitialize(): cannot be initialized" << endmsg;
500  return sc;
501  }
502  */
503  return StatusCode::SUCCESS;
504 }
505 
506 //-----------------------------------------------------------------------------
508  //-----------------------------------------------------------------------------
509  StatusCode sc;
510 
511  // Check that the current status is the correct one.
513  MsgStream log ( msgSvc() , name() );
514  log << MSG::ERROR
515  << "sysRestart(): cannot reinitialize tool not started"
516  << endmsg;
517  return StatusCode::FAILURE;
518  }
519 
520  try {
522  Gaudi::Guards::AuditorGuard guard(this,
523  // check if we want to audit the initialize
524  (m_auditorRestart) ? auditorSvc() : 0,
526  sc = restart();
527  return sc;
528  }
529  catch( const GaudiException& Exception ) {
530  MsgStream log ( msgSvc() , name() + ".sysRestart()" );
531  log << MSG::FATAL << " Exception with tag=" << Exception.tag()
532  << " is caught" << endmsg;
533  log << MSG::ERROR << Exception << endmsg;
534  }
535  catch( const std::exception& Exception ) {
536  MsgStream log ( msgSvc() , name() + ".sysRestart()" );
537  log << MSG::FATAL << " Standard std::exception is caught" << endmsg;
538  log << MSG::ERROR << Exception.what() << endmsg;
539  }
540  catch( ... ) {
541  MsgStream log ( msgSvc() , name() + ".sysRestart()" );
542  log << MSG::FATAL << "UNKNOWN Exception is caught" << endmsg;
543  }
544  return StatusCode::FAILURE ;
545 
546 }
547 
548 //------------------------------------------------------------------------------
550  //------------------------------------------------------------------------------
551 {
552  // Default implementation is stop+start
553  StatusCode sc = stop();
554  if (sc.isFailure()) {
555  MsgStream log ( msgSvc() , name() );
556  log << MSG::ERROR << "restart(): cannot be stopped" << endmsg;
557  return sc;
558  }
559  sc = start();
560  if (sc.isFailure()) {
561  MsgStream log ( msgSvc() , name() );
562  log << MSG::ERROR << "restart(): cannot be started" << endmsg;
563  return sc;
564  }
565  return StatusCode::SUCCESS;
566 }
567 
568 //------------------------------------------------------------------------------
570  //------------------------------------------------------------------------------
571 {
572  delete m_propertyMgr;
573  if( m_ptoolSvc ) m_ptoolSvc->release();
576 }
577 
578 //------------------------------------------------------------------------------
582  bool createIf,
583  const InterfaceID& iid,
584  void** ppSvc) const {
585  const ServiceLocatorHelper helper(*serviceLocator(), *this);
586  return helper.getService(svcName, createIf, iid, ppSvc);
587 }
588 
589 //------------------------------------------------------------------------------
592  const std::string& svcName,
593  const InterfaceID& iid,
594  void** ppSvc) const {
595  const ServiceLocatorHelper helper(*serviceLocator(), *this);
596  return helper.createService(svcType, svcName, iid, ppSvc);
597 }
598 
599 SmartIF<IService> AlgTool::service(const std::string& name, const bool createIf, const bool quiet) const {
600  const ServiceLocatorHelper helper(*serviceLocator(), *this);
601  return helper.service(name, quiet, createIf);
602 }
603 
604 //-----------------------------------------------------------------------------
606 //---------------------------------------------------------------------------
607  if ( 0 == m_pAuditorSvc ) {
608  StatusCode sc = service( "AuditorSvc", m_pAuditorSvc, true );
609  if( sc.isFailure() ) {
610  throw GaudiException("Service [AuditorSvc] not found", name(), sc);
611  }
612  }
613  return m_pAuditorSvc;
614 }
615 
616 
617 //-----------------------------------------------------------------------------
619 //-----------------------------------------------------------------------------
620  // do nothing... yet ?
621 }
622 

Generated at Thu Jul 18 2013 12:18:03 for Gaudi Framework, version v23r9 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004