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