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