Allow role lookup with no protocol.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / ObservableMetadataProvider.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 saml/saml2/metadata/ObservableMetadataProvider.h
19  * 
20  * A metadata provider that notifies interested parties of changes.
21  */
22
23 #ifndef __saml2_obsmetadataprov_h__
24 #define __saml2_obsmetadataprov_h__
25
26 #include <saml/saml2/metadata/MetadataProvider.h>
27 #include <xmltooling/util/Threads.h>
28
29 namespace opensaml {
30     
31     namespace saml2md {
32         
33 #if defined (_MSC_VER)
34         #pragma warning( push )
35         #pragma warning( disable : 4251 )
36 #endif
37         /**
38          * A metadata provider that notifies interested parties of changes.
39          */
40         class SAML_API ObservableMetadataProvider : public MetadataProvider
41         {
42         protected:
43             /**
44              * Constructor.
45              * 
46              * @param e DOM to supply configuration for provider
47              */
48             ObservableMetadataProvider(const xercesc::DOMElement* e=NULL)
49                 : MetadataProvider(e), m_observerLock(xmltooling::Mutex::create()) {
50             }
51             
52             /**
53              * Convenience method for notifying every registered Observer of an event.
54              */
55             virtual void emitChangeEvent() const {
56                 xmltooling::Lock lock(m_observerLock);
57                 for (std::vector<const Observer*>::const_iterator i=m_observers.begin(); i!=m_observers.end(); i++) {
58                     (*i)->onEvent(*this);
59                 }
60             }
61
62         public:
63             virtual ~ObservableMetadataProvider() {
64                 delete m_observerLock;
65             }
66             
67             /**
68              * An observer of metadata provider changes.
69              */
70             class SAML_API Observer {
71                 MAKE_NONCOPYABLE(Observer);
72             protected:
73                 Observer() {}
74             public:
75                 virtual ~Observer() {}
76         
77                 /**
78                  * Called when a provider signals an event has occured.
79                  * The provider is already locked. 
80                  * 
81                  * @param provider the provider being observed
82                  */
83                 virtual void onEvent(const ObservableMetadataProvider& provider) const=0;
84             };
85             
86             /**
87              * Adds a metadata observer.
88              * 
89              * @param newObserver metadata observer to add
90              */
91             virtual void addObserver(const Observer* newObserver) const {
92                 xmltooling::Lock lock(m_observerLock);
93                 m_observers.push_back(newObserver);
94             }
95
96             /**
97              * Removes a metadata observer.
98              * 
99              * @param oldObserver metadata observer to remove
100              * @return  the old observer
101              */
102             virtual const Observer* removeObserver(const Observer* oldObserver) const {
103                 xmltooling::Lock lock(m_observerLock);
104                 for (std::vector<const Observer*>::iterator i=m_observers.begin(); i!=m_observers.end(); i++) {
105                     if (oldObserver==(*i)) {
106                         m_observers.erase(i);
107                         return oldObserver;
108                     }
109                 }
110                 return NULL;
111             }
112
113         private:
114             mutable xmltooling::Mutex* m_observerLock;
115             mutable std::vector<const Observer*> m_observers;
116         };
117
118 #if defined (_MSC_VER)
119         #pragma warning( pop )
120 #endif
121
122     };
123 };
124
125 #endif /* __saml2_obsmetadataprov_h__ */