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 Marshaller::registerDefaultMarshaller(new UnknownElementMarshaller());
144 Unmarshaller::registerDefaultUnmarshaller(new UnknownElementUnmarshaller());
145 #ifndef XMLTOOLING_NO_XMLSEC
146 QName dsig(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME);
147 XMLObjectBuilder::registerBuilder(dsig,new XMLSecSignatureBuilder());
148 Marshaller::registerMarshaller(dsig,new XMLSecSignatureMarshaller());
149 Unmarshaller::registerUnmarshaller(dsig,new XMLSecSignatureUnmarshaller());
152 catch (const xercesc::XMLException&) {
153 log.fatal("caught exception while initializing Xerces");
157 log.info("library initialization complete");
161 void XMLToolingInternalConfig::term()
163 XMLObjectBuilder::destroyBuilders();
164 Marshaller::destroyMarshallers();
165 Unmarshaller::destroyUnmarshallers();
167 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
169 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
172 FreeLibrary(static_cast<HMODULE>(*i));
173 #elif defined(HAVE_DLFCN_H)
174 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
179 # error "Don't know about dynamic loading on this platform!"
182 m_libhandles.clear();
187 #ifndef XMLTOOLING_NO_XMLSEC
188 delete m_xsecProvider;
190 XSECPlatformUtils::Terminate();
193 xercesc::XMLPlatformUtils::closeMutex(m_lock);
195 xercesc::XMLPlatformUtils::Terminate();
198 xmltooling::NDC ndc("term");
200 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
203 ILockable& XMLToolingInternalConfig::lock()
205 xercesc::XMLPlatformUtils::lockMutex(m_lock);
209 void XMLToolingInternalConfig::unlock()
211 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
214 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
217 xmltooling::NDC ndc("LoadLibrary");
219 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
220 log.info("loading extension: %s", path);
226 char* fixed=const_cast<char*>(path);
227 if (strchr(fixed,'/')) {
230 while (p=strchr(p,'/'))
234 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
236 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
238 handle=LoadLibraryEx(fixed,NULL,0);
240 throw runtime_error(string("unable to load extension library: ") + fixed);
241 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
243 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
244 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
245 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
250 catch(runtime_error& e) {
260 #elif defined(HAVE_DLFCN_H)
261 void* handle=dlopen(path,RTLD_LAZY);
263 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
264 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
268 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
269 (dlerror() ? dlerror() : "unknown error")
274 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
276 catch(runtime_error& e) {
283 # error "Don't know about dynamic loading on this platform!"
285 m_libhandles.push_back(handle);
286 log.info("loaded extension: %s", path);