Layered basic metadata plugin on top of new base class.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / XMLMetadataProvider.cpp
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  * XMLMetadataProvider.cpp
19  * 
20  * Supplies metadata from an XML file
21  */
22
23 #include "internal.h"
24 #include "saml2/metadata/Metadata.h"
25 #include "saml2/metadata/AbstractMetadataProvider.h"
26
27 #include <log4cpp/Category.hh>
28 #include <xmltooling/util/NDC.h>
29 #include <xmltooling/util/ReloadableXMLFile.h>
30 #include <xmltooling/util/XMLConstants.h>
31
32 using namespace opensaml::saml2md;
33 using namespace xmltooling;
34 using namespace log4cpp;
35 using namespace std;
36
37 #if defined (_MSC_VER)
38     #pragma warning( push )
39     #pragma warning( disable : 4250 )
40 #endif
41
42 namespace opensaml {
43     namespace saml2md {
44
45         class SAML_DLLLOCAL XMLMetadataProvider : public AbstractMetadataProvider, public ReloadableXMLFile
46         {
47         public:
48             XMLMetadataProvider(const DOMElement* e) : AbstractMetadataProvider(e), ReloadableXMLFile(e), m_object(NULL) {}
49             virtual ~XMLMetadataProvider() {
50                 delete m_object;
51             }
52
53             void init() {
54                 load(); // guarantees an exception or the metadata is loaded
55             }
56             
57             pair<bool,DOMElement*> load();
58
59             const XMLObject* getMetadata() const {
60                 return m_object;
61             }
62
63         private:
64             void index();
65         
66             XMLObject* m_object;
67         }; 
68
69         MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
70         {
71             return new XMLMetadataProvider(e);
72         }
73
74     };
75 };
76
77 #if defined (_MSC_VER)
78     #pragma warning( pop )
79 #endif
80
81 pair<bool,DOMElement*> XMLMetadataProvider::load()
82 {
83 #ifdef _DEBUG
84     NDC ndc("load");
85 #endif
86     
87     try {
88         // Load from source using base class.
89         pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
90         
91         // If we own it, wrap it for now.
92         XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
93                 
94         // Unmarshall objects, binding the document.
95         XMLObject* xmlObject=XMLObjectBuilder::buildOneFromElement(raw.second, true);
96         docjanitor.release();
97         
98         // Preprocess the metadata.
99         auto_ptr<XMLObject> xmlObjectPtr(xmlObject);
100         doFilters(*xmlObject);
101         xmlObjectPtr->releaseThisAndChildrenDOM();
102         xmlObjectPtr->setDocument(NULL);
103         
104         // Swap it in.
105         bool changed = m_object!=NULL;
106         delete m_object;
107         m_object = xmlObjectPtr.release();
108         index();
109         if (changed)
110             emitChangeEvent();
111         return make_pair(false,(DOMElement*)NULL);
112     }
113     catch (XMLToolingException& e) {
114         Category::getInstance(SAML_LOGCAT".Metadata").error("error while loading metadata: %s", e.what());
115         throw;
116     }
117 }
118
119 void XMLMetadataProvider::index()
120 {
121     clearDescriptorIndex();
122     EntitiesDescriptor* group=dynamic_cast<EntitiesDescriptor*>(m_object);
123     if (group) {
124         AbstractMetadataProvider::index(group, SAMLTIME_MAX);
125         return;
126     }
127     EntityDescriptor* site=dynamic_cast<EntityDescriptor*>(m_object);
128     AbstractMetadataProvider::index(site, SAMLTIME_MAX);
129 }