Copied over mapping and filtering schemas.
[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 #include "AccessControl.h"
26 #include "exceptions.h"
27 #include "RequestMapper.h"
28 #include "ServiceProvider.h"
29 #include "SessionCache.h"
30 #include "SPConfig.h"
31 #include "attribute/AttributeDecoder.h"
32 #include "attribute/resolver/AttributeExtractor.h"
33 #include "attribute/resolver/AttributeResolver.h"
34 #include "binding/ArtifactResolver.h"
35 #include "handler/SessionInitiator.h"
36 #include "metadata/MetadataExt.h"
37 #include "remoting/ListenerService.h"
38 #include "security/PKIXTrustEngine.h"
39
40 #include <log4cpp/Category.hh>
41 #include <saml/SAMLConfig.h>
42 #include <xmltooling/util/NDC.h>
43 #include <xmltooling/util/TemplateEngine.h>
44
45 using namespace shibsp;
46 using namespace opensaml;
47 using namespace xmltooling;
48 using namespace log4cpp;
49
50 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
51 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
52 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
53 DECL_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
54 DECL_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
55 DECL_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
56
57 namespace shibsp {
58    SPInternalConfig g_config;
59 }
60
61 SPConfig& SPConfig::getConfig()
62 {
63     return g_config;
64 }
65
66 SPInternalConfig& SPInternalConfig::getInternalConfig()
67 {
68     return g_config;
69 }
70
71 void SPConfig::setServiceProvider(ServiceProvider* serviceProvider)
72 {
73     delete m_serviceProvider;
74     m_serviceProvider = serviceProvider;
75 }
76
77 bool SPInternalConfig::init(const char* catalog_path)
78 {
79 #ifdef _DEBUG
80     NDC ndc("init");
81 #endif
82     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
83     log.debug("library initialization started");
84
85     const char* loglevel=getenv("SHIBSP_LOGGING");
86     if (!loglevel)
87         loglevel = SHIBSP_LOGGING;
88     XMLToolingConfig::getConfig().log_config(loglevel);
89
90     if (!catalog_path)
91         catalog_path = getenv("SHIBSP_SCHEMAS");
92     if (!catalog_path)
93         catalog_path = SHIBSP_SCHEMAS;
94     XMLToolingConfig::getConfig().catalog_path = catalog_path;
95
96     if (!SAMLConfig::getConfig().init()) {
97         log.fatal("failed to initialize OpenSAML library");
98         return false;
99     }
100
101     XMLToolingConfig::getConfig().setTemplateEngine(new TemplateEngine());
102     XMLToolingConfig::getConfig().getTemplateEngine()->setTagPrefix("shibmlp");
103     
104     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeException,shibsp);
105     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeExtractionException,shibsp);
106     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeFilteringException,shibsp);
107     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(AttributeResolutionException,shibsp);
108     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ConfigurationException,shibsp);
109     REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ListenerException,shibsp);
110     
111     registerMetadataExtClasses();
112     registerPKIXTrustEngine();
113
114     registerAccessControls();
115     registerAttributeDecoders();
116     registerAttributeExtractors();
117     registerAttributeFactories();
118     registerAttributeResolvers();
119     registerHandlers();
120     registerSessionInitiators();
121     registerListenerServices();
122     registerRequestMappers();
123     registerSessionCaches();
124     registerServiceProviders();
125
126     if (isEnabled(OutOfProcess))
127         m_artifactResolver = new ArtifactResolver();
128     
129     log.info("library initialization complete");
130     return true;
131 }
132
133 void SPInternalConfig::term()
134 {
135 #ifdef _DEBUG
136     NDC ndc("term");
137 #endif
138     Category& log=Category::getInstance(SHIBSP_LOGCAT".Config");
139     log.info("shutting down the library");
140
141     setServiceProvider(NULL);
142     setArtifactResolver(NULL);
143
144     AssertionConsumerServiceManager.deregisterFactories();
145     ManageNameIDServiceManager.deregisterFactories();
146     SessionInitiatorManager.deregisterFactories();
147     SingleLogoutServiceManager.deregisterFactories();
148     
149     ServiceProviderManager.deregisterFactories();
150     SessionCacheManager.deregisterFactories();
151     RequestMapperManager.deregisterFactories();
152     ListenerServiceManager.deregisterFactories();
153     HandlerManager.deregisterFactories();
154     AttributeDecoderManager.deregisterFactories();
155     AttributeExtractorManager.deregisterFactories();
156     AttributeResolverManager.deregisterFactories();
157     Attribute::deregisterFactories();
158     AccessControlManager.deregisterFactories();
159
160     SAMLConfig::getConfig().term();
161     log.info("library shutdown complete");
162 }