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 "XMLToolingConfig.h"
25 #include "impl/UnknownElement.h"
26 #include "signature/impl/XMLSecSignature.h"
33 #include <log4cpp/Category.hh>
34 #include <log4cpp/PropertyConfigurator.hh>
35 #include <log4cpp/OstreamAppender.hh>
36 #include <xercesc/util/PlatformUtils.hpp>
37 #ifndef XMLTOOLING_NO_XMLSEC
38 #include <xsec/framework/XSECProvider.hpp>
43 using namespace log4cpp;
44 using namespace xmltooling;
48 XMLToolingInternalConfig g_config;
51 XMLToolingConfig& XMLToolingConfig::getConfig()
56 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
61 bool XMLToolingInternalConfig::log_config(const char* config)
64 if (!config || !*config)
65 config=getenv("XMLTOOLING_LOG_CONFIG");
66 if (!config || !*config)
70 Category& root = Category::getRoot();
71 if (!strcmp(config,"DEBUG")) {
72 root.setPriority(Priority::DEBUG);
75 else if (!strcmp(config,"INFO")) {
76 root.setPriority(Priority::INFO);
79 else if (!strcmp(config,"NOTICE")) {
80 root.setPriority(Priority::NOTICE);
83 else if (!strcmp(config,"WARN")) {
84 root.setPriority(Priority::WARN);
87 else if (!strcmp(config,"ERROR")) {
88 root.setPriority(Priority::ERROR);
91 else if (!strcmp(config,"CRIT")) {
92 root.setPriority(Priority::CRIT);
95 else if (!strcmp(config,"ALERT")) {
96 root.setPriority(Priority::ALERT);
99 else if (!strcmp(config,"EMERG")) {
100 root.setPriority(Priority::EMERG);
103 else if (!strcmp(config,"FATAL")) {
104 root.setPriority(Priority::FATAL);
108 root.setAppender(new OstreamAppender("default",&cerr));
110 PropertyConfigurator::configure(config);
112 catch (const ConfigureFailure& e) {
113 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
120 bool XMLToolingInternalConfig::init()
123 xmltooling::NDC ndc("init");
125 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
127 log.debug("library initialization started");
129 xercesc::XMLPlatformUtils::Initialize();
130 log.debug("Xerces initialization complete");
132 #ifndef XMLTOOLING_NO_XMLSEC
133 XSECPlatformUtils::Initialise();
134 m_xsecProvider=new XSECProvider();
135 log.debug("XMLSec initialization complete");
138 m_parserPool=new ParserPool();
139 m_lock=xercesc::XMLPlatformUtils::makeMutex();
141 // default registrations
142 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
143 #ifndef XMLTOOLING_NO_XMLSEC
144 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new XMLSecSignatureBuilder());
147 catch (const xercesc::XMLException&) {
148 log.fatal("caught exception while initializing Xerces");
152 log.info("library initialization complete");
156 void XMLToolingInternalConfig::term()
158 XMLObjectBuilder::destroyBuilders();
160 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
162 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
165 FreeLibrary(static_cast<HMODULE>(*i));
166 #elif defined(HAVE_DLFCN_H)
167 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
172 # error "Don't know about dynamic loading on this platform!"
175 m_libhandles.clear();
180 #ifndef XMLTOOLING_NO_XMLSEC
181 delete m_xsecProvider;
183 XSECPlatformUtils::Terminate();
186 xercesc::XMLPlatformUtils::closeMutex(m_lock);
188 xercesc::XMLPlatformUtils::Terminate();
191 xmltooling::NDC ndc("term");
193 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
196 ILockable& XMLToolingInternalConfig::lock()
198 xercesc::XMLPlatformUtils::lockMutex(m_lock);
202 void XMLToolingInternalConfig::unlock()
204 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
207 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
210 xmltooling::NDC ndc("LoadLibrary");
212 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
213 log.info("loading extension: %s", path);
219 char* fixed=const_cast<char*>(path);
220 if (strchr(fixed,'/')) {
223 while (p=strchr(p,'/'))
227 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
229 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
231 handle=LoadLibraryEx(fixed,NULL,0);
233 throw runtime_error(string("unable to load extension library: ") + fixed);
234 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
236 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
237 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
238 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
243 catch(runtime_error& e) {
253 #elif defined(HAVE_DLFCN_H)
254 void* handle=dlopen(path,RTLD_LAZY);
256 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
257 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
261 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
262 (dlerror() ? dlerror() : "unknown error")
267 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
269 catch(runtime_error& e) {
276 # error "Don't know about dynamic loading on this platform!"
278 m_libhandles.push_back(handle);
279 log.info("loaded extension: %s", path);