| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
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 namespace Gaudi
18 {
19
35 class BootSvcLocator : public implements1<ISvcLocator> {
36 public:
37 BootSvcLocator();
38 virtual ~BootSvcLocator();
39 #if !defined(GAUDI_V22_API)|| defined(G22_NEW_SVCLOCATOR)
40 virtual StatusCode getService( const Gaudi::Utils::TypeNameString& typeName,
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 //------------------------------------------------------------------------------
98 ISvcLocator* Gaudi::svcLocator()
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 //------------------------------------------------------------------------------
137 ISvcLocator* Gaudi::setInstance(ISvcLocator* newInstance)
138 //------------------------------------------------------------------------------
139 {
140 ISvcLocator* oldInstance = s_svclocInstance.get();
141 s_svclocInstance = newInstance;
142 s_appmgrInstance = s_svclocInstance;
143 return oldInstance;
144 }
145
146 //------------------------------------------------------------------------------
147 IAppMgrUI* Gaudi::setInstance(IAppMgrUI* newInstance)
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 namespace {
192 class ShadowEntry {
193 public:
194 std::string dllName;
195 std::string facName;
196 IFactory* fac;
197 public:
198 ShadowEntry() {
199 }
200 ShadowEntry(const std::string& d, const std::string& n, const IFactory* f) {
201 dllName = d;
202 facName = n;
203 fac = const_cast<IFactory*>(f);
204 }
205 ShadowEntry(const ShadowEntry& copy) {
206 dllName = copy.dllName;
207 facName = copy.facName;
208 fac = copy.fac;
209 }
210 };
211 }
212
213 //------------------------------------------------------------------------------
214 IAppMgrUI* Gaudi::createApplicationMgr(const std::string& dllname ) {
215 //------------------------------------------------------------------------------
216 return createApplicationMgr(dllname, "ApplicationMgr");
217 }
218
219 //------------------------------------------------------------------------------
220 IAppMgrUI* Gaudi::createApplicationMgr() {
221 //------------------------------------------------------------------------------
222 return createApplicationMgr("GaudiCoreSvc", "ApplicationMgr");
223 }
224
225 //=======================================================================
226 // BootSvcLocator
227 //=======================================================================
228
229 static std::list<IService*> s_bootServices;
230 static SmartIF<IService> s_bootService;
231 static SmartIF<IInterface> s_bootInterface;
232
233 using Gaudi::BootSvcLocator;
234
235 BootSvcLocator::BootSvcLocator() {
236 }
237 BootSvcLocator::~BootSvcLocator() {
238 }
239
240 #if !defined(GAUDI_V22_API) || defined(G22_NEW_SVCLOCATOR)
241 StatusCode Gaudi::BootSvcLocator::getService( const Gaudi::Utils::TypeNameString& typeName,
242 const InterfaceID& iid,
243 IInterface*& pinterface ) {
244 StatusCode sc = StatusCode::FAILURE;
245 if ( s_appmgrInstance.isValid() ) {
246 sc = s_svclocInstance->getService(typeName, iid, pinterface );
247 } else {
248 pinterface = s_bootInterface.get();
249 }
250 return sc;
251 }
252 StatusCode Gaudi::BootSvcLocator::getService( const Gaudi::Utils::TypeNameString& typeName,
253 IService*& svc,
254 const bool createIf ) {
255 StatusCode sc = StatusCode::FAILURE;
256 if ( s_appmgrInstance.isValid() ) {
257 sc = s_svclocInstance->getService(typeName, svc, createIf );
258 } else {
259 svc = s_bootService.get();
260 }
261 return sc;
262 }
263 #endif
264
265 const std::list<IService*>& Gaudi::BootSvcLocator::getServices( ) const {
266 StatusCode sc = StatusCode::FAILURE;
267 if ( s_appmgrInstance.isValid() ) {
268 return s_svclocInstance->getServices( );
269 } else {
270 return s_bootServices;
271 }
272 }
273 bool Gaudi::BootSvcLocator::existsService( const std::string& name ) const {
274 bool result = false;
275 if ( s_appmgrInstance.isValid() ) {
276 result = s_svclocInstance->existsService(name);
277 }
278 return result;
279 }
280
281
282 SmartIF<IService>& Gaudi::BootSvcLocator::service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf) {
283 if ( s_appmgrInstance.isValid() ) {
284 return s_svclocInstance->service(typeName, createIf);
285 } else {
286 return s_bootService;
287 }
288 }