CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Member Functions | Private Attributes
cond::XMLAuthenticationService::XMLAuthenticationService Class Reference

#include <XMLAuthenticationService.h>

Inheritance diagram for cond::XMLAuthenticationService::XMLAuthenticationService:

Public Member Functions

const
coral::IAuthenticationCredentials & 
credentials (const std::string &connectionString) const
 
const
coral::IAuthenticationCredentials & 
credentials (const std::string &connectionString, const std::string &role) const
 
void setInputFileName (const std::string &inputFileName)
 Sets the input file name. More...
 
 XMLAuthenticationService (const std::string &name)
 Standard Constructor. More...
 
virtual ~XMLAuthenticationService ()
 Standard Destructor. More...
 

Private Member Functions

bool initialize ()
 Service framework related initialization. More...
 
bool processFile (const std::string &inputFileName)
 Parses an xml file. More...
 
void reset ()
 Reset parsed data. More...
 
std::set< std::string > verifyFileName ()
 Verifies the existence of the authentication files. More...
 

Private Attributes

coral::Property::CallbackID m_callbackID
 
std::map< std::string,
DataSourceEntry * > 
m_data
 The structure with the authentication data. More...
 
std::string m_inputFileName
 The input file with the data. More...
 
bool m_isInitialized
 Flag indicating whether the service has been initialized. More...
 
boost::mutex m_mutexLock
 the mutex lock More...
 

Detailed Description

Definition at line 85 of file XMLAuthenticationService.h.

Constructor & Destructor Documentation

cond::XMLAuthenticationService::XMLAuthenticationService::XMLAuthenticationService ( const std::string &  name)
explicit

Standard Constructor.

Definition at line 83 of file XMLAuthenticationService.cc.

References m_callbackID, m_inputFileName, and setInputFileName().

84  : coral::Service( key ),
85  m_isInitialized( false ),
86  m_inputFileName( "" ),
87  m_data(),
88  m_mutexLock(),
89  m_callbackID(0)
90 {
91  boost::function1<void, std::string> cb(boost::bind(&cond::XMLAuthenticationService::XMLAuthenticationService::setInputFileName, this, _1));
92 
93  coral::Property* pm = dynamic_cast<coral::Property*>(coral::Context::instance().PropertyManager().property("AuthenticationFile"));
94  if(pm){
95  m_inputFileName = pm->get();
96  m_callbackID = pm->registerCallback(cb);
97  }
98 }
bool m_isInitialized
Flag indicating whether the service has been initialized.
void setInputFileName(const std::string &inputFileName)
Sets the input file name.
std::map< std::string, DataSourceEntry * > m_data
The structure with the authentication data.
list key
Definition: combine.py:13
std::string m_inputFileName
The input file with the data.
cond::XMLAuthenticationService::XMLAuthenticationService::~XMLAuthenticationService ( )
virtual

Standard Destructor.

Definition at line 100 of file XMLAuthenticationService.cc.

101 {
102  for ( std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::iterator iConnection = m_data.begin();
103  iConnection != m_data.end(); ++iConnection ) delete iConnection->second;
104 }
std::map< std::string, DataSourceEntry * > m_data
The structure with the authentication data.

Member Function Documentation

const coral::IAuthenticationCredentials & cond::XMLAuthenticationService::XMLAuthenticationService::credentials ( const std::string &  connectionString) const

Returns a reference to the credentials object for a given connection string. If the connection string is not known to the service an UnknownConnectionException is thrown.

Definition at line 369 of file XMLAuthenticationService.cc.

References credentials(), CommonMethods::lock(), and AlCaRecoCosmics_cfg::name.

Referenced by credentials().

370 {
371  boost::mutex::scoped_lock lock(m_mutexLock);
372  if ( ! m_isInitialized ) {
374  }
375  std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::const_iterator iConnection = m_data.find( connectionString );
376  if ( iConnection == m_data.end() )
377  throw coral::UnknownConnectionException( this->name(), connectionString );
378  return iConnection->second->credentials();
379 }
bool initialize()
Service framework related initialization.
bool m_isInitialized
Flag indicating whether the service has been initialized.
std::map< std::string, DataSourceEntry * > m_data
The structure with the authentication data.
const coral::IAuthenticationCredentials & cond::XMLAuthenticationService::XMLAuthenticationService::credentials ( const std::string &  connectionString,
const std::string &  role 
) const

Returns a reference to the credentials object for a given connection string. If the connection string is not known to the service an UnknownConnectionException is thrown. If the role is not known to the service an UnknownRoleException is thrown.

