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 "signature/CredentialResolver.h"
29 #include "signature/KeyInfo.h"
30 #include "signature/Signature.h"
32 #include "util/XMLConstants.h"
33 #include "validation/Validator.h"
40 #include <log4cpp/Category.hh>
41 #include <log4cpp/PropertyConfigurator.hh>
42 #include <log4cpp/OstreamAppender.hh>
43 #include <xercesc/util/PlatformUtils.hpp>
44 #ifndef XMLTOOLING_NO_XMLSEC
45 #include <xsec/framework/XSECProvider.hpp>
46 #include <openssl/err.h>
49 using namespace xmlencryption;
50 using namespace xmlsignature;
51 using namespace xmltooling;
52 using namespace log4cpp;
55 DECL_EXCEPTION_FACTORY(XMLParserException,xmltooling);
56 DECL_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
57 DECL_EXCEPTION_FACTORY(MarshallingException,xmltooling);
58 DECL_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
59 DECL_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
60 DECL_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
61 DECL_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
62 DECL_EXCEPTION_FACTORY(ValidationException,xmltooling);
64 #ifndef XMLTOOLING_NO_XMLSEC
65 DECL_EXCEPTION_FACTORY(SignatureException,xmlsignature);
68 namespace xmltooling {
69 XMLToolingInternalConfig g_config;
72 XMLToolingConfig& XMLToolingConfig::getConfig()
77 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
82 bool XMLToolingInternalConfig::log_config(const char* config)
85 if (!config || !*config)
86 config=getenv("XMLTOOLING_LOG_CONFIG");
87 if (!config || !*config)
91 Category& root = Category::getRoot();
92 if (!strcmp(config,"DEBUG")) {
93 root.setPriority(Priority::DEBUG);
96 else if (!strcmp(config,"INFO")) {
97 root.setPriority(Priority::INFO);
100 else if (!strcmp(config,"NOTICE")) {
101 root.setPriority(Priority::NOTICE);
104 else if (!strcmp(config,"WARN")) {
105 root.setPriority(Priority::WARN);
108 else if (!strcmp(config,"ERROR")) {
109 root.setPriority(Priority::ERROR);
112 else if (!strcmp(config,"CRIT")) {
113 root.setPriority(Priority::CRIT);
116 else if (!strcmp(config,"ALERT")) {
117 root.setPriority(Priority::ALERT);
120 else if (!strcmp(config,"EMERG")) {
121 root.setPriority(Priority::EMERG);
124 else if (!strcmp(config,"FATAL")) {
125 root.setPriority(Priority::FATAL);
129 root.setAppender(new OstreamAppender("default",&cerr));
131 PropertyConfigurator::configure(config);
133 catch (const ConfigureFailure& e) {
134 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
141 bool XMLToolingInternalConfig::init()
144 xmltooling::NDC ndc("init");
146 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
148 log.debug("library initialization started");
150 xercesc::XMLPlatformUtils::Initialize();
151 log.debug("Xerces initialization complete");
153 #ifndef XMLTOOLING_NO_XMLSEC
154 XSECPlatformUtils::Initialise();
155 m_xsecProvider=new XSECProvider();
156 log.debug("XMLSec initialization complete");
159 m_parserPool=new ParserPool();
160 m_validatingPool=new ParserPool(true,true);
161 m_lock=xercesc::XMLPlatformUtils::makeMutex();
163 // default registrations
164 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
166 registerKeyInfoClasses();
167 registerEncryptionClasses();
169 REGISTER_EXCEPTION_FACTORY(XMLParserException,xmltooling);
170 REGISTER_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
171 REGISTER_EXCEPTION_FACTORY(MarshallingException,xmltooling);
172 REGISTER_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
173 REGISTER_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
174 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
175 REGISTER_EXCEPTION_FACTORY(ValidationException,xmltooling);
177 #ifndef XMLTOOLING_NO_XMLSEC
178 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
179 REGISTER_EXCEPTION_FACTORY(SignatureException,xmlsignature);
180 registerCredentialResolvers();
183 catch (const xercesc::XMLException&) {
184 log.fatal("caught exception while initializing Xerces");
188 log.info("library initialization complete");
192 void XMLToolingInternalConfig::term()
194 XMLObjectBuilder::destroyBuilders();
195 KeyInfoSchemaValidators.destroyValidators();
196 EncryptionSchemaValidators.destroyValidators();
197 XMLToolingException::deregisterFactories();
199 #ifndef XMLTOOLING_NO_XMLSEC
200 CredentialResolverManager.deregisterFactories();
201 KeyResolverManager.deregisterFactories();
204 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
206 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
209 FreeLibrary(static_cast<HMODULE>(*i));
210 #elif defined(HAVE_DLFCN_H)
211 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
216 # error "Don't know about dynamic loading on this platform!"
219 m_libhandles.clear();
223 delete m_validatingPool;
224 m_validatingPool=NULL;
226 #ifndef XMLTOOLING_NO_XMLSEC
227 delete m_xsecProvider;
229 XSECPlatformUtils::Terminate();
232 xercesc::XMLPlatformUtils::closeMutex(m_lock);
234 xercesc::XMLPlatformUtils::Terminate();
237 xmltooling::NDC ndc("term");
239 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
242 Lockable* XMLToolingInternalConfig::lock()
244 xercesc::XMLPlatformUtils::lockMutex(m_lock);
248 void XMLToolingInternalConfig::unlock()
250 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
253 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
256 xmltooling::NDC ndc("LoadLibrary");
258 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
259 log.info("loading extension: %s", path);
265 char* fixed=const_cast<char*>(path);
266 if (strchr(fixed,'/')) {
269 while (p=strchr(p,'/'))
273 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
275 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
277 handle=LoadLibraryEx(fixed,NULL,0);
279 throw runtime_error(string("unable to load extension library: ") + fixed);
280 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
282 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
283 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
284 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
289 catch(runtime_error& e) {
299 #elif defined(HAVE_DLFCN_H)
300 void* handle=dlopen(path,RTLD_LAZY);
302 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
303 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
307 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
308 (dlerror() ? dlerror() : "unknown error")
313 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
315 catch(runtime_error& e) {
322 # error "Don't know about dynamic loading on this platform!"
324 m_libhandles.push_back(handle);
325 log.info("loaded extension: %s", path);
329 #ifndef XMLTOOLING_NO_XMLSEC
330 void xmltooling::log_openssl()
336 unsigned long code=ERR_get_error_line_data(&file,&line,&data,&flags);
338 Category& log=Category::getInstance("OpenSSL");
339 log.errorStream() << "error code: " << code << " in " << file << ", line " << line << CategoryStream::ENDLINE;
340 if (data && (flags & ERR_TXT_STRING))
341 log.errorStream() << "error data: " << data << CategoryStream::ENDLINE;
342 code=ERR_get_error_line_data(&file,&line,&data,&flags);