2 * Copyright 2001-2006 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * XMLToolingConfig.cpp
20 * Library configuration
24 #include "exceptions.h"
25 #include "XMLToolingConfig.h"
26 #include "encryption/Encryption.h"
27 #include "impl/UnknownElement.h"
28 #include "security/TrustEngine.h"
29 #include "signature/CredentialResolver.h"
31 #include "util/XMLConstants.h"
32 #include "validation/Validator.h"
39 #include <log4cpp/Category.hh>
40 #include <log4cpp/PropertyConfigurator.hh>
41 #include <log4cpp/OstreamAppender.hh>
42 #include <xercesc/util/PlatformUtils.hpp>
43 #ifndef XMLTOOLING_NO_XMLSEC
44 #include <xsec/framework/XSECProvider.hpp>
45 #include <openssl/err.h>
48 using namespace xmlencryption;
49 using namespace xmlsignature;
50 using namespace xmltooling;
51 using namespace log4cpp;
54 DECL_EXCEPTION_FACTORY(XMLParserException,xmltooling);
55 DECL_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
56 DECL_EXCEPTION_FACTORY(MarshallingException,xmltooling);
57 DECL_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
58 DECL_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
59 DECL_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
60 DECL_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
61 DECL_EXCEPTION_FACTORY(ValidationException,xmltooling);
63 #ifndef XMLTOOLING_NO_XMLSEC
64 DECL_EXCEPTION_FACTORY(SignatureException,xmlsignature);
67 namespace xmltooling {
68 XMLToolingInternalConfig g_config;
71 XMLToolingConfig& XMLToolingConfig::getConfig()
76 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
81 bool XMLToolingInternalConfig::log_config(const char* config)
84 if (!config || !*config)
85 config=getenv("XMLTOOLING_LOG_CONFIG");
86 if (!config || !*config)
90 Category& root = Category::getRoot();
91 if (!strcmp(config,"DEBUG")) {
92 root.setPriority(Priority::DEBUG);
95 else if (!strcmp(config,"INFO")) {
96 root.setPriority(Priority::INFO);
99 else if (!strcmp(config,"NOTICE")) {
100 root.setPriority(Priority::NOTICE);
103 else if (!strcmp(config,"WARN")) {
104 root.setPriority(Priority::WARN);
107 else if (!strcmp(config,"ERROR")) {
108 root.setPriority(Priority::ERROR);
111 else if (!strcmp(config,"CRIT")) {
112 root.setPriority(Priority::CRIT);
115 else if (!strcmp(config,"ALERT")) {
116 root.setPriority(Priority::ALERT);
119 else if (!strcmp(config,"EMERG")) {
120 root.setPriority(Priority::EMERG);
123 else if (!strcmp(config,"FATAL")) {
124 root.setPriority(Priority::FATAL);
128 root.setAppender(new OstreamAppender("default",&cerr));
130 PropertyConfigurator::configure(config);
132 catch (const ConfigureFailure& e) {
133 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
140 bool XMLToolingInternalConfig::init()
143 xmltooling::NDC ndc("init");
145 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
147 log.debug("library initialization started");
149 xercesc::XMLPlatformUtils::Initialize();
150 log.debug("Xerces initialization complete");
152 #ifndef XMLTOOLING_NO_XMLSEC
153 XSECPlatformUtils::Initialise();
154 m_xsecProvider=new XSECProvider();
155 log.debug("XMLSec initialization complete");
158 m_parserPool=new ParserPool();
159 m_validatingPool=new ParserPool(true,true);
160 m_lock=xercesc::XMLPlatformUtils::makeMutex();
162 // default registrations
163 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
165 registerKeyInfoClasses();
166 registerEncryptionClasses();
168 REGISTER_EXCEPTION_FACTORY(XMLParserException,xmltooling);
169 REGISTER_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
170 REGISTER_EXCEPTION_FACTORY(MarshallingException,xmltooling);
171 REGISTER_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
172 REGISTER_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
173 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
174 REGISTER_EXCEPTION_FACTORY(ValidationException,xmltooling);
176 #ifndef XMLTOOLING_NO_XMLSEC
177 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
178 REGISTER_EXCEPTION_FACTORY(SignatureException,xmlsignature);
179 registerKeyResolvers();
180 registerCredentialResolvers();
181 registerTrustEngines();
184 catch (const xercesc::XMLException&) {
185 log.fatal("caught exception while initializing Xerces");
189 log.info("library initialization complete");
193 void XMLToolingInternalConfig::term()
195 XMLObjectBuilder::destroyBuilders();
196 KeyInfoSchemaValidators.destroyValidators();
197 EncryptionSchemaValidators.destroyValidators();
198 XMLToolingException::deregisterFactories();
200 #ifndef XMLTOOLING_NO_XMLSEC
201 TrustEngineManager.deregisterFactories();
202 CredentialResolverManager.deregisterFactories();
203 KeyResolverManager.deregisterFactories();
206 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
208 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
211 FreeLibrary(static_cast<HMODULE>(*i));
212 #elif defined(HAVE_DLFCN_H)
213 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
218 # error "Don't know about dynamic loading on this platform!"
221 m_libhandles.clear();
225 delete m_validatingPool;
226 m_validatingPool=NULL;
228 #ifndef XMLTOOLING_NO_XMLSEC
229 delete m_xsecProvider;
231 XSECPlatformUtils::Terminate();
234 xercesc::XMLPlatformUtils::closeMutex(m_lock);
236 xercesc::XMLPlatformUtils::Terminate();
239 xmltooling::NDC ndc("term");
241 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
244 Lockable* XMLToolingInternalConfig::lock()
246 xercesc::XMLPlatformUtils::lockMutex(m_lock);
250 void XMLToolingInternalConfig::unlock()
252 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
255 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
258 xmltooling::NDC ndc("LoadLibrary");
260 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
261 log.info("loading extension: %s", path);
267 char* fixed=const_cast<char*>(path);
268 if (strchr(fixed,'/')) {
271 while (p=strchr(p,'/'))
275 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
277 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
279 handle=LoadLibraryEx(fixed,NULL,0);
281 throw runtime_error(string("unable to load extension library: ") + fixed);
282 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
284 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
285 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
286 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
291 catch(runtime_error& e) {
301 #elif defined(HAVE_DLFCN_H)
302 void* handle=dlopen(path,RTLD_LAZY);
304 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
305 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
309 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
310 (dlerror() ? dlerror() : "unknown error")
315 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
317 catch(runtime_error& e) {
324 # error "Don't know about dynamic loading on this platform!"
326 m_libhandles.push_back(handle);
327 log.info("loaded extension: %s", path);
331 #ifndef XMLTOOLING_NO_XMLSEC
332 void xmltooling::log_openssl()
338 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
340 Category& log=Category::getInstance("OpenSSL");
341 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
342 if (data && (flags & ERR_TXT_STRING))
343 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
344 code=ERR_get_error_line_data(&file,&line,&data,&flags);