1 /* $Id: p2_config.cpp,v 1.1 2005-10-05 12:07:14 adam 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"
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32 #include <libxml/xinclude.h>
33 #include <libxslt/xsltutils.h>
34 #include <libxslt/transform.h>
41 class P2_Config::Rep {
49 xmlNodePtr m_proxyPtr;
61 P2_Config::Rep::~Rep()
69 P2_Config::P2_Config()
72 m_client_idletime = 600;
76 m_target_idletime = 600;
81 bool P2_Config::parse_options(int argc, char **argv)
85 bool show_config = false;
86 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:n:h:XS",
87 argv, argc, &arg)) != -2)
92 if (m_listen_address.length())
94 yaz_log(YLOG_FATAL, "Multiple listener address given");
97 m_listen_address = arg;
103 if (m_xml_fname.length())
105 yaz_log(YLOG_FATAL, "Multiple -c options given");
108 if (!read_xml_config(arg))
115 m_client_idletime = atoi(arg);
121 m_max_clients = atoi(arg);
124 m_no_limit_files = atoi(arg);
127 m_no_threads = atoi(arg);
130 m_optimize_flags = arg;
133 if (m_pid_fname.length())
135 yaz_log(YLOG_LOG, "Multiple -p options given");
141 if (m_default_target.length())
143 yaz_log(YLOG_LOG, "Multiple -t options given");
146 m_default_target = arg;
149 m_target_idletime = atoi(arg);
154 yaz_log(YLOG_FATAL, "-u specified more than once");
160 yaz_log_init_level(yaz_log_mask_str(arg));
169 yaz_log(YLOG_FATAL, "Bad option %s", arg);
173 if (m_log_file.length())
174 yaz_log_init_file(m_log_file.c_str());
180 bool P2_Config::parse_xml_text(void *xml_ptr, bool &val)
183 if (!parse_xml_text(xml_ptr, v))
185 if (v.length() == 1 && v[0] == '1')
192 bool P2_Config::parse_xml_text(void *xml_ptr, string &val)
194 xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
197 for(ptr = ptr->children; ptr; ptr = ptr->next)
198 if (ptr->type == XML_TEXT_NODE)
200 xmlChar *t = ptr->content;
203 v += (const char *) t;
212 void P2_Config::parse_xml_element_target(void *xml_ptr,
215 xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
217 for (ptr = ptr->children; ptr; ptr = ptr->next)
219 if (ptr->type != XML_ELEMENT_NODE)
221 if (!strcmp((const char *) ptr->name, "url"))
223 parse_xml_text(ptr, t->m_target_address);
225 else if (!strcmp((const char *) ptr->name, "database"))
227 parse_xml_text(ptr, t->m_target_database);
231 yaz_log(YLOG_WARN, "Unknown element '%s' inside target",
232 (const char *) ptr->name);
238 void P2_Config::parse_xml_element_proxy(void *xml_ptr)
240 xmlNodePtr ptr = (xmlNodePtr) xml_ptr;
242 for (ptr = ptr->children; ptr; ptr = ptr->next)
244 if (ptr->type != XML_ELEMENT_NODE)
246 if (!strcmp((const char *) ptr->name, "target"))
248 P2_ConfigTarget *t = new P2_ConfigTarget();
250 struct _xmlAttr *attr;
251 for (attr = ptr->properties; attr; attr = attr->next)
252 if (!strcmp((const char *) attr->name, "name")
253 || !strcmp((const char *) attr->name, "host"))
255 parse_xml_text(attr, t->m_virt_address);
257 else if (!strcmp((const char *) attr->name, "database"))
259 parse_xml_text(attr, t->m_virt_database);
261 else if (!strcmp((const char *) attr->name, "default"))
263 parse_xml_text(attr, t->m_default);
265 else if (!strcmp((const char *) attr->name, "type"))
267 parse_xml_text(attr, t->m_type);
271 yaz_log(YLOG_WARN, "Unknown attribute '%s' for "
273 (const char *) attr->name);
276 parse_xml_element_target(ptr, t);
277 m_target_list.push_back(t);
279 else if (!strcmp((const char *) ptr->name, "max-clients"))
282 if (parse_xml_text(ptr, v))
283 m_max_clients = atoi(v.c_str());
285 else if (!strcmp((const char *) ptr->name, "module"))
287 P2_ConfigModule *t = new P2_ConfigModule();
290 if (parse_xml_text(ptr, v))
293 m_modules.push_back(t);
298 yaz_log(YLOG_WARN, "Unknown element '%s' inside proxy", ptr->name);
304 void P2_Config::print()
306 cout << "max_clients=" << m_max_clients << endl;
307 list<P2_ConfigTarget *>::const_iterator it;
309 for (it = m_target_list.begin(); it != m_target_list.end(); it++)
311 cout << "type=" << (*it)->m_type << " ";
312 cout << "v-address=" << (*it)->m_virt_address << " ";
313 cout << "v-db=" << (*it)->m_virt_database << " ";
314 cout << "t-address=" << (*it)->m_target_address << " ";
315 cout << "t-db=" << (*it)->m_target_database << " ";
316 cout << "default=" << (*it)->m_default << endl;
320 bool P2_Config::read_xml_config(const char *fname)
322 xmlDocPtr ndoc = xmlParseFile(fname);
326 yaz_log(YLOG_WARN, "Config file %s not found or parse error", fname);
329 int noSubstitutions = xmlXIncludeProcess(ndoc);
330 if (noSubstitutions == -1)
331 yaz_log(YLOG_WARN, "XInclude processing failed on config %s", fname);
333 xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
334 if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
335 strcmp((const char *) proxyPtr->name, "proxy"))
337 yaz_log(YLOG_WARN, "No proxy element in %s", fname);
341 m_rep->m_proxyPtr = proxyPtr;
343 // OK: release previous and make it the current one.
345 xmlFreeDoc(m_rep->m_docPtr);
346 m_rep->m_docPtr = ndoc;
349 parse_xml_element_proxy(proxyPtr);
350 if (m_errors && !m_debug_mode)
355 P2_Config::~P2_Config()
360 P2_ConfigTarget::P2_ConfigTarget()
365 P2_ConfigTarget *P2_Config::find_target(string db)
367 list<P2_ConfigTarget *>::const_iterator it;
368 for (it = m_target_list.begin(); it != m_target_list.end(); it++)
370 if ((*it)->m_virt_database == db)
380 * indent-tabs-mode: nil
382 * vim: shiftwidth=4 tabstop=8 expandtab