00001
00002
00003 #include "FWCore/Services/src/SiteLocalConfigService.h"
00004 #include "FWCore/Utilities/interface/Exception.h"
00005
00006 #include <xercesc/dom/DOM.hpp>
00007 #include <xercesc/parsers/XercesDOMParser.hpp>
00008 #include <xercesc/util/PlatformUtils.hpp>
00009 #include <xercesc/util/XMLString.hpp>
00010 #include <sstream>
00011
00012 using namespace xercesc;
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 inline std::string _toString(const XMLCh *toTranscode)
00025 {
00026 std::string tmp(XMLString::transcode(toTranscode));
00027 return tmp;
00028 }
00029
00030 inline XMLCh* _toDOMS( std::string temp )
00031 {
00032 XMLCh* buff = XMLString::transcode(temp.c_str());
00033 return buff;
00034 }
00035
00036
00037
00038 inline std::string _toParenString(const DOMNode &nodeToConvert)
00039 {
00040 std::ostringstream oss;
00041
00042 DOMNodeList *childList = nodeToConvert.getChildNodes();
00043
00044 unsigned int numNodes = childList->getLength ();
00045 for (unsigned int i = 0; i < numNodes; ++i)
00046 {
00047 DOMNode *childNode = childList->item(i);
00048 if (childNode->getNodeType() != DOMNode::ELEMENT_NODE)
00049 continue;
00050 DOMElement *child = static_cast <DOMElement *> (childNode);
00051
00052 DOMNamedNodeMap *attributes = child->getAttributes();
00053 unsigned int numAttributes = attributes->getLength ();
00054 for (unsigned int j = 0; j < numAttributes; ++j)
00055 {
00056 DOMNode *attributeNode = attributes->item(j);
00057 if (attributeNode->getNodeType() != DOMNode::ATTRIBUTE_NODE)
00058 continue;
00059 DOMAttr *attribute = static_cast <DOMAttr *> (attributeNode);
00060
00061 oss << "(" << _toString(child->getTagName()) <<
00062 _toString(attribute->getName()) << "=" <<
00063 _toString(attribute->getValue()) << ")";
00064 }
00065 }
00066 return oss.str();
00067 }
00068
00069
00070 edm::service::SiteLocalConfigService::SiteLocalConfigService (const edm::ParameterSet &pset,
00071 const edm::ActivityRegistry &activityRegistry)
00072 : m_connected (false)
00073 {
00074 m_rfioType = "castor";
00075 m_url = "/SITECONF/local/JobConfig/site-local-config.xml";
00076 char * tmp = getenv ("CMS_PATH");
00077
00078 if (tmp)
00079 m_url = tmp + m_url;
00080
00081 this->parse (m_url);
00082 }
00083
00084 const std::string
00085 edm::service::SiteLocalConfigService::dataCatalog (void) const
00086 {
00087 if (! m_connected) {
00088
00089
00090
00091 return "file:PoolFileCatalog.xml";
00092 }
00093
00094 if (m_dataCatalog == "")
00095 {
00096 throw cms::Exception("Incomplete configuration")
00097 << "Did not find catalog in event-data section in " << m_url ;
00098 }
00099
00100 return m_dataCatalog;
00101 }
00102
00103 const std::string
00104 edm::service::SiteLocalConfigService::frontierConnect (const std::string& servlet) const
00105 {
00106 if (! m_connected)
00107 throw cms::Exception("Incomplete configuration")
00108 << "Valid site-local-config not found at " << m_url ;
00109
00110 if (m_frontierConnect == "")
00111 {
00112 throw cms::Exception("Incomplete configuration")
00113 << "Did not find frontier-connect in calib-data section in " << m_url ;
00114 }
00115
00116 if (servlet == "")
00117 return m_frontierConnect;
00118
00119
00120
00121 std::string::size_type nextparen = 0;
00122 std::string::size_type serverurl, lastslash;
00123 std::string complexstr = "";
00124 while ((serverurl = m_frontierConnect.find("(serverurl=", nextparen)) != std::string::npos)
00125 {
00126 complexstr.append(m_frontierConnect, nextparen, serverurl - nextparen);
00127 nextparen = m_frontierConnect.find(')', serverurl);
00128 lastslash = m_frontierConnect.rfind('/', nextparen);
00129 complexstr.append(m_frontierConnect, serverurl, lastslash - serverurl + 1);
00130 complexstr.append(servlet);
00131 }
00132 complexstr.append(m_frontierConnect, nextparen, m_frontierConnect.length()-nextparen);
00133
00134 return complexstr;
00135 }
00136
00137 const std::string
00138 edm::service::SiteLocalConfigService::lookupCalibConnect (const std::string& input) const
00139 {
00140 static const std::string proto = "frontier://";
00141
00142 if (input.substr(0,proto.length()) == proto)
00143 {
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 std::string::size_type startservlet = proto.length();
00159
00160 std::string::size_type endservlet = input.find("(", startservlet);
00161 if (endservlet == std::string::npos)
00162 endservlet = input.rfind('/', input.length());
00163 std::string servlet = input.substr(startservlet, endservlet - startservlet);
00164 if ((servlet != "") && (servlet.find_first_of(":/)[]") == std::string::npos))
00165 {
00166 if (servlet == "cms_conditions_data")
00167
00168 servlet = "";
00169
00170 return proto + frontierConnect(servlet) + input.substr(endservlet);
00171 }
00172 }
00173 return input;
00174 }
00175
00176 const std::string
00177 edm::service::SiteLocalConfigService::rfioType (void) const
00178 {
00179 return m_rfioType;
00180 }
00181
00182 void
00183 edm::service::SiteLocalConfigService::parse (const std::string &url)
00184 {
00185 XMLPlatformUtils::Initialize();
00186 XercesDOMParser* parser = new XercesDOMParser;
00187 try
00188 {
00189 parser->setValidationScheme(XercesDOMParser::Val_Auto);
00190 parser->setDoNamespaces(false);
00191
00192 parser->parse(url.c_str());
00193 DOMDocument* doc = parser->getDocument();
00194 if (! doc)
00195 {
00196 return;
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 DOMNodeList *sites = doc->getElementsByTagName (_toDOMS ("site"));
00218 unsigned int numSites = sites->getLength ();
00219 for (unsigned int i = 0;
00220 i < numSites;
00221 ++i)
00222 {
00223 DOMElement *site = static_cast <DOMElement *> (sites->item (i));
00224
00225
00226 {
00227 DOMNodeList * eventDataList
00228 = site->getElementsByTagName (_toDOMS ("event-data"));
00229 if (eventDataList->getLength () > 0)
00230 {
00231 DOMElement *eventData
00232 = static_cast <DOMElement *> (eventDataList->item (0));
00233
00234 DOMNodeList *catalogs
00235 = eventData->getElementsByTagName (_toDOMS ("catalog"));
00236
00237 if (catalogs->getLength () > 0)
00238 {
00239 DOMElement * catalog
00240 = static_cast <DOMElement *> (catalogs->item (0));
00241
00242 m_dataCatalog = _toString (catalog->getAttribute (_toDOMS ("url")));
00243 }
00244
00245 DOMNodeList *rfiotypes
00246 = eventData->getElementsByTagName (_toDOMS ("rfiotype"));
00247
00248 if (rfiotypes->getLength () > 0)
00249 {
00250 DOMElement * rfiotype
00251 = static_cast <DOMElement *> (rfiotypes->item (0));
00252
00253 m_rfioType = _toString (rfiotype->getAttribute (_toDOMS ("value")));
00254 }
00255
00256 }
00257 }
00258
00259
00260 {
00261 DOMNodeList * calibDataList
00262 = site->getElementsByTagName (_toDOMS ("calib-data"));
00263
00264 if (calibDataList->getLength () > 0)
00265 {
00266 DOMElement *calibData
00267 = static_cast <DOMElement *> (calibDataList->item (0));
00268
00269 DOMNodeList *frontierConnectList
00270 = calibData->getElementsByTagName (_toDOMS ("frontier-connect"));
00271
00272 if (frontierConnectList->getLength () > 0)
00273 {
00274 DOMElement *frontierConnect
00275 = static_cast <DOMElement *> (frontierConnectList->item (0));
00276
00277 m_frontierConnect = _toParenString(*frontierConnect);
00278 }
00279 }
00280 }
00281 }
00282 m_connected = true;
00283 }
00284 catch (xercesc::DOMException &e)
00285 {
00286 }
00287 }