Definition at line 383 of file XMLAuthenticationService.cc.

References credentials(), CommonMethods::lock(), and AlCaRecoCosmics_cfg::name.

385 {
386  boost::mutex::scoped_lock lock(m_mutexLock);
387  if ( ! m_isInitialized ) {
389  }
390  std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::const_iterator iConnection = m_data.find( connectionString );
391  if ( iConnection == m_data.end() )
392  throw coral::UnknownConnectionException( this->name(), connectionString );
393  return iConnection->second->credentials( role );
394 }
bool initialize()
Service framework related initialization.
bool m_isInitialized
Flag indicating whether the service has been initialized.
std::map< std::string, DataSourceEntry * > m_data
The structure with the authentication data.
bool cond::XMLAuthenticationService::XMLAuthenticationService::initialize ( )
private

Service framework related initialization.

Definition at line 318 of file XMLAuthenticationService.cc.

References Debug, edm::endmsg(), funct::log(), argparse::message, valtools::processFile(), cmsPerfSuiteHarvest::release, reset(), and query::result.

319 {
320  coral::MessageStream log("cond::XMLAuthenticationService::initialize");
321  std::set< std::string > inputFileNames = this->verifyFileName();
322  if ( inputFileNames.empty() )
323  {
324  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
325  //std::cout<< "Could not open \"" << m_inputFileName << "\" for reading" << std::endl;
326  log<<coral::Debug<<"Could not open \"" << m_inputFileName << "\" for reading" <<coral::MessageStream::endmsg;
327  return false;
328  }
329 
330  try
331  {
332  xercesc::XMLPlatformUtils::Initialize();
333  }
334  catch ( const xercesc::XMLException& toCatch )
335  {
336  char* message = xercesc::XMLString::transcode( toCatch.getMessage() );
337  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
338  //log << seal::Msg::Error << message << seal::flush;
339  log<<coral::Error<<std::string(message)<<coral::MessageStream::endmsg;
340  xercesc::XMLString::release( &message );
341  return false;
342  }
343 
344  bool result = false;
345  for ( std::set< std::string >::const_reverse_iterator iFileName = inputFileNames.rbegin();
346  iFileName != inputFileNames.rend(); ++iFileName ) {
347  if ( this->processFile( *iFileName ) ) {
348  result = true;
349  }
350  }
351 
352  xercesc::XMLPlatformUtils::Terminate();
353 
355  if(!m_isInitialized) reset();
356  return result;
357 }
void endmsg(ErrorLog &)
bool m_isInitialized
Flag indicating whether the service has been initialized.
std::set< std::string > verifyFileName()
Verifies the existence of the authentication files.
tuple result
Definition: query.py:137
Log< T >::type log(const T &t)
Definition: Log.h:22
string message
Definition: argparse.py:126
bool processFile(const std::string &inputFileName)
Parses an xml file.
std::string m_inputFileName
The input file with the data.
const bool Debug
bool cond::XMLAuthenticationService::XMLAuthenticationService::processFile ( const std::string &  inputFileName)
private

Parses an xml file.

Definition at line 114 of file XMLAuthenticationService.cc.

References cond::XMLAuthenticationService::DataSourceEntry::appendCredentialItem(), cond::XMLAuthenticationService::DataSourceEntry::appendCredentialItemForRole(), cont, cond::FileReader::content(), cond::DecodingKey::dataSource(), Debug, edm::decode(), edm::endmsg(), analyzePatCleaning_cfg::inputFile, combine::key, cond::DecodingKey::key(), funct::log(), argparse::message, AlCaRecoCosmics_cfg::name, geometryXMLtoCSV::parser, path(), cond::FileReader::read(), cond::DecodingKey::readUserKeyString(), cmsPerfSuiteHarvest::release, query::result, cms::Exception::what(), and XML_AUTHENTICATION_FILE().

