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