d8dda9e5a2735cca86412806641fa1ebef5a07a8
[shibboleth/cpp-xmltooling.git] / xmltooling / util / ReloadableXMLFile.h
1 /*
2  *  Copyright 2001-2009 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file xmltooling/util/ReloadableXMLFile.h
19  * 
20  * Base class for file-based XML configuration.
21  */
22
23 #ifndef __xmltooling_reloadable_h__
24 #define __xmltooling_reloadable_h__
25
26 #include <xmltooling/logging.h>
27 #include <xmltooling/Lockable.h>
28 #include <xmltooling/util/Threads.h>
29
30 #include <ctime>
31 #include <string>
32 #include <xercesc/dom/DOM.hpp>
33
34 namespace xmltooling {
35
36     /**
37      * Base class for file-based XML configuration.
38      */
39     class XMLTOOL_API ReloadableXMLFile : protected virtual Lockable
40     {
41         MAKE_NONCOPYABLE(ReloadableXMLFile);
42         
43     protected:
44         /**
45          * Constructor taking a DOM element supporting the following content:
46          * 
47          * <dl>
48          *  <dt>file | filename | path | pathname</dt>
49          *  <dd>identifies a local file</dd>
50          *  <dt>uri | url</dt>
51          *  <dd>identifies a remote resource</dd>
52          *  <dt>validate</dt>
53          *  <dd>use a validating parser</dd>
54          *  <dt>reloadChanges</dt>
55          *  <dd>enables monitoring of local file for changes</dd>
56          *  <dt>reloadInterval</dt>
57          *  <dd>enables periodic refresh of remote file</dd>
58          *  <dt>backingFilePath</dt>
59          *  <dd>location for backup of remote resource</dd>
60          * </dl>
61          * 
62          * @param e     DOM to supply configuration
63          * @param log   logging object to use
64          */
65         ReloadableXMLFile(const xercesc::DOMElement* e, logging::Category& log);
66     
67         virtual ~ReloadableXMLFile() {
68             delete m_lock;
69         }
70
71         /**
72          * Loads configuration material.
73          * 
74          * <p>This method is called to load configuration material
75          * initially and any time a change is detected. The base version
76          * performs basic parsing duties and returns the result.
77          * 
78          * @return a pair consisting of a flag indicating whether to take ownership of
79          *      the document, and the root element of the tree to load
80          */
81         virtual std::pair<bool,xercesc::DOMElement*> load() {
82             return load(false);
83         }
84         
85         /** Root of the original DOM element passed into constructor. */
86         const xercesc::DOMElement* m_root;
87         
88         /** Indicates whether resources is local or remote. */
89         bool m_local;
90         
91         /** Use a validating parser when parsing XML. */
92         bool m_validate;
93         
94         /** Resource location, may be a local path or a URI. */
95         std::string m_source;
96
97         /** Path to backup copy for remote resources. */
98         std::string m_backing;
99         
100         /** Last modification of local resource or reload of remote resource. */
101         time_t m_filestamp;
102
103         /** Time in seconds to wait before trying for new copy of remote resource. */
104         time_t m_reloadInterval;
105
106         /** Shared lock for guarding reloads. */
107         RWLock* m_lock;
108         
109         /** Logging object. */
110         logging::Category& m_log;
111
112     public:
113         Lockable* lock();
114
115         void unlock() {
116             if (m_lock)
117                 m_lock->unlock();
118         }
119
120     private:
121         std::pair<bool,xercesc::DOMElement*> load(bool backup);
122     };
123
124 };
125
126 #endif /* __xmltooling_reloadable_h__ */