https://bugs.internet2.edu/jira/browse/CPPOST-44
authorcantor <cantor@de75baf8-a10c-0410-a50a-987c0e22f00f>
Wed, 31 Mar 2010 21:51:05 +0000 (21:51 +0000)
committercantor <cantor@de75baf8-a10c-0410-a50a-987c0e22f00f>
Wed, 31 Mar 2010 21:51:05 +0000 (21:51 +0000)
https://bugs.internet2.edu/jira/browse/CPPOST-45

git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/branches/REL_1@727 de75baf8-a10c-0410-a50a-987c0e22f00f

xmltooling/util/ReloadableXMLFile.cpp
xmltooling/util/ReloadableXMLFile.h
xmltooling/util/Threads.h

index dfeab59..f8c4b00 100644 (file)
@@ -62,7 +62,7 @@ static const XMLCh backingFilePath[] =  UNICODE_LITERAL_15(b,a,c,k,i,n,g,F,i,l,e
 
 
 ReloadableXMLFile::ReloadableXMLFile(const DOMElement* e, Category& log)
-    : m_root(e), m_local(true), m_validate(false), m_autocommit(true), m_filestamp(0), m_reloadInterval(0), m_lock(NULL), m_log(log),
+    : m_root(e), m_local(true), m_validate(false), m_backupIndicator(true), m_filestamp(0), m_reloadInterval(0), m_lock(NULL), m_log(log),
         m_shutdown(false), m_reload_wait(NULL), m_reload_thread(NULL)
 {
 #ifdef _DEBUG
@@ -341,15 +341,23 @@ pair<bool,DOMElement*> ReloadableXMLFile::load(bool backup)
 
             m_log.infoStream() << "loaded XML resource (" << (backup ? m_backing : m_source) << ")" << logging::eol;
 
-            if (!backup && m_autocommit && !m_backing.empty()) {
-                m_log.debug("backing up remote resource to (%s)", m_backing.c_str());
-                try {
-                    Locker locker(getBackupLock());
-                    ofstream backer(m_backing.c_str());
-                    backer << *doc;
+            if (!backup && !m_backing.empty()) {
+                // If the indicator is true, we're responsible for the backup.
+                if (m_backupIndicator) {
+                    m_log.debug("backing up remote resource to (%s)", m_backing.c_str());
+                    try {
+                        Locker locker(getBackupLock());
+                        ofstream backer(m_backing.c_str());
+                        backer << *doc;
+                    }
+                    catch (exception& ex) {
+                        m_log.crit("exception while backing up resource: %s", ex.what());
+                    }
                 }
-                catch (exception& ex) {
-                    m_log.crit("exception while backing up resource: %s", ex.what());
+                else {
+                    // If the indicator was false, set true to signal that a backup is needed.
+                    // The caller will presumably flip it back to false once that's done.
+                    m_backupIndicator = true;
                 }
             }
 
@@ -382,7 +390,8 @@ pair<bool,DOMElement*> ReloadableXMLFile::background_load()
 {
     // If this method isn't overridden, we acquire a write lock
     // and just call the old override.
-    m_lock->wrlock();
+    if (m_lock)
+        m_lock->wrlock();
     SharedLock locker(m_lock, false);
     return load();
 }
index 68a2b94..bf32fcf 100644 (file)
@@ -126,8 +126,11 @@ namespace xmltooling {
         /** Path to backup copy for remote resource. */
         std::string m_backing;
 
-        /** Indicates whether loading process needs to defer creation of backup file. */
-        bool m_autocommit;
+        /**
+         * Before load, indicates whether the backup is handled by the base class,
+         * after load, will be true iff it started false and a backup needs to be done.
+         */
+        bool m_backupIndicator;
 
         /** Last modification of local resource. */
         time_t m_filestamp;
index 443009c..d184d5c 100644 (file)
@@ -287,14 +287,16 @@ namespace xmltooling
          * @param mtx mutex to lock
          */
         Lock(Mutex* mtx) : mutex(mtx) {
-            mutex->lock();
+            if (mutex)
+                mutex->lock();
         }
 
         /**
          * Unlocks the wrapped mutex.
          */
         ~Lock() {
-            mutex->unlock();
+            if (mutex)
+                mutex->unlock();
         }
 
     private:
@@ -314,7 +316,7 @@ namespace xmltooling
          * @param lockit    true if the lock should be acquired here, false if already acquired
          */
         SharedLock(RWLock* lock, bool lockit=true) : rwlock(lock) {
-            if (lockit)
+            if (rwlock && lockit)
                 rwlock->rdlock();
         }
 
@@ -322,7 +324,8 @@ namespace xmltooling
          * Unlocks the wrapped shared lock.
          */
         ~SharedLock() {
-            rwlock->unlock();
+            if (rwlock)
+                rwlock->unlock();
         }
 
     private: