Declare factory maps, map cleanup.
[shibboleth/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 "XMLObjectBuilder.h"\r
26 #include "io/Marshaller.h"\r
27 #include "io/Unmarshaller.h"\r
28 #include "util/NDC.h"\r
29 \r
30 #ifdef HAVE_DLFCN_H\r
31 # include <dlfcn.h>\r
32 #endif\r
33 \r
34 #include <log4cpp/Category.hh>\r
35 #include <log4cpp/PropertyConfigurator.hh>\r
36 #include <log4cpp/OstreamAppender.hh>\r
37 #include <xercesc/util/PlatformUtils.hpp>\r
38 #include <xsec/framework/XSECProvider.hpp>\r
39 \r
40 #include <stdexcept>\r
41 \r
42 using namespace log4cpp;\r
43 using namespace xmltooling;\r
44 using namespace std;\r
45 \r
46 namespace {\r
47    XMLToolingInternalConfig g_config;\r
48 }\r
49 \r
50 XMLToolingConfig& XMLToolingConfig::getConfig()\r
51 {\r
52     return g_config;\r
53 }\r
54 \r
55 bool XMLToolingInternalConfig::log_config(const char* config)\r
56 {\r
57     try {\r
58         if (!config && !*config)\r
59             config=getenv("XMLTOOLING_LOG_CONFIG");\r
60         if (!config && !*config)\r
61             config="WARN";\r
62         \r
63         bool level=false;\r
64         Category& root = Category::getRoot();\r
65         if (!strcmp(config,"DEBUG")) {\r
66             root.setPriority(Priority::DEBUG);\r
67             level=true;\r
68         }\r
69         else if (!strcmp(config,"INFO")) {\r
70             root.setPriority(Priority::INFO);\r
71             level=true;\r
72         }\r
73         else if (!strcmp(config,"NOTICE")) {\r
74             root.setPriority(Priority::NOTICE);\r
75             level=true;\r
76         }\r
77         else if (!strcmp(config,"WARN")) {\r
78             root.setPriority(Priority::WARN);\r
79             level=true;\r
80         }\r
81         else if (!strcmp(config,"ERROR")) {\r
82             root.setPriority(Priority::ERROR);\r
83             level=true;\r
84         }\r
85         else if (!strcmp(config,"CRIT")) {\r
86             root.setPriority(Priority::CRIT);\r
87             level=true;\r
88         }\r
89         else if (!strcmp(config,"ALERT")) {\r
90             root.setPriority(Priority::ALERT);\r
91             level=true;\r
92         }\r
93         else if (!strcmp(config,"EMERG")) {\r
94             root.setPriority(Priority::EMERG);\r
95             level=true;\r
96         }\r
97         else if (!strcmp(config,"FATAL")) {\r
98             root.setPriority(Priority::FATAL);\r
99             level=true;\r
100         }\r
101         if (level)\r
102             root.setAppender(new OstreamAppender("default",&cerr));\r
103         else\r
104             PropertyConfigurator::configure(config);\r
105     }\r
106     catch (const ConfigureFailure& e) {\r
107         Category::getInstance(XMLTOOLING_LOGCAT".Logging").crit("failed to initialize log4cpp: %s", e.what());\r
108         return false;\r
109     }\r
110     \r
111     return true;\r
112 }\r
113 \r
114 bool XMLToolingInternalConfig::init()\r
115 {\r
116 #ifdef _DEBUG\r
117     xmltooling::NDC ndc("init");\r
118 #endif\r
119     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");\r
120     try {\r
121         log.debug("library initialization started");\r
122 \r
123         xercesc::XMLPlatformUtils::Initialize();\r
124         log.debug("Xerces initialization complete");\r
125 \r
126         XSECPlatformUtils::Initialise();\r
127         //m_xsec=new XSECProvider();\r
128         log.debug("XMLSec initialization complete");\r
129         \r
130         m_lock=xercesc::XMLPlatformUtils::makeMutex();\r
131     }\r
132     catch (const xercesc::XMLException&) {\r
133         log.fatal("caught exception while initializing Xerces");\r
134         return false;\r
135     }\r
136 \r
137     log.info("library initialization complete");\r
138     return true;\r
139 }\r
140 \r
141 void XMLToolingInternalConfig::term()\r
142 {\r
143     for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {\r
144 #if defined(WIN32)\r
145         FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");\r
146         if (fn)\r
147             fn();\r
148         FreeLibrary(static_cast<HMODULE>(*i));\r
149 #elif defined(HAVE_DLFCN_H)\r
150         void (*fn)()=(void (*)())dlsym(*i,"xmltooling_extension_term");\r
151         if (fn)\r
152             fn();\r
153         dlclose(*i);\r
154 #else\r
155 # error "Don't know about dynamic loading on this platform!"\r
156 #endif\r
157     }\r
158     m_libhandles.clear();\r
159     \r
160     //delete m_xsec; m_xsec=NULL;\r
161     XSECPlatformUtils::Terminate();\r
162     xercesc::XMLPlatformUtils::closeMutex(m_lock);\r
163     xercesc::XMLPlatformUtils::Terminate();\r
164 \r
165  #ifdef _DEBUG\r
166     xmltooling::NDC ndc("term");\r
167 #endif\r
168    Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig").info("library shutdown complete");\r
169 }\r
170 \r
171 ILockable& XMLToolingInternalConfig::lock()\r
172 {\r
173     xercesc::XMLPlatformUtils::lockMutex(m_lock);\r
174     return *this;\r
175 }\r
176 \r
177 void XMLToolingInternalConfig::unlock()\r
178 {\r
179     xercesc::XMLPlatformUtils::unlockMutex(m_lock);\r
180 }\r
181 \r
182 bool XMLToolingInternalConfig::load_library(const char* path, void* context)\r
183 {\r
184 #ifdef _DEBUG\r
185     xmltooling::NDC ndc("LoadLibrary");\r
186 #endif\r
187     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLToolingConfig");\r
188     log.info("loading extension: %s", path);\r
189 \r
190     Locker locker(this);\r
191 \r
192 #if defined(WIN32)\r
193     HMODULE handle=NULL;\r
194     char* fixed=const_cast<char*>(path);\r
195     if (strchr(fixed,'/')) {\r
196         fixed=strdup(path);\r
197         char* p=fixed;\r
198         while (p=strchr(p,'/'))\r
199             *p='\\';\r
200     }\r
201 \r
202     UINT em=SetErrorMode(SEM_FAILCRITICALERRORS);\r
203     try {\r
204         handle=LoadLibraryEx(fixed,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);\r
205         if (!handle)\r
206              handle=LoadLibraryEx(fixed,NULL,0);\r
207         if (!handle)\r
208             throw runtime_error(string("unable to load extension library: ") + fixed);\r
209         FARPROC fn=GetProcAddress(handle,"xmltooling_extension_init");\r
210         if (!fn)\r
211             throw runtime_error(string("unable to locate xmltooling_extension_init entry point: ") + fixed);\r
212         if (reinterpret_cast<int(*)(void*)>(fn)(context)!=0)\r
213             throw runtime_error(string("detected error in xmltooling_extension_init: ") + fixed);\r
214         if (fixed!=path)\r
215             free(fixed);\r
216         SetErrorMode(em);\r
217     }\r
218     catch(runtime_error& e) {\r
219         log.error(e.what());\r
220         if (handle)\r
221             FreeLibrary(handle);\r
222         SetErrorMode(em);\r
223         if (fixed!=path)\r
224             free(fixed);\r
225         return false;\r
226     }\r
227 \r
228 #elif defined(HAVE_DLFCN_H)\r
229     void* handle=dlopen(path,RTLD_LAZY);\r
230     if (!handle)\r
231         throw runtime_error(string("unable to load extension library '") + path + "': " + dlerror());\r
232     int (*fn)(void*)=(int (*)(void*))(dlsym(handle,"xmltooling_extension_init"));\r
233     if (!fn) {\r
234         dlclose(handle);\r
235         throw runtime_error(\r
236             string("unable to locate xmltooling_extension_init entry point in '") + path + "': " +\r
237                 (dlerror() ? dlerror() : "unknown error")\r
238             );\r
239     }\r
240     try {\r
241         if (fn(context)!=0)\r
242             throw runtime_error(string("detected error in xmltooling_extension_init in ") + path);\r
243     }\r
244     catch(runtime_error& e) {\r
245         log.error(e.what());\r
246         if (handle)\r
247             dlclose(handle);\r
248         return false;\r
249     }\r
250 #else\r
251 # error "Don't know about dynamic loading on this platform!"\r
252 #endif\r
253     m_libhandles.push_back(handle);\r
254     log.info("loaded extension: %s", path);\r
255     return true;\r
256 }\r
257 \r
258 map<QName,XMLObjectBuilder*> XMLObjectBuilder::m_map;\r
259 \r
260 void XMLObjectBuilder::destroyBuilders()\r
261 {\r
262     for_each(m_map.begin(),m_map.end(),cleanup_pair<QName,XMLObjectBuilder>());\r
263 }\r
264 \r
265 map<QName,Marshaller*> Marshaller::m_map;\r
266 \r
267 void Marshaller::destroyMarshallers()\r
268 {\r
269     for_each(m_map.begin(),m_map.end(),cleanup_pair<QName,Marshaller>());\r
270 }\r
271 \r
272 map<QName,Unmarshaller*> Unmarshaller::m_map;\r
273 \r
274 void Unmarshaller::destroyUnmarshallers()\r
275 {\r
276     for_each(m_map.begin(),m_map.end(),cleanup_pair<QName,Unmarshaller>());\r
277 }\r