| # Gaudi Framework, version v21r7 |

Home |  | Generated: 22 Jan 2010

# Bootstrap.cpp

Go to the documentation of this file.

    00001 //$Id: Bootstrap.cpp,v 1.14 2007/12/12 16:02:32 marcocle Exp $
    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   // Allow not for multiple AppManagers: If already instantiated then just
    00068   // return it
    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   // Create an instance of the application Manager
    00086   iif = Gaudi::createInstance( "ApplicationMgr", factname, dllname );
    00087   if( iif == 0 ) {
    00088     return 0;
    00089   }
    00090   // Locate few interfaces of the Application Manager
    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 // A dual-stage boostrap mechanism is used to ensure an orderly startup
    00104 // of the ApplicationMgr. If this function is called before the singleton
    00105 // ApplicationMgr instance exists, a BootSvcLocator singleton instance is
    00106 // created. This responds to any subsequent requests for services by
    00107 // returning StatusCode::FAILURE, unless the ApplicationMgr singleton
    00108 // instance has been created in the interim. In this case, the BootSvcLocator
    00109 // forwards the request to the ApplicationMgr instance. The motiviation for
    00110 // this is to handle static object instances where the constructor attempts
    00111 // to locate services and would otherwise instantiate the ApplicationMgr
    00112 // instance in an unorderly manner. This logic requires that the
    00113 // ApplicationMgr instance is created explicitly.
    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     //---Reverted change to create a Minimal SvcLocator in case is requested before AppMgr is created
    00125     //if( 0 == s_appmgrInstance ) {
    00126     //  s_svclocInstance = new BootSvcLocator();
    00127     //} else {
    00128     //  StatusCode sc = s_appmgrInstance->queryInterface( ISvcLocator::interfaceID(),
    00129     //                                                  pp_cast<void>(&s_svclocInstance) );
    00130     //  if( sc.isSuccess() ) {
    00131     //    return s_svclocInstance;
    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     // DLL library not loaded. Try in the local module
    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   };
    00213 }
    00214
    00215 //------------------------------------------------------------------------------
    00216 IAppMgrUI* Gaudi::createApplicationMgr(const std::string& dllname )    {
    00217 //------------------------------------------------------------------------------
    00218   return createApplicationMgr(dllname, "ApplicationMgr");
    00219 }
    00220
    00221 //------------------------------------------------------------------------------
    00222 IAppMgrUI* Gaudi::createApplicationMgr()    {
    00223 //------------------------------------------------------------------------------
    00224   return createApplicationMgr("GaudiSvc", "ApplicationMgr");
    00225 }
    00226
    00227 //=======================================================================
    00228 // BootSvcLocator
    00229 //=======================================================================
    00230
    00231 static std::list<IService*> s_bootServices;
    00232 static SmartIF<IService>    s_bootService;
    00233 static SmartIF<IInterface>  s_bootInterface;
    00234
    00235 using Gaudi::BootSvcLocator;
    00236
    00237 BootSvcLocator::BootSvcLocator() {
    00238 }
    00239 BootSvcLocator::~BootSvcLocator() {
    00240 }
    00241
    00242 #if !defined(GAUDI_V22_API) || defined(G22_NEW_SVCLOCATOR)
    00243 StatusCode Gaudi::BootSvcLocator::getService( const Gaudi::Utils::TypeNameString& typeName,
    00244                                        const InterfaceID& iid,
    00245                                        IInterface*& pinterface ) {
    00246   StatusCode sc = StatusCode::FAILURE;
    00247   if ( s_appmgrInstance.isValid() ) {
    00248     sc = s_svclocInstance->getService(typeName, iid, pinterface );
    00249   } else {
    00250     pinterface = s_bootInterface.get();
    00251   }
    00252   return sc;
    00253 }
    00254 StatusCode Gaudi::BootSvcLocator::getService( const Gaudi::Utils::TypeNameString& typeName,
    00255                                        IService*& svc,
    00256                                        const bool createIf ) {
    00257   StatusCode sc = StatusCode::FAILURE;
    00258   if ( s_appmgrInstance.isValid() ) {
    00259     sc = s_svclocInstance->getService(typeName, svc, createIf );
    00260   } else {
    00261     svc = s_bootService.get();
    00262   }
    00263   return sc;
    00264 }
    00265 #endif
    00266
    00267 const std::list<IService*>& Gaudi::BootSvcLocator::getServices( ) const {
    00268   StatusCode sc = StatusCode::FAILURE;
    00269   if ( s_appmgrInstance.isValid() ) {
    00270     return s_svclocInstance->getServices( );
    00271   } else {
    00272     return s_bootServices;
    00273   }
    00274 }
    00275 bool Gaudi::BootSvcLocator::existsService( const std::string& name ) const {
    00276   bool result = false;
    00277   if ( s_appmgrInstance.isValid() ) {
    00278     result = s_svclocInstance->existsService(name);
    00279   }
    00280   return result;
    00281 }
    00282
    00283
    00284 SmartIF<IService>& Gaudi::BootSvcLocator::service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf) {
    00285   if ( s_appmgrInstance.isValid() ) {
    00286     return s_svclocInstance->service(typeName, createIf);
    00287   } else {
    00288     return s_bootService;
    00289   }
    00290 }