CMS 3D CMS Logo

Public Member Functions | Private Member Functions | Private Attributes

cond::Utilities Class Reference

#include <Utilities.h>

Inheritance diagram for cond::Utilities:
cond::AlignSplitIOV

List of all members.

Public Member Functions

void addAuthenticationOptions ()
void addConfigFileOption ()
void addConnectOption ()
void addConnectOption (const std::string &connectionOptionName, const std::string &shortName, const std::string &helpEntry)
void addDictionaryOption ()
void addLogDBOption ()
template<typename T >
void addOption (const std::string &fullName, const std::string &shortName, const std::string &helpEntry)
void addSQLOutputOption ()
virtual int execute ()
std::string getAuthenticationPathValue ()
std::string getConfigFileValue ()
std::string getConnectValue ()
std::string getDictionaryValue ()
std::string getLogDBValue ()
template<typename T >
T getOptionValue (const std::string &fullName)
std::string getPasswordValue ()
std::string getUserValue ()
bool hasDebug ()
bool hasOptionValue (const std::string &fullName)
void initializePluginManager ()
cond::DbSession openDbSession (const std::string &connectionParameterName, bool readOnly=false)
void parseCommand (int argc, char **argv)
int run (int argc, char **argv)
 Utilities (const std::string &commandName, std::string positionalParameter=std::string(""))
virtual ~Utilities ()

Private Member Functions

std::string getValueIfExists (const std::string &fullName)
void initializeForDbConnection ()
void sendError (const std::string &message)
void sendException (const std::string &message)

Private Attributes

cond::DbConnectionm_dbConnection
std::set< std::string > m_dbSessions
std::string m_name
boost::program_options::options_description m_options
bool m_pluginMgrInitialized
boost::program_options::positional_options_description m_positionalOptions
boost::program_options::variables_map m_values

Detailed Description

Definition at line 19 of file Utilities.h.


Constructor & Destructor Documentation

cond::Utilities::Utilities ( const std::string &  commandName,
std::string  positionalParameter = std::string("") 
)

cond::Utilities::Utilities( const std::string& commandName):m_name(commandName), m_options(std::string("Usage: ")+m_name+std::string(" [options] \n")), m_positionalOptions(), m_values(), m_dbConnection(0), m_pluginMgrInitialized(false), m_dbSessions(){ m_options.add_options() ("debug","switch on debug mode") ("help,h", "help message") ; }

Definition at line 35 of file Utilities.cc.

References m_options, and m_positionalOptions.

                                                          :m_name(commandName),
                                                             m_options(std::string("Usage: ")+m_name+
                                                                       std::string(" [options] ")+positionalParameter
                                                                       +std::string(" \n")),
                                                             m_positionalOptions(),
                                                             m_values(),
                                                             m_dbConnection(0),
                                                             m_pluginMgrInitialized(false),
                                                             m_dbSessions(){
  m_options.add_options()
    ("debug","switch on debug mode")
    ("help,h", "help message")
    ;
  if(!positionalParameter.empty()) {
    m_positionalOptions.add( positionalParameter.c_str(), -1);
    addOption<std::string>(positionalParameter,"",positionalParameter);
  }
}
cond::Utilities::~Utilities ( ) [virtual]

Definition at line 56 of file Utilities.cc.

                         {
  if(m_dbConnection) delete m_dbConnection;
}

Member Function Documentation

void cond::Utilities::addAuthenticationOptions ( )

Definition at line 104 of file Utilities.cc.

Referenced by cond::AlignSplitIOV::AlignSplitIOV().

                                       {
  addOption<std::string>("authPath","P","path to authentication xml(default .)");
  addOption<std::string>("user","u","user name (default \"\")");
  addOption<std::string>("pass","p","password (default \"\")");
}
void cond::Utilities::addConfigFileOption ( )

Definition at line 136 of file Utilities.cc.

                                  {
  addOption<std::string>("configFile","f","configuration file(optional)");
}
void cond::Utilities::addConnectOption ( )

Definition at line 111 of file Utilities.cc.

Referenced by cond::AlignSplitIOV::AlignSplitIOV().

                               {
  addOption<std::string>("connect","c","connection string(required)");
  m_dbSessions.insert("connect");
}
void cond::Utilities::addConnectOption ( const std::string &  connectionOptionName,
const std::string &  shortName,
const std::string &  helpEntry 
)

Definition at line 117 of file Utilities.cc.

                                                              {
  addOption<std::string>(connectionOptionName,shortName,helpEntry);
  m_dbSessions.insert(connectionOptionName);
}
void cond::Utilities::addDictionaryOption ( )

Definition at line 131 of file Utilities.cc.

                                  {
  addOption<std::vector<std::string> >("dictionary","D","data dictionaries (required if no plugin available)");
}
void cond::Utilities::addLogDBOption ( )

