Merged marshalling/unmarshalling methods into core interface.
[shibboleth/cpp-xmltooling.git] / xmltooling / XMLToolingConfig.cpp
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * XMLToolingConfig.cpp
19  * 
20  * Library configuration 
21  */
22
23 #include "internal.h"
24 #include "XMLToolingConfig.h"
25 #include "impl/UnknownElement.h"
26 #include "signature/impl/XMLSecSignature.h"
27 #include "util/NDC.h"
28
29 #ifdef HAVE_DLFCN_H
30 # include <dlfcn.h>
31 #endif
32
33 #include <log4cpp/Category.hh>
34 #include <log4cpp/PropertyConfigurator.hh>
35 #include <log4cpp/OstreamAppender.hh>
36 #include <xercesc/util/PlatformUtils.hpp>
37 #ifndef XMLTOOLING_NO_XMLSEC
38     #include <xsec/framework/XSECProvider.hpp>
39 #endif
40
41 #include <stdexcept>
42
43 using namespace log4cpp;
44 using namespace xmltooling;
45 using namespace std;
46
47 namespace {
48    XMLToolingInternalConfig g_config;
49 }
50
51 XMLToolingConfig& XMLToolingConfig::getConfig()
52 {
53     return g_config;
54 }
55
56 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
57 {
58     return g_config;
59 }
60
61 bool XMLToolingInternalConfig::log_config(const char* config)
62 {
63     try {
64         if (!config || !*config)
65             config=getenv("XMLTOOLING_LOG_CONFIG");
66         if (!config || !*config)
67             config="WARN";
68         
69         bool level=false;
70         Category& root = Category::getRoot();
71         if (!strcmp(config,"DEBUG")) {
72             root.setPriority(Priority::DEBUG);
73             level=true;
74         }
75         else if (!strcmp(config,"INFO")) {
76             root.setPriority(Priority::INFO);
77             level=true;
78         }
79         else if (!strcmp(config,"NOTICE")) {
80             root.setPriority(Priority::NOTICE);
81             level=true;
82         }
83         else if (!strcmp(config,"WARN")) {
84             root.setPriority(Priority::WARN);
85             level=true;
86         }
87         else if (!strcmp(config,"ERROR")) {
88             root.setPriority(Priority::ERROR);
89             level=true;
90         }
91         else if (!strcmp(config,"CRIT")) {
92             root.setPriority(Priority::CRIT);
93             level=true;
94         }
95         else if (!strcmp(config,"ALERT")) {
96             root.setPriority(Priority::ALERT);
97             level=true;
98         }
99         else if (!strcmp(config,"EMERG")) {
100             root.setPriority(Priority::EMERG);
101             level=true;
102         }
103         else if (!strcmp(config,"FATAL")) {
104             root.setPriority(Priority::FATAL);
105             level=true;
106         }
107         if (level)
108             root.setAppender(new OstreamAppender("default",&cerr));
109         else
110             PropertyConfigurator::configure(config);
111     }
112     catch (const ConfigureFailure& e) {
113         Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
114         return false;
115     }
116     
117     return true;
118 }
119
120 bool XMLToolingInternalConfig::init()
121 {
122 #ifdef _DEBUG
123     xmltooling::NDC ndc("init");
124 #endif
125     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
126     try {
127         log.debug("library initialization started");
128
129         xercesc::XMLPlatformUtils::Initialize();
130         log.debug("Xerces initialization complete");
131
132 #ifndef XMLTOOLING_NO_XMLSEC
133         XSECPlatformUtils::Initialise();
134         m_xsecProvider=new XSECProvider();
135         log.debug("XMLSec initialization complete");
136 #endif
137
138         m_parserPool=new ParserPool();
139         m_lock=xercesc::XMLPlatformUtils::makeMutex();
140
141         // default registrations
142         XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
143 #ifndef XMLTOOLING_NO_XMLSEC
144         XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new XMLSecSignatureBuilder());
145 #endif
146     }
147     catch (const xercesc::XMLException&) {
148         log.fatal("caught exception while initializing Xerces");
149         return false;
150     }
151
152     log.info("library initialization complete");
153     return true;
154 }
155
156 void XMLToolingInternalConfig::term()
157 {
158     XMLObjectBuilder::destroyBuilders();
159
160     for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
161 #if defined(WIN32)
162         FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
163         if (fn)
164             fn();
165         FreeLibrary(static_cast<HMODULE>(*i));
166 #elif defined(HAVE_DLFCN_H)
167         void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
168         if (fn)
169             fn();
170         dlclose(*i);
171 #else
172 # error "Don't know about dynamic loading on this platform!"
173 #endif
174     }
175     m_libhandles.clear();
176     
177     delete m_parserPool;
178     m_parserPool=NULL;
179
180 #ifndef XMLTOOLING_NO_XMLSEC
181     delete m_xsecProvider;
182     m_xsecProvider=NULL;
183     XSECPlatformUtils::Terminate();
184 #endif
185
186     xercesc::XMLPlatformUtils::closeMutex(m_lock);
187     m_lock=NULL;
188     xercesc::XMLPlatformUtils::Terminate();
189
190  #ifdef _DEBUG
191     xmltooling::NDC ndc("term");
192 #endif
193    Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
194 }
195
196 ILockable& XMLToolingInternalConfig::lock()
197 {
198     xercesc::XMLPlatformUtils::lockMutex(m_lock);
199     return *this;
200 }
201
202 void XMLToolingInternalConfig::unlock()
203 {
204     xercesc::XMLPlatformUtils::unlockMutex(m_lock);
205 }
206
207 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
208 {
209 #ifdef _DEBUG
210     xmltooling::NDC ndc("LoadLibrary");
211 #endif
212     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
213     log.info("loading extension: %s", path);
214
215     Locker locker(this);
216
217 #if defined(WIN32)
218     HMODULE handle=NULL;
219     char* fixed=const_cast<char*>(path);
220     if (strchr(fixed,'/')) {
221         fixed=strdup(path);
222         char* p=fixed;
223         while (p=strchr(p,'/'))
224             *p='\\';
225     }
226
227     UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
228     try {
229         handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
230         if (!handle)
231              handle=LoadLibraryEx(fixed,NULL,0);
232         if (!handle)
233             throw runtime_error(string("unable to load extension library: ") + fixed);
234         FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
235         if (!fn)
236             throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
237         if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
238             throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
239         if (fixed!=path)
240             free(fixed);
241         SetErrorMode(em);
242     }
243     catch(runtime_error& e) {
244         log.error(e.what());
245         if (handle)
246             FreeLibrary(handle);
247         SetErrorMode(em);
248         if (fixed!=path)
249             free(fixed);
250         return false;
251     }
252
253 #elif defined(HAVE_DLFCN_H)
254     void* handle=dlopen(path,RTLD_LAZY);
255     if (!handle)
256         throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
257     int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
258     if (!fn) {
259         dlclose(handle);
260         throw runtime_error(
261             string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
262                 (dlerror() ? dlerror() : "unknown error")
263             );
264     }
265     try {
266         if (fn(context)!=0)
267             throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
268     }
269     catch(runtime_error& e) {
270         log.error(e.what());
271         if (handle)
272             dlclose(handle);
273         return false;
274     }
275 #else
276 # error "Don't know about dynamic loading on this platform!"
277 #endif
278     m_libhandles.push_back(handle);
279     log.info("loaded extension: %s", path);
280     return true;
281 }