[cpp-sp] branch main updated: Revise and generalize reloadable file class.

Scott Cantor cantor.2 at osu.edu
Tue Dec 3 20:03:55 UTC 2024


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository cpp-sp.

View the commit online:
http://git.shibboleth.net/view/?p=cpp-sp.git;a=commit;h=4e5a2585b3ee31aa8767204201038688183cb7b5

The following commit(s) were added to refs/heads/main by this push:
     new 4e5a2585 Revise and generalize reloadable file class.
4e5a2585 is described below

commit 4e5a2585b3ee31aa8767204201038688183cb7b5
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Dec 3 15:03:36 2024 -0500

    Revise and generalize reloadable file class.
---
 shibsp/Makefile.am                                 |  4 +-
 .../{ReloadableXMLFile.cpp => ReloadableFile.cpp}  | 92 +++++++++++++---------
 .../util/{ReloadableXMLFile.h => ReloadableFile.h} | 52 +++++++-----
 3 files changed, 90 insertions(+), 58 deletions(-)

diff --git a/shibsp/Makefile.am b/shibsp/Makefile.am
index 96adee88..1355b6e3 100644
--- a/shibsp/Makefile.am
+++ b/shibsp/Makefile.am
@@ -73,7 +73,7 @@ utilinclude_HEADERS = \
 	util/IPRange.h \
 	util/PathResolver.h \
 	util/PropertySet.h \
-	util/ReloadableXMLFile.h \
+	util/ReloadableFile.h \
 	util/SPConstants.h \
 	util/TemplateParameters.h \
 	util/URLEncoder.h
@@ -145,7 +145,7 @@ libshibsp_la_SOURCES = \
 	util/DOMPropertySet.cpp \
 	util/IPRange.cpp \
 	util/PathResolver.cpp \
-	util/ReloadableXMLFile.cpp \
+	util/ReloadableFile.cpp \
 	util/SPConstants.cpp \
 	util/TemplateParameters.cpp \
 	util/URLEncoder.cpp
diff --git a/shibsp/util/ReloadableXMLFile.cpp b/shibsp/util/ReloadableFile.cpp
similarity index 70%
rename from shibsp/util/ReloadableXMLFile.cpp
rename to shibsp/util/ReloadableFile.cpp
index 443109c6..be195657 100644
--- a/shibsp/util/ReloadableXMLFile.cpp
+++ b/shibsp/util/ReloadableFile.cpp
@@ -19,9 +19,9 @@
  */
 
 /**
- * @file ReloadableXMLFile.cpp
+ * util/ReloadableFile.cpp
  *
- * Base class for file-based XML configuration.
+ * Base class for file-based configuration.
  */
 
 #include "internal.h"
@@ -29,21 +29,18 @@
 #include "AgentConfig.h"
 #include "logging/Category.h"
 #include "util/PathResolver.h"
-#include "util/ReloadableXMLFile.h"
+#include "util/ReloadableFile.h"
 
-#include <fstream>
+#include <limits>
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include <boost/property_tree/ptree.hpp>
-#include <boost/property_tree/xml_parser.hpp>
-
 using namespace boost::property_tree;
 using namespace shibsp;
 using namespace std;
 
-ReloadableXMLFile::ReloadableXMLFile(const std::string& path, Category& log, bool reloadChanges, bool deprecationSupport)
-    : m_tree(nullptr), m_log(log), m_source(path), m_filestamp(0)
+ReloadableFile::ReloadableFile(const std::string& path, Category& log, bool reloadChanges, bool deprecationSupport)
+    : m_log(log), m_source(path), m_filestamp(0)
 #ifdef HAVE_CXX17
         , m_lock(nullptr)
 #elif HAVE_CXX14
@@ -62,14 +59,17 @@ ReloadableXMLFile::ReloadableXMLFile(const std::string& path, Category& log, boo
 #endif
     }
 
-    m_tree = load();
+    if (!load()) {
+        m_log.error("initial configuration was invalid");
+    }
 }
 
-ReloadableXMLFile::~ReloadableXMLFile()
+ReloadableFile::~ReloadableFile()
 {
 }
 
-unique_ptr<ptree> ReloadableXMLFile::load()
+/*
+unique_ptr<ptree> ReloadableFile::load()
 {
     try {
         unique_ptr<ptree> pt = unique_ptr<ptree>(new ptree());
@@ -83,27 +83,59 @@ unique_ptr<ptree> ReloadableXMLFile::load()
     return nullptr;
 }
 
-void ReloadableXMLFile::lock()
+        m_lock->lock();
+#ifdef WIN32
+        if (_stat(m_source.c_str(), &stat_buf) == 0) {
+#else
+        if (stat(m_source.c_str(), &stat_buf) == 0) {
+#endif
+            m_filestamp = stat_buf.st_mtime;
+        }
+
+*/
+
+const std::string& ReloadableFile::getSource() const
+{
+    return m_source;
+}
+
+const time_t ReloadableFile::getModificationTime() const
+{
+#ifdef WIN32
+    struct _stat stat_buf;
+    if (_stat(m_source.c_str(), &stat_buf) != 0) {
+        return 0;
+    }
+#else
+    struct stat stat_buf;
+    if (stat(m_source.c_str(), &stat_buf) != 0) {
+        return 0;
+    }
+#endif
+    return stat_buf.st_mtime;
+}
+
+void ReloadableFile::lock()
 {
     if (m_lock) {
         m_lock->lock();
     }
 }
 
