CMS 3D CMS Logo

SiteLocalConfigService.cc

Go to the documentation of this file.
00001 //<<<<<< INCLUDES                                                       >>>>>>
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 //<<<<<< PRIVATE DEFINES                                                >>>>>>
00015 //<<<<<< PRIVATE CONSTANTS                                              >>>>>>
00016 //<<<<<< PRIVATE TYPES                                                  >>>>>>
00017 //<<<<<< PRIVATE VARIABLE DEFINITIONS                                   >>>>>>
00018 //<<<<<< PUBLIC VARIABLE DEFINITIONS                                    >>>>>>
00019 //<<<<<< CLASS STRUCTURE INITIALIZATION                                 >>>>>>
00020 //<<<<<< PRIVATE FUNCTION DEFINITIONS                                   >>>>>>
00021 //<<<<<< PUBLIC FUNCTION DEFINITIONS                                    >>>>>>
00022 //<<<<<< MEMBER FUNCTION DEFINITIONS                                    >>>>>>
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 // concatenate all the XML node attribute/value pairs into a
00037 // paren-separated string (for use by CORAL and frontier_client)
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         //throw cms::Exception("Incomplete configuration") 
00089         //    << "Valid site-local-config not found at " << m_url ;
00090         // Return PoolFileCatalog.xml for now
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     // Replace the last component of every "serverurl=" piece (up to the
00120     //   next close-paren) with the servlet
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         // Replace the part after the frontier:// and before either an open-
00145         //  parentheses (which indicates user-supplied options) or the last
00146         //  slash (which indicates start of the schema) with the complex
00147         //  parenthesized string returned from frontierConnect() (which
00148         //  contains all the information needed to connect to frontier),
00149         //  if that part is a simple servlet name (non-empty and not
00150         //  containing special characters)
00151         // Example connect strings where servlet is replaced:
00152         //  frontier://cms_conditions_data/CMS_COND_ECAL
00153         //  frontier://FrontierInt/CMS_COND_ECAL
00154         //  frontier://FrontierInt(retrieve-ziplevel=0)/CMS_COND_ECAL
00155         // Example connect strings left untouched:
00156         //  frontier://cmsfrontier.cern.ch:8000/FrontierInt/CMS_COND_ECAL
00157         //  frontier://(serverurl=cmsfrontier.cern.ch:8000/FrontierInt)/CMS_COND_ECAL
00158         std::string::size_type startservlet = proto.length();
00159         // if user supplied extra parenthesized options, stop servlet there
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                 // use the default servlet from site-local-config.xml
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         // The Site Config has the following format
00200         // <site-local-config>
00201         // <site name="FNAL">
00202         //   <event-data>
00203         //     <catalog url="trivialcatalog_file:/x/y/z.xml"/>
00204         //     <rfiotype value="castor"/>
00205         //   </event-data>
00206         //   <calib-data>
00207         //     <catalog url="trivialcatalog_file:/x/y/z.xml"/>
00208         //     <frontier-connect>
00209         //       ... frontier-interpreted server/proxy xml ...
00210         //     </frontier-connect>
00211         //   </calib-data>
00212         // </site>
00213         // </site-local-config>
00214     
00215         // FIXME: should probably use the parser for validating the XML.
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             // Parsing of the event data section
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             // Parsing of the calib-data section
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 }       

Generated on Tue Jun 9 17:36:40 2009 for CMSSW by  doxygen 1.5.4