All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Bootstrap.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
4 #include "GaudiKernel/System.h"
5 
8 #include "GaudiKernel/IService.h"
12 
14 #include "GaudiKernel/Service.h"
15 #include "GaudiKernel/Algorithm.h"
16 
17 namespace Gaudi
18 {
19 
35  class BootSvcLocator : public implements1<ISvcLocator> {
36  public:
38  virtual ~BootSvcLocator();
39 #if !defined(GAUDI_V22_API)|| defined(G22_NEW_SVCLOCATOR)
41  const InterfaceID& iid,
42  IInterface*& pinterface );
43  virtual StatusCode getService( const Gaudi::Utils::TypeNameString& typeName,
44  IService*& svc,
45  const bool createIf = true);
46 #endif
47  virtual const std::list<IService*>& getServices( ) const;
48  virtual bool existsService( const std::string& name ) const;
49 
51  virtual SmartIF<IService> &service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf = true);
52 
53  };
54 }
55 
56 
57 static SmartIF<ISvcLocator> s_svclocInstance;
58 static SmartIF<IAppMgrUI> s_appmgrInstance;
59 
60 //------------------------------------------------------------------------------
61 IAppMgrUI* Gaudi::createApplicationMgr(const std::string& dllname,
62  const std::string& factname)
63 //------------------------------------------------------------------------------
64 {
65  // Allow not for multiple AppManagers: If already instantiated then just
66  // return it
67  if ( !s_appmgrInstance.isValid() ) {
68  s_appmgrInstance = createApplicationMgrEx(dllname, factname);
69  s_svclocInstance = s_appmgrInstance;
70  }
71  return s_appmgrInstance.get();
72 }
73 
74 //------------------------------------------------------------------------------
75 IAppMgrUI* Gaudi::createApplicationMgrEx(const std::string& dllname,
76  const std::string& factname)
77 //------------------------------------------------------------------------------
78 {
79  StatusCode status;
80  IInterface* iif;
81  IAppMgrUI* iappmgr;
82 
83  // Create an instance of the application Manager
84  iif = Gaudi::createInstance( "ApplicationMgr", factname, dllname );
85  if( iif == 0 ) {
86  return 0;
87  }
88  // Locate few interfaces of the Application Manager
89  status = iif->queryInterface( IAppMgrUI::interfaceID(), pp_cast<void>(&iappmgr) );
90  if( status.isFailure() ) {
91  return 0;
92  }
93  iif->release();
94  return iappmgr;
95 }
96 
97 //------------------------------------------------------------------------------
99 //------------------------------------------------------------------------------
100 //
101 // A dual-stage bootstrap mechanism is used to ensure an orderly startup
102 // of the ApplicationMgr. If this function is called before the singleton
103 // ApplicationMgr instance exists, a BootSvcLocator singleton instance is
104 // created. This responds to any subsequent requests for services by
105 // returning StatusCode::FAILURE, unless the ApplicationMgr singleton
106 // instance has been created in the interim. In this case, the BootSvcLocator
107 // forwards the request to the ApplicationMgr instance. The motivation for
108 // this is to handle static object instances where the constructor attempts
109 // to locate services and would otherwise instantiate the ApplicationMgr
110 // instance in an unorderly manner. This logic requires that the
111 // ApplicationMgr instance is created explicitly.
112 
113 {
114  if( !s_svclocInstance.isValid() ) {
115  IAppMgrUI* iappmgr = createApplicationMgr();
116  if( iappmgr ) {
117  s_svclocInstance = iappmgr;
118  if( s_svclocInstance.isValid() ) {
119  return s_svclocInstance;
120  }
121  }
122  //---Reverted change to create a Minimal SvcLocator in case is requested before AppMgr is created
123  //if( 0 == s_appmgrInstance ) {
124  // s_svclocInstance = new BootSvcLocator();
125  //} else {
126  // StatusCode sc = s_appmgrInstance->queryInterface( ISvcLocator::interfaceID(),
127  // pp_cast<void>(&s_svclocInstance) );
128  // if( sc.isSuccess() ) {
129  // return s_svclocInstance;
130  // }
131  //}
132  }
133  return s_svclocInstance;
134 }
135 
136 //------------------------------------------------------------------------------
138 //------------------------------------------------------------------------------
139 {
140  ISvcLocator* oldInstance = s_svclocInstance.get();
141  s_svclocInstance = newInstance;
142  s_appmgrInstance = s_svclocInstance;
143  return oldInstance;
144 }
145 
146 //------------------------------------------------------------------------------
148 //------------------------------------------------------------------------------
149 {
150  IAppMgrUI* oldInstance = s_appmgrInstance.get();
151  s_appmgrInstance = newInstance;
152  s_svclocInstance = s_appmgrInstance;
153  return oldInstance;
154 }
155 
156 //------------------------------------------------------------------------------
157 IInterface* Gaudi::createInstance( const std::string& name,
158  const std::string& factname,
159  const std::string& dllname)
160 //------------------------------------------------------------------------------
161 {
162 
163  IInterface* ii = ObjFactory::create(factname, (IInterface*)0);
164  if ( ii ) return ii;
165  IService* is = Service::Factory::create(factname, name, (ISvcLocator*)0);
166  if ( is ) return is;
167  IAlgorithm* ia = Algorithm::Factory::create(factname, name, (ISvcLocator*)0);
168  if ( ia ) return ia;
169 
170  StatusCode status;
171  void* libHandle = 0;
172  status = System::loadDynamicLib( dllname, &libHandle);
173  if ( status.isSuccess() ) {
174  ii = ObjFactory::create(factname, (IInterface*)0);
175  if ( ii ) return ii;
176  is = Service::Factory::create(factname, name, (ISvcLocator*)0);
177  if ( is ) return is;
178  ia = Algorithm::Factory::create(factname, name, (ISvcLocator*)0);
179  if ( ia ) return ia;
180 
181  return 0;
182  }
183  else {
184  // DLL library not loaded. Try in the local module
185  std::cout << System::getLastErrorString() << std::endl;
186  std::cout << "Gaudi::Bootstrap: Not found DLL " << dllname << std::endl;
187  return 0;
188  }
189 }
190 
191 //------------------------------------------------------------------------------
192 IAppMgrUI* Gaudi::createApplicationMgr(const std::string& dllname ) {
193 //------------------------------------------------------------------------------
194  return createApplicationMgr(dllname, "ApplicationMgr");
195 }
196 
197 //------------------------------------------------------------------------------
199 //------------------------------------------------------------------------------
200  return createApplicationMgr("GaudiCoreSvc", "ApplicationMgr");
201 }
202 
203 //=======================================================================
204 // BootSvcLocator
205 //=======================================================================
206 
207 static std::list<IService*> s_bootServices;
208 static SmartIF<IService> s_bootService;
209 static SmartIF<IInterface> s_bootInterface;
210 
212 
213 BootSvcLocator::BootSvcLocator() {
214 }
216 }
217 
218 #if !defined(GAUDI_V22_API) || defined(G22_NEW_SVCLOCATOR)
220  const InterfaceID& iid,
221  IInterface*& pinterface ) {
223  if ( s_appmgrInstance.isValid() ) {
224  sc = s_svclocInstance->getService(typeName, iid, pinterface );
225  } else {
226  pinterface = s_bootInterface.get();
227  }
228  return sc;
229 }
231  IService*& svc,
232  const bool createIf ) {
234  if ( s_appmgrInstance.isValid() ) {
235  sc = s_svclocInstance->getService(typeName, svc, createIf );
236  } else {
237  svc = s_bootService.get();
238  }
239  return sc;
240 }
241 #endif
242 
243 const std::list<IService*>& Gaudi::BootSvcLocator::getServices( ) const {
245  if ( s_appmgrInstance.isValid() ) {
246  return s_svclocInstance->getServices( );
247  } else {
248  return s_bootServices;
249  }
250 }
251 bool Gaudi::BootSvcLocator::existsService( const std::string& name ) const {
252  bool result = false;
253  if ( s_appmgrInstance.isValid() ) {
254  result = s_svclocInstance->existsService(name);
255  }
256  return result;
257 }
258 
259 
261  if ( s_appmgrInstance.isValid() ) {
262  return s_svclocInstance->service(typeName, createIf);
263  } else {
264  return s_bootService;
265  }
266 }
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
virtual bool existsService(const std::string &name) const
Check the existence of a service given a service name.
Definition: Bootstrap.cpp:251
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
virtual ~BootSvcLocator()
Definition: Bootstrap.cpp:215
GAUDI_API IAppMgrUI * createApplicationMgrEx(const std::string &dllname, const std::string &factname)
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)
Returns a smart pointer to a service.
Definition: Bootstrap.cpp:260
Gaudi::InterfaceId< IInterface, 0, 0 > iid
Interface ID.
Definition: IInterface.h:164
Base class used to implement the interfaces.
Definition: implements.h:133
Interface ID class.
Definition: IInterface.h:55
virtual StatusCode getService(const Gaudi::Utils::TypeNameString &typeName, const InterfaceID &iid, IInterface *&pinterface)
Get a specific interface pointer given a service name and interface id.
Definition: Bootstrap.cpp:219
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
GAUDI_API ISvcLocator * svcLocator()
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
General service interface definition.
Definition: IService.h:19
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Definition of the basic interface.
Definition: IInterface.h:160
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:62
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:20
virtual const std::list< IService * > & getServices() const
Get a reference to a service and create it if it does not exists.
Definition: Bootstrap.cpp:243
Application Manager User Interface.
Definition: IAppMgrUI.h:21
GAUDI_API ISvcLocator * setInstance(ISvcLocator *newInstance)
Set new instance of service locator.
virtual unsigned long release()=0
Release Interface instance.
A dual-stage boostrap mechanism is used to ensure an orderly startup of the ApplicationMgr.
Definition: Bootstrap.cpp:35
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:253
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:14
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:22
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:171
GAUDI_API IInterface * createInstance(const std::string &name, const std::string &factname, const std::string &ddlname)
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:123
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.