X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2Futil%2FReloadableXMLFile.h;h=bce4b8e4b6d59c8ae88ed4decc874a9ae3ecf7db;hb=a0d768778a8f5f539b909baf5b115e70ea765f0f;hp=019f9b25afa164dd20493ccc99bb97433a27792a;hpb=b7919011c7e2f7a3264181627ea659062b61f5c8;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/util/ReloadableXMLFile.h b/xmltooling/util/ReloadableXMLFile.h index 019f9b2..bce4b8e 100644 --- a/xmltooling/util/ReloadableXMLFile.h +++ b/xmltooling/util/ReloadableXMLFile.h @@ -1,5 +1,5 @@ /* - * Copyright 2001-2006 Internet2 + * Copyright 2001-2010 Internet2 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,21 +23,25 @@ #ifndef __xmltooling_reloadable_h__ #define __xmltooling_reloadable_h__ +#include #include -#include #include #include +#include namespace xmltooling { + class XMLTOOL_API CondWait; + class XMLTOOL_API RWLock; + class XMLTOOL_API Thread; + /** * Base class for file-based XML configuration. */ class XMLTOOL_API ReloadableXMLFile : protected virtual Lockable { - MAKE_NONCOPYABLE(ReloadableXMLFile); - + MAKE_NONCOPYABLE(ReloadableXMLFile); protected: /** * Constructor taking a DOM element supporting the following content: @@ -50,24 +54,21 @@ namespace xmltooling { *
validate
*
use a validating parser
*
reloadChanges
- *
enables monitoring of resources for changes
+ *
enables monitoring of local file for changes
+ *
reloadInterval
+ *
enables periodic refresh of remote file
+ *
backingFilePath
+ *
location for backup of remote resource
+ *
id
+ *
identifies the plugin instance for logging purposes
* * - * @param e DOM to supply configuration + * @param e DOM to supply configuration + * @param log logging object to use */ - ReloadableXMLFile(const DOMElement* e); + ReloadableXMLFile(const xercesc::DOMElement* e, logging::Category& log); - virtual ~ReloadableXMLFile() { - delete m_lock; - } - - public: - Lockable* lock(); - - void unlock() { - if (m_lock) - m_lock->unlock(); - } + virtual ~ReloadableXMLFile(); /** * Loads configuration material. @@ -75,27 +76,97 @@ namespace xmltooling { *

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. + * + *

This method is not called with the object locked, so actual + * modification of implementation state requires explicit locking within + * the method override. * * @return a pair consisting of a flag indicating whether to take ownership of * the document, and the root element of the tree to load */ - virtual std::pair load(); - + virtual std::pair background_load(); + /** - * Overrideable method to determine whether a remote resource remains valid. + * Basic load/parse of configuration material. * - * @return true iff the resource remains valid and should not be reloaded + *

The base version performs basic parsing duties and returns the result. + * Subclasses should override the new background_load() method and perform + * their own locking in conjunction with use of this method. + * + *

Subclasses that continue to override this method will function, but + * a write lock will be acquired and held for the entire operation. + * + * @return a pair consisting of a flag indicating whether to take ownership of + * the document, and the root element of the tree to load */ - virtual bool isValid() const { - return true; - } + virtual std::pair load(); - private: - const DOMElement* m_root; - bool m_local, m_validate; + /** + * Accesses a lock interface protecting use of backup file associated with the + * object. + * + *

The lock is NOT acquired automatically. + * + * @return pointer to a lock interface, or nullptr if unnecessary + */ + virtual Lockable* getBackupLock(); + + /** + * Shuts down reload thread, should be called from subclass destructor. + */ + void shutdown(); + + /** Root of the original DOM element passed into constructor. */ + const xercesc::DOMElement* m_root; + + /** Indicates whether resources is local or remote. */ + bool m_local; + + /** Use a validating parser when parsing XML. */ + bool m_validate; + + /** Resource location, may be a local path or a URI. */ std::string m_source; + + /** Path to backup copy for remote resource. */ + std::string m_backing; + + /** + * 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; + + /** Time in seconds to wait before trying for new copy of remote resource. */ + time_t m_reloadInterval; + + /** Caching tag associated with remote resource. */ + std::string m_cacheTag; + + /** Shared lock for guarding reloads. */ RWLock* m_lock; + + /** Logging object. */ + logging::Category& m_log; + + /** Plugin identifier. */ + std::string m_id; + + public: + Lockable* lock(); + void unlock(); + + private: + std::pair load(bool backup); + + // Used to manage background reload/refresh. + bool m_shutdown; + CondWait* m_reload_wait; + Thread* m_reload_thread; + static void* reload_fn(void*); }; };