All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Bootstrap.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "GaudiKernel/Bootstrap.h"
4 #include "GaudiKernel/System.h"
5 
6 #include "GaudiKernel/IInterface.h"
7 #include "GaudiKernel/IAlgorithm.h"
8 #include "GaudiKernel/IService.h"
9 #include "GaudiKernel/IAppMgrUI.h"
10 #include "GaudiKernel/ISvcLocator.h"
11 #include "GaudiKernel/IClassManager.h"
12 
13 #include "GaudiKernel/ObjectFactory.h"
14 #include "GaudiKernel/Service.h"
15 #include "GaudiKernel/Algorithm.h"
16 
17 #include "GaudiKernel/IJobOptionsSvc.h"
18 #include "GaudiKernel/IEventProcessor.h"
19 
20 #include "RVersion.h"
21 
22 namespace Gaudi
23 {
24 
40  class BootSvcLocator : public implements1<ISvcLocator> {
41  public:
42  BootSvcLocator() = default;
43  ~BootSvcLocator() override = default;
44 #if !defined(GAUDI_V22_API)|| defined(G22_NEW_SVCLOCATOR)
46  const InterfaceID& iid,
47  IInterface*& pinterface ) override;
49  IService*& svc,
50  const bool createIf = true) override;
51 #endif
52  const std::list<IService*>& getServices( ) const override;
53  bool existsService( const std::string& name ) const override;
54 
56  SmartIF<IService> &service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf = true) override;
57 
58  };
59 }
60 
61 
62 static SmartIF<ISvcLocator> s_svclocInstance;
63 static SmartIF<IAppMgrUI> s_appmgrInstance;
64 
65 //------------------------------------------------------------------------------
66 IAppMgrUI* Gaudi::createApplicationMgr(const std::string& dllname,
67  const std::string& factname)
68 //------------------------------------------------------------------------------
69 {
70  // Allow not for multiple AppManagers: If already instantiated then just
71  // return it
72  if ( !s_appmgrInstance ) {
73  s_appmgrInstance = createApplicationMgrEx(dllname, factname);
74  s_svclocInstance = s_appmgrInstance;
75  }
76  return s_appmgrInstance.get();
77 }
78 
79 //------------------------------------------------------------------------------
80 IAppMgrUI* Gaudi::createApplicationMgrEx(const std::string& dllname,
81  const std::string& factname)
82 //------------------------------------------------------------------------------
83 {
84  // Create an instance of the application Manager
85  auto iif = make_SmartIF( Gaudi::createInstance( "ApplicationMgr", factname, dllname ) );
86  // Locate few interfaces of the Application Manager
87  return iif ? iif.as<IAppMgrUI>().get() : nullptr;
88 }
89 
90 //------------------------------------------------------------------------------
92 //------------------------------------------------------------------------------
93 //
94 // A dual-stage bootstrap mechanism is used to ensure an orderly startup
95 // of the ApplicationMgr. If this function is called before the singleton
96 // ApplicationMgr instance exists, a BootSvcLocator singleton instance is
97 // created. This responds to any subsequent requests for services by
98 // returning StatusCode::FAILURE, unless the ApplicationMgr singleton
99 // instance has been created in the interim. In this case, the BootSvcLocator
100 // forwards the request to the ApplicationMgr instance. The motivation for
101 // this is to handle static object instances where the constructor attempts
102 // to locate services and would otherwise instantiate the ApplicationMgr
103 // instance in an unorderly manner. This logic requires that the
104 // ApplicationMgr instance is created explicitly.
105 
106 {
107  if( !s_svclocInstance ) {
108  IAppMgrUI* iappmgr = createApplicationMgr();
109  if( iappmgr ) {
110  s_svclocInstance = iappmgr;
111  if( s_svclocInstance ) return s_svclocInstance;
112  }
113  //---Reverted change to create a Minimal SvcLocator in case is requested before AppMgr is created
114  //if( 0 == s_appmgrInstance ) {
115  // s_svclocInstance = new BootSvcLocator();
116  //} else {
117  // StatusCode sc = s_appmgrInstance->queryInterface( ISvcLocator::interfaceID(),
118  // pp_cast<void>(&s_svclocInstance) );
119  // if( sc.isSuccess() ) {
120  // return s_svclocInstance;
121  // }
122  //}
123  }
124  return s_svclocInstance;
125 }
126 
127 //------------------------------------------------------------------------------
129 //------------------------------------------------------------------------------
130 {
131  ISvcLocator* oldInstance = s_svclocInstance.get();
132  s_svclocInstance = newInstance;
133  s_appmgrInstance = s_svclocInstance;
134  return oldInstance;
135 }
136 
137 //------------------------------------------------------------------------------
139 //------------------------------------------------------------------------------
140 {
141  IAppMgrUI* oldInstance = s_appmgrInstance.get();
142  s_appmgrInstance = newInstance;
143  s_svclocInstance = s_appmgrInstance;
144  return oldInstance;
145 }
146 
147 //------------------------------------------------------------------------------
148 IInterface* Gaudi::createInstance( const std::string& name,
149  const std::string& factname,
150  const std::string& dllname)
151 //------------------------------------------------------------------------------
152 {
153 
154  IInterface* ii = ObjFactory::create(factname, nullptr);
155  if ( ii ) return ii;
156  IService* is = Service::Factory::create(factname, name, nullptr);
157  if ( is ) return is;
158  IAlgorithm* ia = Algorithm::Factory::create(factname, name, nullptr);
159  if ( ia ) return ia;
160 
161  void* libHandle = nullptr;
162  StatusCode status = System::loadDynamicLib( dllname, &libHandle);
163  if ( status.isSuccess() ) {
164  ii = ObjFactory::create(factname, nullptr);
165  if ( ii ) return ii;
166  is = Service::Factory::create(factname, name, nullptr);
167  if ( is ) return is;
168  ia = Algorithm::Factory::create(factname, name, nullptr);
169  if ( ia ) return ia;
170  } else {
171  // DLL library not loaded. Try in the local module
172  std::cout << System::getLastErrorString() << std::endl;
173  std::cout << "Gaudi::Bootstrap: Not found DLL " << dllname << std::endl;
174  }
175  return nullptr;
176 }
177 
178 //------------------------------------------------------------------------------
179 IAppMgrUI* Gaudi::createApplicationMgr(const std::string& dllname ) {
180 //------------------------------------------------------------------------------
181  return createApplicationMgr(dllname, "ApplicationMgr");
182 }
183 
184 //------------------------------------------------------------------------------
186 //------------------------------------------------------------------------------
187  return createApplicationMgr("GaudiCoreSvc", "ApplicationMgr");
188 }
189 
190 //=======================================================================
191 // BootSvcLocator
192 //=======================================================================
193 
194 static std::list<IService*> s_bootServices;
195 static SmartIF<IService> s_bootService;
196 static SmartIF<IInterface> s_bootInterface;
197 
199 
200 #if !defined(GAUDI_V22_API) || defined(G22_NEW_SVCLOCATOR)
202  const InterfaceID& iid,
203  IInterface*& pinterface ) {
205  if ( s_appmgrInstance ) {
206  sc = s_svclocInstance->getService(typeName, iid, pinterface );
207  } else {
208  pinterface = s_bootInterface.get();
209  }
210  return sc;
211 }
213  IService*& svc,
214  const bool createIf ) {
216  if ( s_appmgrInstance ) {
217  sc = s_svclocInstance->getService(typeName, svc, createIf );
218  } else {
219  svc = s_bootService.get();
220  }
221  return sc;
222 }
223 #endif
224 
225 const std::list<IService*>& Gaudi::BootSvcLocator::getServices( ) const {
226  return s_appmgrInstance ? s_svclocInstance->getServices( )
227  : s_bootServices;
228 }
229 bool Gaudi::BootSvcLocator::existsService( const std::string& name ) const {
230  return s_appmgrInstance && s_svclocInstance->existsService(name) ;
231 }
232 
233 
235  return s_appmgrInstance ? s_svclocInstance->service(typeName, createIf)
236  : s_bootService;
237 }
238 
239 
240 #ifdef GAUDI_HASCLASSVISIBILITY
241 #pragma GCC visibility push(default)
242 #endif
243 // Python bootstrap helpers
244 extern "C" {
245 #define PyHelper(x) py_bootstrap_ ## x
248  }
250  auto svcloc = SmartIF<ISvcLocator>(app);
251  return svcloc ? svcloc->service<IInterface>(name).get()
252  : nullptr;
253  }
254  bool PyHelper(setProperty)(IInterface* p, char* name, char* value) {
255  auto prop = SmartIF<IProperty>(p);
256  return prop && prop->setProperty(name, value).isSuccess();
257  }
258  const char* PyHelper(getProperty)(IInterface* p, char* name) {
259  auto prop = SmartIF<IProperty>(p);
260  return prop ? prop->getProperty(name).toString().c_str() : nullptr;
261  }
263  auto ui = SmartIF<IAppMgrUI>(app);
264  return ui && ui->configure().isSuccess();
265  }
266  bool PyHelper(addPropertyToCatalogue)(IInterface* p, char* comp, char* name, char* value) {
267  auto jos = SmartIF<IJobOptionsSvc>(p);
268  return jos && jos->addPropertyToCatalogue(comp, StringProperty(name, value)).isSuccess();
269  }
271  return ROOT_VERSION_CODE;
272  }
273 
274 #define PyFSMHelper(s) bool py_bootstrap_fsm_ ## s (IInterface* i) \
275  { auto fsm = SmartIF<IStateful>(i); return fsm && fsm->s().isSuccess(); }
276 
277  PyFSMHelper(configure)
280  PyFSMHelper(stop)
281  PyFSMHelper(finalize)
282  PyFSMHelper(terminate)
283 
284  bool py_bootstrap_app_run(IInterface* i, int maxevt) {
286  return ep && ep->executeRun(maxevt).isSuccess();
287  }
288 }
289 #ifdef GAUDI_HASCLASSVISIBILITY
290 #pragma GCC visibility pop
291 #endif
#define PyHelper(x)
Definition: Bootstrap.cpp:245
int PyHelper() ROOT_VERSION_CODE()
Definition: Bootstrap.cpp:270
#define PyFSMHelper(s)
Definition: Bootstrap.cpp:274
Base class used to implement the interfaces.
Definition: implements.h:9
def initialize()
Definition: AnalysisTest.py:12
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
virtual StatusCode getProperty(Property *p) const =0
Get the property by property.
const std::list< IService * > & getServices() const override
Definition: Bootstrap.cpp:225
int maxevt auto ep
Definition: Bootstrap.cpp:285
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
~BootSvcLocator() override=default
bool PyHelper() addPropertyToCatalogue(IInterface *p, char *comp, char *name, char *value)
Definition: Bootstrap.cpp:266
GAUDI_API IAppMgrUI * createApplicationMgrEx(const std::string &dllname, const std::string &factname)
const char *PyHelper() getProperty(IInterface *p, char *name)
Definition: Bootstrap.cpp:258
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
SimpleProperty< std::string > StringProperty
Definition: Property.h:718
Interface ID class.
Definition: IInterface.h:30
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
GAUDI_API ISvcLocator * svcLocator()
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
Definition: Bootstrap.cpp:234
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Definition of the basic interface.
Definition: IInterface.h:234
virtual StatusCode setProperty(const Property &p)=0
Set the property by property.
virtual StatusCode getService(const Gaudi::Utils::TypeNameString &typeName, IService *&svc, const bool createIf=true)
Get a reference to the service given a service name.
Definition: ISvcLocator.h:35
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:254
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:23
Application Manager User Interface.
Definition: IAppMgrUI.h:20
GAUDI_API ISvcLocator * setInstance(ISvcLocator *newInstance)
Set new instance of service locator.
StatusCode getService(const Gaudi::Utils::TypeNameString &typeName, const InterfaceID &iid, IInterface *&pinterface) override
Definition: Bootstrap.cpp:201
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:254
virtual StatusCode addPropertyToCatalogue(const std::string &client, const Property &property)=0
Add a property into the JobOptions catalog.
A dual-stage boostrap mechanism is used to ensure an orderly startup of the ApplicationMgr.
Definition: Bootstrap.cpp:40
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:124
virtual bool existsService(const std::string &name) const =0
Check the existence of a service given a service name.
virtual const std::list< IService * > & getServices() const =0
Get a reference to a service and create it if it does not exists.
bool existsService(const std::string &name) const override
Definition: Bootstrap.cpp:229
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:21
virtual StatusCode configure()=0
Configure the job.
list i
Definition: ana.py:128
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition: SmartIF.h:143
bool PyHelper() configureApp(IInterface *app)
Definition: Bootstrap.cpp:262
IInterface *PyHelper() getService(IInterface *app, char *name)
Definition: Bootstrap.cpp:249
Helper functions to set/get the application return code.
Definition: __init__.py:1
GAUDI_API IInterface * createInstance(const std::string &name, const std::string &factname, const std::string &ddlname)
tuple start
Definition: IOTest.py:88
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)