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,xmltooling);
66 DECL_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
67 DECL_EXCEPTION_FACTORY(MarshallingException,xmltooling);
68 DECL_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
69 DECL_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
70 DECL_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
71 DECL_EXCEPTION_FACTORY(UnknownExtensionException,xmltooling);
72 DECL_EXCEPTION_FACTORY(ValidationException,xmltooling);
74 #ifndef XMLTOOLING_NO_XMLSEC
75 DECL_EXCEPTION_FACTORY(SignatureException,xmlsignature);
78 namespace xmltooling {
79 XMLToolingInternalConfig g_config;
82 XMLToolingConfig& XMLToolingConfig::getConfig()
87 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
92 bool XMLToolingInternalConfig::log_config(const char* config)
95 if (!config || !*config)
96 config=getenv("XMLTOOLING_LOG_CONFIG");
97 if (!config || !*config)
101 Category& root = Category::getRoot();
102 if (!strcmp(config,"DEBUG")) {
103 root.setPriority(Priority::DEBUG);
106 else if (!strcmp(config,"INFO")) {
107 root.setPriority(Priority::INFO);
110 else if (!strcmp(config,"NOTICE")) {
111 root.setPriority(Priority::NOTICE);
114 else if (!strcmp(config,"WARN")) {
115 root.setPriority(Priority::WARN);
118 else if (!strcmp(config,"ERROR")) {
119 root.setPriority(Priority::ERROR);
122 else if (!strcmp(config,"CRIT")) {
123 root.setPriority(Priority::CRIT);
126 else if (!strcmp(config,"ALERT")) {
127 root.setPriority(Priority::ALERT);
130 else if (!strcmp(config,"EMERG")) {
131 root.setPriority(Priority::EMERG);
134 else if (!strcmp(config,"FATAL")) {
135 root.setPriority(Priority::FATAL);
139 root.setAppender(new OstreamAppender("default",&cerr));
141 PropertyConfigurator::configure(config);
143 catch (const ConfigureFailure& e) {
144 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
151 bool XMLToolingInternalConfig::init()
154 xmltooling::NDC ndc("init");
156 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
158 log.debug("library initialization started");
160 xercesc::XMLPlatformUtils::Initialize();
161 log.debug("Xerces initialization complete");
163 #ifndef XMLTOOLING_NO_XMLSEC
164 XSECPlatformUtils::Initialise();
165 m_xsecProvider=new XSECProvider();
166 log.debug("XMLSec initialization complete");
169 m_parserPool=new ParserPool();
170 m_lock=xercesc::XMLPlatformUtils::makeMutex();
172 // default registrations
173 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
176 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,KeyInfo);
177 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,KeyName);
178 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,KeyValue);
179 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,MgmtData);
180 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,DSAKeyValue);
181 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,RSAKeyValue);
182 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Exponent);
183 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Modulus);
184 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,P);
185 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Q);
186 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,G);
187 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Y);
188 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,J);
189 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Seed);
190 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,PgenCounter);
191 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,XPath);
192 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Transform);
193 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,Transforms);
194 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,RetrievalMethod);
195 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,X509IssuerSerial);
196 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,X509IssuerName);
197 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,X509SerialNumber);
198 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,X509SKI);
199 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,X509SubjectName);
200 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,X509Certificate);
201 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,X509CRL);
202 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,X509Data);
203 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,SPKISexp);
204 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,SPKIData);
205 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,PGPKeyID);
206 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,PGPKeyPacket);
207 REGISTER_ELEMENT(XMLConstants::XMLSIG_NS,PGPData);
208 REGISTER_TYPE(XMLConstants::XMLSIG_NS,KeyInfo);
209 REGISTER_TYPE(XMLConstants::XMLSIG_NS,KeyValue);
210 REGISTER_TYPE(XMLConstants::XMLSIG_NS,DSAKeyValue);
211 REGISTER_TYPE(XMLConstants::XMLSIG_NS,RSAKeyValue);
212 REGISTER_TYPE(XMLConstants::XMLSIG_NS,Transform);
213 REGISTER_TYPE(XMLConstants::XMLSIG_NS,Transforms);
214 REGISTER_TYPE(XMLConstants::XMLSIG_NS,RetrievalMethod);
215 REGISTER_TYPE(XMLConstants::XMLSIG_NS,X509IssuerSerial);
216 REGISTER_TYPE(XMLConstants::XMLSIG_NS,X509Data);
217 REGISTER_TYPE(XMLConstants::XMLSIG_NS,SPKIData);
218 REGISTER_TYPE(XMLConstants::XMLSIG_NS,PGPData);
220 REGISTER_EXCEPTION_FACTORY(XMLParserException,xmltooling);
221 REGISTER_EXCEPTION_FACTORY(XMLObjectException,xmltooling);
222 REGISTER_EXCEPTION_FACTORY(MarshallingException,xmltooling);
223 REGISTER_EXCEPTION_FACTORY(UnmarshallingException,xmltooling);
224 REGISTER_EXCEPTION_FACTORY(UnknownElementException,xmltooling);
225 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException,xmltooling);
226 REGISTER_EXCEPTION_FACTORY(ValidationException,xmltooling);
228 #ifndef XMLTOOLING_NO_XMLSEC
229 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new SignatureBuilder());
230 REGISTER_EXCEPTION_FACTORY(SignatureException,xmlsignature);
233 catch (const xercesc::XMLException&) {
234 log.fatal("caught exception while initializing Xerces");
238 log.info("library initialization complete");
242 void XMLToolingInternalConfig::term()
244 XMLObjectBuilder::destroyBuilders();
245 Validator::destroyValidators();
246 XMLToolingException::deregisterFactories();
248 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
250 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
253 FreeLibrary(static_cast<HMODULE>(*i));
254 #elif defined(HAVE_DLFCN_H)
255 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
260 # error "Don't know about dynamic loading on this platform!"
263 m_libhandles.clear();
268 #ifndef XMLTOOLING_NO_XMLSEC
269 delete m_xsecProvider;
271 XSECPlatformUtils::Terminate();
274 xercesc::XMLPlatformUtils::closeMutex(m_lock);
276 xercesc::XMLPlatformUtils::Terminate();
279 xmltooling::NDC ndc("term");
281 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
284 Lockable& XMLToolingInternalConfig::lock()
286 xercesc::XMLPlatformUtils::lockMutex(m_lock);
290 void XMLToolingInternalConfig::unlock()
292 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
295 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
298 xmltooling::NDC ndc("LoadLibrary");
300 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
301 log.info("loading extension: %s", path);
307 char* fixed=const_cast<char*>(path);
308 if (strchr(fixed,'/')) {
311 while (p=strchr(p,'/'))
315 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
317 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
319 handle=LoadLibraryEx(fixed,NULL,0);
321 throw runtime_error(string("unable to load extension library: ") + fixed);
322 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
324 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
325 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
326 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
331 catch(runtime_error& e) {
341 #elif defined(HAVE_DLFCN_H)
342 void* handle=dlopen(path,RTLD_LAZY);
344 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
345 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
349 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
350 (dlerror() ? dlerror() : "unknown error")
355 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
357 catch(runtime_error& e) {
364 # error "Don't know about dynamic loading on this platform!"
366 m_libhandles.push_back(handle);
367 log.info("loaded extension: %s", path);