-bool ReloadableXMLFile::try_lock()
+bool ReloadableFile::try_lock()
 {
     if (m_lock) {
         return m_lock->try_lock();
     }
 }
 
-void ReloadableXMLFile::unlock()
+void ReloadableFile::unlock()
 {
     if (m_lock)
         m_lock->unlock();
 }
 
-void ReloadableXMLFile::lock_shared()
+void ReloadableFile::lock_shared()
 {
     if (!m_lock) {
         return;
@@ -128,30 +160,18 @@ void ReloadableXMLFile::lock_shared()
     }
 
     m_lock->unlock();
-    m_log.info("change detected...");
-
-    unique_ptr<ptree> newtree = load();
+    m_log.info("change detected, attempting reload...");
 
-    if (newtree) {
-        m_log.info("swapping in new configuration");
-        m_lock->lock();
-#ifdef WIN32
-        if (_stat(m_source.c_str(), &stat_buf) == 0) {
-#else
-        if (stat(m_source.c_str(), &stat_buf) == 0) {
-#endif
-            m_filestamp = stat_buf.st_mtime;
-        }
-        m_tree.swap(newtree);
-        m_lock->unlock();
-        m_lock->lock_shared();
+    if (load()) {
+        m_log.info("swapped in new configuration");
     } else {
-        m_log.info("new configuration was invalid, retaining original");
-        m_lock->lock_shared();
+        m_log.info("new configuration was invalid");
     }
+
+    m_lock->lock_shared();
 }
 
-bool ReloadableXMLFile::try_lock_shared()
+bool ReloadableFile::try_lock_shared()
 {
     if (m_lock)
         return m_lock->try_lock_shared();
@@ -159,7 +179,7 @@ bool ReloadableXMLFile::try_lock_shared()
         return true;
 }
 
-void ReloadableXMLFile::unlock_shared()
+void ReloadableFile::unlock_shared()
 {
     if (m_lock)
         m_lock->unlock_shared();
diff --git a/shibsp/util/ReloadableXMLFile.h b/shibsp/util/ReloadableFile.h
similarity index 65%
rename from shibsp/util/ReloadableXMLFile.h
rename to shibsp/util/ReloadableFile.h
index 9e79c616..c2986605 100644
--- a/shibsp/util/ReloadableXMLFile.h
+++ b/shibsp/util/ReloadableFile.h
@@ -13,20 +13,19 @@
  */
 
 /**
- * @file shibsp/util/ReloadableXMLFile.h
+ * @file shibsp/util/ReloadableFile.h
  * 
- * Base class for reloadable file-based XML configuration.
+ * Base class for reloadable file-based configuration.
  */
 
-#ifndef __shibsp_reloadablexml_h__
-#define __shibsp_reloadablexml_h__
+#ifndef __shibsp_reloadablefile_h__
+#define __shibsp_reloadablefile_h__
 
 #include <shibsp/base.h>
 
 #include <ctime>
 #include <memory>
 #include <string>
-#include <boost/property_tree/ptree_fwd.hpp>
 
 #ifdef HAVE_CXX14
 #include <shared_mutex>
@@ -37,11 +36,11 @@ namespace shibsp {
     class SHIBSP_API Category;
 
     /**
-     * Base class for file-based XML configuration.
+     * Base class for file-based configuration, provides locking and reload semantics.
      */
-    class SHIBSP_API ReloadableXMLFile
+    class SHIBSP_API ReloadableFile
     {
-    MAKE_NONCOPYABLE(ReloadableXMLFile);
+    MAKE_NONCOPYABLE(ReloadableFile);
     protected:
         /**
          * Base class constructor.
@@ -51,35 +50,48 @@ namespace shibsp {
          * @param reloadChanges         whether to monitor for changes
          * @param deprecationSupport    true iff deprecated options and settings should be accepted
          */
-        ReloadableXMLFile(
+        ReloadableFile(
             const std::string& path,
             Category& log,
             bool reloadChanges=false,
             bool deprecationSupport=true
             );
     
-        virtual ~ReloadableXMLFile();
+        virtual ~ReloadableFile();
 
         /**
-         * Loads configuration material.
+         * Loads (or reloads) configuration material.
          * 
          * <p>This method is called to load configuration material
-         * initially and any time a change is detected. The base version
-         * performs basic parsing duties and returns the result.</p>
+         * initially and any time a change is detected.</p>
          *
          * <p>This method is not called with the object locked, so actual
          * modification of implementation state requires explicit locking within
-         * the method override.</p>
+         * the method override, and the method should return with the object
+         * unlocked.</p>
          * 
          * <p>This method should NOT throw exceptions.</p>
+         */
+        virtual bool load()=0;
+
+        /**
+         * Gets the source path for the configuration.
+         * 
+         * @return source path
+         */
+        const std::string& getSource() const;
+
+        /**
+         * Gets the time of last modification of the source, or a zero fence value
+         * in the event of an error to prevent churn.
+         * 
+         * <p>This methid must be called with the object locked, shared or exclusive.</p>
          * 
-         * @return a possibly empty smart pointer holding the replacement tree
+         * @return modification time
          */
-        virtual std::unique_ptr<boost::property_tree::ptree> load();
-        
-        /** The owned property tree. */
-        std::unique_ptr<boost::property_tree::ptree> m_tree;
+        const time_t getModificationTime() const;
 
+    private:
         /** Logging object. */
         Category& m_log;
 
@@ -109,4 +121,4 @@ namespace shibsp {
 
 };
 
-#endif /* __shibsp_reloadablexml_h__ */
+#endif /* __shibsp_reloadablefile_h__ */

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list