metaproxy 1.22.4
router_flexml.cpp
Go to the documentation of this file.
1/* This file is part of Metaproxy.
2 Copyright (C) Index Data
3
4Metaproxy is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "config.hpp"
20#include <metaproxy/xmlutil.hpp>
21#include <string.h>
22#include "router_flexml.hpp"
23#include "factory_filter.hpp"
24#include "factory_static.hpp"
25
26#include <iostream>
27#include <map>
28#include <list>
29#include <yaz/log.h>
30#include <yaz/xml_include.h>
31
32#include <boost/shared_ptr.hpp>
33
34#include <libxml/xmlversion.h>
35#include <libxml/parser.h>
36#include <libxml/tree.h>
37
38namespace mp = metaproxy_1;
39
40namespace metaproxy_1 {
42 friend class RouterFleXML::Rep;
43 friend class RouterFleXML::Pos;
44 friend class RouterFleXML;
45 std::list<boost::shared_ptr<const mp::filter::Base> > m_list;
46 };
48 friend class RouterFleXML;
49 friend class RouterFleXML::Pos;
50 Rep();
51
52 void base(xmlDocPtr doc, mp::FactoryFilter &factory, bool test_only,
53 const char *file_include_path);
54
55 typedef std::map<std::string,
56 boost::shared_ptr<const mp::filter::Base > >
58
60
61 std::map<std::string,RouterFleXML::Route> m_routes;
62
63 std::string m_start_route;
64
65 std::string m_dl_path;
66
67 void parse_xml_config_dom(xmlDocPtr doc, bool test_only,
68 const char *file_include_path);
69
70 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node,
71 bool test_only, const char *file_include_path);
72 void parse_xml_filters1(xmlDocPtr doc, const xmlNode *node,
73 bool test_only, const char *file_include_path,
74 Route &route);
75 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node,
76 bool test_only, const char *file_include_path);
77 void check_routes_in_filters(const xmlNode *n);
78
80 private:
81 FactoryFilter *m_factory; // TODO shared_ptr
82 };
83
84 class RouterFleXML::Pos : public RoutePos {
85 public:
86 virtual const filter::Base *move(const char *route);
87 virtual RoutePos *clone();
88 virtual ~Pos();
90
91 std::map<std::string,
93 std::list<boost::shared_ptr <const mp::filter::Base> >::iterator m_filter_it;
94 };
95}
96
98 const xmlNode *node,
99 bool test_only,
100 const char *file_include_path)
101{
102 while (node && mp::xml::check_element_mp(node, "filter"))
103 {
104 const struct _xmlAttr *attr;
105 std::string id_value;
106 std::string type_value;
107 for (attr = node->properties; attr; attr = attr->next)
108 {
109 std::string name = std::string((const char *) attr->name);
110 std::string value;
111
112 if (attr->children && attr->children->type == XML_TEXT_NODE)
113 value = std::string((const char *)attr->children->content);
114
115 if (name == "id")
116 id_value = value;
117 else if (name == "type")
118 type_value = value;
119 else if (name == "base")
120 ;// Ignore XInclude base attribute.
121 else
122 throw mp::XMLError("Only attribute id or type allowed"
123 " in filter element. Got " + name);
124 }
125
126 if (!m_factory->exist(type_value))
127 {
128 yaz_log(YLOG_LOG, "Loading %s (dlpath %s)",
129 type_value.c_str(), m_dl_path.c_str());
130 m_factory->add_creator_dl(type_value, m_dl_path);
131 }
132 boost::shared_ptr<mp::filter::Base> fb(m_factory->create(type_value));
133
134 fb->configure(node, test_only, file_include_path);
135
136 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
137 throw mp::XMLError("Filter " + id_value + " already defined");
138
139 m_id_filter_map[id_value] = fb;
140
141 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
142 }
143}
144
146 const xmlNode *node,
147 bool test_only,
148 const char *file_include_path,
149 Route &route)
150{
151 while (node)
152 {
153 if (mp::xml::is_element_mp(node, "filters"))
154 {
155 const xmlNode* node1 =
156 mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
157
158 parse_xml_filters1(doc, node1, test_only, file_include_path, route);
159 }
160 else if (mp::xml::check_element_mp(node, "filter"))
161 {
162 const struct _xmlAttr *attr;
163 std::string refid_value;
164 std::string type_value;
165 for (attr = node->properties; attr; attr = attr->next)
166 {
167 std::string name = std::string((const char *) attr->name);
168 std::string value;
169
170 if (attr->children && attr->children->type == XML_TEXT_NODE)
171 value = std::string((const char *)attr->children->content);
172
173 if (name == "refid")
174 refid_value = value;
175 else if (name == "type")
176 type_value = value;
177 else if (name == "base")
178 ;// Ignore XInclude base attribute.
179 else
180 throw mp::XMLError("Only attribute 'refid' or 'type'"
181 " allowed for element 'filter'."
182 " Got " + name);
183 }
184 if (refid_value.length())
185 {
186 std::map<std::string,
187 boost::shared_ptr<const mp::filter::Base > >::iterator it;
188 it = m_id_filter_map.find(refid_value);
189 if (it == m_id_filter_map.end())
190 throw mp::XMLError("Unknown filter refid "
191 + refid_value);
192 else
193 route.m_list.push_back(it->second);
194 }
195 else if (type_value.length())
196 {
197 if (!m_factory->exist(type_value))
198 {
199 yaz_log(YLOG_LOG, "Loading %s (dlpath %s)",
200 type_value.c_str(), m_dl_path.c_str());
201 m_factory->add_creator_dl(type_value, m_dl_path);
202 }
203 boost::shared_ptr<mp::filter::Base>
204 filter_base(m_factory->create(type_value));
205
206 filter_base->configure(node, test_only, file_include_path);
207
208 route.m_list.push_back(filter_base);
209 }
210
211 }
212 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
213 }
214}
215
216
218 const xmlNode *node,
219 bool test_only,
220 const char *file_include_path)
221{
222 mp::xml::check_element_mp(node, "route");
223
224 while (mp::xml::is_element_mp(node, "route"))
225 {
226 const struct _xmlAttr *attr;
227 std::string id_value;
228 for (attr = node->properties; attr; attr = attr->next)
229 {
230 std::string name = std::string((const char *) attr->name);
231 std::string value;
232
233 if (attr->children && attr->children->type == XML_TEXT_NODE)
234 value = std::string((const char *)attr->children->content);
235
236 if (name == "id")
237 id_value = value;
238 else if (name == "base")
239 ;// Ignore XInclude base attribute.
240 else
241 throw mp::XMLError("Only attribute 'id' allowed for"
242 " element 'route'."
243 " Got " + name);
244 }
245
246 Route route;
247
248 // process <filter> / <filters> nodes in third level
249 const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
250
251
252
253 parse_xml_filters1(doc, node3, test_only, file_include_path, route);
254
255 std::map<std::string,RouterFleXML::Route>::iterator it;
256 it = m_routes.find(id_value);
257 if (it != m_routes.end())
258 throw mp::XMLError("Route id='" + id_value
259 + "' already exist");
260 else
261 m_routes[id_value] = route;
262 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
263 }
264}
265
267{
268 while (node)
269 {
270 if (mp::xml::is_element_mp(node, "filters"))
271 {
272 const xmlNode *n =
273 mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
275 }
276 else if (mp::xml::is_element_mp(node, "filter"))
277 {
278 const xmlNode *n =
279 mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
280 while (n)
281 {
282 const struct _xmlAttr *attr;
283 // we assume thar that route attribute is only at one level
284 // below filter.. At least that works for multi and virt_db.
285 for (attr = n->properties; attr; attr = attr->next)
286 {
287 if (!strcmp((const char *) attr->name, "route"))
288 {
289 std::string value;
290
291 if (attr->children && attr->children->type == XML_TEXT_NODE)
292 value = std::string((const char *)attr->children->content);
293
294 std::map<std::string,RouterFleXML::Route>::iterator it;
295 it = m_routes.find(value);
296 if (it == m_routes.end())
297 {
298 throw mp::XMLError("Route '" + value + "' does not exist");
299 }
300 }
301 }
302 n = mp::xml::jump_to_next(n, XML_ELEMENT_NODE);
303 }
304 }
305 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
306 }
307}
308
310 bool test_only,
311 const char *file_include_path)
312{
313 if (!doc)
314 throw mp::XMLError("Empty XML Document");
315
316 const xmlNode* root = xmlDocGetRootElement(doc);
317
318 if (file_include_path)
319 {
320 int r = yaz_xml_include_glob((xmlNode *) root, file_include_path,
321 YAZ_FILE_GLOB_FAIL_NOTEXIST);
322 if (r)
323 throw mp::XMLError("YAZ XML Include failed");
324 }
325
326 mp::xml::check_element_mp(root, "metaproxy");
327
328 const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
329
330 if (mp::xml::is_element_mp(node, "dlpath"))
331 {
332 m_dl_path = mp::xml::get_text(node);
333 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
334 }
335 // process <start> node which is expected first element node
336 if (mp::xml::check_element_mp(node, "start"))
337 {
338 const struct _xmlAttr *attr;
339 for (attr = node->properties; attr; attr = attr->next)
340 {
341 std::string name = std::string((const char *) attr->name);
342 std::string value;
343
344 if (attr->children && attr->children->type == XML_TEXT_NODE)
345 value = std::string((const char *)attr->children->content);
346
347 if (name == "route")
348 m_start_route = value;
349 else if (name == "base")
350 ;// Ignore XInclude base attribute.
351 else
352 throw mp::XMLError("Only attribute route allowed"
353 " in element 'start'. Got " + name);
354 }
355 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
356 }
357 // process <filters> node if given
358 if (mp::xml::is_element_mp(node, "filters"))
359 {
360 parse_xml_filters(doc, mp::xml::jump_to_children(node,
361 XML_ELEMENT_NODE),
362 test_only, file_include_path);
363
364 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
365 }
366 // process <routes> node which is expected third element node
367 mp::xml::check_element_mp(node, "routes");
368
369 parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE),
370 test_only, file_include_path);
371
372 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
373 if (node)
374 {
375 throw mp::XMLError("Unexpected element "
376 + std::string((const char *)node->name));
377 }
378
379 node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
380 while (node)
381 {
382 if (mp::xml::is_element_mp(node, "filters"))
384 mp::xml::jump_to_children(node,
385 XML_ELEMENT_NODE));
386 else if (mp::xml::is_element_mp(node, "routes"))
387 {
388 const xmlNode* n = mp::xml::jump_to_children(node,
389 XML_ELEMENT_NODE);
390 while (n)
391 {
392 if (mp::xml::is_element_mp(n, "route"))
393 {
395 mp::xml::jump_to_children(n, XML_ELEMENT_NODE));
396
397 }
398 n = mp::xml::jump_to_next(n, XML_ELEMENT_NODE);
399 }
400 }
401 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
402 }
403 std::map<std::string,RouterFleXML::Route>::iterator it;
404 it = m_routes.find(m_start_route);
405 if (it == m_routes.end())
406 throw mp::XMLError("Start route '" + m_start_route + "' not found");
407}
408
412
414 bool test_only, const char *file_include_path)
415{
416 m_factory = &factory;
417 m_start_route = "start";
418 parse_xml_config_dom(doc, test_only, file_include_path);
419}
420
422 bool test_only, const char *file_include_path)
423 : m_p(new Rep)
424{
425 m_p->base(doc, factory, test_only, file_include_path);
426}
427
429 bool test_only)
430 : m_p(new Rep)
431{
432 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
433 xmlconf.size());
434 if (!doc)
435 throw mp::XMLError("xmlParseMemory failed");
436 else
437 {
438 m_p->base(doc, factory, test_only, 0);
439 xmlFreeDoc(doc);
440 }
441}
442
446
447const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
448{
449 if (route && *route)
450 {
451 m_route_it = m_p->m_routes.find(route);
452 if (m_route_it == m_p->m_routes.end())
453 {
454 yaz_log(YLOG_FATAL, "no such route %s", route);
455 return 0;
456 }
457 m_filter_it = m_route_it->second.m_list.begin();
458 }
459 if (m_filter_it == m_route_it->second.m_list.end())
460 return 0;
461 const mp::filter::Base *f = (*m_filter_it).get();
462 m_filter_it++;
463 return f;
464}
465
466mp::RoutePos *mp::RouterFleXML::createpos() const
467{
469
470 p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
471 if (p->m_route_it == m_p->m_routes.end())
472 {
473 delete p;
474 return 0;
475 }
476 p->m_filter_it = p->m_route_it->second.m_list.begin();
477 p->m_p = m_p.get();
478 return p;
479}
480
482{
486 p->m_p = m_p;
487 return p;
488}
489
493
494
496{
497 std::map<std::string,RouterFleXML::Route>::iterator route_it;
498
499 route_it = m_p->m_routes.begin();
500 while (route_it != m_p->m_routes.end())
501 {
502 RouterFleXML::Route route = route_it->second;
503
504 std::list<boost::shared_ptr<const mp::filter::Base> >::iterator it;
505
506 for (it = route.m_list.begin(); it != route.m_list.end(); it++)
507 (*it)->start();
508 route_it++;
509 }
510}
511
513{
514 std::map<std::string,RouterFleXML::Route>::iterator route_it;
515
516 route_it = m_p->m_routes.begin();
517 while (route_it != m_p->m_routes.end())
518 {
519 RouterFleXML::Route route = route_it->second;
520
521 std::list<boost::shared_ptr<const mp::filter::Base> >::iterator it;
522
523 for (it = route.m_list.begin(); it != route.m_list.end(); it++)
524 (*it)->stop(signo);
525 route_it++;
526 }
527}
528
529
530/*
531 * Local variables:
532 * c-basic-offset: 4
533 * c-file-style: "Stroustrup"
534 * indent-tabs-mode: nil
535 * End:
536 * vim: shiftwidth=4 tabstop=8 expandtab
537 */
538
mp::RouterFleXML::Rep * m_p
virtual const filter::Base * move(const char *route)
std::list< boost::shared_ptr< constmp::filter::Base > >::iterator m_filter_it
std::map< std::string, RouterFleXML::Route >::iterator m_route_it
void base(xmlDocPtr doc, mp::FactoryFilter &factory, bool test_only, const char *file_include_path)
void check_routes_in_filters(const xmlNode *n)
void parse_xml_filters(xmlDocPtr doc, const xmlNode *node, bool test_only, const char *file_include_path)
void parse_xml_filters1(xmlDocPtr doc, const xmlNode *node, bool test_only, const char *file_include_path, Route &route)
void parse_xml_routes(xmlDocPtr doc, const xmlNode *node, bool test_only, const char *file_include_path)
std::map< std::string, boost::shared_ptr< const mp::filter::Base > > IdFilterMap
void parse_xml_config_dom(xmlDocPtr doc, bool test_only, const char *file_include_path)
std::map< std::string, RouterFleXML::Route > m_routes
std::list< boost::shared_ptr< const mp::filter::Base > > m_list
RouterFleXML(std::string xmlconf, metaproxy_1::FactoryFilter &factory, bool test_only)
boost::scoped_ptr< Rep > m_p
virtual RoutePos * createpos() const