Starting to refactor session cache, eliminated IConfig class.
[shibboleth/cpp-sp.git] / odbc_ccache / odbc-ccache.cpp
index 77a149b..f8f8cf5 100644 (file)
 # define SHIBODBC_EXPORTS
 #endif
 
-#include <shib/shib-threads.h>
 #include <shib-target/shib-target.h>
+#include <shibsp/exceptions.h>
+#include <shibsp/SPConfig.h>
 #include <log4cpp/Category.hh>
+#include <xmltooling/util/NDC.h>
+#include <xmltooling/util/Threads.h>
 
+#include <ctime>
 #include <algorithm>
 #include <sstream>
 
 #include <dmalloc.h>
 #endif
 
-using namespace std;
-using namespace saml;
-using namespace shibboleth;
+using namespace shibsp;
 using namespace shibtarget;
+using namespace opensaml::saml2md;
+using namespace saml;
+using namespace xmltooling;
 using namespace log4cpp;
+using namespace std;
 
 #define PLUGIN_VER_MAJOR 3
 #define PLUGIN_VER_MINOR 0
@@ -118,10 +124,9 @@ public:
 
     SQLHDBC getHDBC();
 
-    log4cpp::Category* log;
+    Category* log;
 
 protected:
-    //ThreadKey* m_mysql;
     const DOMElement* m_root; // can only use this during initialization
     string m_connstring;
 
@@ -139,7 +144,7 @@ const char* ODBCBase::p_connstring = NULL;
 ODBCBase::ODBCBase(const DOMElement* e) : m_root(e), m_bInitializedODBC(false)
 {
 #ifdef _DEBUG
-    saml::NDC ndc("ODBCBase");
+    xmltooling::NDC ndc("ODBCBase");
 #endif
     log = &(Category::getInstance("shibtarget.ODBC"));
 
@@ -159,7 +164,7 @@ ODBCBase::ODBCBase(const DOMElement* e) : m_root(e), m_bInitializedODBC(false)
     }
 
     // Grab connection string from the configuration.
