Convert logging to log4shib via compile time switch.
[shibboleth/sp.git] / shibsp / SPConfig.cpp
1
2 /*
3  *  Copyright 2001-2007 Internet2
4  * 
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /**
19  * SPConfig.cpp
20  * 
21  * Library configuration 
22  */
23
24 #include "internal.h"
25
26 #if defined(XMLTOOLING_LOG4SHIB)
27 # ifndef SHIBSP_LOG4SHIB
28 #  error "Logging library mismatch (XMLTooling is using log4shib)."
29 # endif
30 #elif defined(XMLTOOLING_LOG4CPP)
31 # ifndef SHIBSP_LOG4CPP
32 #  error "Logging library mismatch (XMLTooling is using log4cpp)."
33 # endif
34 #else
35 # error "No supported logging library."
36 #endif
37
38 #include "AccessControl.h"
39 #include "exceptions.h"
40 #include "RequestMapper.h"
41 #include "ServiceProvider.h"
42 #include "SessionCache.h"
43 #include "SPConfig.h"
44 #include "attribute/Attribute.h"
45 #include "handler/SessionInitiator.h"
46 #include "remoting/ListenerService.h"
47
48 #ifndef SHIBSP_LITE
49 # include "attribute/AttributeDecoder.h"
50 # include "attribute/filtering/AttributeFilter.h"
51 # include "attribute/filtering/MatchFunctor.h"
52 # include "attribute/resolver/AttributeExtractor.h"
53 # include "attribute/resolver/AttributeResolver.h"
54 # include "binding/ArtifactResolver.h"
55 # include "metadata/MetadataExt.h"
56 # include "security/PKIXTrustEngine.h"
57 # include <saml/SAMLConfig.h>
58 #else
59 # include <xmltooling/XMLToolingConfig.h>
60 #endif
61
62 #include <xmltooling/util/NDC.h>
63 #include <xmltooling/util/TemplateEngine.h>
64
65 using namespace shibsp;
66 using namespace opensaml;
67 using namespace xmltooling;
68
69 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
70 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
71 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
72 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
73 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
74 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
75
76 #ifdef SHIBSP_LITE
77 DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
78 DECL_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
79 DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
80 DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
81 DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
82 #endif
83
84 namespace shibsp {
85    SPInternalConfig g_config;
86 }
87
88 SPConfig& SPConfig::getConfig()
89 {
90     return g_config;
91 }
92
93 SPInternalConfig& SPInternalConfig::getInternalConfig()
94 {
95     return g_config;
96 }
97
98 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
99 {
100     delete m_serviceProvider;
101     m_serviceProvider = serviceProvider;
102 }
103
104 bool SPInternalConfig::init(const char* catalog_path)
105 {
106 #ifdef _DEBUG
107     NDC ndc("init");
108 #endif
109     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
110     log.debug("library initialization started");
111
112     const char* loglevel=getenv("SHIBSP_LOGGING");
113     if (!loglevel)
114         loglevel = SHIBSP_LOGGING;
115     XMLToolingConfig::getConfig().log_config(loglevel);
116
117     if (!catalog_path)
118         catalog_path = getenv("SHIBSP_SCHEMAS");
119     if (!catalog_path)
120         catalog_path = SHIBSP_SCHEMAS;
121     XMLToolingConfig::getConfig().catalog_path = catalog_path;
122
123 #ifndef SHIBSP_LITE
124     if (!SAMLConfig::getConfig().init()) {
125         log.fatal("failed to initialize OpenSAML library");
126         return false;
127     }
128 #else
129     if (!XMLToolingConfig::getConfig().init()) {
130         log.fatal("failed to initialize XMLTooling library");
131         return false;
132     }
133 #endif
134
135     XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
136     XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
137     
138     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
139     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
140     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
141     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
142     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
143     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
144
145 #ifdef SHIBSP_LITE
146     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(SecurityPolicyException,opensaml);
147     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
148     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
149     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
150     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
151 #endif
152
153 #ifndef SHIBSP_LITE
154     if (isEnabled(Metadata))
155         registerMetadataExtClasses();
156     if (isEnabled(Trust))
157         registerPKIXTrustEngine();
158 #endif
159
160     registerAttributeFactories();
161     registerHandlers();
162     registerSessionInitiators();
163     registerServiceProviders();
164
165 #ifndef SHIBSP_LITE
166     if (isEnabled(AttributeResolution)) {
167         registerAttributeExtractors();
168         registerAttributeDecoders();
169         registerAttributeResolvers();
170         registerAttributeFilters();
171         registerMatchFunctors();
172     }
173 #endif
174
175     if (isEnabled(Listener))
176         registerListenerServices();
177
178     if (isEnabled(RequestMapping)) {
179         registerAccessControls();
180         registerRequestMappers();
181     }
182
183     if (isEnabled(Caching))
184         registerSessionCaches();
185
186 #ifndef SHIBSP_LITE
187     if (isEnabled(OutOfProcess))
188         m_artifactResolver = new ArtifactResolver();
189 #endif
190
191     log.info("library initialization complete");
192     return true;
193 }
194
195 void SPInternalConfig::term()
196 {
197 #ifdef _DEBUG
198     NDC ndc("term");
199 #endif
200     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
201     log.info("shutting down the library");
202
203     setServiceProvider(NULL);
204 #ifndef SHIBSP_LITE
205     setArtifactResolver(NULL);
206 #endif
207
208     ArtifactResolutionServiceManager.deregisterFactories();
209     AssertionConsumerServiceManager.deregisterFactories();
210     LogoutInitiatorManager.deregisterFactories();
211     ManageNameIDServiceManager.deregisterFactories();
212     SessionInitiatorManager.deregisterFactories();
213     SingleLogoutServiceManager.deregisterFactories();
214     HandlerManager.deregisterFactories();
215     ServiceProviderManager.deregisterFactories();
216     Attribute::deregisterFactories();
217
218 #ifndef SHIBSP_LITE
219     if (isEnabled(AttributeResolution)) {
220         MatchFunctorManager.deregisterFactories();
221         AttributeFilterManager.deregisterFactories();
222         AttributeDecoderManager.deregisterFactories();
223         AttributeExtractorManager.deregisterFactories();
224         AttributeResolverManager.deregisterFactories();
225     }
226 #endif
227
228     if (isEnabled(Listener))
229         ListenerServiceManager.deregisterFactories();
230
231     if (isEnabled(RequestMapping)) {
232         AccessControlManager.deregisterFactories();
233         RequestMapperManager.deregisterFactories();
234     }
235
236     if (isEnabled(Caching))
237         SessionCacheManager.deregisterFactories();
238
239 #ifndef SHIBSP_LITE
240     SAMLConfig::getConfig().term();
241 #else
242     XMLToolingConfig::getConfig().term();
243 #endif
244     log.info("library shutdown complete");
245 }