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