-    e=saml::XML::getFirstChildElement(e,shibtarget::XML::SHIBTARGET_NS,ConnectionString);
+    e=XMLHelper::getFirstChildElement(e,ConnectionString);
     if (!e || !e->hasChildNodes()) {
         if (!p_connstring) {
             this->~ODBCBase();
@@ -168,7 +173,7 @@ ODBCBase::ODBCBase(const DOMElement* e) : m_root(e), m_bInitializedODBC(false)
         m_connstring=p_connstring;
     }
     else {
-        auto_ptr_char arg(e->getFirstChild()->getNodeValue());
+        xmltooling::auto_ptr_char arg(e->getFirstChild()->getNodeValue());
         m_connstring=arg.get();
         p_connstring=m_connstring.c_str();
     }
@@ -215,7 +220,7 @@ void ODBCBase::log_error(SQLHANDLE handle, SQLSMALLINT htype)
 SQLHDBC ODBCBase::getHDBC()
 {
 #ifdef _DEBUG
-    saml::NDC ndc("getMYSQL");
+    xmltooling::NDC ndc("getMYSQL");
 #endif
 
     // Get a handle.
@@ -274,13 +279,13 @@ public:
     // Delegate all the ISessionCache methods.
     string insert(
         const IApplication* application,
-        const IEntityDescriptor* source,
+        const RoleDescriptor* role,
         const char* client_addr,
         const SAMLSubject* subject,
         const char* authnContext,
         const SAMLResponse* tokens
         )
-    { return m_cache->insert(application,source,client_addr,subject,authnContext,tokens); }
+    { return m_cache->insert(application,role,client_addr,subject,authnContext,tokens); }
     ISessionCacheEntry* find(const char* key, const IApplication* application, const char* client_addr)
     { return m_cache->find(key,application,client_addr); }
     void remove(const char* key, const IApplication* application, const char* client_addr)
@@ -320,9 +325,9 @@ public:
 private:
     bool m_storeAttributes;
     ISessionCache* m_cache;
-    CondWait* shutdown_wait;
+    xmltooling::CondWait* shutdown_wait;
     bool shutdown;
-    Thread* cleanup_thread;
+    xmltooling::Thread* cleanup_thread;
 
     static void* cleanup_fcn(void*); // XXX Assumed an ODBCCCache
 };
@@ -330,12 +335,12 @@ private:
 ODBCCCache::ODBCCCache(const DOMElement* e) : ODBCBase(e), m_storeAttributes(false)
 {
 #ifdef _DEBUG
-    saml::NDC ndc("ODBCCCache");
+    xmltooling::NDC ndc("ODBCCCache");
 #endif
     log = &(Category::getInstance("shibtarget.SessionCache.ODBC"));
 
     m_cache = dynamic_cast<ISessionCache*>(
-        SAMLConfig::getConfig().getPlugMgr().newPlugin(shibtarget::XML::MemorySessionCacheType, m_root)
+        SAMLConfig::getConfig().getPlugMgr().newPlugin(MEMORY_SESSIONCACHE, m_root)
     );
     if (!m_cache->setBackingStore(this)) {
         delete m_cache;
@@ -384,7 +389,7 @@ HRESULT ODBCCCache::onCreate(
     )
 {
 #ifdef _DEBUG
-    saml::NDC ndc("onCreate");
+    xmltooling::NDC ndc("onCreate");
 #endif
 
     // Get XML data from entry. Default is not to return SAML objects.
@@ -456,7 +461,7 @@ HRESULT ODBCCCache::onRead(
     )
 {
 #ifdef _DEBUG
-    saml::NDC ndc("onRead");
+    xmltooling::NDC ndc("onRead");
 #endif
 
     log->debug("searching database...");
@@ -555,7 +560,7 @@ HRESULT ODBCCCache::onRead(
 HRESULT ODBCCCache::onRead(const char* key, time_t& accessed)
 {
 #ifdef _DEBUG
-    saml::NDC ndc("onRead");
+    xmltooling::NDC ndc("onRead");
 #endif
 
     log->debug("reading last access time from database");
@@ -604,7 +609,7 @@ HRESULT ODBCCCache::onRead(const char* key, time_t& accessed)
 HRESULT ODBCCCache::onRead(const char* key, string& tokens)
 {
 #ifdef _DEBUG
-    saml::NDC ndc("onRead");
+    xmltooling::NDC ndc("onRead");
 #endif
 
     if (!m_storeAttributes)
@@ -651,7 +656,7 @@ HRESULT ODBCCCache::onRead(const char* key, string& tokens)
 HRESULT ODBCCCache::onUpdate(const char* key, const char* tokens, time_t lastAccess)
 {
 #ifdef _DEBUG
-    saml::NDC ndc("onUpdate");
+    xmltooling::NDC ndc("onUpdate");
 #endif
 
     ostringstream q;
@@ -706,7 +711,7 @@ HRESULT ODBCCCache::onUpdate(const char* key, const char* tokens, time_t lastAcc
 HRESULT ODBCCCache::onDelete(const char* key)
 {
 #ifdef _DEBUG
-    saml::NDC ndc("onDelete");
+    xmltooling::NDC ndc("onDelete");
 #endif
 
     SQLHSTMT hstmt;
@@ -731,10 +736,10 @@ HRESULT ODBCCCache::onDelete(const char* key)
 void ODBCCCache::cleanup()
 {
 #ifdef _DEBUG
-    saml::NDC ndc("cleanup");
+    xmltooling::NDC ndc("cleanup");
 #endif
 
-    Mutex* mutex = Mutex::create();
+    Mutex* mutex = xmltooling::Mutex::create();
 
     int rerun_timer = 0;
     int timeout_life = 0;
@@ -807,15 +812,17 @@ void ODBCCCache::cleanup()
 
     mutex->unlock();
     delete mutex;
-    Thread::exit(NULL);
+    xmltooling::Thread::exit(NULL);
 }
 
 void* ODBCCCache::cleanup_fcn(void* cache_p)
 {
   ODBCCCache* cache = (ODBCCCache*)cache_p;
 
+#ifndef WIN32
   // First, let's block all signals
   Thread::mask_all_signals();
+#endif
 
   // Now run the cleanup process.
   cache->cleanup();
@@ -829,7 +836,7 @@ public:
   ODBCReplayCache(const DOMElement* e);
   virtual ~ODBCReplayCache() {}
 
-  bool check(const XMLCh* str, time_t expires) {auto_ptr_XMLCh temp(str); return check(temp.get(),expires);}
+  bool check(const XMLCh* str, time_t expires) {xmltooling::auto_ptr_XMLCh temp(str); return check(temp.get(),expires);}
   bool check(const char* str, time_t expires);
 };
 
@@ -910,7 +917,7 @@ bool ODBCReplayCache::check(const char* str, time_t expires)
 
 // Factories
 
-IPlugIn* new_odbc_ccache(const DOMElement* e)
+SessionCache* new_odbc_ccache(const DOMElement* const & e)
 {
     return new ODBCCCache(e);
 }
@@ -924,13 +931,12 @@ IPlugIn* new_odbc_replay(const DOMElement* e)
 extern "C" int SHIBODBC_EXPORTS saml_extension_init(void*)
 {
     // register this ccache type
-    SAMLConfig::getConfig().getPlugMgr().regFactory(shibtarget::XML::ODBCReplayCacheType, &new_odbc_replay);
-    SAMLConfig::getConfig().getPlugMgr().regFactory(shibtarget::XML::ODBCSessionCacheType, &new_odbc_ccache);
+    SAMLConfig::getConfig().getPlugMgr().regFactory(ODBC_REPLAYCACHE, &new_odbc_replay);
+    SPConfig::getConfig().SessionCacheManager.registerFactory(ODBC_SESSIONCACHE, &new_odbc_ccache);
     return 0;
 }
 
 extern "C" void SHIBODBC_EXPORTS saml_extension_term()
 {
-    SAMLConfig::getConfig().getPlugMgr().unregFactory(shibtarget::XML::ODBCSessionCacheType);
-    SAMLConfig::getConfig().getPlugMgr().unregFactory(shibtarget::XML::ODBCReplayCacheType);
+    SAMLConfig::getConfig().getPlugMgr().unregFactory(ODBC_REPLAYCACHE);
 }