115 {
116  coral::MessageStream log("cond::XMLAuthenticationService::processFile");
117  //std::cout<< "Processing file \""<< inputFileName<<"\"" <<std::endl;
118  bool result = true;
119 
121  std::string cont("");
122  try{
123  inputFile.read(inputFileName);
124  cont = inputFile.content();
125  } catch (const cond::Exception& exc){
126  log << coral::Error << "File \"" << inputFileName << "\" not found."<<std::string(exc.what())<<coral::MessageStream::endmsg;
127  return false;
128  }
129 
130  // check the
131  boost::filesystem::path filePath( inputFileName );
132  std::string name = filePath.leaf();
133  if(name!=XML_AUTHENTICATION_FILE){
135  try{
136  key.readUserKeyString(cont);
137  log << coral::Debug << "Decoding content of file \""<< key.dataSource()<<"\""<<coral::MessageStream::endmsg;
138  cond::FileReader dataFile;
139  dataFile.read(key.dataSource());
140  cont = dataFile.content();
142  } catch (const cond::Exception& exc){
143  log << coral::Error << std::string(exc.what())<<coral::MessageStream::endmsg;
144  return false;
145  }
146 
147  } else {
148  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
149  log<<coral::Debug<< "Authentication file is expected standard XML."<<coral::MessageStream::endmsg;
150  }
151 
152  xercesc::MemBufInputSource* memBufInputSource = 0;
153 
154  try
155  {
156  xercesc::XercesDOMParser parser;
157  parser.setValidationScheme( xercesc::XercesDOMParser::Val_Always );
158  parser.setDoNamespaces( true );
159 
160  xercesc::HandlerBase errorHandler;
161  parser.setErrorHandler( &errorHandler );
162 
163 
164  const char* bufferId="InMemoryDocument";
165  const char* buffer = cont.c_str();
166 
167  memBufInputSource = new xercesc::MemBufInputSource( (const XMLByte*)buffer,strlen(buffer),bufferId,false );
168 
169  parser.parse(*memBufInputSource);
170 
171  xercesc::DOMDocument* document = parser.getDocument();
172 
173  XMLCh tempStr[20];
174  xercesc::XMLString::transcode( "connection", tempStr, 19);
175 
176  xercesc::DOMNodeList* connectionList = document->getElementsByTagName( tempStr );
177 
178  if ( connectionList )
179  {
180  XMLSize_t numberOfConnections = connectionList->getLength();
181 
182  for ( XMLSize_t iConnection = 0; iConnection < numberOfConnections; ++iConnection )
183  {
184  xercesc::DOMNode* connectionNode = connectionList->item( iConnection );
185 
186  if ( connectionNode )
187  {
188  char* connectionName = xercesc::XMLString::transcode( connectionNode->getAttributes()->item( 0 )->getNodeValue() );
189  std::string sConnectionName = connectionName;
190  xercesc::XMLString::release( &connectionName );
191 
192  // Locate the credential
194  std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::iterator iConnection = m_data.find( sConnectionName );
195  if ( iConnection != m_data.end() ) {
196  credential = iConnection->second;
197  // Issue a warning here.
198  //coral::MessageStream log( this, this->name(),seal::Msg::Verbose );
199  log<<coral::Debug<<"Credential parameters for connection string \""
200  << sConnectionName
201  << "\" have already been defined. Only new elements are appended, while existing will be ignored."
203  } else {
204  credential = new cond::XMLAuthenticationService::DataSourceEntry( this->name(), sConnectionName );
205  m_data.insert( std::make_pair( sConnectionName, credential ) );
206  }
207 
208  xercesc::DOMNodeList* parameterList = connectionNode->getChildNodes();
209 
210  if ( parameterList )
211  {
212  XMLSize_t numberOfParameters = parameterList->getLength();
213 
214  for ( XMLSize_t iParameter = 0; iParameter < numberOfParameters; ++iParameter )
215  {
216  xercesc::DOMNode* parameterNode = parameterList->item( iParameter );
217 
218  if ( parameterNode && parameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE )
219  {
220  char* nodeName = xercesc::XMLString::transcode( parameterNode->getNodeName() );
221  std::string sNodeName = nodeName;
222  xercesc::XMLString::release( &nodeName );
223 
224  if ( sNodeName == "parameter" ) { // The default parameters
225  char* parameterName = xercesc::XMLString::transcode( parameterNode->getAttributes()->item( 0 )->getNodeValue() );
226  std::string sParameterName = parameterName;
227  xercesc::XMLString::release( &parameterName );
228  char* parameterValue = xercesc::XMLString::transcode( parameterNode->getAttributes()->item( 1 )->getNodeValue() );
229  std::string sParameterValue = parameterValue;
230  xercesc::XMLString::release( &parameterValue );
231 
232  credential->appendCredentialItem( sParameterName, sParameterValue );
233  }
234  else if ( sNodeName == "role" ) { // A role
235  char* roleName = xercesc::XMLString::transcode( parameterNode->getAttributes()->item( 0 )->getNodeValue() );
236  std::string sRoleName = roleName;
237  xercesc::XMLString::release( &roleName );
238 
239  // Retrieve the parameters for the role
240  xercesc::DOMNodeList* roleParameterList = parameterNode->getChildNodes();
241 
242 
243  if ( roleParameterList )
244  {
245  XMLSize_t numberOfRoleParameters = roleParameterList->getLength();
246 
247  for ( XMLSize_t iRoleParameter = 0; iRoleParameter < numberOfRoleParameters; ++iRoleParameter )
248  {
249  xercesc::DOMNode* roleParameterNode = roleParameterList->item( iRoleParameter );
250  if ( roleParameterNode && roleParameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE )
251  {
252  char* roleNodeName = xercesc::XMLString::transcode( roleParameterNode->getNodeName() );
253  std::string sRoleNodeName = roleNodeName;
254  xercesc::XMLString::release( &roleNodeName );
255 
256  if ( sRoleNodeName == "parameter" ) {
257  char* roleParameterName = xercesc::XMLString::transcode( roleParameterNode->getAttributes()->item( 0 )->getNodeValue() );
258  std::string sRoleParameterName = roleParameterName;
259  xercesc::XMLString::release( &roleParameterName );
260  char* roleParameterValue = xercesc::XMLString::transcode( roleParameterNode->getAttributes()->item( 1 )->getNodeValue() );
261  std::string sRoleParameterValue = roleParameterValue;
262  xercesc::XMLString::release( &roleParameterValue );
263 
264  credential->appendCredentialItemForRole( sRoleParameterName, sRoleParameterValue, sRoleName );
265  }
266  }
267  }
268  }
269  }
270  }
271  }
272  }
273  }
274  }
275  }
276 
277  parser.reset();
278  }
279  catch ( const xercesc::XMLException& toCatch )
280  {
281  char* message = xercesc::XMLString::transcode( toCatch.getMessage() );
282  //coral::MessageStream log( this, this->name(),coral::Msg::Verbose );
283  //log << coral::Msg::Error << message << coral::flush;
284  log<<coral::Error<<std::string(message)<<coral::MessageStream::endmsg;
285  xercesc::XMLString::release( &message );
286  result = false;
287  }
288  catch ( const xercesc::DOMException& toCatch )
289  {
290  char* message = xercesc::XMLString::transcode( toCatch.msg );
291  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
292  //log << seal::Msg::Error << message << seal::flush;
293  log<<coral::Error<<std::string(message)<<coral::MessageStream::endmsg;
294  xercesc::XMLString::release( &message );
295  result = false;
296  }
297  catch ( const xercesc::SAXException& toCatch )
298  {
299  char* message = xercesc::XMLString::transcode( toCatch.getMessage() );
300  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
301  //log << seal::Msg::Error << message << seal::flush;
302  log<<coral::Error<<std::string(message)<<coral::MessageStream::endmsg;
303  xercesc::XMLString::release( &message );
304  result = false;
305  }
306  catch (...){
307  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
308  //log << seal::Msg::Error << "Unexpected Exception parsing file \"" << inputFileName << "\"" << seal::flush;
309  log<<coral::Error<<"Unexpected Exception parsing file \"" << inputFileName << "\"" <<coral::MessageStream::endmsg;
310  result = false;
311  }
312  if(memBufInputSource) delete memBufInputSource;
313  return result;
314 }
virtual char const * what() const
Definition: Exception.cc:97
void appendCredentialItemForRole(const std::string &item, const std::string &value, const std::string &role)
bool readUserKeyString(const std::string &content)
Definition: DecodingKey.cc:16
void endmsg(ErrorLog &)
int path() const
Definition: HLTadd.h:3
bool read(const std::string &fileName)
Definition: FileUtils.cc:6
tuple result
Definition: query.py:137
bool decode(bool &, std::string const &)
Definition: types.cc:67
const std::string & dataSource() const
Definition: DecodingKey.h:53
Log< T >::type log(const T &t)
Definition: Log.h:22
std::map< std::string, DataSourceEntry * > m_data
The structure with the authentication data.
string message
Definition: argparse.py:126
const std::string & content() const
Definition: FileUtils.h:31
int cont
void appendCredentialItem(const std::string &item, const std::string &value)
list key
Definition: combine.py:13
const std::string & key() const
Definition: DecodingKey.h:48
const bool Debug
static std::string XML_AUTHENTICATION_FILE("authentication.xml")
void cond::XMLAuthenticationService::XMLAuthenticationService::reset ( void  )
private

