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
23 #define XMLTOOLING_DECLARE_VALIDATORS
26 #include "exceptions.h"
27 #include "XMLToolingConfig.h"
28 #include "impl/UnknownElement.h"
29 #include "signature/KeyInfo.h"
30 #include "signature/Signature.h"
32 #include "util/XMLConstants.h"
33 #include "validation/Validator.h"
39 #include <log4cpp/Category.hh>
40 #include <log4cpp/PropertyConfigurator.hh>
41 #include <log4cpp/OstreamAppender.hh>
42 #include <xercesc/util/PlatformUtils.hpp>
43 #ifndef XMLTOOLING_NO_XMLSEC
44 #include <xsec/framework/XSECProvider.hpp>
49 using namespace xmlsignature;
50 using namespace xmltooling;
51 using namespace log4cpp;
54 #define REGISTER_ELEMENT(namespaceURI,cname) \
55 q=QName(namespaceURI,cname::LOCAL_NAME); \
56 XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \
57 Validator::registerValidator(q,new cname##SchemaValidator())
59 #define REGISTER_TYPE(namespaceURI,cname) \
60 q=QName(namespaceURI,cname::TYPE_NAME); \
61 XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \
62 Validator::registerValidator(q,new cname##SchemaValidator())
65 DECL_EXCEPTION_FACTORY(XMLParserException);
66 DECL_EXCEPTION_FACTORY(XMLObjectException);
67 DECL_EXCEPTION_FACTORY(MarshallingException);
68 DECL_EXCEPTION_FACTORY(UnmarshallingException);
69 DECL_EXCEPTION_FACTORY(UnknownElementException);
70 DECL_EXCEPTION_FACTORY(UnknownAttributeException);
71 DECL_EXCEPTION_FACTORY(ValidationException);
72 DECL_EXCEPTION_FACTORY(SignatureException);
74 namespace xmltooling {
75 XMLToolingInternalConfig g_config;
78 XMLToolingConfig& XMLToolingConfig::getConfig()
83 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
88 bool XMLToolingInternalConfig::log_config(const char* config)
91 if (!config || !*config)
92 config=getenv("XMLTOOLING_LOG_CONFIG");
93 if (!config || !*config)
97 Category& root = Category::getRoot();
98 if (!strcmp(config,"DEBUG")) {
99 root.setPriority(Priority::DEBUG);
102 else if (!strcmp(config,"INFO")) {
103 root.setPriority(Priority::INFO);
106 else if (!strcmp(config,"NOTICE")) {
107 root.setPriority(Priority::NOTICE);
110 else if (!strcmp(config,"WARN")) {
111 root.setPriority(Priority::WARN);
114 else if (!strcmp(config,"ERROR")) {
115 root.setPriority(Priority::ERROR);
118 else if (!strcmp(config,"CRIT")) {
119 root.setPriority(Priority::CRIT);
122 else if (!strcmp(config,"ALERT")) {
123 root.setPriority(Priority::ALERT);
126 else if (!strcmp(config,"EMERG")) {
127 root.setPriority(Priority::EMERG);
130 else if (!strcmp(config,"FATAL")) {
131 root.setPriority(Priority::FATAL);
135 root.setAppender(new OstreamAppender("default",&cerr));
137 PropertyConfigurator::configure(config);
139 catch (const ConfigureFailure& e) {
140 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
147 bool XMLToolingInternalConfig::init()
150 xmltooling::NDC ndc("init");
152 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
154 log.debug("library initialization started");
156 xercesc::XMLPlatformUtils::Initialize();
157 log.debug("Xerces initialization complete");
159 #ifndef XMLTOOLING_NO_XMLSEC
160 XSECPlatformUtils::Initialise();
161 m_xsecProvider=new XSECProvider();
162 log.debug("XMLSec initialization complete");
165 m_parserPool=new ParserPool();
166 m_lock=xercesc::XMLPlatformUtils::makeMutex();
168 // default registrations
169 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
172 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,KeyInfo);
173 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,KeyName);
174 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,KeyValue);
175 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,MgmtData);
176 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,DSAKeyValue);
177 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,RSAKeyValue);
178 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Exponent);
179 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Modulus);
180 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,P);
181 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Q);
182 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,G);
183 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Y);
184 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,J);
185 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Seed);
186 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,PgenCounter);
187 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,XPath);
188 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Transform);
189 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Transforms);
190 REGISTER_TYPE(XMLConstants::XMLSIG_NS,KeyInfo);
191 REGISTER_TYPE(XMLConstants::XMLSIG_NS,KeyValue);
192 REGISTER_TYPE(XMLConstants::XMLSIG_NS,DSAKeyValue);
193 REGISTER_TYPE(XMLConstants::XMLSIG_NS,RSAKeyValue);
194 REGISTER_TYPE(XMLConstants::XMLSIG_NS,Transform);
195 REGISTER_TYPE(XMLConstants::XMLSIG_NS,Transforms);
197 #ifndef XMLTOOLING_NO_XMLSEC
198 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
201 REGISTER_EXCEPTION_FACTORY(XMLParserException);
202 REGISTER_EXCEPTION_FACTORY(XMLObjectException);
203 REGISTER_EXCEPTION_FACTORY(MarshallingException);
204 REGISTER_EXCEPTION_FACTORY(UnmarshallingException);
205 REGISTER_EXCEPTION_FACTORY(UnknownElementException);
206 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException);
207 REGISTER_EXCEPTION_FACTORY(ValidationException);
208 REGISTER_EXCEPTION_FACTORY(SignatureException);
210 catch (const xercesc::XMLException&) {
211 log.fatal("caught exception while initializing Xerces");
215 log.info("library initialization complete");
219 void XMLToolingInternalConfig::term()
221 XMLObjectBuilder::destroyBuilders();
223 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
225 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
228 FreeLibrary(static_cast<HMODULE>(*i));
229 #elif defined(HAVE_DLFCN_H)
230 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
235 # error "Don't know about dynamic loading on this platform!"
238 m_libhandles.clear();
243 #ifndef XMLTOOLING_NO_XMLSEC
244 delete m_xsecProvider;
246 XSECPlatformUtils::Terminate();
249 xercesc::XMLPlatformUtils::closeMutex(m_lock);
251 xercesc::XMLPlatformUtils::Terminate();
254 xmltooling::NDC ndc("term");
256 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
259 ILockable& XMLToolingInternalConfig::lock()
261 xercesc::XMLPlatformUtils::lockMutex(m_lock);
265 void XMLToolingInternalConfig::unlock()
267 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
270 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
273 xmltooling::NDC ndc("LoadLibrary");
275 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
276 log.info("loading extension: %s", path);
282 char* fixed=const_cast<char*>(path);
283 if (strchr(fixed,'/')) {
286 while (p=strchr(p,'/'))
290 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
292 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
294 handle=LoadLibraryEx(fixed,NULL,0);
296 throw runtime_error(string("unable to load extension library: ") + fixed);
297 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
299 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
300 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
301 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
306 catch(runtime_error& e) {
316 #elif defined(HAVE_DLFCN_H)
317 void* handle=dlopen(path,RTLD_LAZY);
319 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
320 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
324 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
325 (dlerror() ? dlerror() : "unknown error")
330 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
332 catch(runtime_error& e) {
339 # error "Don't know about dynamic loading on this platform!"
341 m_libhandles.push_back(handle);
342 log.info("loaded extension: %s", path);