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