![]() |
|
|
Generated: 8 Jan 2009 |
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/ListItem.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 ListItem item(name) ; 00030 aud = PluginService::Create<IAuditor*>( item.type(), item.name(), serviceLocator() ); 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 << endreq; 00037 aud->release(); 00038 aud = 0; 00039 } 00040 } 00041 } 00042 else { 00043 log << MSG::WARNING << "Unable to retrieve factory for Auditor " << name << endreq; 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 = ListItem(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" << endreq; 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 << endreq; 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 : Service(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 // Query the interfaces. 00140 // Input: riid, Requested interface ID 00141 // ppvInterface, Pointer to requested interface 00142 // Return: StatusCode indicating SUCCESS or FAILURE. 00143 // N.B. Don't forget to release the interface after use!!! 00144 StatusCode AuditorSvc::queryInterface( const InterfaceID& riid, void** ppvInterface ) { 00145 if ( IID_IAuditorSvc == riid ) { 00146 *ppvInterface = (IAuditorSvc*)this; 00147 } 00148 else if ( IID_IAuditor == riid ) { 00149 *ppvInterface = (IAuditor*)this; 00150 } 00151 else { 00152 // Interface is not directly available: try out a base class 00153 return Service::queryInterface(riid, ppvInterface); 00154 } 00155 addRef(); 00156 return StatusCode::SUCCESS; 00157 } 00158 00159 // --------- "Before" methods --------- 00160 void AuditorSvc::before(StandardEventType evt, INamedInterface* obj) { 00161 if (!isEnabled()) return; 00162 for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) { 00163 if((*it)->isEnabled()) { 00164 (*it)->before(evt,obj); 00165 } 00166 } 00167 } 00168 00169 void AuditorSvc::before(StandardEventType evt, const std::string &name) { 00170 if (!isEnabled()) return; 00171 for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) { 00172 if((*it)->isEnabled()) { 00173 (*it)->before(evt,name); 00174 } 00175 } 00176 } 00177 00178 void AuditorSvc::before(CustomEventTypeRef evt, INamedInterface* obj) { 00179 if (!isEnabled()) return; 00180 for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) { 00181 if((*it)->isEnabled()) { 00182 (*it)->before(evt,obj); 00183 } 00184 } 00185 } 00186 00187 void AuditorSvc::before(CustomEventTypeRef evt, const std::string &name) { 00188 if (!isEnabled()) return; 00189 for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) { 00190 if((*it)->isEnabled()) { 00191 (*it)->before(evt,name); 00192 } 00193 } 00194 } 00195 00196 // --------- "After" methods --------- 00197 void AuditorSvc::after(StandardEventType evt, INamedInterface* obj, const StatusCode& sc) { 00198 if (!isEnabled()) return; 00199 for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) { 00200 if((*it)->isEnabled()) { 00201 (*it)->after(evt,obj,sc); 00202 } 00203 } 00204 } 00205 00206 void AuditorSvc::after(StandardEventType evt, const std::string &name, const StatusCode& sc) { 00207 if (!isEnabled()) return; 00208 for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) { 00209 if((*it)->isEnabled()) { 00210 (*it)->after(evt,name,sc); 00211 } 00212 } 00213 } 00214 00215 void AuditorSvc::after(CustomEventTypeRef evt, INamedInterface* obj, const StatusCode& sc) { 00216 if (!isEnabled()) return; 00217 for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) { 00218 if((*it)->isEnabled()) { 00219 (*it)->after(evt,obj,sc); 00220 } 00221 } 00222 } 00223 00224 void AuditorSvc::after(CustomEventTypeRef evt, const std::string &name, const StatusCode& sc) { 00225 if (!isEnabled()) return; 00226 for (ListAudits::iterator it = m_pAudList.begin() ; it != m_pAudList.end(); it++) { 00227 if((*it)->isEnabled()) { 00228 (*it)->after(evt,name,sc); 00229 } 00230 } 00231 } 00232 00233 // --------- obsolete methods --------- 00234 #define OBSOLETION(name) \ 00235 void AuditorSvc::name(INamedInterface*) { \ 00236 throw GaudiException("The method IAuditor::" #name " is obsolete do not call it.", \ 00237 "AuditorSvc::" #name , StatusCode::FAILURE); \ 00238 } 00239 00240 OBSOLETION(beforeInitialize) 00241 OBSOLETION(afterInitialize) 00242 00243 OBSOLETION(beforeReinitialize) 00244 OBSOLETION(afterReinitialize) 00245 00246 OBSOLETION(beforeExecute) 00247 void AuditorSvc::afterExecute(INamedInterface*,const StatusCode&) { 00248 throw GaudiException("The method afterExecute is obsolete do not call it.", 00249 "AuditorSvc::afterExecute" , StatusCode::FAILURE); 00250 } 00251 00252 OBSOLETION(beforeBeginRun) 00253 OBSOLETION(afterBeginRun) 00254 00255 OBSOLETION(beforeEndRun) 00256 OBSOLETION(afterEndRun) 00257 00258 OBSOLETION(beforeFinalize) 00259 OBSOLETION(afterFinalize) 00260 00261 00262 bool AuditorSvc::isEnabled( ) const { 00263 return m_isEnabled; 00264 } 00265 00266 StatusCode AuditorSvc::sysInitialize(){ 00267 return Service::sysInitialize(); 00268 } 00269 StatusCode AuditorSvc::sysFinalize(){ 00270 return Service::sysFinalize(); 00271 } 00272 00273 00274 IAuditor* AuditorSvc::getAuditor( const std::string& name ) { 00275 // by interactively setting properties, auditors might be out of sync 00276 if ( !syncAuditors_().isSuccess() ) { 00277 // as we didn't manage to sync auditors, the safest bet is to assume the 00278 // worse... 00279 // So don't let clients play with an AuditorSvc in an inconsistent state 00280 return 0; 00281 } 00282 00283 // search available auditors, returns 0 on error 00284 return findAuditor_( name ); 00285 }