CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiteLocalConfigService.cc
Go to the documentation of this file.
1 //<<<<<< INCLUDES >>>>>>
2 
8 #include <xercesc/dom/DOM.hpp>
9 #include <xercesc/parsers/XercesDOMParser.hpp>
10 #include <xercesc/util/PlatformUtils.hpp>
11 #include <xercesc/util/XMLString.hpp>
12 #include <sstream>
13 #include <memory>
14 
15 using namespace xercesc;
16 
17 //<<<<<< PRIVATE DEFINES >>>>>>
18 //<<<<<< PRIVATE CONSTANTS >>>>>>
19 //<<<<<< PRIVATE TYPES >>>>>>
20 //<<<<<< PRIVATE VARIABLE DEFINITIONS >>>>>>
21 //<<<<<< PUBLIC VARIABLE DEFINITIONS >>>>>>
22 //<<<<<< CLASS STRUCTURE INITIALIZATION >>>>>>
23 //<<<<<< PRIVATE FUNCTION DEFINITIONS >>>>>>
24 //<<<<<< PUBLIC FUNCTION DEFINITIONS >>>>>>
25 //<<<<<< MEMBER FUNCTION DEFINITIONS >>>>>>
26 
27 namespace {
28  inline std::string _toString(XMLCh const* toTranscode) {
29  std::string tmp(XMLString::transcode(toTranscode));
30  return tmp;
31  }
32 
33  inline unsigned int _toUInt(XMLCh const* toTranscode) {
34  std::istringstream iss(_toString(toTranscode));
35  unsigned int returnValue;
36  iss >> returnValue;
37  return returnValue;
38  }
39 
40  inline double _toDouble(XMLCh const* toTranscode) {
41  std::istringstream iss(_toString(toTranscode));
42  double returnValue;
43  iss >> returnValue;
44  return returnValue;
45  }
46 
47  inline XMLCh* _toDOMS(std::string temp) {
48  XMLCh* buff = XMLString::transcode(temp.c_str());
49  return buff;
50  }
51 
52  // concatenate all the XML node attribute/value pairs into a
53  // paren-separated string (for use by CORAL and frontier_client)
54  inline std::string _toParenString(DOMNode const& nodeToConvert) {
55  std::ostringstream oss;
56 
57  DOMNodeList *childList = nodeToConvert.getChildNodes();
58 
59  unsigned int numNodes = childList->getLength();
60  for (unsigned int i = 0; i < numNodes; ++i) {
61  DOMNode *childNode = childList->item(i);
62  if (childNode->getNodeType() != DOMNode::ELEMENT_NODE) {
63  continue;
64  }
65  DOMElement *child = static_cast<DOMElement *>(childNode);
66 
67  DOMNamedNodeMap *attributes = child->getAttributes();
68  unsigned int numAttributes = attributes->getLength();
69  for (unsigned int j = 0; j < numAttributes; ++j) {
70  DOMNode *attributeNode = attributes->item(j);
71  if (attributeNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) {
72  continue;
73  }
74  DOMAttr *attribute = static_cast<DOMAttr *>(attributeNode);
75 
76  oss << "(" << _toString(child->getTagName()) <<
77  _toString(attribute->getName()) << "=" <<
78  _toString(attribute->getValue()) << ")";
79  }
80  }
81  return oss.str();
82  }
83 
84  template<typename T>
85  static
86  void overrideFromPSet(char const* iName, edm::ParameterSet const& iPSet,
87  T& iHolder, T const*& iPointer) {
88  if(iPSet.exists(iName)) {
89  iHolder = iPSet.getUntrackedParameter<T>(iName);
90  iPointer = &iHolder;
91  }
92  }
93 }
94 
95 namespace edm {
96  namespace service {
97  SiteLocalConfigService::SiteLocalConfigService(ParameterSet const& pset) :
98  m_url("/SITECONF/local/JobConfig/site-local-config.xml"),
99  m_dataCatalog(),
100  m_fallbackDataCatalog(),
101  m_frontierConnect(),
102  m_rfioType("castor"),
103  m_connected(false),
104  m_cacheTempDir(),
105  m_cacheTempDirPtr(0),
106  m_cacheMinFree(),
107  m_cacheMinFreePtr(0),
108  m_cacheHint(),
109  m_cacheHintPtr(0),
110  m_readHint(),
111  m_readHintPtr(0),
112  m_ttreeCacheSize(0U),
113  m_ttreeCacheSizePtr(0),
114  m_timeout(0U),
115  m_timeoutPtr(0),
116  m_nativeProtocols(),
117  m_nativeProtocolsPtr(0) {
118 
119  char* tmp = getenv("CMS_PATH");
120 
121  if (tmp) {
122  m_url = tmp + m_url;
123  }
124 
125  this->parse(m_url);
126 
127  //apply overrides
128  overrideFromPSet("overrideSourceCacheTempDir", pset, m_cacheTempDir, m_cacheTempDirPtr);
129  overrideFromPSet("overrideSourceCacheMinFree", pset, m_cacheMinFree, m_cacheMinFreePtr);
130  overrideFromPSet("overrideSourceCacheHintDir", pset, m_cacheHint, m_cacheHintPtr);
131  overrideFromPSet("overrideSourceReadHint", pset, m_readHint, m_readHintPtr);
132  overrideFromPSet("overrideSourceNativeProtocols", pset, m_nativeProtocols, m_nativeProtocolsPtr);
133  overrideFromPSet("overrideSourceTTreeCacheSize", pset, m_ttreeCacheSize, m_ttreeCacheSizePtr);
134  overrideFromPSet("overrideSourceTimeout", pset, m_timeout, m_timeoutPtr);
135 
136  }
137 
138  std::string const
140  if (!m_connected) {
141  //throw cms::Exception("Incomplete configuration")
142  // << "Valid site-local-config not found at " << m_url;
143  // Return PoolFileCatalog.xml for now
144  return "file:PoolFileCatalog.xml";
145  }
146 
147  if (m_dataCatalog == "") {
148  throw cms::Exception("Incomplete configuration")
149  << "Did not find catalog in event-data section in " << m_url;
150  }
151 
152  return m_dataCatalog;
153  }
154 
155  std::string const
157  if (!m_connected) {
158  //throw cms::Exception("Incomplete configuration")
159  // << "Valid site-local-config not found at " << m_url;
160  // Return PoolFileCatalog.xml for now
161  return "file:PoolFileCatalog.xml";
162  }
163 
164  // Note: Unlike the dataCatalog, the fallbackDataCatalog may be empty!
165  return m_fallbackDataCatalog;
166  }
167 
168  std::string const
169  SiteLocalConfigService::frontierConnect(std::string const& servlet) const {
170  if (!m_connected) {
171  throw cms::Exception("Incomplete configuration")
172  << "Valid site-local-config not found at " << m_url;
173  }
174 
175  if (m_frontierConnect == "") {
176  throw cms::Exception("Incomplete configuration")
177  << "Did not find frontier-connect in calib-data section in " << m_url;
178  }
179 
180  if (servlet == "") {
181  return m_frontierConnect;
182  }
183 
184  // Replace the last component of every "serverurl=" piece (up to the
185  // next close-paren) with the servlet
186  std::string::size_type nextparen = 0;
187  std::string::size_type serverurl, lastslash;
188  std::string complexstr = "";
189  while ((serverurl = m_frontierConnect.find("(serverurl=", nextparen)) != std::string::npos) {
190  complexstr.append(m_frontierConnect, nextparen, serverurl - nextparen);
191  nextparen = m_frontierConnect.find(')', serverurl);
192  lastslash = m_frontierConnect.rfind('/', nextparen);
193  complexstr.append(m_frontierConnect, serverurl, lastslash - serverurl + 1);
194  complexstr.append(servlet);
195  }
196  complexstr.append(m_frontierConnect, nextparen, m_frontierConnect.length()-nextparen);
197 
198  return complexstr;
199  }
200 
201  std::string const
203  static std::string const proto = "frontier://";
204 
205  if (input.substr(0,proto.length()) == proto) {
206  // Replace the part after the frontier:// and before either an open-
207  // parentheses (which indicates user-supplied options) or the last
208  // slash (which indicates start of the schema) with the complex
209  // parenthesized string returned from frontierConnect() (which
210  // contains all the information needed to connect to frontier),
211  // if that part is a simple servlet name (non-empty and not
212  // containing special characters)
213  // Example connect strings where servlet is replaced:
214  // frontier://cms_conditions_data/CMS_COND_ECAL
215  // frontier://FrontierInt/CMS_COND_ECAL
216  // frontier://FrontierInt(retrieve-ziplevel=0)/CMS_COND_ECAL
217  // Example connect strings left untouched:
218  // frontier://cmsfrontier.cern.ch:8000/FrontierInt/CMS_COND_ECAL
219  // frontier://(serverurl=cmsfrontier.cern.ch:8000/FrontierInt)/CMS_COND_ECAL
220  std::string::size_type startservlet = proto.length();
221  // if user supplied extra parenthesized options, stop servlet there
222  std::string::size_type endservlet = input.find("(", startservlet);
223  if (endservlet == std::string::npos) {
224  endservlet = input.rfind('/', input.length());
225  }
226  std::string servlet = input.substr(startservlet, endservlet - startservlet);
227  if ((servlet != "") && (servlet.find_first_of(":/)[]") == std::string::npos)) {
228  if (servlet == "cms_conditions_data") {
229  // use the default servlet from site-local-config.xml
230  servlet = "";
231  }
232  return proto + frontierConnect(servlet) + input.substr(endservlet);
233  }
234  }
235  return input;
236  }
237 
238  std::string const
240  return m_rfioType;
241  }
242 
243  std::string const*
245  return m_cacheTempDirPtr;
246  }
247 
248  double const*
250  return m_cacheMinFreePtr;
251  }
252 
253  std::string const*
255  return m_cacheHintPtr;
256  }
257 
258  std::string const*
260  return m_readHintPtr;
261  }
262 
263  unsigned int const*
265  return m_ttreeCacheSizePtr;
266  }
267 
268  unsigned int const*
270  return m_timeoutPtr;
271  }
272 
273  std::vector<std::string> const*
275  return m_nativeProtocolsPtr;
276  }
277 
278  void
279  SiteLocalConfigService::parse(std::string const& url) {
280  XMLPlatformUtils::Initialize();
281  std::auto_ptr<XercesDOMParser> parser(new XercesDOMParser);
282  try {
283  parser->setValidationScheme(XercesDOMParser::Val_Auto);
284  parser->setDoNamespaces(false);
285 
286  parser->parse(url.c_str());
287  DOMDocument* doc = parser->getDocument();
288  if (!doc) {
289  return;
290  }
291 
292  // The Site Config has the following format
293  // <site-local-config>
294  // <site name="FNAL">
295  // <event-data>
296  // <catalog url="trivialcatalog_file:/x/y/z.xml"/>
297  // <rfiotype value="castor"/>
298  // </event-data>
299  // <calib-data>
300  // <catalog url="trivialcatalog_file:/x/y/z.xml"/>
301  // <frontier-connect>
302  // ... frontier-interpreted server/proxy xml ...
303  // </frontier-connect>
304  // </calib-data>
305  // <source-config>
306  // <cache-temp-dir name="/a/b/c"/>
307  // <cache-hint value="..."/>
308  // <read-hint value="..."/>
309  // <ttree-cache-size value="0"/>
310  // <native-protocols>
311  // <protocol prefix="dcache"/>
312  // <protocol prefix="file"/>
313  // </native-protocols>
314  // </source-config>
315  // </site>
316  // </site-local-config>
317 
318  // FIXME: should probably use the parser for validating the XML.
319 
320  DOMNodeList *sites = doc->getElementsByTagName(_toDOMS("site"));
321  unsigned int numSites = sites->getLength();
322  for (unsigned int i = 0; i < numSites; ++i) {
323  DOMElement *site = static_cast<DOMElement *>(sites->item(i));
324 
325  // Parsing of the event data section
326  {
327  DOMNodeList *eventDataList = site->getElementsByTagName(_toDOMS("event-data"));
328  if (eventDataList->getLength() > 0) {
329  DOMElement *eventData = static_cast<DOMElement *>(eventDataList->item(0));
330 
331  DOMNodeList *catalogs = eventData->getElementsByTagName(_toDOMS("catalog"));
332 
333  if (catalogs->getLength() > 0) {
334  DOMElement * catalog = static_cast<DOMElement *>(catalogs->item(0));
335  m_dataCatalog = _toString(catalog->getAttribute(_toDOMS("url")));
336  }
337 
338  if (catalogs->getLength() > 1) {
339  DOMElement * catalog = static_cast<DOMElement *>(catalogs->item(1));
340  m_fallbackDataCatalog = _toString(catalog->getAttribute(_toDOMS("url")));
341  }
342 
343  DOMNodeList* rfiotypes = eventData->getElementsByTagName(_toDOMS("rfiotype"));
344 
345  if (rfiotypes->getLength() > 0) {
346  DOMElement* rfiotype = static_cast<DOMElement *>(rfiotypes->item(0));
347  m_rfioType = _toString(rfiotype->getAttribute(_toDOMS("value")));
348  }
349  }
350  }
351 
352  // Parsing of the calib-data section
353  {
354  DOMNodeList *calibDataList = site->getElementsByTagName(_toDOMS("calib-data"));
355 
356  if (calibDataList->getLength() > 0) {
357  DOMElement *calibData = static_cast<DOMElement *>(calibDataList->item(0));
358  DOMNodeList *frontierConnectList = calibData->getElementsByTagName(_toDOMS("frontier-connect"));
359 
360  if (frontierConnectList->getLength() > 0) {
361  DOMElement *frontierConnect = static_cast<DOMElement *>(frontierConnectList->item(0));
362  m_frontierConnect = _toParenString(*frontierConnect);
363  }
364  }
365  }
366  // Parsing of the source config section
367  {
368  DOMNodeList *sourceConfigList = site->getElementsByTagName(_toDOMS("source-config"));
369 
370  if (sourceConfigList->getLength() > 0) {
371  DOMElement *sourceConfig = static_cast<DOMElement *>(sourceConfigList->item(0));
372  DOMNodeList *cacheTempDirList = sourceConfig->getElementsByTagName(_toDOMS("cache-temp-dir"));
373 
374  if (cacheTempDirList->getLength() > 0) {
375  DOMElement *cacheTempDir = static_cast<DOMElement *>(cacheTempDirList->item(0));
376  m_cacheTempDir = _toString(cacheTempDir->getAttribute(_toDOMS("name")));
378  }
379 
380  DOMNodeList *cacheMinFreeList = sourceConfig->getElementsByTagName(_toDOMS("cache-min-free"));
381 
382  if (cacheMinFreeList->getLength() > 0) {
383  DOMElement *cacheMinFree = static_cast<DOMElement *>(cacheMinFreeList->item(0));
384  m_cacheMinFree = _toDouble(cacheMinFree->getAttribute(_toDOMS("value")));
386  }
387 
388  DOMNodeList *cacheHintList = sourceConfig->getElementsByTagName(_toDOMS("cache-hint"));
389 
390  if (cacheHintList->getLength() > 0) {
391  DOMElement *cacheHint = static_cast<DOMElement *>(cacheHintList->item(0));
392  m_cacheHint = _toString(cacheHint->getAttribute(_toDOMS("value")));
394  }
395 
396  DOMNodeList *readHintList = sourceConfig->getElementsByTagName(_toDOMS("read-hint"));
397 
398  if (readHintList->getLength() > 0) {
399  DOMElement *readHint = static_cast<DOMElement *>(readHintList->item(0));
400  m_readHint = _toString(readHint->getAttribute(_toDOMS("value")));
402  }
403 
404  DOMNodeList *ttreeCacheSizeList = sourceConfig->getElementsByTagName(_toDOMS("ttree-cache-size"));
405 
406  if (ttreeCacheSizeList->getLength() > 0) {
407  DOMElement *ttreeCacheSize = static_cast<DOMElement *>(ttreeCacheSizeList->item(0));
408  m_ttreeCacheSize = _toUInt(ttreeCacheSize->getAttribute(_toDOMS("value")));
410  }
411 
412  DOMNodeList *timeoutList = sourceConfig->getElementsByTagName(_toDOMS("timeout-in-seconds"));
413 
414  if (timeoutList->getLength() > 0) {
415  DOMElement *timeout = static_cast<DOMElement *>(timeoutList->item(0));
416  m_timeout = _toUInt(timeout->getAttribute(_toDOMS("value")));
418  }
419 
420  DOMNodeList *nativeProtocolsList = sourceConfig->getElementsByTagName(_toDOMS("native-protocols"));
421 
422  if (nativeProtocolsList->getLength() > 0) {
423  DOMElement *nativeProtocol = static_cast<DOMElement *>(nativeProtocolsList->item(0));
424  DOMNodeList *childList = nativeProtocol->getChildNodes();
425 
426  XMLCh* prefixXMLCh = _toDOMS("prefix");
427  unsigned int numNodes = childList->getLength();
428  for (unsigned int i = 0; i < numNodes; ++i) {
429  DOMNode *childNode = childList->item(i);
430  if (childNode->getNodeType() != DOMNode::ELEMENT_NODE) {
431  continue;
432  }
433  DOMElement *child = static_cast<DOMElement *>(childNode);
434  m_nativeProtocols.push_back(_toString(child->getAttribute(prefixXMLCh)));
435  }
437  }
438  }
439  }
440  }
441  m_connected = true;
442  }
443  catch (xercesc::DOMException const& e) {
444  }
445  }
446 
447  void
450  desc.setComment("Service to translate logical file names to physical file names.");
451 
452  desc.addOptionalUntracked<std::string>("overrideSourceCacheTempDir");
453  desc.addOptionalUntracked<double>("overrideSourceCacheMinFree");
454  desc.addOptionalUntracked<std::string>("overrideSourceCacheHintDir");
455  desc.addOptionalUntracked<std::string>("overrideSourceReadHint");
456  desc.addOptionalUntracked<std::vector<std::string> >("overrideSourceNativeProtocols");
457  desc.addOptionalUntracked<unsigned int>("overrideSourceTTreeCacheSize");
458  desc.addOptionalUntracked<unsigned int>("overrideSourceTimeout");
459 
460  descriptions.add("SiteLocalConfigService", desc);
461  }
462  }
463 }
std::string const fallbackDataCatalog(void) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
static void fillDescriptions(ConfigurationDescriptions &descriptions)
std::string const rfioType(void) const
std::string _toString(const XMLCh *toTranscode)
std::string const dataCatalog(void) const
bool exists(std::string const &parameterName) const
checks if a parameter exists
uint16_t size_type
std::vector< std::string > const * m_nativeProtocolsPtr
std::string const lookupCalibConnect(std::string const &input) const
void setComment(std::string const &value)
std::string const * sourceCacheHint() const
list attributes
Definition: asciidump.py:415
std::string const * sourceReadHint() const
int j
Definition: DBlmapReader.cc:9
tuple doc
Definition: asciidump.py:381
std::vector< std::string > m_nativeProtocols
tuple input
Definition: collect_tpl.py:10
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::string const * sourceCacheTempDir() const
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
XMLCh * _toDOMS(std::string temp)
unsigned int const * sourceTimeout() const
ParameterDescriptionBase * addOptionalUntracked(U const &iLabel, T const &value)
std::string const frontierConnect(std::string const &servlet) const
std::vector< std::string > const * sourceNativeProtocols() const
long double T
unsigned int const * sourceTTreeCacheSize() const