2 * Copyright 2001-2005 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* ReloadableXMLFile.cpp - basic implementation of a reloadable XML config file
27 #include <sys/types.h>
30 #include <xercesc/framework/LocalFileInputSource.hpp>
32 using namespace shibboleth;
34 using namespace log4cpp;
37 ReloadableXMLFileImpl::ReloadableXMLFileImpl(const DOMElement* e) : m_doc(NULL), m_root(e) {}
39 ReloadableXMLFileImpl::ReloadableXMLFileImpl(const char* pathname) : m_doc(NULL), m_root(NULL)
42 NDC ndc("ReloadableXMLFileImpl");
44 Category& log=Category::getInstance(SHIB_LOGCAT".ReloadableXMLFileImpl");
49 //static XMLCh base[]={chLatin_f, chLatin_i, chLatin_l, chLatin_e, chColon, chForwardSlash, chForwardSlash, chForwardSlash, chNull};
50 auto_ptr_XMLCh widenit(pathname);
51 LocalFileInputSource src(widenit.get());
52 Wrapper4InputSource dsrc(&src,false);
54 m_root=m_doc->getDocumentElement();
56 log.infoStream() << "Loaded and parsed XML file (" << pathname << ")" << CategoryStream::ENDLINE;
58 catch (XMLException& e)
60 auto_ptr_char msg(e.getMessage());
61 log.errorStream() << "Xerces error while opening configuration file (" << pathname << "): " << msg.get() << CategoryStream::ENDLINE;
66 throw MalformedException(msg.get());
68 catch (SAMLException& e)
70 log.errorStream() << "XML error while parsing configuration file (" << pathname << "): " << e.what() << CategoryStream::ENDLINE;
80 log.errorStream() << "Unexpected error while parsing configuration file (" << pathname << ")" << CategoryStream::ENDLINE;
90 ReloadableXMLFileImpl::~ReloadableXMLFileImpl()
98 ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e) : m_root(e), m_impl(NULL), m_filestamp(0), m_lock(NULL)
100 static const XMLCh uri[] = { chLatin_u, chLatin_r, chLatin_i, chNull };
101 const XMLCh* pathname=e->getAttributeNS(NULL,uri);
102 if (pathname && *pathname)
104 auto_ptr_char temp(pathname);
108 struct _stat stat_buf;
109 if (_stat(m_source.c_str(), &stat_buf) == 0)
111 struct stat stat_buf;
112 if (stat(m_source.c_str(), &stat_buf) == 0)
114 m_filestamp=stat_buf.st_mtime;
115 m_lock=xmltooling::RWLock::create();
119 void ReloadableXMLFile::lock()
126 // Check if we need to refresh.
128 struct _stat stat_buf;
129 if (_stat(m_source.c_str(), &stat_buf) == 0)
131 struct stat stat_buf;
132 if (stat(m_source.c_str(), &stat_buf) == 0)
135 if (m_filestamp>0 && m_filestamp<stat_buf.st_mtime)
137 // Elevate lock and recheck.
140 if (m_filestamp>0 && m_filestamp<stat_buf.st_mtime)
144 // Update the timestamp regardless. No point in repeatedly trying.
145 m_filestamp=stat_buf.st_mtime;
146 ReloadableXMLFileImpl* new_config=newImplementation(m_source.c_str(),false);
151 catch(SAMLException& e)
154 Category::getInstance(SHIB_LOGCAT".ReloadableXMLFile").error("failed to reload config file, sticking with what we have: %s", e.what());
159 Category::getInstance(SHIB_LOGCAT".ReloadableXMLFile").error("caught an unknown exception, sticking with what we have");
171 ReloadableXMLFileImpl* ReloadableXMLFile::getImplementation() const
174 if (m_source.empty())
175 m_impl=newImplementation(saml::XML::getFirstChildElement(m_root));
177 m_impl=newImplementation(m_source.c_str());