CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/CondCore/DBCommon/src/DbConnectionConfiguration.cc

Go to the documentation of this file.
00001 //local includes
00002 #include "CondCore/DBCommon/interface/DbConnectionConfiguration.h"
00003 #include "CondCore/DBCommon/interface/CoralServiceManager.h"
00004 #include "CondCore/DBCommon/interface/Auth.h"
00005 // CMSSW includes
00006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00007 //#include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 // coral includes
00009 #include "RelationalAccess/IConnectionServiceConfiguration.h"
00010 #include "CoralKernel/Context.h"
00011 #include "CoralKernel/IProperty.h"
00012 #include "CoralKernel/IPropertyManager.h"
00013 // externals
00014 #include <boost/filesystem/operations.hpp>
00015 
00016 std::vector<cond::DbConnectionConfiguration>&
00017 cond::DbConnectionConfiguration::defaultConfigurations(){
00018   static std::vector<DbConnectionConfiguration> s_defaultConfigurations;
00019   // coral default
00020   s_defaultConfigurations.push_back( cond::DbConnectionConfiguration() );
00021   // cms default
00022   s_defaultConfigurations.push_back( cond::DbConnectionConfiguration( false, 0, false, 10, 60, false, "", "", coral::Error, coral::monitor::Off, false ) );
00023   // prod default
00024   s_defaultConfigurations.push_back( cond::DbConnectionConfiguration( false, 0, false, 10, 60, false, "", "", coral::Error, coral::monitor::Off, false ) );
00025   // tool default
00026   s_defaultConfigurations.push_back( cond::DbConnectionConfiguration( false, 0, false, 10, 60, false, "", "",coral::Error, coral::monitor::Off, false ) );
00027   // web default
00028   s_defaultConfigurations.push_back( cond::DbConnectionConfiguration( false, 0, false, 10, 60, false, "", "",coral::Error, coral::monitor::Off, false ) );
00029   return s_defaultConfigurations;
00030 }
00031 
00032 cond::DbConnectionConfiguration::DbConnectionConfiguration():
00033   m_connectionSharing(false,false),
00034   m_connectionTimeOut(false,0),
00035   m_readOnlySessionOnUpdateConnections(false,false),
00036   m_connectionRetrialPeriod(false,0),
00037   m_connectionRetrialTimeOut(false,0),
00038   m_poolAutomaticCleanUp(false,false),
00039   m_authPath(""),
00040   m_authSys(0),
00041   m_transactionId(),
00042   m_messageLevel(coral::Error),
00043   m_monitoringLevel(coral::monitor::Off),
00044   m_SQLMonitoring(false),
00045   m_pluginManager(new CoralServiceManager){
00046 }
00047 
00048 cond::DbConnectionConfiguration::DbConnectionConfiguration( bool connectionSharing,
00049                                                             int connectionTimeOut,
00050                                                             bool readOnlySessionOnUpdateConnections,
00051                                                             int connectionRetrialPeriod,
00052                                                             int connectionRetrialTimeOut,
00053                                                             bool poolAutomaticCleanUp,
00054                                                             const::std::string& authenticationPath,
00055                                                             const::std::string& transactionId,
00056                                                             coral::MsgLevel msgLev,
00057                                                             coral::monitor::Level monitorLev,
00058                                                             bool SQLMonitoring ):
00059   m_connectionSharing(true,connectionSharing),
00060   m_connectionTimeOut(true,connectionTimeOut),
00061   m_readOnlySessionOnUpdateConnections(true,readOnlySessionOnUpdateConnections),
00062   m_connectionRetrialPeriod(true,connectionRetrialPeriod),
00063   m_connectionRetrialTimeOut(true,connectionRetrialTimeOut),
00064   m_poolAutomaticCleanUp(true,poolAutomaticCleanUp),
00065   m_authPath(authenticationPath),
00066   m_authSys(0),
00067   m_transactionId(transactionId),
00068   m_messageLevel(msgLev),
00069   m_monitoringLevel(monitorLev),
00070   m_SQLMonitoring(SQLMonitoring),
00071   m_pluginManager(new CoralServiceManager){
00072 }
00073 
00074 cond::DbConnectionConfiguration::DbConnectionConfiguration( const cond::DbConnectionConfiguration& rhs):
00075   m_connectionSharing(rhs.m_connectionSharing),
00076   m_connectionTimeOut(rhs.m_connectionTimeOut),
00077   m_readOnlySessionOnUpdateConnections(rhs.m_readOnlySessionOnUpdateConnections),
00078   m_connectionRetrialPeriod(rhs.m_connectionRetrialPeriod),
00079   m_connectionRetrialTimeOut(rhs.m_connectionRetrialTimeOut),
00080   m_poolAutomaticCleanUp(rhs.m_poolAutomaticCleanUp),
00081   m_authPath(rhs.m_authPath),
00082   m_authSys(rhs.m_authSys),
00083   m_transactionId(rhs.m_transactionId),
00084   m_messageLevel(rhs.m_messageLevel),
00085   m_monitoringLevel(rhs.m_monitoringLevel),
00086   m_SQLMonitoring(rhs.m_SQLMonitoring),
00087   m_pluginManager(new CoralServiceManager){
00088 }
00089 
00090 cond::DbConnectionConfiguration::~DbConnectionConfiguration(){
00091   delete m_pluginManager;
00092 }
00093 
00094 cond::DbConnectionConfiguration&
00095 cond::DbConnectionConfiguration::operator=( const cond::DbConnectionConfiguration& rhs){
00096   m_connectionSharing = rhs.m_connectionSharing;
00097   m_connectionTimeOut = rhs.m_connectionTimeOut;
00098   m_readOnlySessionOnUpdateConnections = rhs.m_readOnlySessionOnUpdateConnections;
00099   m_connectionRetrialPeriod = rhs.m_connectionRetrialPeriod;
00100   m_connectionRetrialTimeOut = rhs.m_connectionRetrialTimeOut;
00101   m_poolAutomaticCleanUp = rhs.m_poolAutomaticCleanUp;
00102   m_authPath = rhs.m_authPath;
00103   m_authSys = rhs.m_authSys;
00104   m_transactionId=rhs.m_transactionId;
00105   m_messageLevel = rhs.m_messageLevel;
00106   m_monitoringLevel = rhs.m_monitoringLevel;
00107   m_SQLMonitoring = rhs.m_SQLMonitoring;
00108   return *this;
00109 }
00110 
00111 void cond::DbConnectionConfiguration::setParameters( const edm::ParameterSet& connectionPset ){
00112   std::string authPath = connectionPset.getUntrackedParameter<std::string>("authenticationPath","");
00113   setAuthenticationPath(authPath);
00114   int authSysPar = connectionPset.getUntrackedParameter<int>("authenticationSystem",0);
00115   setAuthenticationSystem( authSysPar );
00116   setTransactionId(connectionPset.getUntrackedParameter<std::string>("transactionId",""));
00117   int messageLevel = connectionPset.getUntrackedParameter<int>("messageLevel",0);
00118   coral::MsgLevel level = coral::Error;
00119   switch (messageLevel) {
00120     case 0 :
00121       level = coral::Error;
00122       break;    
00123     case 1:
00124       level = coral::Warning;
00125       break;
00126     case 2:
00127       level = coral::Info;
00128       break;
00129     case 3:
00130       level = coral::Debug;
00131       break;
00132     default:
00133       level = coral::Error;
00134   }
00135   setMessageLevel(level);
00136   bool enableConnectionSharing = connectionPset.getUntrackedParameter<bool>("enableConnectionSharing",true);
00137   setConnectionSharing( enableConnectionSharing );
00138   int connectionTimeOut = connectionPset.getUntrackedParameter<int>("connectionTimeOut",600);
00139   setConnectionTimeOut( connectionTimeOut );
00140   bool enableReadOnlySessionOnUpdateConnection = connectionPset.getUntrackedParameter<bool>("enableReadOnlySessionOnUpdateConnection",true);
00141   setReadOnlySessionOnUpdateConnections( enableReadOnlySessionOnUpdateConnection );
00142   int connectionRetrialPeriod = connectionPset.getUntrackedParameter<int>("connectionRetrialPeriod",30);
00143   setConnectionRetrialPeriod( connectionRetrialPeriod );
00144   int connectionRetrialTimeOut = connectionPset.getUntrackedParameter<int>("connectionRetrialTimeOut",180);
00145   setConnectionRetrialTimeOut( connectionRetrialTimeOut );
00146   bool enablePoolAutomaticCleanUp = connectionPset.getUntrackedParameter<bool>("enablePoolAutomaticCleanUp",false);
00147   setPoolAutomaticCleanUp( enablePoolAutomaticCleanUp );
00148   //int idleConnectionCleanupPeriod = connectionPset.getUntrackedParameter<int>("idleConnectionCleanupPeriod",300);
00149 }
00150 
00151 void cond::DbConnectionConfiguration::setConnectionSharing( bool flag ){
00152   m_connectionSharing.first = true;
00153   m_connectionSharing.second = flag;
00154 }
00155 
00156 void cond::DbConnectionConfiguration::setConnectionTimeOut( int timeOut ){
00157   m_connectionTimeOut.first = true;
00158   m_connectionTimeOut.second = timeOut;
00159 }
00160 
00161 void cond::DbConnectionConfiguration::setReadOnlySessionOnUpdateConnections( bool flag ){
00162   m_readOnlySessionOnUpdateConnections.first = true;
00163   m_readOnlySessionOnUpdateConnections.second = flag;
00164 }
00165 
00166 void cond::DbConnectionConfiguration::setConnectionRetrialPeriod( int period ){
00167   m_connectionRetrialPeriod.first = true;
00168   m_connectionRetrialPeriod.second = period;
00169 }
00170 
00171 void cond::DbConnectionConfiguration::setConnectionRetrialTimeOut( int timeout ){
00172   m_connectionRetrialTimeOut.first = true;
00173   m_connectionRetrialTimeOut.second = timeout;
00174 }
00175 
00176 void cond::DbConnectionConfiguration::setPoolAutomaticCleanUp( bool flag ){
00177   m_poolAutomaticCleanUp.first = true;
00178   m_poolAutomaticCleanUp.second = flag;
00179 }
00180 
00181 void cond::DbConnectionConfiguration::setAuthenticationPath( const std::string& p ){
00182   m_authPath = p;
00183 }
00184 
00185 void cond::DbConnectionConfiguration::setAuthenticationSystem( int authSysCode ){
00186   m_authSys = authSysCode;
00187 }
00188 
00189 void cond::DbConnectionConfiguration::setTransactionId( std::string const & tid) {
00190   m_transactionId=tid;
00191 }
00192 
00193 void cond::DbConnectionConfiguration::setMessageLevel( coral::MsgLevel l ) {
00194   m_messageLevel = l; 
00195 }
00196 
00197 void cond::DbConnectionConfiguration::setMonitoringLevel( coral::monitor::Level l)
00198 {
00199   m_monitoringLevel = l;  
00200 }
00201 
00202 
00203 void cond::DbConnectionConfiguration::setSQLMonitoring( bool flag ){
00204   m_SQLMonitoring = flag;
00205 }
00206 
00207 void cond::DbConnectionConfiguration::configure( coral::IConnectionServiceConfiguration& coralConfig) const 
00208 {
00209   // message streaming
00210   coral::MessageStream::setMsgVerbosity( m_messageLevel );
00211   std::string authServiceName("CORAL/Services/EnvironmentAuthenticationService");
00212   std::string authPath = m_authPath;
00213   // authentication
00214   if( authPath.empty() ){
00215     // first try to check the env...
00216     const char* authEnv = ::getenv( Auth::COND_AUTH_PATH );
00217     if(authEnv){
00218       authPath += authEnv;
00219     } 
00220   }
00221   int authSys = m_authSys;
00222   if( authSys != CondDbKey && authSys != CoralXMLFile ){
00223     // first attempt, look at the env...
00224     const char* authSysEnv = ::getenv( Auth::COND_AUTH_SYS );
00225     if( authSysEnv ){
00226       authSys = ::atoi( authSysEnv );
00227     }
00228   }
00229   if( authSys != CondDbKey && authSys != CoralXMLFile ){
00230     // take the default
00231     authSys = CoralXMLFile;
00232   }  
00233   std::string servName("");
00234   if( authSys == CondDbKey ){
00235     if( authPath.empty() ){
00236       const char* authEnv = ::getenv("HOME");
00237       if(authEnv){
00238         authPath += authEnv;
00239       } 
00240     }
00241     servName = "COND/Services/RelationalAuthenticationService";     
00242     //edm::LogInfo("DbSessionInfo") << "Authentication using Keys";  
00243   } else if( authSys == CoralXMLFile ){
00244     if( authPath.empty() ){
00245       authPath = ".";
00246     }
00247     servName = "COND/Services/XMLAuthenticationService";  
00248     //edm::LogInfo("DbSessionInfo") << "Authentication using XML File";  
00249   }
00250   if( !authPath.empty() ){
00251     authServiceName = servName;    
00252     coral::Context::instance().PropertyManager().property(Auth::COND_AUTH_PATH_PROPERTY)->set(authPath);  
00253     coral::Context::instance().loadComponent( authServiceName, m_pluginManager );
00254   }
00255   coralConfig.setAuthenticationService( authServiceName );
00256   // connection sharing
00257   if(m_connectionSharing.first)
00258   {
00259     if(m_connectionSharing.second) coralConfig.enableConnectionSharing();
00260     else coralConfig.disableConnectionSharing();   
00261   }
00262   // connection timeout
00263   if(m_connectionTimeOut.first)
00264   {
00265     coralConfig.setConnectionTimeOut(m_connectionTimeOut.second);    
00266   }
00267   // read only session on update connection
00268   if(m_readOnlySessionOnUpdateConnections.first)
00269   {
00270     if(m_readOnlySessionOnUpdateConnections.second) coralConfig.enableReadOnlySessionOnUpdateConnections();
00271     else coralConfig.disableReadOnlySessionOnUpdateConnections();    
00272   }
00273   // pool automatic clean up
00274   if(m_poolAutomaticCleanUp.first)
00275   {
00276     if(m_poolAutomaticCleanUp.second) coralConfig.enablePoolAutomaticCleanUp();
00277     else coralConfig.disablePoolAutomaticCleanUp();
00278   }
00279   // connection retrial period
00280   if(m_connectionRetrialPeriod.first) 
00281   {
00282     coralConfig.setConnectionRetrialPeriod( m_connectionRetrialPeriod.second );    
00283   }
00284   // connection retrial timeout
00285   if( m_connectionRetrialTimeOut.first)
00286   {
00287     coralConfig.setConnectionRetrialTimeOut(m_connectionRetrialTimeOut.second );    
00288   }
00289   // monitoring level
00290   coralConfig.setMonitoringLevel( m_monitoringLevel );
00291   // SQL monitoring
00292   if( m_SQLMonitoring )
00293   {
00294     std::string pluginName("COND/Services/SQLMonitoringService");
00295     coral::Context::instance().loadComponent( pluginName, m_pluginManager );
00296     coralConfig.setMonitoringLevel(coral::monitor::Trace);   
00297   }
00298   
00299 }
00300 
00301 bool cond::DbConnectionConfiguration::isConnectionSharingEnabled() const 
00302 {
00303   return m_connectionSharing.second;  
00304 }
00305 
00306 int cond::DbConnectionConfiguration::connectionTimeOut() const {
00307   return m_connectionTimeOut.second;  
00308 }
00309 
00310 bool cond::DbConnectionConfiguration::isReadOnlySessionOnUpdateConnectionEnabled() const {
00311   return m_readOnlySessionOnUpdateConnections.second;  
00312 }
00313 
00314 int cond::DbConnectionConfiguration::connectionRetrialPeriod() const {
00315   return m_connectionRetrialPeriod.second;  
00316 }
00317 
00318 int cond::DbConnectionConfiguration::connectionRetrialTimeOut() const {
00319   return m_connectionRetrialTimeOut.second;
00320 }
00321 
00322 bool cond::DbConnectionConfiguration::isPoolAutomaticCleanUpEnabled() const {
00323   return m_poolAutomaticCleanUp.second;  
00324 }
00325 
00326 const std::string& cond::DbConnectionConfiguration::authenticationPath() const 
00327 {
00328   return m_authPath;
00329 }
00330 
00331 const std::string& cond::DbConnectionConfiguration::transactionId() const {
00332   return m_transactionId;
00333 }
00334 
00335 
00336 coral::MsgLevel cond::DbConnectionConfiguration::messageLevel() const 
00337 {
00338   return m_messageLevel;
00339 }
00340 
00341 bool cond::DbConnectionConfiguration::isSQLMonitoringEnabled() const
00342 {
00343   return m_SQLMonitoring;  
00344 }
00345