Definition at line 126 of file Utilities.cc.

                             {
  addConnectOption("logDB","l","logDB(optional");
}
void cond::Utilities::addOption< bool > ( const std::string &  fullName,
const std::string &  shortName,
const std::string &  helpEntry 
) [inline]

Definition at line 79 of file Utilities.h.

References newFWLiteAna::fullName, and m_options.

                                                                                 {
  std::stringstream optInfo;
  optInfo << fullName;
  if(!shortName.empty()) optInfo << ","<<shortName;
  m_options.add_options()
    (optInfo.str().c_str(),boost::program_options::value<T>(),helpEntry.c_str());
}
void cond::Utilities::addSQLOutputOption ( )

Definition at line 141 of file Utilities.cc.

                                 {
  addOption<bool>("sql","S","dump the sql output (optional)");  
}
int cond::Utilities::execute ( ) [virtual]

Reimplemented in cond::AlignSplitIOV.

Definition at line 60 of file Utilities.cc.

                          {
  return 0;
}
std::string cond::Utilities::getAuthenticationPathValue ( )

Definition at line 159 of file Utilities.cc.

                                                   {
  return getOptionValue<std::string>("authPath");
}
std::string cond::Utilities::getConfigFileValue ( )

Definition at line 183 of file Utilities.cc.

                                           {
  return getOptionValue<std::string>("configFile");
}
std::string cond::Utilities::getConnectValue ( )

Definition at line 171 of file Utilities.cc.

                                        {
  return getOptionValue<std::string>("connect");  
}
std::string cond::Utilities::getDictionaryValue ( )

Definition at line 179 of file Utilities.cc.

                                           {
  return getOptionValue<std::string>("dictionary");
}
std::string cond::Utilities::getLogDBValue ( )

Definition at line 175 of file Utilities.cc.

                                      {
  return getOptionValue<std::string>("logDB");  
}
template<typename T >
T cond::Utilities::getOptionValue ( const std::string &  fullName) [inline]

Definition at line 102 of file Utilities.h.

References newFWLiteAna::found, newFWLiteAna::fullName, m_options, m_values, argparse::message, sendError(), and sendException().

                                                                                     {
  const void* found = m_options.find_nothrow(fullName, false);
  if(!found){
    std::stringstream message;
    message << "Utilities::getOptionValue: option \"" << fullName << "\" is not known by the command.";
    sendException(message.str());
  } 
  
  if (!m_values.count(fullName)) {
    std::stringstream message;
    message << "Error: Option \"" << fullName << "\" has not been provided.";
    sendError(message.str());
  }
  return m_values[fullName].as<T>();
}
std::string cond::Utilities::getPasswordValue ( )

Definition at line 167 of file Utilities.cc.

                                         {
  return getOptionValue<std::string>("pass");  
}
std::string cond::Utilities::getUserValue ( )

Definition at line 163 of file Utilities.cc.

                                     {
  return getOptionValue<std::string>("user");  
}
std::string cond::Utilities::getValueIfExists ( const std::string &  fullName) [private]

Definition at line 264 of file Utilities.cc.

References newFWLiteAna::fullName.

                                                                  {
  std::string val("");
  if(m_values.count(fullName)){
    val = m_values[fullName].as<std::string>();
  }
  return val;
}
bool cond::Utilities::hasDebug ( )

Definition at line 198 of file Utilities.cc.

                            {
  return m_values.count("debug");  
}
bool cond::Utilities::hasOptionValue ( const std::string &  fullName)

Definition at line 188 of file Utilities.cc.

References newFWLiteAna::found, and argparse::message.

                                                           {
  const void* found = m_options.find_nothrow(fullName, false);
  if(!found){
    std::stringstream message;
    message << "Utilities::hasOptionValue: option \"" << fullName << "\" is not known by the command.";
    sendException(message.str());
  }
  return m_values.count(fullName);
}
void cond::Utilities::initializeForDbConnection ( ) [private]

Definition at line 218 of file Utilities.cc.

