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/impl/XMLSecSignature.h"
34 #include <log4cpp/Category.hh>
35 #include <log4cpp/PropertyConfigurator.hh>
36 #include <log4cpp/OstreamAppender.hh>
37 #include <xercesc/util/PlatformUtils.hpp>
38 #ifndef XMLTOOLING_NO_XMLSEC
39 #include <xsec/framework/XSECProvider.hpp>
44 using namespace log4cpp;
45 using namespace xmltooling;
48 DECL_EXCEPTION_FACTORY(XMLParserException);
49 DECL_EXCEPTION_FACTORY(XMLObjectException);
50 DECL_EXCEPTION_FACTORY(MarshallingException);
51 DECL_EXCEPTION_FACTORY(UnmarshallingException);
52 DECL_EXCEPTION_FACTORY(UnknownElementException);
53 DECL_EXCEPTION_FACTORY(UnknownAttributeException);
54 DECL_EXCEPTION_FACTORY(ValidationException);
55 DECL_EXCEPTION_FACTORY(SignatureException);
58 XMLToolingInternalConfig g_config;
61 XMLToolingConfig& XMLToolingConfig::getConfig()
66 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
71 bool XMLToolingInternalConfig::log_config(const char* config)
74 if (!config || !*config)
75 config=getenv("XMLTOOLING_LOG_CONFIG");
76 if (!config || !*config)
80 Category& root = Category::getRoot();
81 if (!strcmp(config,"DEBUG")) {
82 root.setPriority(Priority::DEBUG);
85 else if (!strcmp(config,"INFO")) {
86 root.setPriority(Priority::INFO);
89 else if (!strcmp(config,"NOTICE")) {
90 root.setPriority(Priority::NOTICE);
93 else if (!strcmp(config,"WARN")) {
94 root.setPriority(Priority::WARN);
97 else if (!strcmp(config,"ERROR")) {
98 root.setPriority(Priority::ERROR);
101 else if (!strcmp(config,"CRIT")) {
102 root.setPriority(Priority::CRIT);
105 else if (!strcmp(config,"ALERT")) {
106 root.setPriority(Priority::ALERT);
109 else if (!strcmp(config,"EMERG")) {
110 root.setPriority(Priority::EMERG);
113 else if (!strcmp(config,"FATAL")) {
114 root.setPriority(Priority::FATAL);
118 root.setAppender(new OstreamAppender("default",&cerr));
120 PropertyConfigurator::configure(config);
122 catch (const ConfigureFailure& e) {
123 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
130 bool XMLToolingInternalConfig::init()
133 xmltooling::NDC ndc("init");
135 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
137 log.debug("library initialization started");
139 xercesc::XMLPlatformUtils::Initialize();
140 log.debug("Xerces initialization complete");
142 #ifndef XMLTOOLING_NO_XMLSEC
143 XSECPlatformUtils::Initialise();
144 m_xsecProvider=new XSECProvider();
145 log.debug("XMLSec initialization complete");
148 m_parserPool=new ParserPool();
149 m_lock=xercesc::XMLPlatformUtils::makeMutex();
151 // default registrations
152 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
153 #ifndef XMLTOOLING_NO_XMLSEC
154 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new XMLSecSignatureBuilder());
156 REGISTER_EXCEPTION_FACTORY(XMLParserException);
157 REGISTER_EXCEPTION_FACTORY(XMLObjectException);
158 REGISTER_EXCEPTION_FACTORY(MarshallingException);
159 REGISTER_EXCEPTION_FACTORY(UnmarshallingException);
160 REGISTER_EXCEPTION_FACTORY(UnknownElementException);
161 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException);
162 REGISTER_EXCEPTION_FACTORY(ValidationException);
163 REGISTER_EXCEPTION_FACTORY(SignatureException);
165 catch (const xercesc::XMLException&) {
166 log.fatal("caught exception while initializing Xerces");
170 log.info("library initialization complete");
174 void XMLToolingInternalConfig::term()
176 XMLObjectBuilder::destroyBuilders();
178 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
180 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
183 FreeLibrary(static_cast<HMODULE>(*i));
184 #elif defined(HAVE_DLFCN_H)
185 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
190 # error "Don't know about dynamic loading on this platform!"
193 m_libhandles.clear();
198 #ifndef XMLTOOLING_NO_XMLSEC
199 delete m_xsecProvider;
201 XSECPlatformUtils::Terminate();
204 xercesc::XMLPlatformUtils::closeMutex(m_lock);
206 xercesc::XMLPlatformUtils::Terminate();
209 xmltooling::NDC ndc("term");
211 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
214 ILockable& XMLToolingInternalConfig::lock()
216 xercesc::XMLPlatformUtils::lockMutex(m_lock);
220 void XMLToolingInternalConfig::unlock()
222 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
225 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
228 xmltooling::NDC ndc("LoadLibrary");
230 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
231 log.info("loading extension: %s", path);
237 char* fixed=const_cast<char*>(path);
238 if (strchr(fixed,'/')) {
241 while (p=strchr(p,'/'))
245 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
247 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
249 handle=LoadLibraryEx(fixed,NULL,0);
251 throw runtime_error(string("unable to load extension library: ") + fixed);
252 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
254 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
255 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
256 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
261 catch(runtime_error& e) {
271 #elif defined(HAVE_DLFCN_H)
272 void* handle=dlopen(path,RTLD_LAZY);
274 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
275 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
279 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
280 (dlerror() ? dlerror() : "unknown error")
285 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
287 catch(runtime_error& e) {
294 # error "Don't know about dynamic loading on this platform!"
296 m_libhandles.push_back(handle);
297 log.info("loaded extension: %s", path);