Fixes associated with wildcard test classes.
[shibboleth/cpp-xmltooling.git] / xmltooling / XMLToolingConfig.cpp
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * XMLToolingConfig.cpp\r
19  * \r
20  * Library configuration \r
21  */\r
22 \r
23 #include "internal.h"\r
24 #include "XMLToolingConfig.h"\r
25 #include "impl/UnknownElement.h"\r
26 #include "util/NDC.h"\r
27 \r
28 #ifdef HAVE_DLFCN_H\r
29 # include <dlfcn.h>\r
30 #endif\r
31 \r
32 #include <log4cpp/Category.hh>\r
33 #include <log4cpp/PropertyConfigurator.hh>\r
34 #include <log4cpp/OstreamAppender.hh>\r
35 #include <xercesc/util/PlatformUtils.hpp>\r
36 #include <xsec/framework/XSECProvider.hpp>\r
37 \r
38 #include <stdexcept>\r
39 \r
40 using namespace log4cpp;\r
41 using namespace xmltooling;\r
42 using namespace std;\r
43 \r
44 namespace {\r
45    XMLToolingInternalConfig g_config;\r
46 }\r
47 \r
48 XMLToolingConfig& XMLToolingConfig::getConfig()\r
49 {\r
50     return g_config;\r
51 }\r
52 \r
53 XMLToolingInternalConfig& XMLToolingInternalConfig::getInternalConfig()\r
54 {\r
55     return g_config;\r
56 }\r
57 \r
58 bool XMLToolingInternalConfig::log_config(const char* config)\r
59 {\r
60     try {\r
61         if (!config || !*config)\r
62             config=getenv("XMLTOOLING_LOG_CONFIG");\r
63         if (!config || !*config)\r
64             config="WARN";\r
65         \r
66         bool level=false;\r
67         Category& root = Category::getRoot();\r
68         if (!strcmp(config,"DEBUG")) {\r
69             root.setPriority(Priority::DEBUG);\r
70             level=true;\r
71         }\r
72         else if (!strcmp(config,"INFO")) {\r
73             root.setPriority(Priority::INFO);\r
74             level=true;\r
75         }\r
76         else if (!strcmp(config,"NOTICE")) {\r
77             root.setPriority(Priority::NOTICE);\r
78             level=true;\r
79         }\r
80         else if (!strcmp(config,"WARN")) {\r
81             root.setPriority(Priority::WARN);\r
82             level=true;\r
83         }\r
84         else if (!strcmp(config,"ERROR")) {\r
85             root.setPriority(Priority::ERROR);\r
86             level=true;\r
87         }\r
88         else if (!strcmp(config,"CRIT")) {\r
89             root.setPriority(Priority::CRIT);\r
90             level=true;\r
91         }\r
92         else if (!strcmp(config,"ALERT")) {\r
93             root.setPriority(Priority::ALERT);\r
94             level=true;\r
95         }\r
96         else if (!strcmp(config,"EMERG")) {\r
97             root.setPriority(Priority::EMERG);\r
98             level=true;\r
99         }\r
100         else if (!strcmp(config,"FATAL")) {\r
101             root.setPriority(Priority::FATAL);\r
102             level=true;\r
103         }\r
104         if (level)\r
105             root.setAppender(new OstreamAppender("default",&cerr));\r
106         else\r
107             PropertyConfigurator::configure(config);\r
108     }\r
109     catch (const ConfigureFailure& e) {\r
110         Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());\r
111         return false;\r
112     }\r
113     \r
114     return true;\r
115 }\r
116 \r
117 bool XMLToolingInternalConfig::init()\r
118 {\r
119 #ifdef _DEBUG\r
120     xmltooling::NDC ndc("init");\r
121 #endif\r
122     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");\r
123     try {\r
124         log.debug("library initialization started");\r
125 \r
126         xercesc::XMLPlatformUtils::Initialize();\r
127         log.debug("Xerces initialization complete");\r
128 \r
129         XSECPlatformUtils::Initialise();\r
130         //m_xsec=new XSECProvider();\r
131         log.debug("XMLSec initialization complete");\r
132         \r
133         m_parserPool=new ParserPool();\r
134         m_lock=xercesc::XMLPlatformUtils::makeMutex();\r
135 \r
136         // default registrations\r
137         XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder());\r
138         Marshaller::registerDefaultMarshaller(new UnknownElementMarshaller());\r
139         Unmarshaller::registerDefaultUnmarshaller(new UnknownElementUnmarshaller());\r
140     }\r
141     catch (const xercesc::XMLException&) {\r
142         log.fatal("caught exception while initializing Xerces");\r
143         return false;\r
144     }\r
145 \r
146     log.info("library initialization complete");\r
147     return true;\r
148 }\r
149 \r
150 void XMLToolingInternalConfig::term()\r
151 {\r
152     // default registrations\r
153     XMLObjectBuilder::deregisterDefaultBuilder();\r
154     Marshaller::deregisterDefaultMarshaller();\r
155     Unmarshaller::deregisterDefaultUnmarshaller();\r
156 \r
157     for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {\r
158 #if defined(WIN32)\r
159         FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");\r
160         if (fn)\r
161             fn();\r
162         FreeLibrary(static_cast<HMODULE>(*i));\r
163 #elif defined(HAVE_DLFCN_H)\r
164         void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");\r
165         if (fn)\r
166             fn();\r
167         dlclose(*i);\r
168 #else\r
169 # error "Don't know about dynamic loading on this platform!"\r
170 #endif\r
171     }\r
172     m_libhandles.clear();\r
173     \r
174     delete m_parserPool;\r
175     m_parserPool=NULL;\r
176 \r
177     //delete m_xsec; m_xsec=NULL;\r
178     XSECPlatformUtils::Terminate();\r
179     xercesc::XMLPlatformUtils::closeMutex(m_lock);\r
180     m_lock=NULL;\r
181     xercesc::XMLPlatformUtils::Terminate();\r
182 \r
183  #ifdef _DEBUG\r
184     xmltooling::NDC ndc("term");\r
185 #endif\r
186    Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");\r
187 }\r
188 \r
189 ILockable& XMLToolingInternalConfig::lock()\r
190 {\r
191     xercesc::XMLPlatformUtils::lockMutex(m_lock);\r
192     return *this;\r
193 }\r
194 \r
195 void XMLToolingInternalConfig::unlock()\r
196 {\r
197     xercesc::XMLPlatformUtils::unlockMutex(m_lock);\r
198 }\r
199 \r
200 bool XMLToolingInternalConfig::load_library(const char* path, void* context)\r
201 {\r
202 #ifdef _DEBUG\r
203     xmltooling::NDC ndc("LoadLibrary");\r
204 #endif\r
205     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");\r
206     log.info("loading extension: %s", path);\r
207 \r
208     Locker locker(this);\r
209 \r
210 #if defined(WIN32)\r
211     HMODULE handle=NULL;\r
212     char* fixed=const_cast<char*>(path);\r
213     if (strchr(fixed,'/')) {\r
214         fixed=strdup(path);\r
215         char* p=fixed;\r
216         while (p=strchr(p,'/'))\r
217             *p='\\';\r
218     }\r
219 \r
220     UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);\r
221     try {\r
222         handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);\r
223         if (!handle)\r
224              handle=LoadLibraryEx(fixed,NULL,0);\r
225         if (!handle)\r
226             throw runtime_error(string("unable to load extension library: ") + fixed);\r
227         FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");\r
228         if (!fn)\r
229             throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);\r
230         if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)\r
231             throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);\r
232         if (fixed!=path)\r
233             free(fixed);\r
234         SetErrorMode(em);\r
235     }\r
236     catch(runtime_error& e) {\r
237         log.error(e.what());\r
238         if (handle)\r
239             FreeLibrary(handle);\r
240         SetErrorMode(em);\r
241         if (fixed!=path)\r
242             free(fixed);\r
243         return false;\r
244     }\r
245 \r
246 #elif defined(HAVE_DLFCN_H)\r
247     void* handle=dlopen(path,RTLD_LAZY);\r
248     if (!handle)\r
249         throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());\r
250     int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));\r
251     if (!fn) {\r
252         dlclose(handle);\r
253         throw runtime_error(\r
254             string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +\r
255                 (dlerror() ? dlerror() : "unknown error")\r
256             );\r
257     }\r
258     try {\r
259         if (fn(context)!=0)\r
260             throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);\r
261     }\r
262     catch(runtime_error& e) {\r
263         log.error(e.what());\r
264         if (handle)\r
265             dlclose(handle);\r
266         return false;\r
267     }\r
268 #else\r
269 # error "Don't know about dynamic loading on this platform!"\r
270 #endif\r
271     m_libhandles.push_back(handle);\r
272     log.info("loaded extension: %s", path);\r
273     return true;\r
274 }\r