The configure method takes test_only flag so we can avoid
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* $Id: metaproxy_prog.cpp,v 1.14 2008-02-20 15:07:52 adam Exp $
2    Copyright (c) 2005-2008, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy 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
9 version.
10
11 Metaproxy 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
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23
24 #include <yaz/log.h>
25 #include <yaz/options.h>
26 #include <yaz/daemon.h>
27
28 #include <iostream>
29 #include <stdexcept>
30 #include <libxml/xinclude.h>
31
32 #include "filter.hpp"
33 #include "package.hpp"
34 #include "router_flexml.hpp"
35 #include "factory_static.hpp"
36
37 namespace mp = metaproxy_1;
38
39 static void handler(void *data)
40 {
41     mp::RouterFleXML *routerp = (mp::RouterFleXML*) data;
42
43     mp::Package pack;
44     pack.router(*routerp).move();
45 }
46     
47 int main(int argc, char **argv)
48 {
49     try 
50     {
51         const char *fname = 0;
52         int ret;
53         char *arg;
54         unsigned mode = 0;
55         const char *pidfile = 0;
56         const char *uid = 0;
57
58         while ((ret = options("c{config}:Dh{help}l:p:u:V{version}X", 
59                               argv, argc, &arg)) != -2)
60         {
61             switch (ret)
62             {
63             case 'c':
64                 fname = arg;
65                 break;
66             case 'D':
67                 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
68                 break;
69             case 'h':
70                 std::cerr << "metaproxy\n"
71                     " -h|--help     help\n"
72                     " -V|--version  version\n"
73                     " -c|--config f config filename\n"
74                     " -D            daemon and keepalive operation\n"
75                     " -l f          log file f\n"
76                     " -p f          pid file f\n"
77                     " -u id         change uid to id\n"
78                     " -X            debug mode (no fork/daemon mode)\n"
79                           << std::endl;
80                 break;
81             case 'l':
82                 yaz_log_init_file(arg);
83                 yaz_log(YLOG_LOG, "Metaproxy " VERSION " started");
84                 break;
85             case 'p':
86                 pidfile = arg;
87                 break;
88             case 'u':
89                 uid = arg;
90                 break;
91             case 'V':
92                 std::cout << "Metaproxy " VERSION "\n";
93                 break;
94             case 'X':
95                 mode = YAZ_DAEMON_DEBUG;
96                 break;
97             case -1:
98                 std::cerr << "bad option: " << arg << std::endl;
99                 std::exit(1);
100             }
101         }
102         if (!fname)
103         {
104             std::cerr << "No configuration given\n";
105             std::exit(1);
106         }
107
108         xmlDocPtr doc = xmlReadFile(fname,
109                                     NULL, 
110                                     XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
111                                     + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
112         
113         if (!doc)
114         {
115             std::cerr << "XML parsing failed\n";
116             std::exit(1);
117         }
118         // and perform Xinclude then
119         if (xmlXIncludeProcess(doc) > 0) {
120             std::cerr << "processing XInclude directive\n";
121         }
122         mp::FactoryStatic factory;
123         mp::RouterFleXML router(doc, factory, false);
124
125         yaz_daemon("metaproxy", mode, handler, &router, pidfile, uid);
126     }
127     catch (std::logic_error &e) {
128         std::cerr << "std::logic error: " << e.what() << "\n";
129         std::exit(1);
130     }
131     catch (std::runtime_error &e) {
132         std::cerr << "std::runtime error: " << e.what() << "\n";
133         std::exit(1);
134     }
135     catch ( ... ) {
136         std::cerr << "Unknown Exception" << std::endl;
137         std::exit(1);
138     }
139     std::exit(0);
140 }
141
142
143 /*
144  * Local variables:
145  * c-basic-offset: 4
146  * indent-tabs-mode: nil
147  * c-file-style: "stroustrup"
148  * End:
149  * vim: shiftwidth=4 tabstop=8 expandtab
150  */