CMS 3D CMS Logo

ConnectionHandler.cc

Go to the documentation of this file.
00001 #include "CondCore/DBCommon/interface/ConnectionHandler.h"
00002 #include "CondCore/DBCommon/interface/Connection.h"
00003 #include "CondCore/DBCommon/interface/Exception.h"
00004 #include "CondCore/DBCommon/interface/TechnologyProxy.h"
00005 #include "CondCore/DBCommon/interface/TechnologyProxyFactory.h"
00006 #include "FWCore/PluginManager/interface/PluginManager.h"
00007 //#include <iostream>
00008 void
00009 cond::ConnectionHandler::registerConnection(const std::string& name,
00010                                             const std::string& con,
00011                                             int connectionTimeoutInSec){
00012   m_registry.insert(std::make_pair<std::string,cond::Connection*>(name, new cond::Connection(con,connectionTimeoutInSec)));
00013 }
00014 void 
00015 cond::ConnectionHandler::registerConnection(const std::string& userconnect,
00016                                             cond::DBSession& session,
00017                                             int connectionTimeOutInSec){
00018   std::string realconnect(userconnect);
00019   std::string protocol;
00020   std::size_t pos=userconnect.find_first_of(':');
00021   if( pos!=std::string::npos ){
00022     protocol=userconnect.substr(0,pos);
00023     std::size_t p=protocol.find_first_of('_');
00024     if(p!=std::string::npos){
00025       protocol=protocol.substr(0,p);
00026     }
00027   }else{
00028     throw cond::Exception("connection string format error");
00029   }
00030   //std::cout<<"userconnect "<<userconnect<<std::endl;
00031   //std::cout<<"protocol "<<protocol<<std::endl;  
00032   std::auto_ptr<cond::TechnologyProxy> ptr(cond::TechnologyProxyFactory::get()->create(protocol,userconnect));
00033   realconnect=ptr->getRealConnectString();
00034   //std::cout<<"realconnect "<<realconnect<<std::endl;
00035   m_registry.insert(std::make_pair<std::string,cond::Connection*>(userconnect, new cond::Connection(realconnect,connectionTimeOutInSec)));
00036   ptr->setupSession(session);
00037 }
00038 void
00039 cond::ConnectionHandler::removeConnection(const std::string& name ){
00040   std::map<std::string,cond::Connection*>::iterator pos=m_registry.find(name);
00041   if( pos!=m_registry.end() ){
00042     m_registry.erase(pos);
00043   }
00044 }
00045 cond::ConnectionHandler& 
00046 cond::ConnectionHandler::Instance(){
00047   //std::cout<<"in ConnectionHandler::Instance"<<std::endl;
00048   /*if(!edmplugin::PluginManager::isAvailable()){
00049     edmplugin::PluginManager::Config config;
00050     const char* path = getenv("LD_LIBRARY_PATH");
00051     std::string spath(path? path: "");
00052     std::string::size_type last=0;
00053     std::string::size_type i=0;
00054     std::vector<std::string> paths;
00055     while( (i=spath.find_first_of(':',last))!=std::string::npos) {
00056       paths.push_back(spath.substr(last,i-last));
00057       last = i+1;
00058       //std::cout <<paths.back()<<std::endl;
00059     }
00060     paths.push_back(spath.substr(last,std::string::npos));
00061     config.searchPath(paths);
00062     edmplugin::PluginManager::configure(config);
00063   }
00064   */
00065   static cond::ConnectionHandler me;
00066   return me;
00067 }
00068 void
00069 cond::ConnectionHandler::connect(cond::DBSession* session){
00070   std::map<std::string,cond::Connection*>::iterator it;  
00071   std::map<std::string,cond::Connection*>::iterator itEnd=m_registry.end();
00072   for(it=m_registry.begin();it!=itEnd;++it){
00073     it->second->connect(session);
00074   }
00075 }
00076 void 
00077 cond::ConnectionHandler::disconnectAll(){
00078   if( m_registry.size()==0 ) return;
00079   std::map<std::string,cond::Connection*>::iterator it;  
00080   std::map<std::string,cond::Connection*>::iterator itEnd=m_registry.end();
00081   for(it=m_registry.begin();it!=itEnd;++it){
00082     if(it->second!=0){
00083       delete it->second;
00084       it->second=0;
00085     }
00086   }
00087   m_registry.clear();
00088 }
00089 cond::ConnectionHandler::~ConnectionHandler(){
00090   if( m_registry.size() != 0){
00091     this->disconnectAll();
00092   }
00093 }
00094 cond::Connection* 
00095 cond::ConnectionHandler::getConnection( const std::string& name ){
00096   std::map<std::string,cond::Connection*>::iterator it=m_registry.find(name);
00097   if( it!=m_registry.end() ){
00098     return it->second;
00099   }
00100   return 0;
00101 }

Generated on Tue Jun 9 17:26:05 2009 for CMSSW by  doxygen 1.5.4