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/KeyInfo.h"
29 #include "signature/Signature.h"
31 #include "util/XMLConstants.h"
32 #include "validation/Validator.h"
38 #include <log4cpp/Category.hh>
39 #include <log4cpp/PropertyConfigurator.hh>
40 #include <log4cpp/OstreamAppender.hh>
41 #include <xercesc/util/PlatformUtils.hpp>
42 #ifndef XMLTOOLING_NO_XMLSEC
43 #include <xsec/framework/XSECProvider.hpp>
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);
181 catch (const xercesc::XMLException&) {
182 log.fatal("caught exception while initializing Xerces");
186 log.info("library initialization complete");
190 void XMLToolingInternalConfig::term()
192 XMLObjectBuilder::destroyBuilders();
193 KeyInfoSchemaValidators.destroyValidators();
194 EncryptionSchemaValidators.destroyValidators();
195 XMLToolingException::deregisterFactories();
197 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
199 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
202 FreeLibrary(static_cast<HMODULE>(*i));
203 #elif defined(HAVE_DLFCN_H)
204 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
209 # error "Don't know about dynamic loading on this platform!"
212 m_libhandles.clear();
216 delete m_validatingPool;
217 m_validatingPool=NULL;
219 #ifndef XMLTOOLING_NO_XMLSEC
220 delete m_xsecProvider;
222 XSECPlatformUtils::Terminate();
225 xercesc::XMLPlatformUtils::closeMutex(m_lock);
227 xercesc::XMLPlatformUtils::Terminate();
230 xmltooling::NDC ndc("term");
232 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
235 Lockable* XMLToolingInternalConfig::lock()
237 xercesc::XMLPlatformUtils::lockMutex(m_lock);
241 void XMLToolingInternalConfig::unlock()
243 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
246 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
249 xmltooling::NDC ndc("LoadLibrary");
251 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
252 log.info("loading extension: %s", path);
258 char* fixed=const_cast<char*>(path);
259 if (strchr(fixed,'/')) {
262 while (p=strchr(p,'/'))
266 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
268 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
270 handle=LoadLibraryEx(fixed,NULL,0);
272 throw runtime_error(string("unable to load extension library: ") + fixed);
273 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
275 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
276 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
277 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
282 catch(runtime_error& e) {
292 #elif defined(HAVE_DLFCN_H)
293 void* handle=dlopen(path,RTLD_LAZY);
295 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
296 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
300 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
301 (dlerror() ? dlerror() : "unknown error")
306 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
308 catch(runtime_error& e) {
315 # error "Don't know about dynamic loading on this platform!"
317 m_libhandles.push_back(handle);
318 log.info("loaded extension: %s", path);