CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/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   // first attempt, look at the env...
00223   const char* authSysEnv = ::getenv( Auth::COND_AUTH_SYS );
00224   if( authSysEnv ){
00225     authSys = ::atoi( authSysEnv );
00226   }
00227   if( authSys != CondDbKey && authSys != CoralXMLFile ){
00228     // take the default
00229     authSys = CondDbKey;
00230   }  
00231   std::string servName("");
00232   if( authSys == CondDbKey ){
00233     if( authPath.empty() ){
00234       const char* authEnv = ::getenv("HOME");
00235       if(authEnv){
00236         authPath += authEnv;
00237       } 
00238     }
00239     servName = "COND/Services/RelationalAuthenticationService";     
00240     //edm::LogInfo("DbSessionInfo") << "Authentication using Keys";  
00241   } else if( authSys == CoralXMLFile ){
00242     if( authPath.empty() ){
00243       authPath = ".";
00244     }
00245     servName = "COND/Services/XMLAuthenticationService";  
00246     //edm::LogInfo("DbSessionInfo") << "Authentication using XML File";  
00247   }
00248   if( !authPath.empty() ){
00249     authServiceName = servName;    
00250     coral::Context::instance().PropertyManager().property(Auth::COND_AUTH_PATH_PROPERTY)->set(authPath);  
00251     coral::Context::instance().loadComponent( authServiceName, m_pluginManager );
00252   }
00253   coralConfig.setAuthenticationService( authServiceName );
00254   // connection sharing
00255   if(m_connectionSharing.first)
00256   {
00257     if(m_connectionSharing.second) coralConfig.enableConnectionSharing();
00258     else coralConfig.disableConnectionSharing();   
00259   }
00260   // connection timeout
00261   if(m_connectionTimeOut.first)
00262   {
00263     coralConfig.setConnectionTimeOut(m_connectionTimeOut.second);    
00264   }
00265   // read only session on update connection
00266   if(m_readOnlySessionOnUpdateConnections.first)
00267   {
00268     if(m_readOnlySessionOnUpdateConnections.second) coralConfig.enableReadOnlySessionOnUpdateConnections();
00269     else coralConfig.disableReadOnlySessionOnUpdateConnections();    
00270   }
00271   // pool automatic clean up
00272   if(m_poolAutomaticCleanUp.first)
00273   {
00274     if(m_poolAutomaticCleanUp.second) coralConfig.enablePoolAutomaticCleanUp();
00275     else coralConfig.disablePoolAutomaticCleanUp();
00276   }
00277   // connection retrial period
00278   if(m_connectionRetrialPeriod.first) 
00279   {
00280     coralConfig.setConnectionRetrialPeriod( m_connectionRetrialPeriod.second );    
00281   }
00282   // connection retrial timeout
00283   if( m_connectionRetrialTimeOut.first)
00284   {
00285     coralConfig.setConnectionRetrialTimeOut(m_connectionRetrialTimeOut.second );    
00286   }
00287   // monitoring level
00288   coralConfig.setMonitoringLevel( m_monitoringLevel );
00289   // SQL monitoring
00290   if( m_SQLMonitoring )
00291   {
00292     std::string pluginName("COND/Services/SQLMonitoringService");
00293     coral::Context::instance().loadComponent( pluginName, m_pluginManager );
00294     coralConfig.setMonitoringLevel(coral::monitor::Trace);   
00295   }
00296   
00297 }
00298 
00299 bool cond::DbConnectionConfiguration::isConnectionSharingEnabled() const 
00300 {
00301   return m_connectionSharing.second;  
00302 }
00303 
00304 int cond::DbConnectionConfiguration::connectionTimeOut() const {
00305   return m_connectionTimeOut.second;  
00306 }
00307 
00308 bool cond::DbConnectionConfiguration::isReadOnlySessionOnUpdateConnectionEnabled() const {
00309   return m_readOnlySessionOnUpdateConnections.second;  
00310 }
00311 
00312 int cond::DbConnectionConfiguration::connectionRetrialPeriod() const {
00313   return m_connectionRetrialPeriod.second;  
00314 }
00315 
00316 int cond::DbConnectionConfiguration::connectionRetrialTimeOut() const {
00317   return m_connectionRetrialTimeOut.second;
00318 }
00319 
00320 bool cond::DbConnectionConfiguration::isPoolAutomaticCleanUpEnabled() const {
00321   return m_poolAutomaticCleanUp.second;  
00322 }
00323 
00324 const std::string& cond::DbConnectionConfiguration::authenticationPath() const 
00325 {
00326   return m_authPath;
00327 }
00328 
00329 const std::string& cond::DbConnectionConfiguration::transactionId() const {
00330   return m_transactionId;
00331 }
00332 
00333 
00334 coral::MsgLevel cond::DbConnectionConfiguration::messageLevel() const 
00335 {
00336   return m_messageLevel;
00337 }
00338 
00339 bool cond::DbConnectionConfiguration::isSQLMonitoringEnabled() const
00340 {
00341   return m_SQLMonitoring;  
00342 }
00343