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
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
00031
00032 std::auto_ptr<cond::TechnologyProxy> ptr(cond::TechnologyProxyFactory::get()->create(protocol,userconnect));
00033 realconnect=ptr->getRealConnectString();
00034
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
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
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 }