| # Gaudi Framework, version v21r8 |

Home |  | Generated: 17 Mar 2010

# AuditorSvc.cpp

Go to the documentation of this file.

    00001 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiSvc/src/AuditorSvc/AuditorSvc.cpp,v 1.19 2008/10/27 19:22:21 marcocle Exp $
    00002
    00003 // Include Files
    00004 #include "GaudiKernel/MsgStream.h"
    00005 #include "GaudiKernel/ISvcLocator.h"
    00006 #include "GaudiKernel/IAuditor.h"
    00007 #include "GaudiKernel/INamedInterface.h"
    00008 #include "GaudiKernel/SvcFactory.h"
    00009 #include "GaudiKernel/TypeNameString.h"
    00010 #include "GaudiKernel/GaudiException.h"
    00011 #include "AuditorSvc.h"
    00012
    00013 // Instantiation of a static factory class used by clients to create
    00014 //  instances of this service
    00015 DECLARE_SERVICE_FACTORY(AuditorSvc)
    00016
    00017 using ROOT::Reflex::PluginService;
    00018 //
    00019 // ClassName:   AuditorSvc
    00020 //
    00021 // Description: This service manages Auditors.
    00022 //------------------------------------------------------------------
    00023
    00024 //- private helpers ---
    00025 IAuditor* AuditorSvc::newAuditor_( MsgStream& log, const std::string& name ) {
    00026   // locate the auditor factory, instantiate a new auditor, initialize it
    00027   IAuditor* aud = 0;
    00028   StatusCode sc;
    00029   Gaudi::Utils::TypeNameString item(name) ;
    00030   aud = PluginService::Create<IAuditor*>( item.type(), item.name(), serviceLocator().get() );
    00031   if ( aud ) {
    00032     aud->addRef();
    00033     if ( m_targetState >= Gaudi::StateMachine::INITIALIZED ) {
    00034       sc = aud->sysInitialize();
    00035       if ( sc.isFailure() ) {
    00036         log << MSG::WARNING << "Failed to initialize Auditor " << name << endmsg;
    00037         aud->release();
    00038         aud = 0;
    00039       }
    00040     }
    00041   }
    00042   else {
    00043     log << MSG::WARNING << "Unable to retrieve factory for Auditor " << name << endmsg;
    00044   }
    00045
    00046   return aud;
    00047 }
    00048
    00049 IAuditor* AuditorSvc::findAuditor_( const std::string& name ) {
    00050   // find an auditor by name, return 0 on error
    00051   IAuditor* aud = 0;
    00052   const std::string item_name = Gaudi::Utils::TypeNameString(name).name();
    00053   for ( ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); ++it ) {
    00054     if ( (*it)->name() == item_name ) {
    00055       (*it)->addRef();
    00056       aud = *it;
    00057       break;
    00058     }
    00059   }
    00060
    00061   return aud;
    00062 }
    00063
    00064 StatusCode AuditorSvc::syncAuditors_() {
    00065   if ( m_audNameList.size() == m_pAudList.size() )
    00066     return StatusCode::SUCCESS;
    00067
    00068   MsgStream log( msgSvc(), name() );
    00069   StatusCode sc;
    00070
    00071 //   if ( sc.isFailure() ) {
    00072 //     log << MSG::ERROR << "Unable to locate ObjectManager Service" << endmsg;
    00073 //     return sc;
    00074 //   }
    00075
    00076   // create all declared Auditors that do not yet exist
    00077   for ( VectorName::iterator it = m_audNameList.begin(); it != m_audNameList.end(); it++ ) {
    00078
    00079     // this is clumsy, but the PropertyMgr won't tell us when my property changes right
    00080     // under my nose, so I'll have to figure this out the hard way
    00081     if ( !findAuditor_( *it ) ) { // if auditor does not yet exist
    00082       IAuditor* aud = newAuditor_( log, *it );
    00083
    00084       if ( aud != 0 ) {
    00085         m_pAudList.push_back( aud );
    00086       }
    00087       else {
    00088         log << MSG::ERROR << "Error constructing Auditor " << *it << endmsg;
    00089         sc = StatusCode::FAILURE;
    00090       }
    00091     }
    00092   }
    00093   return sc;
    00094 }
    00095
    00096 // Standard Constructor.
    00097 //   Input:  name   String with service name
    00098 //   Input:  svc    Pointer to service locator interface
    00099 AuditorSvc::AuditorSvc( const std::string& name, ISvcLocator* svc )
    00100 : base_class(name, svc) {
    00101   declareProperty("Auditors", m_audNameList );
    00102   declareProperty("Enable", m_isEnabled = true);
    00103   m_pAudList.clear();
    00104 }
    00105
    00106 // Destructor.
    00107 AuditorSvc::~AuditorSvc() {
    00108 }
    00109
    00110 // Inherited Service overrides:
    00111 //
    00112   // Initialize the service.
    00113 StatusCode AuditorSvc::initialize() {
    00114   StatusCode sc = Service::initialize();
    00115   if ( sc.isFailure() )
    00116     return sc;
    00117
    00118   // create auditor objects for all named auditors
    00119   sc = syncAuditors_();
    00120
    00121   return sc;
    00122 }
    00123
    00124   // Finalise the service.
    00125 StatusCode AuditorSvc::finalize() {
    00126
    00127   for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) {
    00128     if((*it)->isEnabled()) {
    00129        (*it)->sysFinalize().ignore();
    00130     }
    00131     (*it)->release();
    00132   }
    00133   m_pAudList.clear();
    00134
    00135   // Finalize this specific service
    00136   return Service::finalize();
    00137 }
    00138
    00139 // --------- "Before" methods ---------
    00140 void AuditorSvc::before(StandardEventType evt, INamedInterface* obj) {
    00141   if (!isEnabled()) return;
    00142   for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) {
    00143     if((*it)->isEnabled()) {
    00144       (*it)->before(evt,obj);
    00145     }
    00146   }
    00147 }
    00148
    00149 void AuditorSvc::before(StandardEventType evt, const std::string &name) {
    00150   if (!isEnabled()) return;
    00151   for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) {
    00152     if((*it)->isEnabled()) {
    00153       (*it)->before(evt,name);
    00154     }
    00155   }
    00156 }
    00157
    00158 void AuditorSvc::before(CustomEventTypeRef evt, INamedInterface* obj) {
    00159   if (!isEnabled()) return;
    00160   for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) {
    00161     if((*it)->isEnabled()) {
    00162       (*it)->before(evt,obj);
    00163     }
    00164   }
    00165 }
    00166
    00167 void AuditorSvc::before(CustomEventTypeRef evt, const std::string &name) {
    00168   if (!isEnabled()) return;
    00169   for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) {
    00170     if((*it)->isEnabled()) {
    00171       (*it)->before(evt,name);
    00172     }
    00173   }
    00174 }
    00175
    00176 // --------- "After" methods ---------
    00177 void AuditorSvc::after(StandardEventType evt, INamedInterface* obj, const StatusCode& sc) {
    00178   if (!isEnabled()) return;
    00179   for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) {
    00180     if((*it)->isEnabled()) {
    00181       (*it)->after(evt,obj,sc);
    00182     }
    00183   }
    00184 }
    00185
    00186 void AuditorSvc::after(StandardEventType evt, const std::string &name, const StatusCode& sc) {
    00187   if (!isEnabled()) return;
    00188   for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) {
    00189     if((*it)->isEnabled()) {
    00190       (*it)->after(evt,name,sc);
    00191     }
    00192   }
    00193 }
    00194
    00195 void AuditorSvc::after(CustomEventTypeRef evt, INamedInterface* obj, const StatusCode& sc) {
    00196   if (!isEnabled()) return;
    00197   for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) {
    00198     if((*it)->isEnabled()) {
    00199       (*it)->after(evt,obj,sc);
    00200     }
    00201   }
    00202 }
    00203
    00204 void AuditorSvc::after(CustomEventTypeRef evt, const std::string &name, const StatusCode& sc) {
    00205   if (!isEnabled()) return;
    00206   for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) {
    00207     if((*it)->isEnabled()) {
    00208       (*it)->after(evt,name,sc);
    00209     }
    00210   }
    00211 }
    00212
    00213 // --------- obsolete methods ---------
    00214 #define OBSOLETION(name) \
    00215   void AuditorSvc::name(INamedInterface*) { \
    00216     throw GaudiException("The method IAuditor::" #name " is obsolete do not call it.", \
    00217                          "AuditorSvc::" #name , StatusCode::FAILURE); \
    00218   }
    00219
    00220 OBSOLETION(beforeInitialize)
    00221 OBSOLETION(afterInitialize)
    00222
    00223 OBSOLETION(beforeReinitialize)
    00224 OBSOLETION(afterReinitialize)
    00225
    00226 OBSOLETION(beforeExecute)
    00227 void AuditorSvc::afterExecute(INamedInterface*,const StatusCode&) {
    00228   throw GaudiException("The method afterExecute is obsolete do not call it.",
    00229                        "AuditorSvc::afterExecute" , StatusCode::FAILURE);
    00230 }
    00231
    00232 OBSOLETION(beforeBeginRun)
    00233 OBSOLETION(afterBeginRun)
    00234
    00235 OBSOLETION(beforeEndRun)
    00236 OBSOLETION(afterEndRun)
    00237
    00238 OBSOLETION(beforeFinalize)
    00239 OBSOLETION(afterFinalize)
    00240
    00241
    00242 bool AuditorSvc::isEnabled( ) const {
    00243   return m_isEnabled;
    00244 }
    00245
    00246 StatusCode AuditorSvc::sysInitialize(){
    00247   return Service::sysInitialize();
    00248 }
    00249 StatusCode AuditorSvc::sysFinalize(){
    00250   return Service::sysFinalize();
    00251 }
    00252
    00253
    00254 IAuditor* AuditorSvc::getAuditor( const std::string& name ) {
    00255   // by interactively setting properties, auditors might be out of sync
    00256   if ( !syncAuditors_().isSuccess() ) {
    00257     // as we didn't manage to sync auditors, the safest bet is to assume the
    00258     // worse...
    00259     // So don't let clients play with an AuditorSvc in an inconsistent state
    00260     return 0;
    00261   }
    00262
    00263   // search available auditors, returns 0 on error
    00264   return findAuditor_( name );
    00265 }