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