1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
30 #include <yaz/yaz-util.h>
32 #include <yaz/snprintf.h>
33 #include <yaz/tpath.h>
34 #include <yaz/xml_include.h>
36 #include <sys/types.h>
43 #include "pazpar2_config.h"
44 #include "service_xslt.h"
51 NMEM nmem; /* for conf_config and servers memory */
52 struct conf_server *servers;
56 iochan_man_t iochan_man;
57 database_hosts_t database_hosts;
63 xsltStylesheetPtr xsp;
64 struct service_xslt *next;
67 static char *xml_context(const xmlNode *ptr, char *res, size_t len)
69 xmlAttr *attr = ptr->properties;
73 for (; attr; attr = attr->next)
75 size_t l = strlen((const char *) attr->name);
79 memcpy(res + off, attr->name, l);
82 while (ptr && ptr->type == XML_ELEMENT_NODE)
84 size_t l = strlen((const char *) ptr->name);
89 memcpy(res + off, ptr->name, l);
97 struct conf_service *service_init(struct conf_server *server,
98 int num_metadata, int num_sortkeys,
99 const char *service_id)
101 struct conf_service * service = 0;
102 NMEM nmem = nmem_create();
105 service = nmem_malloc(nmem, sizeof(struct conf_service));
107 service->ref_count = 1;
108 service->nmem = nmem;
110 service->databases = 0;
111 service->xslt_list = 0;
112 service->ccl_bibset = 0;
113 service->server = server;
114 service->session_timeout = 60; /* default session timeout */
115 service->z3950_session_timeout = 180;
116 service->z3950_operation_timeout = 30;
117 service->rank_cluster = 1;
118 service->rank_debug = 0;
119 service->rank_follow = 0.0;
120 service->rank_lead = 0.0;
121 service->rank_length = 2;
123 service->charsets = 0;
125 service->id = service_id ? nmem_strdup(nmem, service_id) : 0;
127 // Setup a dictionary from server.
128 service->dictionary = 0;
130 service->settings = nmem_malloc(nmem, sizeof(*service->settings));
131 service->settings->num_settings = PZ_MAX_EOF;
132 service->settings->settings = nmem_malloc(nmem, sizeof(struct setting*) * service->settings->num_settings);
133 memset(service->settings->settings, 0, sizeof(struct setting*) * service->settings->num_settings);
134 // inherit_server_settings_values(service);
138 service->num_metadata = num_metadata;
140 service->metadata = 0;
141 if (service->num_metadata)
144 sizeof(struct conf_metadata) * service->num_metadata);
145 service->num_sortkeys = num_sortkeys;
147 service->default_sort = nmem_strdup(nmem, "relevance");
148 service->sortkeys = 0;
149 if (service->num_sortkeys)
152 sizeof(struct conf_sortkey) * service->num_sortkeys);
158 static struct conf_metadata* conf_service_add_metadata(
159 struct conf_service *service,
162 enum conf_metadata_type type,
163 enum conf_metadata_merge merge,
164 enum conf_setting_type setting,
169 enum conf_metadata_mergekey mt,
170 const char *facetrule,
171 const char *limitmap,
172 const char *limitcluster
175 struct conf_metadata * md = 0;
176 NMEM nmem = service->nmem;
178 if (!service->metadata || !service->num_metadata
179 || field_id < 0 || !(field_id < service->num_metadata))
182 md = service->metadata + field_id;
183 assert(nmem && md && name);
185 md->name = nmem_strdup(nmem, name);
189 // enforcing that type_year is always range_merge
190 if (md->type == Metadata_type_year)
191 md->merge = Metadata_merge_range;
195 md->setting = setting;
197 md->termlist = termlist;
198 md->rank = nmem_strdup_null(nmem, rank);
199 md->sortkey_offset = sortkey_offset;
201 md->facetrule = nmem_strdup_null(nmem, facetrule);
202 md->limitmap = nmem_strdup_null(nmem, limitmap);
203 md->limitcluster = nmem_strdup_null(nmem, limitcluster);
207 static struct conf_sortkey *conf_service_add_sortkey(
208 struct conf_service *service,
211 enum conf_sortkey_type type)
213 struct conf_sortkey *sk = 0;
214 NMEM nmem = service->nmem;
216 if (!service->sortkeys || !service->num_sortkeys
217 || field_id < 0 || !(field_id < service->num_sortkeys))
220 sk = service->sortkeys + field_id;
222 assert(nmem && sk && name);
224 sk->name = nmem_strdup(nmem, name);
229 int conf_service_metadata_field_id(struct conf_service *service,
234 if (!service || !service->metadata || !service->num_metadata)
237 for (i = 0; i < service->num_metadata; i++)
238 if (!strcmp(name, (service->metadata[i]).name))
243 int conf_service_sortkey_field_id(struct conf_service *service,
248 if (!service || !service->sortkeys || !service->num_sortkeys)
251 for (i = 0; i < service->num_sortkeys; i++)
252 if (!strcmp(name, (service->sortkeys[i]).name))
257 static void conf_dir_path(struct conf_config *config, WRBUF w, const char *src)
259 if (config->confdir && wrbuf_len(config->confdir) > 0 &&
260 !yaz_is_abspath(src))
262 wrbuf_printf(w, "%s/%s", wrbuf_cstr(config->confdir), src);
268 void service_destroy(struct conf_service *service)
272 if (!pazpar2_decref(&service->ref_count, service->mutex))
274 service_xslt_destroy(service);
275 pp2_charset_fact_destroy(service->charsets);
276 ccl_qual_rm(&service->ccl_bibset);
277 yaz_mutex_destroy(&service->mutex);
278 nmem_destroy(service->nmem);
283 void service_incref(struct conf_service *service)
285 yaz_log(YLOG_LOG, "service_incref. p=%p cnt=%d", service,
287 pazpar2_incref(&service->ref_count, service->mutex);
290 static int parse_metadata(struct conf_service *service, xmlNode *n,
291 int *md_node, int *sk_node)
293 enum conf_metadata_type type = Metadata_type_generic;
294 enum conf_metadata_merge merge = Metadata_merge_no;
295 enum conf_setting_type setting = Metadata_setting_no;
296 enum conf_metadata_mergekey mergekey_type = Metadata_mergekey_no;
299 int sortkey_offset = 0;
300 xmlChar *xml_name = 0;
301 xmlChar *xml_brief = 0;
302 xmlChar *xml_sortkey = 0;
303 xmlChar *xml_merge = 0;
304 xmlChar *xml_type = 0;
305 xmlChar *xml_termlist = 0;
306 xmlChar *xml_rank = 0;
307 xmlChar *xml_setting = 0;
308 xmlChar *xml_mergekey = 0;
309 xmlChar *xml_limitmap = 0;
310 xmlChar *xml_limitcluster = 0;
311 xmlChar *xml_icu_chain = 0;
313 struct _xmlAttr *attr;
317 for (attr = n->properties; attr; attr = attr->next)
319 if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
320 attr->children && attr->children->type == XML_TEXT_NODE)
321 xml_name = attr->children->content;
322 else if (!xmlStrcmp(attr->name, BAD_CAST "brief") &&
323 attr->children && attr->children->type == XML_TEXT_NODE)
324 xml_brief = attr->children->content;
325 else if (!xmlStrcmp(attr->name, BAD_CAST "sortkey") &&
326 attr->children && attr->children->type == XML_TEXT_NODE)
327 xml_sortkey = attr->children->content;
328 else if (!xmlStrcmp(attr->name, BAD_CAST "merge") &&
329 attr->children && attr->children->type == XML_TEXT_NODE)
330 xml_merge = attr->children->content;
331 else if (!xmlStrcmp(attr->name, BAD_CAST "type") &&
332 attr->children && attr->children->type == XML_TEXT_NODE)
333 xml_type = attr->children->content;
334 else if (!xmlStrcmp(attr->name, BAD_CAST "termlist") &&
335 attr->children && attr->children->type == XML_TEXT_NODE)
336 xml_termlist = attr->children->content;
337 else if (!xmlStrcmp(attr->name, BAD_CAST "rank") &&
338 attr->children && attr->children->type == XML_TEXT_NODE)
339 xml_rank = attr->children->content;
340 else if (!xmlStrcmp(attr->name, BAD_CAST "setting") &&
341 attr->children && attr->children->type == XML_TEXT_NODE)
342 xml_setting = attr->children->content;
343 else if (!xmlStrcmp(attr->name, BAD_CAST "mergekey") &&
344 attr->children && attr->children->type == XML_TEXT_NODE)
345 xml_mergekey = attr->children->content;
346 else if (!xmlStrcmp(attr->name, BAD_CAST "facetrule") &&
347 attr->children && attr->children->type == XML_TEXT_NODE)
348 xml_icu_chain = attr->children->content;
349 else if (!xmlStrcmp(attr->name, BAD_CAST "limitmap") &&
350 attr->children && attr->children->type == XML_TEXT_NODE)
351 xml_limitmap = attr->children->content;
352 else if (!xmlStrcmp(attr->name, BAD_CAST "limitcluster") &&
353 attr->children && attr->children->type == XML_TEXT_NODE)
354 xml_limitcluster = attr->children->content;
357 yaz_log(YLOG_FATAL, "Unknown metadata attribute '%s'", attr->name);
362 // now do the parsing logic
365 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
370 if (!strcmp((const char *) xml_brief, "yes"))
372 else if (strcmp((const char *) xml_brief, "no"))
374 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
381 if (!strcmp((const char *) xml_termlist, "yes"))
383 else if (strcmp((const char *) xml_termlist, "no"))
385 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
392 if (!strcmp((const char *) xml_type, "generic"))
393 type = Metadata_type_generic;
394 else if (!strcmp((const char *) xml_type, "year"))
395 type = Metadata_type_year;
396 else if (!strcmp((const char *) xml_type, "date"))
397 type = Metadata_type_date;
401 "Unknown value for metadata/type: %s", xml_type);
408 if (!strcmp((const char *) xml_merge, "no"))
409 merge = Metadata_merge_no;
410 else if (!strcmp((const char *) xml_merge, "unique"))
411 merge = Metadata_merge_unique;
412 else if (!strcmp((const char *) xml_merge, "longest"))
413 merge = Metadata_merge_longest;
414 else if (!strcmp((const char *) xml_merge, "range"))
415 merge = Metadata_merge_range;
416 else if (!strcmp((const char *) xml_merge, "all"))
417 merge = Metadata_merge_all;
418 else if (!strcmp((const char *) xml_merge, "first"))
419 merge = Metadata_merge_first;
423 "Unknown value for metadata/merge: %s", xml_merge);
430 if (!strcmp((const char *) xml_setting, "no"))
431 setting = Metadata_setting_no;
432 else if (!strcmp((const char *) xml_setting, "postproc"))
433 setting = Metadata_setting_postproc;
434 else if (!strcmp((const char *) xml_setting, "parameter"))
435 setting = Metadata_setting_parameter;
439 "Unknown value for medadata/setting: %s", xml_setting);
444 // add a sortkey if so specified
445 if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
447 enum conf_sortkey_type sk_type;
448 if (merge == Metadata_merge_no)
451 "Can't specify sortkey on a non-merged field");
454 if (!strcmp((const char *) xml_sortkey, "numeric"))
455 sk_type = Metadata_sortkey_numeric;
456 else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
457 sk_type = Metadata_sortkey_skiparticle;
461 "Unknown sortkey in metadata element: %s",
465 sortkey_offset = *sk_node;
467 conf_service_add_sortkey(service, *sk_node,
468 (const char *) xml_name, sk_type);
476 if (!strcmp((const char *) xml_mergekey, "required"))
477 mergekey_type = Metadata_mergekey_required;
478 else if (!strcmp((const char *) xml_mergekey, "optional"))
479 mergekey_type = Metadata_mergekey_optional;
480 else if (!strcmp((const char *) xml_mergekey, "no"))
481 mergekey_type = Metadata_mergekey_no;
484 yaz_log(YLOG_FATAL, "Unknown value for mergekey: %s", xml_mergekey);
489 // metadata known, assign values
490 conf_service_add_metadata(service, *md_node,
491 (const char *) xml_name,
492 type, merge, setting,
494 (const char *) xml_rank, sortkey_offset,
496 (const char *) xml_icu_chain,
497 (const char *) xml_limitmap,
498 (const char *) xml_limitcluster);
503 static struct conf_service *service_create_static(struct conf_server *server,
505 const char *service_id)
511 struct conf_service *service = 0;
512 int num_metadata = 0;
513 int num_sortkeys = 0;
514 int got_settings = 0;
516 // count num_metadata and num_sortkeys
517 for (n = node->children; n; n = n->next)
518 if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
519 n->name, "metadata"))
521 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
523 if (sortkey && strcmp((const char *) sortkey, "no"))
528 service = service_init(server, num_metadata, num_sortkeys, service_id);
530 for (n = node->children; n; n = n->next)
532 if (n->type != XML_ELEMENT_NODE)
534 if (!strcmp((const char *) n->name, "timeout"))
536 xmlChar *src = xmlGetProp(n, (xmlChar *) "session");
539 service->session_timeout = atoi((const char *) src);
541 if (service->session_timeout < 9)
543 yaz_log(YLOG_FATAL, "session timeout out of range");
547 src = xmlGetProp(n, (xmlChar *) "z3950_operation");
550 service->z3950_operation_timeout = atoi((const char *) src);
552 if (service->z3950_session_timeout < 9)
554 yaz_log(YLOG_FATAL, "Z39.50 operation timeout out of range");
558 src = xmlGetProp(n, (xmlChar *) "z3950_session");
561 service->z3950_session_timeout = atoi((const char *) src);
563 if (service->z3950_session_timeout < 9)
565 yaz_log(YLOG_FATAL, "Z39.50 session timeout out of range");
570 else if (!strcmp((const char *) n->name, "ccldirective"))
574 if (!service->ccl_bibset)
575 service->ccl_bibset = ccl_qual_mk();
576 name = (char *) xmlGetProp(n, (xmlChar *) "name");
579 yaz_log(YLOG_FATAL, "ccldirective: missing @name");
582 value = (char *) xmlGetProp(n, (xmlChar *) "value");
586 yaz_log(YLOG_FATAL, "ccldirective: missing @value");
589 ccl_qual_add_special(service->ccl_bibset, name, value);
593 else if (!strcmp((const char *) n->name, "settings"))
595 else if (!strcmp((const char *) n->name, "icu_chain"))
597 if (!service->charsets)
598 service->charsets = pp2_charset_fact_create();
599 if (pp2_charset_fact_define(service->charsets, n, 0))
601 yaz_log(YLOG_FATAL, "ICU chain definition error");
605 else if (!strcmp((const char *) n->name, "relevance")
606 || !strcmp((const char *) n->name, "sort")
607 || !strcmp((const char *) n->name, "mergekey")
608 || !strcmp((const char *) n->name, "facet"))
611 if (!service->charsets)
612 service->charsets = pp2_charset_fact_create();
613 if (pp2_charset_fact_define(service->charsets,
614 n->children, (const char *) n->name))
616 yaz_log(YLOG_FATAL, "ICU chain definition error");
620 else if (!strcmp((const char *) n->name, (const char *) "metadata"))
622 if (parse_metadata(service, n, &md_node, &sk_node))
625 else if (!strcmp((const char *) n->name, (const char *) "xslt"))
627 if (service_xslt_config(service, n))
630 else if (!strcmp((const char *) n->name, "rank"))
632 char *rank_cluster = (char *) xmlGetProp(n, (xmlChar *) "cluster");
633 char *rank_debug = (char *) xmlGetProp(n, (xmlChar *) "debug");
634 char *rank_follow = (char *) xmlGetProp(n, (xmlChar *) "follow");
635 char *rank_lead = (char *) xmlGetProp(n, (xmlChar *) "lead");
636 char *rank_length= (char *) xmlGetProp(n, (xmlChar *) "length");
639 if (!strcmp(rank_cluster, "yes"))
640 service->rank_cluster = 1;
641 else if (!strcmp(rank_cluster, "no"))
642 service->rank_cluster = 0;
645 yaz_log(YLOG_FATAL, "service: rank@cluster boolean");
651 if (!strcmp(rank_debug, "yes"))
652 service->rank_debug = 1;
653 else if (!strcmp(rank_debug, "no"))
654 service->rank_debug = 0;
657 yaz_log(YLOG_FATAL, "service: rank@debug boolean");
663 service->rank_follow = atof(rank_follow);
667 service->rank_lead = atof(rank_lead);
671 if (!strcmp(rank_length, "linear"))
672 service->rank_length = 2;
673 else if (!strcmp(rank_length, "log"))
674 service->rank_length = 1;
675 else if (!strcmp(rank_length, "none"))
676 service->rank_length = 0;
679 yaz_log(YLOG_FATAL, "service: rank@length linear|log|none");
683 xmlFree(rank_cluster);
685 xmlFree(rank_follow);
687 xmlFree(rank_length);
689 else if (!strcmp((const char *) n->name, "sort-default"))
691 char *default_sort = (char *) xmlGetProp(n, (xmlChar *) "field");
693 if (default_sort && strcmp(default_sort, "")) {
694 service->default_sort = nmem_strdup(service->nmem, default_sort);
695 yaz_log(YLOG_LOG, "service %s: default sort order configured to: %s",
696 service_id ? service_id : "unnamed", default_sort);
700 yaz_log(YLOG_FATAL, "default sort order is invalid: %s", default_sort);
703 xmlFree(default_sort);
708 yaz_log(YLOG_FATAL, "Bad element: %s . Context: %s", n->name,
709 xml_context(n, tmp, sizeof tmp));
716 /* metadata has been read.. Consider now settings */
717 init_settings(service);
718 for (pass = 1; pass <= 2; pass++)
720 for (n = node->children; n; n = n->next)
722 if (n->type != XML_ELEMENT_NODE)
724 if (!strcmp((const char *) n->name, "settings"))
727 xmlChar *src = xmlGetProp(n, (xmlChar *) "src");
730 WRBUF w = wrbuf_alloc();
731 conf_dir_path(server->config, w, (const char *) src);
732 ret = settings_read_file(service, wrbuf_cstr(w), pass);
738 ret = settings_read_node(service, n, pass);
749 static int inherit_server_settings(struct conf_service *s)
752 struct conf_server *server = s->server;
753 if (!s->dictionary) /* service has no config settings ? */
755 if (server->settings_fname)
757 /* inherit settings from server */
759 if (settings_read_file(s, server->settings_fname, 1))
761 if (settings_read_file(s, server->settings_fname, 2))
766 yaz_log(YLOG_WARN, "server '%s' has no settings", s->id ? s->id : "unnamed");
771 /* use relevance/sort/mergekey/facet from server if not defined
772 for this service.. */
775 if (server->charsets)
777 s->charsets = server->charsets;
778 pp2_charset_fact_incref(s->charsets);
782 s->charsets = pp2_charset_fact_create();
788 struct conf_service *service_create(struct conf_server *server,
791 struct conf_service *service = service_create_static(server, node, 0);
794 inherit_server_settings(service);
795 assert(service->mutex == 0);
796 pazpar2_mutex_create(&service->mutex, "conf");
801 static struct conf_server *server_create(struct conf_config *config,
802 NMEM nmem, xmlNode *node)
805 struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server));
806 xmlChar *server_id = xmlGetProp(node, (xmlChar *) "id");
810 server->proxy_host = 0;
811 server->proxy_port = 0;
814 server->config = config;
816 server->charsets = 0;
817 server->http_server = 0;
818 server->iochan_man = 0;
819 server->database_hosts = config->database_hosts;
820 server->settings_fname = 0;
824 server->server_id = nmem_strdup(nmem, (const char *)server_id);
828 server->server_id = 0;
829 for (n = node->children; n; n = n->next)
831 if (n->type != XML_ELEMENT_NODE)
833 if (!strcmp((const char *) n->name, "listen"))
835 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
836 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
839 server->port = nmem_strdup(nmem, (const char *) port);
841 server->host = nmem_strdup(nmem, (const char *) host);
846 else if (!strcmp((const char *) n->name, "proxy"))
848 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
849 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
850 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
852 server->proxy_port = atoi((const char *) port);
854 server->proxy_host = nmem_strdup(nmem, (const char *) host);
856 server->myurl = nmem_strdup(nmem, (const char *) myurl);
861 else if (!strcmp((const char *) n->name, "settings"))
863 xmlChar *src = xmlGetProp(n, (xmlChar *) "src");
867 yaz_log(YLOG_FATAL, "Missing src attribute for settings");
870 if (server->settings_fname)
873 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
877 conf_dir_path(config, w, (const char *) src);
878 server->settings_fname = nmem_strdup(nmem, wrbuf_cstr(w));
882 else if (!strcmp((const char *) n->name, "icu_chain"))
884 if (!server->charsets)
885 server->charsets = pp2_charset_fact_create();
886 if (pp2_charset_fact_define(server->charsets, n, 0))
888 yaz_log(YLOG_FATAL, "ICU chain definition error");
892 else if (!strcmp((const char *) n->name, "relevance")
893 || !strcmp((const char *) n->name, "sort")
894 || !strcmp((const char *) n->name, "mergekey")
895 || !strcmp((const char *) n->name, "facet"))
897 if (!server->charsets)
898 server->charsets = pp2_charset_fact_create();
899 if (pp2_charset_fact_define(server->charsets,
900 n->children, (const char *) n->name))
902 yaz_log(YLOG_FATAL, "ICU chain definition error");
906 else if (!strcmp((const char *) n->name, "service"))
908 char *service_id = (char *)
909 xmlGetProp(n, (xmlChar *) "id");
911 struct conf_service **sp = &server->service;
912 for (; *sp; sp = &(*sp)->next)
913 if ((*sp)->id && service_id &&
914 0 == strcmp((*sp)->id, service_id))
916 yaz_log(YLOG_FATAL, "Duplicate service: %s", service_id);
919 else if (!(*sp)->id && !service_id)
921 yaz_log(YLOG_FATAL, "Duplicate unnamed service");
925 if (*sp) /* service already exist */
932 struct conf_service *s = service_create_static(server, n,
942 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
948 yaz_log(YLOG_FATAL, "No listening port given");
953 struct conf_service *s;
954 for (s = server->service; s; s = s->next)
955 inherit_server_settings(s);
960 WRBUF conf_get_fname(struct conf_config *config, const char *fname)
962 WRBUF w = wrbuf_alloc();
964 conf_dir_path(config, w, fname);
968 struct conf_service *locate_service(struct conf_server *server,
969 const char *service_id)
971 struct conf_service *s = server->service;
972 for (; s; s = s->next)
973 if (s->id && service_id && 0 == strcmp(s->id, service_id))
975 else if (!s->id && !service_id)
982 void info_services(struct conf_server *server, WRBUF w)
984 struct conf_service *s = server->service;
985 wrbuf_puts(w, " <services>\n");
986 for (; s; s = s->next)
988 wrbuf_puts(w, " <service");
991 wrbuf_puts(w, " id=\"");
992 wrbuf_xmlputs(w, s->id);
999 wrbuf_puts(w, " </services>\n");
1002 static int parse_config(struct conf_config *config, xmlNode *root)
1006 for (n = root->children; n; n = n->next)
1008 if (n->type != XML_ELEMENT_NODE)
1010 if (!strcmp((const char *) n->name, "server"))
1012 struct conf_server *tmp = server_create(config, config->nmem, n);
1015 tmp->next = config->servers;
1016 config->servers = tmp;
1018 else if (!strcmp((const char *) n->name, "threads"))
1020 xmlChar *number = xmlGetProp(n, (xmlChar *) "number");
1023 config->no_threads = atoi((const char *) number);
1027 else if (!strcmp((const char *) n->name, "targetprofiles"))
1029 yaz_log(YLOG_FATAL, "targetprofiles unsupported here. Must be part of service");
1035 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
1042 struct conf_config *config_create(const char *fname, int verbose)
1044 xmlDoc *doc = xmlParseFile(fname);
1048 NMEM nmem = nmem_create();
1049 struct conf_config *config = nmem_malloc(nmem, sizeof(struct conf_config));
1051 xmlSubstituteEntitiesDefault(1);
1052 xmlLoadExtDtdDefaultValue = 1;
1055 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
1060 config->nmem = nmem;
1061 config->servers = 0;
1062 config->no_threads = 0;
1063 config->iochan_man = 0;
1064 config->database_hosts = database_hosts_create();
1066 config->confdir = wrbuf_alloc();
1067 if ((p = strrchr(fname,
1075 int len = p - fname;
1076 wrbuf_write(config->confdir, fname, len);
1078 wrbuf_puts(config->confdir, "");
1080 n = xmlDocGetRootElement(doc);
1081 r = yaz_xml_include_simple(n, wrbuf_cstr(config->confdir));
1082 if (r == 0) /* OK */
1086 yaz_log(YLOG_LOG, "Configuration %s after include processing",
1088 #if LIBXML_VERSION >= 20600
1089 xmlDocFormatDump(yaz_log_file(), doc, 0);
1091 xmlDocDump(yaz_log_file(), doc);
1094 r = parse_config(config, n);
1100 config_destroy(config);
1106 void server_destroy(struct conf_server *server)
1108 struct conf_service *s = server->service;
1111 struct conf_service *s_next = s->next;
1115 pp2_charset_fact_destroy(server->charsets);
1116 yaz_log(YLOG_LOG, "server_destroy server=%p", server);
1117 http_server_destroy(server->http_server);
1120 void config_destroy(struct conf_config *config)
1124 struct conf_server *server = config->servers;
1125 iochan_man_destroy(&config->iochan_man);
1128 struct conf_server *s_next = server->next;
1129 server_destroy(server);
1131 database_hosts_destroy(&config->database_hosts);
1133 wrbuf_destroy(config->confdir);
1134 nmem_destroy(config->nmem);
1138 void config_stop_listeners(struct conf_config *conf)
1140 struct conf_server *ser;
1141 for (ser = conf->servers; ser; ser = ser->next)
1142 http_close_server(ser);
1145 void config_process_events(struct conf_config *conf)
1147 struct conf_server *ser;
1149 for (ser = conf->servers; ser; ser = ser->next)
1151 struct conf_service *s = ser->service;
1153 for (;s ; s = s->next)
1155 assert(s->mutex == 0);
1156 pazpar2_mutex_create(&s->mutex, "service");
1158 http_mutex_init(ser);
1160 iochan_man_events(conf->iochan_man);
1163 int config_start_listeners(struct conf_config *conf,
1164 const char *listener_override,
1165 const char *record_fname)
1167 struct conf_server *ser;
1169 conf->iochan_man = iochan_man_create(conf->no_threads);
1170 for (ser = conf->servers; ser; ser = ser->next)
1175 ser->iochan_man = conf->iochan_man;
1176 if (listener_override)
1178 const char *cp = strrchr(listener_override, ':');
1181 ser->host = nmem_strdupn(conf->nmem, listener_override,
1182 cp - listener_override);
1183 ser->port = nmem_strdup(conf->nmem, cp + 1);
1188 ser->port = nmem_strdup(conf->nmem, listener_override);
1190 listener_override = 0; /* only first server is overriden */
1192 r = http_init(ser, record_fname);
1197 if (ser->proxy_host || ser->proxy_port)
1199 if (ser->proxy_host)
1200 wrbuf_puts(w, ser->proxy_host);
1201 if (ser->proxy_port)
1205 wrbuf_printf(w, "%d", ser->proxy_port);
1209 http_set_proxyaddr(wrbuf_cstr(w), ser);
1218 * c-file-style: "Stroustrup"
1219 * indent-tabs-mode: nil
1221 * vim: shiftwidth=4 tabstop=8 expandtab