![]() |
|
|
Generated: 18 Jul 2008 |
00001 // $Id: PoolDbCacheSvc.cpp,v 1.20 2008/05/05 19:48:13 marcocle Exp $ 00002 //==================================================================== 00003 // PoolDbCacheSvc implementation 00004 //-------------------------------------------------------------------- 00005 // Author : M.Frank 00006 // 00007 //==================================================================== 00008 #include "GaudiPoolDb/PoolDbCacheSvc.h" 00009 #include "GaudiKernel/ClassID.h" 00010 #include "GaudiKernel/MsgStream.h" 00011 #include "GaudiKernel/strcasecmp.h" 00012 #include "GaudiKernel/SvcFactory.h" 00013 #include "StorageSvc/DbPrint.h" 00014 #include "StorageSvc/DbOption.h" 00015 #include "StorageSvc/IDbOptionProxy.h" 00016 #include "StorageSvc/DbInstanceCount.h" 00017 #include "GaudiUtils/IFileCatalog.h" 00018 #include "GaudiUtils/IIODataManager.h" 00019 00020 #include "Reflex/Reflex.h" 00021 00022 namespace GaudiPoolDb { bool patchStreamers(MsgStream& log); } 00023 00024 static pool::DbInstanceCount::Counter* s_count = 00025 pool::DbInstanceCount::getCounter(typeid(PoolDbCacheSvc)); 00026 00027 DECLARE_SERVICE_FACTORY(PoolDbCacheSvc); 00028 00037 class PoolDbMsgReporter : public coral::MsgReporter { 00038 protected: 00040 IMessageSvc* m_svc; 00041 int m_lvl; 00042 public: 00044 PoolDbMsgReporter(IMessageSvc* s, int l) : coral::MsgReporter(), m_svc(s), m_lvl(l) {} 00046 virtual ~PoolDbMsgReporter() { } 00048 virtual void report(int lvl, const std::string& src, const std::string& msg) { 00049 if ( lvl >= m_lvl ) m_svc->reportMessage(src,lvl,msg); 00050 } 00051 }; 00052 00054 PoolDbCacheSvc::PoolDbCacheSvc(const std::string& nam, ISvcLocator* svc) 00055 : Service(nam, svc), m_callbackHandler(0) 00056 { 00057 s_count->increment(); 00058 m_callbackHandler = this; 00059 declareProperty("Dlls", m_dlls); 00060 declareProperty("DomainOpts", m_domainOpts); 00061 declareProperty("DatabaseOpts", m_databaseOpts); 00062 declareProperty("DatabaseOnOpenOpts", m_databaseOpenOpts); 00063 declareProperty("ContainerOpts", m_containerOpts); 00064 declareProperty("ContainerOnOpenOpts",m_containerOpenOpts); 00065 } 00066 00068 PoolDbCacheSvc::~PoolDbCacheSvc() 00069 { 00070 s_count->decrement(); 00071 } 00072 00074 StatusCode PoolDbCacheSvc::queryInterface(const InterfaceID& riid, void** ppvInterface) 00075 { 00076 if ( IPoolCacheSvc::interfaceID().versionMatch(riid) ) { 00077 *ppvInterface = (IPoolCacheSvc*)this; 00078 addRef(); 00079 return StatusCode::SUCCESS; 00080 } 00081 // Interface is not directly availible: try out a base class 00082 return Service::queryInterface(riid, ppvInterface); 00083 } 00084 00086 StatusCode PoolDbCacheSvc::initialize() { 00087 StatusCode status = Service::initialize(); 00088 MsgStream log(messageService(), name()); 00089 if ( !status.isSuccess() ) { 00090 log << MSG::ERROR << "Failed to initialize Service base class." 00091 << endmsg; 00092 return status; 00093 } 00094 coral::MessageStream::installMsgReporter(new PoolDbMsgReporter(messageService(),m_outputLevel.value())); 00095 coral::MessageStream::setMsgVerbosity(static_cast<coral::MsgLevel>(m_outputLevel.value())); 00096 00097 log << MSG::INFO << "POOL output threshold:" << m_outputLevel 00098 << endmsg; 00099 status = loadLibraries(); 00100 if ( !status.isSuccess() ) { 00101 log << MSG::ERROR << "Failed to load POOL libraries." 00102 << endmsg; 00103 return status; 00104 } 00105 session().open(0); 00106 // All dictionaries should be loaded. Let's patch the streamers 00107 // for ContainedObject, DataObject and SmartRefbase 00108 if ( !GaudiPoolDb::patchStreamers(log) ) { 00109 log << MSG::ERROR << "Failed to install customized IO!" << endmsg; 00110 return StatusCode::FAILURE; 00111 } 00112 return status; 00113 } 00114 00116 StatusCode PoolDbCacheSvc::finalize() { 00117 MsgStream log(messageService(), name()); 00118 session().close(); 00120 std::vector<System::ImageHandle>::iterator i; 00121 for(i=m_sharedHdls.begin(); i != m_sharedHdls.end(); ++i) { 00122 if ( *i ) { 00123 // System::unloadDynamicLib(*i); 00124 } 00125 } 00126 m_sharedHdls.clear(); 00127 coral::MessageStream::installMsgReporter(0); 00128 return Service::finalize(); 00129 } 00130 00132 pool::DbStatus 00133 PoolDbCacheSvc::setCallbackOptions( pool::IDbOptionProxy* pObj, 00134 const std::vector<std::string>& v, 00135 const std::string& obj) 00136 { 00137 std::string opt_nam, opt_val, opt_typ; 00138 typedef std::vector<std::string> StringV; 00139 for (StringV::const_iterator i=v.begin(); i!=v.end(); ++i) { 00140 const std::string& id = *i; 00141 if ( id.length() > obj.length() ) { 00142 if ( obj == id.substr(0, obj.length()) ) { 00143 std::string n = id.substr(obj.length()+1); 00144 size_t idx = n.find("="); 00145 size_t idx2 = n.find("TYP="); 00146 if ( idx != std::string::npos && idx2 != std::string::npos ) { 00147 opt_nam = n.substr(0, idx); 00148 opt_val = n.substr(idx+1, idx2-2-idx); 00149 opt_typ = n.substr(idx2+4,1); 00150 std::stringstream s(opt_val); 00151 pool::DbOption opt(opt_nam); 00152 float fval; 00153 int ival; 00154 long long int lval; 00155 switch(::toupper(opt_typ[0])) { 00156 case 'I': 00157 s >> ival; 00158 opt._setValue(ival); 00159 pObj->setOption(opt); 00160 break; 00161 case 'L': 00162 s >> lval; 00163 opt._setValue(lval); 00164 pObj->setOption(opt); 00165 break; 00166 case 'F': 00167 s >> fval; 00168 opt._setValue(fval); 00169 pObj->setOption(opt); 00170 break; 00171 case 'S': 00172 opt._setValue(opt_val.c_str()); 00173 pObj->setOption(opt); 00174 break; 00175 default: 00176 break; 00177 } 00178 } 00179 } 00180 } 00181 } 00182 return pool::Success; 00183 } 00184 00186 pool::DbStatus 00187 PoolDbCacheSvc::setMyOptions( pool::IDbOptionProxy* pObj, 00188 pool::DbOptionCallback::OptionType typ, 00189 const std::string& name) 00190 { 00191 switch (typ) { 00192 case pool::DbOptionCallback::_DOMAIN_OPT: 00193 return setCallbackOptions(pObj, m_domainOpts, name); 00194 case pool::DbOptionCallback::_DATABASE_OPT: 00195 return setCallbackOptions(pObj, m_databaseOpts, name); 00196 case pool::DbOptionCallback::_DATABASE_ONOPEN: 00197 return setCallbackOptions(pObj, m_databaseOpenOpts, name); 00198 case pool::DbOptionCallback::_CONTAINER_OPT: 00199 return setCallbackOptions(pObj, m_containerOpts, name); 00200 case pool::DbOptionCallback::_CONTAINER_ONOPEN: 00201 return setCallbackOptions(pObj, m_containerOpenOpts, name); 00202 default: 00203 break; 00204 } 00205 return pool::Error; 00206 } 00207 00208 // Load all required libraries 00209 StatusCode PoolDbCacheSvc::loadLibraries() { 00210 StatusCode status = StatusCode::SUCCESS; 00211 if ( !m_dlls.empty() ) { 00212 std::vector<std::string>::const_iterator i; 00213 for(i=m_dlls.begin(); i != m_dlls.end(); ++i) { 00214 StatusCode iret = loadDictionary(*i); 00215 if ( !iret.isSuccess() ) { 00216 status = iret; 00217 } 00218 } 00219 } 00220 return status; 00221 } 00222 00224 StatusCode PoolDbCacheSvc::loadDictionary(const std::string& nam) { 00225 System::ImageHandle hdl = 0; 00226 StatusCode status = System::loadDynamicLib(nam, &hdl); 00227 if ( !status.isSuccess() ) { 00228 MsgStream log(messageService(), name()); 00229 log << MSG::ERROR << System::getLastErrorString() << endmsg 00230 << "Failed to load POOL implementation library:" 00231 << nam << endmsg; 00232 return status; 00233 } 00234 m_sharedHdls.push_back(hdl); 00235 return status; 00236 }