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
25 #define XMLTOOLING_DEFINE_CONSTANTS
26 #include <xercesc/util/XMLUniDefs.hpp>
28 #include "exceptions.h"
29 #include "XMLToolingConfig.h"
30 #include "impl/UnknownElement.h"
31 #include "signature/impl/KeyInfoImpl.h"
32 #include "signature/impl/XMLSecSignatureImpl.h"
34 #include "util/XMLConstants.h"
40 #include <log4cpp/Category.hh>
41 #include <log4cpp/PropertyConfigurator.hh>
42 #include <log4cpp/OstreamAppender.hh>
43 #include <xercesc/util/PlatformUtils.hpp>
44 #ifndef XMLTOOLING_NO_XMLSEC
45 #include <xsec/framework/XSECProvider.hpp>
50 using namespace log4cpp;
51 using namespace xmltooling;
54 DECL_EXCEPTION_FACTORY(XMLParserException);
55 DECL_EXCEPTION_FACTORY(XMLObjectException);
56 DECL_EXCEPTION_FACTORY(MarshallingException);
57 DECL_EXCEPTION_FACTORY(UnmarshallingException);
58 DECL_EXCEPTION_FACTORY(UnknownElementException);
59 DECL_EXCEPTION_FACTORY(UnknownAttributeException);
60 DECL_EXCEPTION_FACTORY(ValidationException);
61 DECL_EXCEPTION_FACTORY(SignatureException);
63 namespace xmltooling {
64 XMLToolingInternalConfig g_config;
67 XMLToolingConfig& XMLToolingConfig::getConfig()
72 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
77 bool XMLToolingInternalConfig::log_config(const char* config)
80 if (!config || !*config)
81 config=getenv("XMLTOOLING_LOG_CONFIG");
82 if (!config || !*config)
86 Category& root = Category::getRoot();
87 if (!strcmp(config,"DEBUG")) {
88 root.setPriority(Priority::DEBUG);
91 else if (!strcmp(config,"INFO")) {
92 root.setPriority(Priority::INFO);
95 else if (!strcmp(config,"NOTICE")) {
96 root.setPriority(Priority::NOTICE);
99 else if (!strcmp(config,"WARN")) {
100 root.setPriority(Priority::WARN);
103 else if (!strcmp(config,"ERROR")) {
104 root.setPriority(Priority::ERROR);
107 else if (!strcmp(config,"CRIT")) {
108 root.setPriority(Priority::CRIT);
111 else if (!strcmp(config,"ALERT")) {
112 root.setPriority(Priority::ALERT);
115 else if (!strcmp(config,"EMERG")) {
116 root.setPriority(Priority::EMERG);
119 else if (!strcmp(config,"FATAL")) {
120 root.setPriority(Priority::FATAL);
124 root.setAppender(new OstreamAppender("default",&cerr));
126 PropertyConfigurator::configure(config);
128 catch (const ConfigureFailure& e) {
129 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
136 bool XMLToolingInternalConfig::init()
139 xmltooling::NDC ndc("init");
141 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
143 log.debug("library initialization started");
145 xercesc::XMLPlatformUtils::Initialize();
146 log.debug("Xerces initialization complete");
148 #ifndef XMLTOOLING_NO_XMLSEC
149 XSECPlatformUtils::Initialise();
150 m_xsecProvider=new XSECProvider();
151 log.debug("XMLSec initialization complete");
154 m_parserPool=new ParserPool();
155 m_lock=xercesc::XMLPlatformUtils::makeMutex();
157 // default registrations
158 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
159 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,KeyInfo::LOCAL_NAME),new KeyInfoBuilderImpl());
160 #ifndef XMLTOOLING_NO_XMLSEC
161 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new XMLSecSignatureBuilder());
163 REGISTER_EXCEPTION_FACTORY(XMLParserException);
164 REGISTER_EXCEPTION_FACTORY(XMLObjectException);
165 REGISTER_EXCEPTION_FACTORY(MarshallingException);
166 REGISTER_EXCEPTION_FACTORY(UnmarshallingException);
167 REGISTER_EXCEPTION_FACTORY(UnknownElementException);
168 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException);
169 REGISTER_EXCEPTION_FACTORY(ValidationException);
170 REGISTER_EXCEPTION_FACTORY(SignatureException);
172 catch (const xercesc::XMLException&) {
173 log.fatal("caught exception while initializing Xerces");
177 log.info("library initialization complete");
181 void XMLToolingInternalConfig::term()
183 XMLObjectBuilder::destroyBuilders();
185 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
187 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
190 FreeLibrary(static_cast<HMODULE>(*i));
191 #elif defined(HAVE_DLFCN_H)
192 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
197 # error "Don't know about dynamic loading on this platform!"
200 m_libhandles.clear();
205 #ifndef XMLTOOLING_NO_XMLSEC
206 delete m_xsecProvider;
208 XSECPlatformUtils::Terminate();
211 xercesc::XMLPlatformUtils::closeMutex(m_lock);
213 xercesc::XMLPlatformUtils::Terminate();
216 xmltooling::NDC ndc("term");
218 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
221 ILockable& XMLToolingInternalConfig::lock()
223 xercesc::XMLPlatformUtils::lockMutex(m_lock);
227 void XMLToolingInternalConfig::unlock()
229 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
232 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
235 xmltooling::NDC ndc("LoadLibrary");
237 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
238 log.info("loading extension: %s", path);
244 char* fixed=const_cast<char*>(path);
245 if (strchr(fixed,'/')) {
248 while (p=strchr(p,'/'))
252 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
254 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
256 handle=LoadLibraryEx(fixed,NULL,0);
258 throw runtime_error(string("unable to load extension library: ") + fixed);
259 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
261 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
262 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
263 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
268 catch(runtime_error& e) {
278 #elif defined(HAVE_DLFCN_H)
279 void* handle=dlopen(path,RTLD_LAZY);
281 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
282 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
286 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
287 (dlerror() ? dlerror() : "unknown error")
292 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
294 catch(runtime_error& e) {
301 # error "Don't know about dynamic loading on this platform!"
303 m_libhandles.push_back(handle);
304 log.info("loaded extension: %s", path);