Bootstrap.cpp
Go to the documentation of this file.00001
00002
00003 #include <iostream>
00004
00005 #include "GaudiKernel/Bootstrap.h"
00006 #include "GaudiKernel/System.h"
00007
00008 #include "GaudiKernel/IInterface.h"
00009 #include "GaudiKernel/IAlgorithm.h"
00010 #include "GaudiKernel/IService.h"
00011 #include "GaudiKernel/IAppMgrUI.h"
00012 #include "GaudiKernel/ISvcLocator.h"
00013 #include "GaudiKernel/IClassManager.h"
00014
00015 #include "Reflex/PluginService.h"
00016 using ROOT::Reflex::PluginService;
00017
00018
00019 namespace Gaudi
00020 {
00021
00037 class BootSvcLocator : public implements1<ISvcLocator> {
00038 public:
00039 BootSvcLocator();
00040 virtual ~BootSvcLocator();
00041 #if !defined(GAUDI_V22_API)|| defined(G22_NEW_SVCLOCATOR)
00042 virtual StatusCode getService( const Gaudi::Utils::TypeNameString& typeName,
00043 const InterfaceID& iid,
00044 IInterface*& pinterface );
00045 virtual StatusCode getService( const Gaudi::Utils::TypeNameString& typeName,
00046 IService*& svc,
00047 const bool createIf = true);
00048 #endif
00049 virtual const std::list<IService*>& getServices( ) const;
00050 virtual bool existsService( const std::string& name ) const;
00051
00053 virtual SmartIF<IService> &service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf = true);
00054
00055 };
00056 }
00057
00058
00059 static SmartIF<ISvcLocator> s_svclocInstance;
00060 static SmartIF<IAppMgrUI> s_appmgrInstance;
00061
00062
00063 IAppMgrUI* Gaudi::createApplicationMgr(const std::string& dllname,
00064 const std::string& factname)
00065
00066 {
00067
00068
00069 if ( !s_appmgrInstance.isValid() ) {
00070 s_appmgrInstance = createApplicationMgrEx(dllname, factname);
00071 s_svclocInstance = s_appmgrInstance;
00072 }
00073 return s_appmgrInstance.get();
00074 }
00075
00076
00077 IAppMgrUI* Gaudi::createApplicationMgrEx(const std::string& dllname,
00078 const std::string& factname)
00079
00080 {
00081 StatusCode status;
00082 IInterface* iif;
00083 IAppMgrUI* iappmgr;
00084
00085
00086 iif = Gaudi::createInstance( "ApplicationMgr", factname, dllname );
00087 if( iif == 0 ) {
00088 return 0;
00089 }
00090
00091 status = iif->queryInterface( IAppMgrUI::interfaceID(), pp_cast<void>(&iappmgr) );
00092 if( status.isFailure() ) {
00093 return 0;
00094 }
00095 iif->release();
00096 return iappmgr;
00097 }
00098
00099
00100 ISvcLocator* Gaudi::svcLocator()
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 {
00116 if( !s_svclocInstance.isValid() ) {
00117 IAppMgrUI* iappmgr = createApplicationMgr();
00118 if( iappmgr ) {
00119 s_svclocInstance = iappmgr;
00120 if( s_svclocInstance.isValid() ) {
00121 return s_svclocInstance;
00122 }
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 }
00135 return s_svclocInstance;
00136 }
00137
00138
00139 ISvcLocator* Gaudi::setInstance(ISvcLocator* newInstance)
00140
00141 {
00142 ISvcLocator* oldInstance = s_svclocInstance.get();
00143 s_svclocInstance = newInstance;
00144 s_appmgrInstance = s_svclocInstance;
00145 return oldInstance;
00146 }
00147
00148
00149 IAppMgrUI* Gaudi::setInstance(IAppMgrUI* newInstance)
00150
00151 {
00152 IAppMgrUI* oldInstance = s_appmgrInstance.get();
00153 s_appmgrInstance = newInstance;
00154 s_svclocInstance = s_appmgrInstance;
00155 return oldInstance;
00156 }
00157
00158
00159 IInterface* Gaudi::createInstance( const std::string& name,
00160 const std::string& factname,
00161 const std::string& dllname)
00162
00163 {
00164
00165 IInterface* ii = PluginService::Create<IInterface*>(factname,(IInterface*)0);
00166 if ( ii ) return ii;
00167 IService* is = PluginService::Create<IService*>(factname, name, (ISvcLocator*)0);
00168 if ( is ) return is;
00169 IAlgorithm* ia = PluginService::Create<IAlgorithm*>(factname, name, (ISvcLocator*)0);
00170 if ( ia ) return ia;
00171
00172 StatusCode status;
00173 void* libHandle = 0;
00174 status = System::loadDynamicLib( dllname, &libHandle);
00175 if ( status.isSuccess() ) {
00176 IInterface* ii = PluginService::Create<IInterface*>(factname, (IInterface*)0);
00177 if ( ii ) return ii;
00178 IService* is = PluginService::Create<IService*>(factname, name, (ISvcLocator*)0);
00179 if ( is ) return is;
00180 IAlgorithm* ia = PluginService::Create<IAlgorithm*>(factname, name, (ISvcLocator*)0);
00181 if ( ia ) return ia;
00182
00183 return 0;
00184 }
00185 else {
00186
00187 std::cout << System::getLastErrorString() << std::endl;
00188 std::cout << "Gaudi::Bootstrap: Not found DLL " << dllname << std::endl;
00189 return 0;
00190 }
00191 }
00192
00193 namespace {
00194 class ShadowEntry {
00195 public:
00196 std::string dllName;
00197 std::string facName;
00198 IFactory* fac;
00199 public:
00200 ShadowEntry() {
00201 }
00202 ShadowEntry(const std::string& d, const std::string& n, const IFactory* f) {
00203 dllName = d;
00204 facName = n;
00205 fac = const_cast<IFactory*>(f);
00206 }
00207 ShadowEntry(const ShadowEntry& copy) {
00208 dllName = copy.dllName;
00209 facName = copy.facName;
00210 fac = copy.fac;
00211 }
00212 ShadowEntry& operator=(const ShadowEntry& copy) {
00213 dllName = copy.dllName;
00214 facName = copy.facName;
00215 fac = copy.fac;
00216 return *this;
00217 }
00218 };
00219 }
00220
00221
00222 IAppMgrUI* Gaudi::createApplicationMgr(const std::string& dllname ) {
00223
00224 return createApplicationMgr(dllname, "ApplicationMgr");
00225 }
00226
00227
00228 IAppMgrUI* Gaudi::createApplicationMgr() {
00229
00230 return createApplicationMgr("GaudiSvc", "ApplicationMgr");
00231 }
00232
00233
00234
00235
00236
00237 static std::list<IService*> s_bootServices;
00238 static SmartIF<IService> s_bootService;
00239 static SmartIF<IInterface> s_bootInterface;
00240
00241 using Gaudi::BootSvcLocator;
00242
00243 BootSvcLocator::BootSvcLocator() {
00244 }
00245 BootSvcLocator::~BootSvcLocator() {
00246 }
00247
00248 #if !defined(GAUDI_V22_API) || defined(G22_NEW_SVCLOCATOR)
00249 StatusCode Gaudi::BootSvcLocator::getService( const Gaudi::Utils::TypeNameString& typeName,
00250 const InterfaceID& iid,
00251 IInterface*& pinterface ) {
00252 StatusCode sc = StatusCode::FAILURE;
00253 if ( s_appmgrInstance.isValid() ) {
00254 sc = s_svclocInstance->getService(typeName, iid, pinterface );
00255 } else {
00256 pinterface = s_bootInterface.get();
00257 }
00258 return sc;
00259 }
00260 StatusCode Gaudi::BootSvcLocator::getService( const Gaudi::Utils::TypeNameString& typeName,
00261 IService*& svc,
00262 const bool createIf ) {
00263 StatusCode sc = StatusCode::FAILURE;
00264 if ( s_appmgrInstance.isValid() ) {
00265 sc = s_svclocInstance->getService(typeName, svc, createIf );
00266 } else {
00267 svc = s_bootService.get();
00268 }
00269 return sc;
00270 }
00271 #endif
00272
00273 const std::list<IService*>& Gaudi::BootSvcLocator::getServices( ) const {
00274 StatusCode sc = StatusCode::FAILURE;
00275 if ( s_appmgrInstance.isValid() ) {
00276 return s_svclocInstance->getServices( );
00277 } else {
00278 return s_bootServices;
00279 }
00280 }
00281 bool Gaudi::BootSvcLocator::existsService( const std::string& name ) const {
00282 bool result = false;
00283 if ( s_appmgrInstance.isValid() ) {
00284 result = s_svclocInstance->existsService(name);
00285 }
00286 return result;
00287 }
00288
00289
00290 SmartIF<IService>& Gaudi::BootSvcLocator::service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf) {
00291 if ( s_appmgrInstance.isValid() ) {
00292 return s_svclocInstance->service(typeName, createIf);
00293 } else {
00294 return s_bootService;
00295 }
00296 }