References builder_last_value_cfg::authPath, Debug, and hcal_dqm_sourceclient-file_cfg::user.

                                             {
  if(!m_dbConnection){
    initializePluginManager();
    m_dbConnection = new cond::DbConnection();
    if( hasDebug() ){
      m_dbConnection->configuration().setMessageLevel(coral::Debug);
    } else {
      m_dbConnection->configuration().setMessageLevel(coral::Error);
    }
    m_dbConnection->configuration().setPoolAutomaticCleanUp( false );
    m_dbConnection->configuration().setConnectionTimeOut(0);

    if(m_options.find_nothrow("authPath", false) &&
       m_options.find_nothrow("user",false) &&
       m_options.find_nothrow("pass",false)){
      std::string authPath = getValueIfExists("authPath");
      std::string user = getValueIfExists("user");
      std::string pass = getValueIfExists("pass");
      if( !authPath.empty() ){
        m_dbConnection->configuration().setAuthenticationPath(authPath);
      } else {
        if( !user.empty()  && !pass.empty() ){
          std::string userenv(std::string("CORAL_AUTH_USER=")+user);
          std::string passenv(std::string("CORAL_AUTH_PASSWORD=")+pass);
          ::putenv(const_cast<char*>(userenv.c_str()));
          ::putenv(const_cast<char*>(passenv.c_str()));
        }
      }
    }
    if(m_options.find_nothrow("sql",false)){
      if(m_values.count("sql")) m_dbConnection->configuration().setSQLMonitoring( true );
    }
    m_dbConnection->configure();
    
  }
  
}
void cond::Utilities::initializePluginManager ( )
cond::DbSession cond::Utilities::openDbSession ( const std::string &  connectionParameterName,
bool  readOnly = false 
)

Definition at line 256 of file Utilities.cc.

References python::CommonUtils::session.

                                                                                                   {
  initializeForDbConnection();
  std::string connectionString = getOptionValue<std::string>( connectionParameterName );
  cond::DbSession session = m_dbConnection->createSession();
  session.open( connectionString, readOnly );
  return session;
}
void cond::Utilities::parseCommand ( int  argc,
char **  argv 
)

Definition at line 145 of file Utilities.cc.

References HDQMDatabaseProducer::configFile, recoMuon::in, loadConditions::options, and DTTTrigCorrFirst::run.

                                                       {
  boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(m_options).positional(m_positionalOptions).run(), m_values);
  if(m_options.find_nothrow("configFile",false)){
    std::string configFileName = getValueIfExists("configFile");
    if (! configFileName.empty()){
      std::fstream configFile;
      configFile.open(configFileName.c_str(), std::fstream::in);
      boost::program_options::store(boost::program_options::parse_config_file(configFile,m_options), m_values);
      configFile.close();
    }
  }
  boost::program_options::notify(m_values);
}
int cond::Utilities::run ( int  argc,
char **  argv 
)

Definition at line 64 of file Utilities.cc.

References gather_cfg::cout, python::multivaluedict::dict, exception, cmsIgProf_Analysis::execute(), cond::SQLReport::putOnFile(), zeeHLT_cff::report, cond::SQLReport::reportForConnection(), runTheMatrix::ret, and cms::Exception::what().

Referenced by main().

                                             {
  int ret = 0;
  try{
    parseCommand( argc, argv );
    if (m_values.count("help")) {
      std::cout << m_options <<std::endl;;
      return 0;
    }
    if(m_options.find_nothrow("dictionary",false)&& m_values.count("dictionary")){
      std::vector<std::string> dictionaries =
        m_values["dictionary"].as<std::vector<std::string> >();
      if(!dictionaries.empty()){
        initializePluginManager();
        cond::SharedLibraryName libName;
        BOOST_FOREACH(std::string const & dict, dictionaries)
          edmplugin::SharedLibrary( libName(dict) );
      }
    }
    ret = execute();
    if(m_dbConnection){
      if(m_options.find_nothrow("sql",false) && m_values.count("sql")){
        cond::SQLReport report(*m_dbConnection);
        for(std::set<std::string>::const_iterator iSess = m_dbSessions.begin();
            iSess != m_dbSessions.end();
            iSess++) report.reportForConnection(*iSess);
        report.putOnFile();
      }
    }
    
  }catch( cond::Exception& err ){
    std::cout << err.what() << std::endl;
    ret = 1;
  }catch( const std::exception& exc ){
    std::cout << exc.what() << std::endl;
    ret = 1;
  }
  return ret;
}
void cond::Utilities::sendError ( const std::string &  message) [private]

Definition at line 272 of file Utilities.cc.

Referenced by getOptionValue().

void cond::Utilities::sendException ( const std::string &  message) [private]

Definition at line 276 of file Utilities.cc.

References Exception.

Referenced by getOptionValue().

                                                           {
  throw cond::Exception(message);
}

Member Data Documentation

Definition at line 73 of file Utilities.h.

std::set<std::string> cond::Utilities::m_dbSessions [private]

Definition at line 75 of file Utilities.h.

std::string cond::Utilities::m_name [private]

Definition at line 68 of file Utilities.h.

boost::program_options::options_description cond::Utilities::m_options [private]

Definition at line 70 of file Utilities.h.

Referenced by addOption(), getOptionValue(), and Utilities().

Definition at line 74 of file Utilities.h.

boost::program_options::positional_options_description cond::Utilities::m_positionalOptions [private]

Definition at line 71 of file Utilities.h.

Referenced by Utilities().

boost::program_options::variables_map cond::Utilities::m_values [private]

Definition at line 72 of file Utilities.h.

Referenced by getOptionValue().