00001
00002
00003 #include <iostream>
00004
00005 #include "GaudiKernel/Bootstrap.h"
00006 #include "GaudiKernel/System.h"
00007 #include "GaudiKernel/FactoryTable.h"
00008
00009
00010 #include "GaudiKernel/IInterface.h"
00011 #include "GaudiKernel/IFactory.h"
00012 #include "GaudiKernel/IAppMgrUI.h"
00013 #include "GaudiKernel/ISvcLocator.h"
00014 #include "GaudiKernel/IClassManager.h"
00015 #include "GaudiKernel/ISvcFactory.h"
00016 #include "GaudiKernel/IAlgFactory.h"
00017 #include "GaudiKernel/IAuditorFactory.h"
00018
00019
00020
00021 IAppMgrUI* Gaudi::createApplicationMgr(const std::string& dllname,
00022 const std::string& factname)
00023
00024 {
00025
00026 static IAppMgrUI* appmgr_instance = createApplicationMgrEx(dllname, factname);
00027 return appmgr_instance;
00028 }
00029
00030
00031 IAppMgrUI* Gaudi::createApplicationMgrEx(const std::string& dllname,
00032 const std::string& factname)
00033
00034 {
00035 StatusCode status;
00036 IInterface* iif;
00037 IAppMgrUI* iappmgr;
00038 IClassManager* iclassmgr;
00039
00040
00041 iif = Gaudi::createInstance( "ApplicationMgr", factname, dllname );
00042 if( iif == 0 ) {
00043 return 0;
00044 }
00045
00046 status = iif->queryInterface( IID_IAppMgrUI, (void**)&iappmgr );
00047 if( status.isFailure() ) {
00048 return 0;
00049 }
00050 status = iif->queryInterface( IID_IClassManager, (void**)&iclassmgr );
00051 if( status.isFailure() ) {
00052 return 0;
00053 }
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 return iappmgr;
00068 }
00069
00070
00071 ISvcLocator* Gaudi::svcLocator()
00072
00073 {
00074 static ISvcLocator* svcloc_instance = 0;
00075 if( svcloc_instance ) return svcloc_instance;
00076
00077 IAppMgrUI* iappmgr = createApplicationMgr();
00078 if( iappmgr ) {
00079 StatusCode sc = iappmgr->queryInterface( IID_ISvcLocator, (void**)&svcloc_instance );
00080 if( sc.isSuccess() ) {
00081 return svcloc_instance;
00082 }
00083 }
00084 return 0;
00085 }
00086
00087
00088 IInterface* Gaudi::createInstance( const std::string& name,
00089 const std::string& factname,
00090 const std::string& dllname)
00091
00092 {
00093 IFactory* iif;
00094 IAlgFactory* ialgfact;
00095 IAuditorFactory* iaudfact;
00096 ISvcFactory* isvcfact;
00097
00098 iif = const_cast<IFactory*>( Gaudi::getFactory( factname, dllname ) );
00099 if( iif == 0 ) {
00100 return 0;
00101 }
00102
00103 if( iif->queryInterface( IID_IAlgFactory, (void**)&ialgfact ).isSuccess() ) {
00104 return (IInterface*)(ialgfact->instantiate( name, 0 ));
00105 }
00106 else if( iif->queryInterface( IID_IAuditorFactory, (void**)&iaudfact ).isSuccess() ) {
00107 return (IInterface*)(iaudfact->instantiate( name, 0 ));
00108 }
00109 else if ( iif->queryInterface( IID_ISvcFactory, (void**)&isvcfact ).isSuccess() ) {
00110 return (IInterface*)(isvcfact->instantiate( name, 0 ));
00111 }
00112 else {
00113 return iif->instantiate( 0 );
00114 }
00115 }
00116
00117 class ShadowEntry {
00118 public:
00119 std::string dllName;
00120 std::string facName;
00121 IFactory* fac;
00122 public:
00123 ShadowEntry() {
00124 }
00125 ShadowEntry(const std::string& d, const std::string& n, const IFactory* f) {
00126 dllName = d;
00127 facName = n;
00128 fac = const_cast<IFactory*>(f);
00129 }
00130 ShadowEntry(const ShadowEntry& copy) {
00131 dllName = copy.dllName;
00132 facName = copy.facName;
00133 fac = copy.fac;
00134 }
00135 ShadowEntry& operator=(const ShadowEntry& copy) {
00136 dllName = copy.dllName;
00137 facName = copy.facName;
00138 fac = copy.fac;
00139 return *this;
00140 }
00141 };
00142
00143
00144 const IFactory* Gaudi::getFactory(const std::string& factname, const std::string& dllname)
00145
00146 {
00147 StatusCode status;
00148 void* libHandle = 0;
00149 FactoryTable::EntryList* elist = 0;
00150 static std::vector<ShadowEntry> s_shadow;
00151
00152
00153 for ( std::vector<ShadowEntry>::iterator sit = s_shadow.begin(); sit != s_shadow.end(); sit++) {
00154 if ( (*sit).dllName == dllname && (*sit).facName == factname && 0 != (*sit).fac ) {
00155 return (*sit).fac;
00156 }
00157 }
00158
00159
00160 elist = FactoryTable::instance()->getEntries();
00161 for( FactoryTable::EntryList::iterator it = elist->begin(); it != elist->end(); it++ ) {
00162 if( factname == (*it)->ident() ) {
00163 s_shadow.push_back(ShadowEntry(dllname, factname, (*it)));
00164 return (*it);
00165 }
00166 }
00167
00168 elist = 0;
00169
00170 status = System::loadDynamicLib( dllname, &libHandle);
00171 if ( status.isSuccess() ) {
00172 FactoryTable::GetFunction func;
00173 status = System::getProcedureByName(libHandle, "getFactoryEntries", (System::Creator*)&func);
00174 if ( status.isSuccess() ) {
00175 elist = (func)();
00176 }
00177 else {
00178
00179 std::cout << "Gaudi::Bootstrap: Not found entry point" << std::endl;
00180 return 0;
00181 }
00182 }
00183 else {
00184
00185 std::cout << "Gaudi::Bootstrap: Not found DLL " << dllname << std::endl;
00186 return 0;
00187 }
00188
00189 if( elist != 0) {
00190 for( FactoryTable::EntryList::iterator it = elist->begin(); it != elist->end(); it++ ) {
00191 if( factname == (*it)->ident() ) {
00192 s_shadow.push_back(ShadowEntry(dllname, factname, (*it)));
00193 return (*it);
00194 }
00195 }
00196 std::cout << "Gaudi::Bootstrap: Factory with name " << factname << "not found in DLL" << std::endl;
00197 return 0;
00198 }
00199 else {
00200 std::cout << "Gaudi::Bootstrap: FactoryTable not found in DLL" << std::endl;
00201 return 0;
00202 }
00203 }