2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * XMLMetadataProvider.cpp
20 * Supplies metadata from an XML file
24 #include "saml2/metadata/Metadata.h"
25 #include "saml2/metadata/MetadataFilter.h"
26 #include "saml2/metadata/AbstractMetadataProvider.h"
28 #include <xmltooling/util/NDC.h>
29 #include <xmltooling/util/ReloadableXMLFile.h>
31 using namespace opensaml::saml2md;
32 using namespace xmltooling::logging;
33 using namespace xmltooling;
36 #if defined (_MSC_VER)
37 #pragma warning( push )
38 #pragma warning( disable : 4250 )
44 class SAML_DLLLOCAL XMLMetadataProvider : public AbstractMetadataProvider, public ReloadableXMLFile
47 XMLMetadataProvider(const DOMElement* e)
48 : AbstractMetadataProvider(e), ReloadableXMLFile(e, Category::getInstance(SAML_LOGCAT".MetadataProvider.XML")),
51 virtual ~XMLMetadataProvider() {
56 load(); // guarantees an exception or the metadata is loaded
59 const XMLObject* getMetadata() const {
64 pair<bool,DOMElement*> load();
67 using AbstractMetadataProvider::index;
73 MetadataProvider* SAML_DLLLOCAL XMLMetadataProviderFactory(const DOMElement* const & e)
75 return new XMLMetadataProvider(e);
81 #if defined (_MSC_VER)
82 #pragma warning( pop )
85 pair<bool,DOMElement*> XMLMetadataProvider::load()
87 // Load from source using base class.
88 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
90 // If we own it, wrap it for now.
91 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : NULL);
93 // Unmarshall objects, binding the document.
94 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(raw.second, true));
97 if (!dynamic_cast<const EntitiesDescriptor*>(xmlObject.get()) && !dynamic_cast<const EntityDescriptor*>(xmlObject.get()))
98 throw MetadataException(
99 "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
102 // Preprocess the metadata.
103 doFilters(*xmlObject.get());
104 xmlObject->releaseThisAndChildrenDOM();
105 xmlObject->setDocument(NULL);
108 bool changed = m_object!=NULL;
110 m_object = xmlObject.release();
114 return make_pair(false,(DOMElement*)NULL);
117 void XMLMetadataProvider::index()
119 clearDescriptorIndex();
120 EntitiesDescriptor* group=dynamic_cast<EntitiesDescriptor*>(m_object);
122 AbstractMetadataProvider::index(group, SAMLTIME_MAX);
125 EntityDescriptor* site=dynamic_cast<EntityDescriptor*>(m_object);
126 AbstractMetadataProvider::index(site, SAMLTIME_MAX);