Change class access, better logging, trap reload errors.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / ReloadableXMLFile.h
1 /*
2  *  Copyright 2001-2006 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/Lockable.h>
27 #include <xmltooling/util/Threads.h>
28
29 #include <ctime>
30 #include <string>
31
32 namespace xmltooling {
33
34     /**
35      * Base class for file-based XML configuration.
36      */
37     class XMLTOOL_API ReloadableXMLFile : protected virtual Lockable
38     {
39         MAKE_NONCOPYABLE(ReloadableXMLFile);
40         
41     protected:
42         /**
43          * Constructor taking a DOM element supporting the following content:
44          * 
45          * <dl>
46          *  <dt>file | filename | path | pathname</dt>
47          *  <dd>identifies a local file</dd>
48          *  <dt>uri | url</dt>
49          *  <dd>identifies a remote resource</dd>
50          *  <dt>validate</dt>
51          *  <dd>use a validating parser</dd>
52          *  <dt>reloadChanges</dt>
53          *  <dd>enables monitoring of resources for changes</dd>
54          * </dl>
55          * 
56          * @param e DOM to supply configuration
57          */
58         ReloadableXMLFile(const DOMElement* e);
59     
60         virtual ~ReloadableXMLFile() {
61             delete m_lock;
62         }
63
64         /**
65          * Loads configuration material.
66          * 
67          * <p>This method is called to load configuration material
68          * initially and any time a change is detected. The base version
69          * performs basic parsing duties and returns the result.
70          * 
71          * @return a pair consisting of a flag indicating whether to take ownership of
72          *      the document, and the root element of the tree to load
73          */
74         virtual std::pair<bool,DOMElement*> load();
75         
76         /**
77          * Overrideable method to determine whether a remote resource remains valid.
78          * 
79          * @return  true iff the resource remains valid and should not be reloaded
80          */
81         virtual bool isValid() const {
82             return true;
83         }
84
85         /** Root of the original DOM element passed into constructor. */
86         const 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         /** Last modification of local resource. */
98         time_t m_filestamp;
99         
100         /** Shared lock for guarding reloads. */
101         RWLock* m_lock;
102
103     public:
104         Lockable* lock();
105
106         void unlock() {
107             if (m_lock)
108                 m_lock->unlock();
109         }
110     };
111
112 };
113
114 #endif /* __xmltooling_reloadable_h__ */