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 "impl/UnknownElement.h"
27 #include "signature/KeyInfo.h"
28 #include "signature/Signature.h"
30 #include "util/XMLConstants.h"
31 #include "validation/Validator.h"
37 #include <log4cpp/Category.hh>
38 #include <log4cpp/PropertyConfigurator.hh>
39 #include <log4cpp/OstreamAppender.hh>
40 #include <xercesc/util/PlatformUtils.hpp>
41 #ifndef XMLTOOLING_NO_XMLSEC
42 #include <xsec/framework/XSECProvider.hpp>
47 using namespace xmlsignature;
48 using namespace xmltooling;
49 using namespace log4cpp;
52 DECL_EXCEPTION_FACTORY(XMLParserException,xmltooling);
53 DECL_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
54 DECL_EXCEPTION_FACTORY(MarshallingException,xmltooling);
55 DECL_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
56 DECL_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
57 DECL_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
58 DECL_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
59 DECL_EXCEPTION_FACTORY(ValidationException,xmltooling);
61 #ifndef XMLTOOLING_NO_XMLSEC
62 DECL_EXCEPTION_FACTORY(SignatureException,xmlsignature);
65 namespace xmltooling {
66 XMLToolingInternalConfig g_config;
69 XMLToolingConfig& XMLToolingConfig::getConfig()
74 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
79 bool XMLToolingInternalConfig::log_config(const char* config)
82 if (!config || !*config)
83 config=getenv("XMLTOOLING_LOG_CONFIG");
84 if (!config || !*config)
88 Category& root = Category::getRoot();
89 if (!strcmp(config,"DEBUG")) {
90 root.setPriority(Priority::DEBUG);
93 else if (!strcmp(config,"INFO")) {
94 root.setPriority(Priority::INFO);
97 else if (!strcmp(config,"NOTICE")) {
98 root.setPriority(Priority::NOTICE);
101 else if (!strcmp(config,"WARN")) {
102 root.setPriority(Priority::WARN);
105 else if (!strcmp(config,"ERROR")) {
106 root.setPriority(Priority::ERROR);
109 else if (!strcmp(config,"CRIT")) {
110 root.setPriority(Priority::CRIT);
113 else if (!strcmp(config,"ALERT")) {
114 root.setPriority(Priority::ALERT);
117 else if (!strcmp(config,"EMERG")) {
118 root.setPriority(Priority::EMERG);
121 else if (!strcmp(config,"FATAL")) {
122 root.setPriority(Priority::FATAL);
126 root.setAppender(new OstreamAppender("default",&cerr));
128 PropertyConfigurator::configure(config);
130 catch (const ConfigureFailure& e) {
131 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
138 bool XMLToolingInternalConfig::init()
141 xmltooling::NDC ndc("init");
143 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
145 log.debug("library initialization started");
147 xercesc::XMLPlatformUtils::Initialize();
148 log.debug("Xerces initialization complete");
150 #ifndef XMLTOOLING_NO_XMLSEC
151 XSECPlatformUtils::Initialise();
152 m_xsecProvider=new XSECProvider();
153 log.debug("XMLSec initialization complete");
156 m_parserPool=new ParserPool();
157 m_validatingPool=new ParserPool(true,true);
158 m_lock=xercesc::XMLPlatformUtils::makeMutex();
160 // default registrations
161 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
163 registerKeyInfoClasses();
165 REGISTER_EXCEPTION_FACTORY(XMLParserException,xmltooling);
166 REGISTER_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
167 REGISTER_EXCEPTION_FACTORY(MarshallingException,xmltooling);
168 REGISTER_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
169 REGISTER_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
170 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
171 REGISTER_EXCEPTION_FACTORY(ValidationException,xmltooling);
173 #ifndef XMLTOOLING_NO_XMLSEC
174 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
175 REGISTER_EXCEPTION_FACTORY(SignatureException,xmlsignature);
178 catch (const xercesc::XMLException&) {
179 log.fatal("caught exception while initializing Xerces");
183 log.info("library initialization complete");
187 void XMLToolingInternalConfig::term()
189 XMLObjectBuilder::destroyBuilders();
190 Validator::destroyValidators();
191 XMLToolingException::deregisterFactories();
193 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
195 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
198 FreeLibrary(static_cast<HMODULE>(*i));
199 #elif defined(HAVE_DLFCN_H)
200 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
205 # error "Don't know about dynamic loading on this platform!"
208 m_libhandles.clear();
212 delete m_validatingPool;
213 m_validatingPool=NULL;
215 #ifndef XMLTOOLING_NO_XMLSEC
216 delete m_xsecProvider;
218 XSECPlatformUtils::Terminate();
221 xercesc::XMLPlatformUtils::closeMutex(m_lock);
223 xercesc::XMLPlatformUtils::Terminate();
226 xmltooling::NDC ndc("term");
228 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
231 Lockable& XMLToolingInternalConfig::lock()
233 xercesc::XMLPlatformUtils::lockMutex(m_lock);
237 void XMLToolingInternalConfig::unlock()
239 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
242 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
245 xmltooling::NDC ndc("LoadLibrary");
247 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
248 log.info("loading extension: %s", path);
254 char* fixed=const_cast<char*>(path);
255 if (strchr(fixed,'/')) {
258 while (p=strchr(p,'/'))
262 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
264 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
266 handle=LoadLibraryEx(fixed,NULL,0);
268 throw runtime_error(string("unable to load extension library: ") + fixed);
269 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
271 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
272 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
273 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
278 catch(runtime_error& e) {
288 #elif defined(HAVE_DLFCN_H)
289 void* handle=dlopen(path,RTLD_LAZY);
291 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
292 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
296 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
297 (dlerror() ? dlerror() : "unknown error")
302 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
304 catch(runtime_error& e) {
311 # error "Don't know about dynamic loading on this platform!"
313 m_libhandles.push_back(handle);
314 log.info("loaded extension: %s", path);