1 /* $Id: p2_config.cpp,v 1.2 2005-10-07 09:21:41 marc Exp $
2 Copyright (c) 1998-2005, Index Data.
4 This file is part of the yaz-proxy.
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include <yaz/options.h>
26 #include <yaz/diagbib1.h>
27 #include "p2_config.h"
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33 #include <libxml/xinclude.h>
34 #include <libxslt/xsltutils.h>
35 #include <libxslt/transform.h>
42 class P2_Config::Rep {
50 xmlNodePtr m_proxyPtr;
62 P2_Config::Rep::~Rep()
70 P2_Config::P2_Config()
73 m_client_idletime = 600;
77 m_target_idletime = 600;
82 bool P2_Config::parse_options(int argc, char **argv)
86 bool show_config = false;
87 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:n:h:XS",
88 argv, argc, &arg)) != -2)
93 if (m_listen_address.length())
95 yaz_log(YLOG_FATAL, "Multiple listener address given");
98 m_listen_address = arg;
104 if (m_xml_fname.length())
106 yaz_log(YLOG_FATAL, "Multiple -c options given");
109 if (!read_xml_config(arg))
116 m_client_idletime = atoi(arg);
122 m_max_clients = atoi(arg);
125 m_no_limit_files = atoi(arg);
128 m_no_threads = atoi(arg);
131 m_optimize_flags = arg;
134 if (m_pid_fname.length())
136 yaz_log(YLOG_LOG, "Multiple -p options given");
142 if (m_default_target.length())
144 yaz_log(YLOG_LOG, "Multiple -t options given");
147 m_default_target = arg;
150 m_target_idletime = atoi(arg);
155 yaz_log(YLOG_FATAL, "-u specified more than once");
161 yaz_log_init_level(yaz_log_mask_str(arg));
170 yaz_log(YLOG_FATAL, "Bad option %s", arg);
174 if (m_log_file.length())
175 yaz_log_init_file(m_log_file.c_str());
181 bool P2_Config::parse_xml_text(void *xml_ptr, bool &val)
184 if (!parse_xml_text(xml_ptr, v))
186 if (v.length() == 1 && v[0] == '1')
193 bool P2_Config::parse_xml_text(void *xml_ptr, string &val)
195 xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
198 for(ptr = ptr->children; ptr; ptr = ptr->next)
199 if (ptr->type == XML_TEXT_NODE)
201 xmlChar *t = ptr->content;
204 v += (const char *) t;
213 void P2_Config::parse_xml_element_target(void *xml_ptr,
216 xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
218 for (ptr = ptr->children; ptr; ptr = ptr->next)
220 if (ptr->type != XML_ELEMENT_NODE)
222 if (!strcmp((const char *) ptr->name, "url"))
224 parse_xml_text(ptr, t->m_target_address);
226 else if (!strcmp((const char *) ptr->name, "database"))
228 parse_xml_text(ptr, t->m_target_database);
232 yaz_log(YLOG_WARN, "Unknown element '%s' inside target",
233 (const char *) ptr->name);
239 void P2_Config::parse_xml_element_proxy(void *xml_ptr)
241 xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
243 for (ptr = ptr->children; ptr; ptr = ptr->next)
245 if (ptr->type != XML_ELEMENT_NODE)
247 if (!strcmp((const char *) ptr->name, "target"))
249 P2_ConfigTarget *t = new P2_ConfigTarget();
251 struct _xmlAttr *attr;
252 for (attr = ptr->properties; attr; attr = attr->next)
253 if (!strcmp((const char *) attr->name, "name")
254 || !strcmp((const char *) attr->name, "host"))
256 parse_xml_text(attr, t->m_virt_address);
258 else if (!strcmp((const char *) attr->name, "database"))
260 parse_xml_text(attr, t->m_virt_database);
262 else if (!strcmp((const char *) attr->name, "default"))
264 parse_xml_text(attr, t->m_default);
266 else if (!strcmp((const char *) attr->name, "type"))
268 parse_xml_text(attr, t->m_type);
272 yaz_log(YLOG_WARN, "Unknown attribute '%s' for "
274 (const char *) attr->name);
277 parse_xml_element_target(ptr, t);
278 m_target_list.push_back(t);
280 else if (!strcmp((const char *) ptr->name, "max-clients"))
283 if (parse_xml_text(ptr, v))
284 m_max_clients = atoi(v.c_str());
286 else if (!strcmp((const char *) ptr->name, "module"))
288 P2_ConfigModule *t = new P2_ConfigModule();
291 if (parse_xml_text(ptr, v))
294 m_modules.push_back(t);
299 yaz_log(YLOG_WARN, "Unknown element '%s' inside proxy", ptr->name);
305 void P2_Config::print()
307 cout << "max_clients=" << m_max_clients << endl;
308 list<P2_ConfigTarget *>::const_iterator it;
310 for (it = m_target_list.begin(); it != m_target_list.end(); it++)
312 cout << "type=" << (*it)->m_type << " ";
313 cout << "v-address=" << (*it)->m_virt_address << " ";
314 cout << "v-db=" << (*it)->m_virt_database << " ";
315 cout << "t-address=" << (*it)->m_target_address << " ";
316 cout << "t-db=" << (*it)->m_target_database << " ";
317 cout << "default=" << (*it)->m_default << endl;
321 bool P2_Config::read_xml_config(const char *fname)
323 xmlDocPtr ndoc = xmlParseFile(fname);
327 yaz_log(YLOG_WARN, "Config file %s not found or parse error", fname);
330 int noSubstitutions = xmlXIncludeProcess(ndoc);
331 if (noSubstitutions == -1)
332 yaz_log(YLOG_WARN, "XInclude processing failed on config %s", fname);
334 xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
335 if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
336 strcmp((const char *) proxyPtr->name, "proxy"))
338 yaz_log(YLOG_WARN, "No proxy element in %s", fname);
342 m_rep->m_proxyPtr = proxyPtr;
344 // OK: release previous and make it the current one.
346 xmlFreeDoc(m_rep->m_docPtr);
347 m_rep->m_docPtr = ndoc;
350 parse_xml_element_proxy(proxyPtr);
351 if (m_errors && !m_debug_mode)
356 P2_Config::~P2_Config()
361 P2_ConfigTarget::P2_ConfigTarget()
366 P2_ConfigTarget *P2_Config::find_target(string db)
368 list<P2_ConfigTarget *>::const_iterator it;
369 for (it = m_target_list.begin(); it != m_target_list.end(); it++)
371 if ((*it)->m_virt_database == db)
381 * indent-tabs-mode: nil
383 * vim: shiftwidth=4 tabstop=8 expandtab