refresh the INI file whenever it changes on disk (stat the file
authorwarlord <warlord@cb58f699-b61c-0410-a6fe-9272a202ed29>
Tue, 24 Sep 2002 04:34:02 +0000 (04:34 +0000)
committerwarlord <warlord@cb58f699-b61c-0410-a6fe-9272a202ed29>
Tue, 24 Sep 2002 04:34:02 +0000 (04:34 +0000)
before every tag; re-read the file if it has changed)

git-svn-id: https://svn.middleware.georgetown.edu/cpp-sp/trunk@90 cb58f699-b61c-0410-a6fe-9272a202ed29

include/shib-target.h
shib-target/shib-ini.cpp

index ec827fe..897752b 100644 (file)
@@ -262,28 +262,28 @@ namespace shibtarget {
 
     void refresh(void);
 
-    const std::string& get (const std::string& header, const std::string& tag) const;
-    const std::string& get (const char* header, const char* tag) const {
+    const std::string& get (const std::string& header, const std::string& tag);
+    const std::string& get (const char* header, const char* tag) {
       std::string h = header, t = tag;
       return get(h,t);
     }
 
-    const std::string& operator() (const std::string& header, const std::string& tag) const {
+    const std::string& operator() (const std::string& header, const std::string& tag)  {
       return get(header,tag);
     }
-    const std::string& operator() (const char* header, const char* tag) const {
+    const std::string& operator() (const char* header, const char* tag) {
       std::string h = header, t = tag;
       return get(h,t);
     }
 
-    bool exists(const std::string& header) const;
-    bool exists(const std::string& header, const std::string& tag) const;
+    bool exists(const std::string& header);
+    bool exists(const std::string& header, const std::string& tag);
 
-    bool exists(const char* header) const {
+    bool exists(const char* header) {
       std::string s = header;
       return exists(s);
     }
-    bool exists(const char* header, const char* tag) const {
+    bool exists(const char* header, const char* tag) {
       std::string h = header, t = tag;
       return exists(h,t);
     }
@@ -291,22 +291,22 @@ namespace shibtarget {
     // Special method to look for a tag in one header and maybe in the
     // 'SHIBTARGET_GENERAL' header
     bool get_tag(std::string& header, std::string& tag, bool try_general,
-                std::string* result) const;
+                std::string* result);
 
     bool get_tag(std::string& header, const char* tag, bool try_general,
-                std::string* result) const {
+                std::string* result) {
       std::string t = tag;
       return get_tag (header,t,try_general,result);
     }
 
     bool get_tag(const char* header, const char* tag, bool try_general,
-                std::string* result) const {
+                std::string* result) {
       std::string h = header, t = tag;
       return get_tag (h,t,try_general,result);
     }
 
     // Dump out the inifile to the output stream
-    void dump(std::ostream& os) const;
+    void dump(std::ostream& os);
 
     // Iterators
 
@@ -326,8 +326,8 @@ namespace shibtarget {
       virtual const std::string* next() = 0;
     };
 
-    Iterator* header_iterator() const;
-    Iterator* tag_iterator(const std::string& header) const;
+    Iterator* header_iterator();
+    Iterator* tag_iterator(const std::string& header);
 
   private:
     ShibINIPriv *m_priv;
index b238a46..fac83e3 100644 (file)
@@ -12,6 +12,9 @@
 #include <fstream>
 #include <ctype.h>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+
 #include <log4cpp/Category.hh>
 
 using namespace std;
@@ -53,6 +56,8 @@ public:
   map<string, map<string, string> > table;
   string file;
   bool cs;
+
+  unsigned long        modtime;
 };
 
 ShibINIPriv::ShibINIPriv()
@@ -97,6 +102,23 @@ void ShibINI::init (string& f, bool case_sensitive)
 
 void ShibINI::refresh(void)
 {
+  saml::NDC ndc("refresh");
+
+  // check if we need to refresh
+#ifdef _WIN32
+  struct _stat stat_buf;
+  if (_stat (m_priv->file.c_str(), &stat_buf) < 0)
+#else
+  struct stat stat_buf;
+  if (stat (m_priv->file.c_str(), &stat_buf) < 0)
+#endif
+    m_priv->log->error("stat failed: %s", m_priv->file.c_str());
+
+  if (m_priv->modtime == stat_buf.st_mtime)
+    return;
+
+  m_priv->modtime = stat_buf.st_mtime;
+
   // clear the existing maps
   m_priv->table.clear();
 
@@ -189,8 +211,10 @@ void ShibINI::refresh(void)
   }
 }
 
-const std::string& ShibINI::get (const string& header, const string& tag) const
+const std::string& ShibINI::get (const string& header, const string& tag)
 {
+  refresh();
+
   static string empty = "";
 
   string h = header;
@@ -209,16 +233,20 @@ const std::string& ShibINI::get (const string& header, const string& tag) const
   return i->second;
 }
 
-bool ShibINI::exists(const std::string& header) const
+bool ShibINI::exists(const std::string& header)
 {
+  refresh();
+
   string h = header;
   if (!m_priv->cs) to_lowercase (h);
 
   return (m_priv->table.find(h) != m_priv->table.end());
 }
 
-bool ShibINI::exists(const std::string& header, const std::string& tag) const
+bool ShibINI::exists(const std::string& header, const std::string& tag)
 {
+  refresh();
+
   string h = header;
   string t = tag;
 
@@ -231,10 +259,12 @@ bool ShibINI::exists(const std::string& header, const std::string& tag) const
   return (m_priv->table[h].find(t) != m_priv->table[h].end());
 }
 
-bool ShibINI::get_tag (string& header, string& tag, bool try_general, string* result) const
+bool ShibINI::get_tag (string& header, string& tag, bool try_general, string* result)
 {
   if (!result) return false;
 
+  refresh();
+
   if (exists (header, tag)) {
     *result = get (header, tag);
     return true;
@@ -247,8 +277,10 @@ bool ShibINI::get_tag (string& header, string& tag, bool try_general, string* re
 }
 
 
-void ShibINI::dump (ostream& os) const
+void ShibINI::dump (ostream& os)
 {
+  refresh();
+
   os << "File: " << m_priv->file << "\n";
   os << "Case-Sensitive: " << ( m_priv->cs ? "Yes\n" : "No\n" );
   os << "File Entries:\n";
@@ -268,14 +300,16 @@ void ShibINI::dump (ostream& os) const
   os << "END\n";
 }
 
-ShibINI::Iterator* ShibINI::header_iterator() const
+ShibINI::Iterator* ShibINI::header_iterator()
 {
+  refresh();
   HeaderIterator* iter = new HeaderIterator(m_priv);
   return (ShibINI::Iterator*) iter;
 }
 
-ShibINI::Iterator* ShibINI::tag_iterator(const std::string& header) const
+ShibINI::Iterator* ShibINI::tag_iterator(const std::string& header)
 {
+  refresh();
   TagIterator* iter = new TagIterator(m_priv, header);
   return (ShibINI::Iterator*) iter;
 }