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"
35 #include "validation/Validator.h"
41 #include <log4cpp/Category.hh>
42 #include <log4cpp/PropertyConfigurator.hh>
43 #include <log4cpp/OstreamAppender.hh>
44 #include <xercesc/util/PlatformUtils.hpp>
45 #ifndef XMLTOOLING_NO_XMLSEC
46 #include <xsec/framework/XSECProvider.hpp>
51 using namespace log4cpp;
52 using namespace xmltooling;
55 DECL_EXCEPTION_FACTORY(XMLParserException);
56 DECL_EXCEPTION_FACTORY(XMLObjectException);
57 DECL_EXCEPTION_FACTORY(MarshallingException);
58 DECL_EXCEPTION_FACTORY(UnmarshallingException);
59 DECL_EXCEPTION_FACTORY(UnknownElementException);
60 DECL_EXCEPTION_FACTORY(UnknownAttributeException);
61 DECL_EXCEPTION_FACTORY(ValidationException);
62 DECL_EXCEPTION_FACTORY(SignatureException);
64 namespace xmltooling {
65 XMLToolingInternalConfig g_config;
68 XMLToolingConfig& XMLToolingConfig::getConfig()
73 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()
78 bool XMLToolingInternalConfig::log_config(const char* config)
81 if (!config || !*config)
82 config=getenv("XMLTOOLING_LOG_CONFIG");
83 if (!config || !*config)
87 Category& root = Category::getRoot();
88 if (!strcmp(config,"DEBUG")) {
89 root.setPriority(Priority::DEBUG);
92 else if (!strcmp(config,"INFO")) {
93 root.setPriority(Priority::INFO);
96 else if (!strcmp(config,"NOTICE")) {
97 root.setPriority(Priority::NOTICE);
100 else if (!strcmp(config,"WARN")) {
101 root.setPriority(Priority::WARN);
104 else if (!strcmp(config,"ERROR")) {
105 root.setPriority(Priority::ERROR);
108 else if (!strcmp(config,"CRIT")) {
109 root.setPriority(Priority::CRIT);
112 else if (!strcmp(config,"ALERT")) {
113 root.setPriority(Priority::ALERT);
116 else if (!strcmp(config,"EMERG")) {
117 root.setPriority(Priority::EMERG);
120 else if (!strcmp(config,"FATAL")) {
121 root.setPriority(Priority::FATAL);
125 root.setAppender(new OstreamAppender("default",&cerr));
127 PropertyConfigurator::configure(config);
129 catch (const ConfigureFailure& e) {
130 Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());
137 bool XMLToolingInternalConfig::init()
140 xmltooling::NDC ndc("init");
142 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
144 log.debug("library initialization started");
146 xercesc::XMLPlatformUtils::Initialize();
147 log.debug("Xerces initialization complete");
149 #ifndef XMLTOOLING_NO_XMLSEC
150 XSECPlatformUtils::Initialise();
151 m_xsecProvider=new XSECProvider();
152 log.debug("XMLSec initialization complete");
155 m_parserPool=new ParserPool();
156 m_lock=xercesc::XMLPlatformUtils::makeMutex();
158 // default registrations
159 XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());
161 QName q(XMLConstants::XMLSIG_NS,KeyInfo::LOCAL_NAME);
162 XMLObjectBuilder::registerBuilder(q,new KeyInfoBuilderImpl());
163 Validator::registerValidator(q,new KeyInfoSchemaValidator());
164 q=QName(XMLConstants::XMLSIG_NS,KeyInfo::TYPE_NAME);
165 XMLObjectBuilder::registerBuilder(q,new KeyInfoBuilderImpl());
166 Validator::registerValidator(q,new KeyInfoSchemaValidator());
168 q=QName(XMLConstants::XMLSIG_NS,KeyName::LOCAL_NAME);
169 XMLObjectBuilder::registerBuilder(q,new KeyNameBuilderImpl());
170 Validator::registerValidator(q,new KeyNameSchemaValidator());
172 q=QName(XMLConstants::XMLSIG_NS,MgmtData::LOCAL_NAME);
173 XMLObjectBuilder::registerBuilder(q,new MgmtDataBuilderImpl());
174 Validator::registerValidator(q,new MgmtDataSchemaValidator());
176 #ifndef XMLTOOLING_NO_XMLSEC
177 XMLObjectBuilder::registerBuilder(QName(XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME),new XMLSecSignatureBuilder());
180 REGISTER_EXCEPTION_FACTORY(XMLParserException);
181 REGISTER_EXCEPTION_FACTORY(XMLObjectException);
182 REGISTER_EXCEPTION_FACTORY(MarshallingException);
183 REGISTER_EXCEPTION_FACTORY(UnmarshallingException);
184 REGISTER_EXCEPTION_FACTORY(UnknownElementException);
185 REGISTER_EXCEPTION_FACTORY(UnknownAttributeException);
186 REGISTER_EXCEPTION_FACTORY(ValidationException);
187 REGISTER_EXCEPTION_FACTORY(SignatureException);
189 catch (const xercesc::XMLException&) {
190 log.fatal("caught exception while initializing Xerces");
194 log.info("library initialization complete");
198 void XMLToolingInternalConfig::term()
200 XMLObjectBuilder::destroyBuilders();
202 for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
204 FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
207 FreeLibrary(static_cast<HMODULE>(*i));
208 #elif defined(HAVE_DLFCN_H)
209 void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");
214 # error "Don't know about dynamic loading on this platform!"
217 m_libhandles.clear();
222 #ifndef XMLTOOLING_NO_XMLSEC
223 delete m_xsecProvider;
225 XSECPlatformUtils::Terminate();
228 xercesc::XMLPlatformUtils::closeMutex(m_lock);
230 xercesc::XMLPlatformUtils::Terminate();
233 xmltooling::NDC ndc("term");
235 Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");
238 ILockable& XMLToolingInternalConfig::lock()
240 xercesc::XMLPlatformUtils::lockMutex(m_lock);
244 void XMLToolingInternalConfig::unlock()
246 xercesc::XMLPlatformUtils::unlockMutex(m_lock);
249 bool XMLToolingInternalConfig::load_library(const char* path, void* context)
252 xmltooling::NDC ndc("LoadLibrary");
254 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");
255 log.info("loading extension: %s", path);
261 char* fixed=const_cast<char*>(path);
262 if (strchr(fixed,'/')) {
265 while (p=strchr(p,'/'))
269 UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);
271 handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
273 handle=LoadLibraryEx(fixed,NULL,0);
275 throw runtime_error(string("unable to load extension library: ") + fixed);
276 FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");
278 throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);
279 if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)
280 throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);
285 catch(runtime_error& e) {
295 #elif defined(HAVE_DLFCN_H)
296 void* handle=dlopen(path,RTLD_LAZY);
298 throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());
299 int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));
303 string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +
304 (dlerror() ? dlerror() : "unknown error")
309 throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);
311 catch(runtime_error& e) {
318 # error "Don't know about dynamic loading on this platform!"
320 m_libhandles.push_back(handle);
321 log.info("loaded extension: %s", path);