+
+ source = e->getAttributeNS(NULL, id);
+ if (source && *source) {
+ auto_ptr_char tempid(source);
+ m_id = tempid.get();
+ }
+}
+
+ReloadableXMLFile::~ReloadableXMLFile()
+{
+ if (m_reload_thread) {
+ // Shut down the reload thread and let it know.
+ m_shutdown = true;
+ m_reload_wait->signal();
+ m_reload_thread->join(NULL);
+ delete m_reload_thread;
+ delete m_reload_wait;
+ }
+
+ delete m_lock;
+}
+
+void* ReloadableXMLFile::reload_fn(void* pv)
+{
+ ReloadableXMLFile* r = reinterpret_cast<ReloadableXMLFile*>(pv);
+
+#ifndef WIN32
+ // First, let's block all signals
+ Thread::mask_all_signals();
+#endif
+
+ if (!r->m_id.empty()) {
+ string threadid("[");
+ threadid += r->m_id + ']';
+ logging::NDC::push(threadid);
+ }
+
+#ifdef _DEBUG
+ NDC ndc("reload");
+#endif
+
+ auto_ptr<Mutex> mutex(Mutex::create());
+ mutex->lock();
+
+ if (r->m_local)
+ r->m_log.info("reload thread started...running when signaled");
+ else
+ r->m_log.info("reload thread started...running every %d seconds", r->m_reloadInterval);
+
+ while (!r->m_shutdown) {
+ if (r->m_local)
+ r->m_reload_wait->wait(mutex.get());
+ else
+ r->m_reload_wait->timedwait(mutex.get(), r->m_reloadInterval);
+ if (r->m_shutdown)
+ break;
+
+ try {
+ r->m_log.info("reloading %s resource...", r->m_local ? "local" : "remote");
+ pair<bool,DOMElement*> ret = r->background_load();
+ if (ret.first)
+ ret.second->getOwnerDocument()->release();
+ }
+ catch (long& ex) {
+ if (ex == HTTPResponse::XMLTOOLING_HTTP_STATUS_NOTMODIFIED) {
+ r->m_log.info("remote resource (%s) unchanged from cached version", r->m_source.c_str());
+ }
+ else {
+ // Shouldn't happen, we should only get codes intended to be gracefully handled.
+ r->m_log.crit("maintaining existing configuration, remote resource fetch returned atypical status code (%d)", ex);
+ }
+ }
+ catch (exception& ex) {
+ r->m_log.crit("maintaining existing configuration, error reloading resource (%s): %s", r->m_source.c_str(), ex.what());
+ }
+ }
+
+ r->m_log.info("reload thread finished");
+
+ mutex->unlock();
+
+ if (!r->m_id.empty()) {
+ logging::NDC::pop();
+ }
+
+ return NULL;
+}
+
+Lockable* ReloadableXMLFile::lock()
+{
+ if (!m_lock)
+ return this;
+
+ m_lock->rdlock();
+
+ if (m_local) {
+ // Check if we need to refresh.
+#ifdef WIN32
+ struct _stat stat_buf;
+ if (_stat(m_source.c_str(), &stat_buf) != 0)
+ return this;
+#else
+ struct stat stat_buf;
+ if (stat(m_source.c_str(), &stat_buf) != 0)
+ return this;
+#endif
+ if (m_filestamp >= stat_buf.st_mtime)
+ return this;
+
+ // Elevate lock and recheck.
+ m_log.debug("timestamp of local resource changed, elevating to a write lock");
+ m_lock->unlock();
+ m_lock->wrlock();
+ if (m_filestamp >= stat_buf.st_mtime) {
+ // Somebody else handled it, just downgrade.
+ m_log.debug("update of local resource handled by another thread, downgrading lock");
+ m_lock->unlock();
+ m_lock->rdlock();
+ return this;
+ }
+
+ // Update the timestamp regardless.
+ m_filestamp = stat_buf.st_mtime;
+ m_log.info("change detected, signaling reload thread...");
+ m_reload_wait->signal();
+ }
+
+ return this;
+}
+
+void ReloadableXMLFile::unlock()
+{
+ if (m_lock)
+ m_lock->unlock();