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 log4cpp;
48 using namespace xmltooling;
51 #define REGISTER_ELEMENT(namespaceURI,cname) \
52 q=QName(namespaceURI,cname##::LOCAL_NAME); \
53 XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \
54 Validator::registerValidator(q,new cname##SchemaValidator())
56 #define REGISTER_TYPE(namespaceURI,cname) \
57 q=QName(namespaceURI,cname##::TYPE_NAME); \
58 XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \
59 Validator::registerValidator(q,new cname##SchemaValidator())
62 DECL_EXCEPTION_FACTORY(XMLParserException);
63 DECL_EXCEPTION_FACTORY(XMLObjectException);
64 DECL_EXCEPTION_FACTORY(MarshallingException);
65 DECL_EXCEPTION_FACTORY(UnmarshallingException);
66 DECL_EXCEPTION_FACTORY(UnknownElementException);
67 DECL_EXCEPTION_FACTORY(UnknownAttributeException);
68 DECL_EXCEPTION_FACTORY(ValidationException);
69 DECL_EXCEPTION_FACTORY(SignatureException);
71 namespace xmltooling {
72 XMLToolingInternalConfig g_config;
75 XMLToolingConfig& XMLToolingConfig::getConfig()
80 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
85 bool XMLToolingInternalConfig::log_config(const char* config)
88 if (!config || !*config)
89 config=getenv("XMLTOOLING_LOG_CONFIG");
90 if (!config || !*config)
94 Category& root = Category::getRoot();
95 if (!strcmp(config,"DEBUG")) {
96 root.setPriority(Priority::DEBUG);
99 else if (!strcmp(config,"INFO")) {
100 root.setPriority(Priority::INFO);
103 else if (!strcmp(config,"NOTICE")) {
104 root.setPriority(Priority::NOTICE);
107 else if (!strcmp(config,"WARN")) {
108 root.setPriority(Priority::WARN);
111 else if (!strcmp(config,"ERROR")) {
112 root.setPriority(Priority::ERROR);
115 else if (!strcmp(config,"CRIT")) {
116 root.setPriority(Priority::CRIT);
119 else if (!strcmp(config,"ALERT")) {
120 root.setPriority(Priority::ALERT);
123 else if (!strcmp(config,"EMERG")) {
124 root.setPriority(Priority::EMERG);
127 else if (!strcmp(config,"FATAL")) {
128 root.setPriority(Priority::FATAL);
132 root.setAppender(new OstreamAppender("default",&cerr));
134 PropertyConfigurator::configure(config);
136 catch (const ConfigureFailure& e) {
137 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
144 bool XMLToolingInternalConfig::init()
147 xmltooling::NDC ndc("init");
149 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
151 log.debug("library initialization started");
153 xercesc::XMLPlatformUtils::Initialize();
154 log.debug("Xerces initialization complete");
156 #ifndef XMLTOOLING_NO_XMLSEC
157 XSECPlatformUtils::Initialise();
158 m_xsecProvider=new XSECProvider();
159 log.debug("XMLSec initialization complete");
162 m_parserPool=new ParserPool();
163 m_lock=xercesc::XMLPlatformUtils::makeMutex();
165 // default registrations
166 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
169 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,KeyInfo);
170 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,KeyName);
171 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,MgmtData);
172 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,RSAKeyValue);
173 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Exponent);
174 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Modulus);
175 REGISTER_TYPE(XMLConstants::XMLSIG_NS,KeyInfo);
176 REGISTER_TYPE(XMLConstants::XMLSIG_NS,RSAKeyValue);
178 #ifndef XMLTOOLING_NO_XMLSEC
179 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
182 REGISTER_EXCEPTION_FACTORY(XMLParserException);
183 REGISTER_EXCEPTION_FACTORY(XMLObjectException);
184 REGISTER_EXCEPTION_FACTORY(MarshallingException);
185 REGISTER_EXCEPTION_FACTORY(UnmarshallingException);
186 REGISTER_EXCEPTION_FACTORY(UnknownElementException);
187 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException);
188 REGISTER_EXCEPTION_FACTORY(ValidationException);
189 REGISTER_EXCEPTION_FACTORY(SignatureException);
191 catch (const xercesc::XMLException&) {
192 log.fatal("caught exception while initializing Xerces");
196 log.info("library initialization complete");
200 void XMLToolingInternalConfig::term()
202 XMLObjectBuilder::destroyBuilders();
204 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
206 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
209 FreeLibrary(static_cast<HMODULE>(*i));
210 #elif defined(HAVE_DLFCN_H)
211 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
216 # error "Don't know about dynamic loading on this platform!"
219 m_libhandles.clear();
224 #ifndef XMLTOOLING_NO_XMLSEC
225 delete m_xsecProvider;
227 XSECPlatformUtils::Terminate();
230 xercesc::XMLPlatformUtils::closeMutex(m_lock);
232 xercesc::XMLPlatformUtils::Terminate();
235 xmltooling::NDC ndc("term");
237 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
240 ILockable& XMLToolingInternalConfig::lock()
242 xercesc::XMLPlatformUtils::lockMutex(m_lock);
246 void XMLToolingInternalConfig::unlock()
248 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
251 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
254 xmltooling::NDC ndc("LoadLibrary");
256 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
257 log.info("loading extension: %s", path);
263 char* fixed=const_cast<char*>(path);
264 if (strchr(fixed,'/')) {
267 while (p=strchr(p,'/'))
271 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
273 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
275 handle=LoadLibraryEx(fixed,NULL,0);
277 throw runtime_error(string("unable to load extension library: ") + fixed);
278 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
280 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
281 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
282 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
287 catch(runtime_error& e) {
297 #elif defined(HAVE_DLFCN_H)
298 void* handle=dlopen(path,RTLD_LAZY);
300 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
301 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
305 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
306 (dlerror() ? dlerror() : "unknown error")
311 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
313 catch(runtime_error& e) {
320 # error "Don't know about dynamic loading on this platform!"
322 m_libhandles.push_back(handle);
323 log.info("loaded extension: %s", path);