Reset parsed data.

Definition at line 359 of file XMLAuthenticationService.cc.

Referenced by runTheMatrix.MatrixReader::__init__(), and BeautifulSoup.BeautifulStoneSoup::__init__().

359  {
360  for ( std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::iterator iConnection = m_data.begin();
361  iConnection != m_data.end(); ++iConnection ) delete iConnection->second;
362  m_data.clear();
363  m_isInitialized = false;
364 }
bool m_isInitialized
Flag indicating whether the service has been initialized.
std::map< std::string, DataSourceEntry * > m_data
The structure with the authentication data.
void cond::XMLAuthenticationService::XMLAuthenticationService::setInputFileName ( const std::string &  inputFileName)

Sets the input file name.

Definition at line 107 of file XMLAuthenticationService.cc.

References reset().

Referenced by XMLAuthenticationService().

108 {
109  m_inputFileName = inputFileName;
110  reset();
111 }
std::string m_inputFileName
The input file with the data.
std::set< std::string > cond::XMLAuthenticationService::XMLAuthenticationService::verifyFileName ( )
private

Verifies the existence of the authentication files.

Definition at line 398 of file XMLAuthenticationService.cc.

References Debug, edm::endmsg(), align_tpl::fileNames, funct::log(), and path().

399 {
400  coral::MessageStream log("cond::XMLAuthenticationService::verifyFileName");
401  std::set< std::string > fileNames;
402 
403  // Try the file name as is...
405  if ( boost::filesystem::exists( m_inputFileName ) ) {
406  if(boost::filesystem::is_directory( m_inputFileName )){
407  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
408  log <<coral::Error << "Provided path \"" << m_inputFileName << "\" is a directory." <<coral::MessageStream::endmsg;
409  return fileNames;
410  }
411  boost::filesystem::path& fullPath = filePath.normalize();
412  fileNames.insert( fullPath.string() );
413  if(filePath.is_complete()) return fileNames;
414  }
415 
416  // Try to find other files in the path variable
417  const char* thePathVariable = ::getenv( "CORAL_AUTH_PATH" );
418  if ( ! thePathVariable ) return fileNames;
419  log<<coral::Debug<< "File \"" << m_inputFileName << "\" not found in the current directory. Trying in the search path." <<coral::MessageStream::endmsg;
420 
421  std::string searchPath(thePathVariable);
422  //std::cout<<"searchPath "<<searchPath<<std::endl;
423  if(boost::filesystem::exists(searchPath)){
424  if(!boost::filesystem::is_directory( searchPath )){
425  log<<coral::Debug<<"Search path \"" << searchPath << "\" is not a directory."<<coral::MessageStream::endmsg;
426  return fileNames;
427  }
428  boost::filesystem::path fullPath( searchPath );
429  fullPath /= filePath;
430  fileNames.insert( fullPath.string() );
431  } else {
432  log<<coral::Debug<<"Search path \"" << searchPath << "\" does not exist."<<coral::MessageStream::endmsg;
433  return fileNames;
434  }
435 
436  return fileNames;
437 }
tuple fileNames
Definition: align_tpl.py:17
void endmsg(ErrorLog &)
int path() const
Definition: HLTadd.h:3
Log< T >::type log(const T &t)
Definition: Log.h:22
std::string m_inputFileName
The input file with the data.
const bool Debug

Member Data Documentation

coral::Property::CallbackID cond::XMLAuthenticationService::XMLAuthenticationService::m_callbackID
private

Definition at line 140 of file XMLAuthenticationService.h.

Referenced by XMLAuthenticationService().

std::map< std::string, DataSourceEntry* > cond::XMLAuthenticationService::XMLAuthenticationService::m_data
private

The structure with the authentication data.

Definition at line 135 of file XMLAuthenticationService.h.

std::string cond::XMLAuthenticationService::XMLAuthenticationService::m_inputFileName
private

The input file with the data.

Definition at line 132 of file XMLAuthenticationService.h.

Referenced by XMLAuthenticationService().

bool cond::XMLAuthenticationService::XMLAuthenticationService::m_isInitialized
private

Flag indicating whether the service has been initialized.

Definition at line 129 of file XMLAuthenticationService.h.

boost::mutex cond::XMLAuthenticationService::XMLAuthenticationService::m_mutexLock
mutableprivate

the mutex lock

Definition at line 138 of file XMLAuthenticationService.h.