source: trunk/gsdl/src/colservr/collectset.cpp@ 2344

Last change on this file since 2344 was 2344, checked in by sjboddie, 23 years ago

Made the web library print some more reasonable debug info if gsdlhome
isn't set to a valid value

  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1/**********************************************************************
2 *
3 * collectset.cpp --
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * $Id: collectset.cpp 2344 2001-04-29 22:44:08Z sjboddie $
25 *
26 *********************************************************************/
27
28
29#include "collectserver.h"
30#include "filter.h"
31#include "browsefilter.h"
32#include "queryfilter.h"
33#include "infodbclass.h"
34#include "mgqueryfilter.h"
35#include "mgppqueryfilter.h"
36#include "mgsearch.h"
37#include "mgppsearch.h"
38#include "mggdbmsource.h"
39#include "fileutil.h"
40#include <assert.h>
41
42#include "colservrconfig.h"
43#include "recptconfig.h"
44#include "fileutil.h"
45#include "collectset.h"
46
47collectset::collectset (text_t &gsdlhome) {
48
49 text_tarray collections;
50 text_t collectdir;
51
52 // get gsdlhome (if we fail the error will be picked up later -- in
53 // cgiwrapper)
54 if (site_cfg_read (gsdlhome, httpdomain, httpprefix)) {
55 if (directory_exists(gsdlhome)) {
56 collectdir = filename_cat (gsdlhome, "collect");
57 if (read_dir (collectdir, collections)) {
58
59 text_tarray::const_iterator thiscol = collections.begin();
60 text_tarray::const_iterator endcol = collections.end();
61
62 while (thiscol != endcol) {
63 // ignore the modelcol
64 if (*thiscol == "modelcol") {
65 thiscol ++;
66 continue;
67 }
68
69 // read config file to see if built with mg or mgpp
70 text_t buildtype = "mg"; // mg is default
71 text_tarray cfgline;
72 text_t key;
73 text_t filename = filename_cat(collectdir, *thiscol, "index" , "build.cfg");
74 ifstream confin(filename.getcstr());
75
76 if (confin) {
77 while (read_cfg_line(confin, cfgline) >= 0) {
78 if (cfgline.size() ==2 ) {
79 key = cfgline[0];
80 cfgline.erase(cfgline.begin());
81 if (key =="buildtype") {
82 buildtype = cfgline[0];
83 break;
84 }
85 }
86 }
87 }
88
89 confin.close();
90
91 // this memory is created but never destroyed
92 // we're also not doing any error checking to make sure we didn't
93 // run out of memory
94 collectserver *cserver = new collectserver();
95 gdbmclass *gdbmhandler = new gdbmclass();
96
97 // add a null filter
98 filterclass *filter = new filterclass ();
99 cserver->add_filter (filter);
100
101 // add a browse filter
102 browsefilterclass *browsefilter = new browsefilterclass();
103 browsefilter->set_gdbmptr (gdbmhandler);
104 cserver->add_filter (browsefilter);
105
106 if (buildtype == "mg") {
107 mgsearchclass *mgsearch = new mgsearchclass();
108
109 // add a query filter
110 mgqueryfilterclass *queryfilter = new mgqueryfilterclass();
111 queryfilter->set_gdbmptr (gdbmhandler);
112 queryfilter->set_mgsearchptr (mgsearch);
113 cserver->add_filter (queryfilter);
114
115 // add a mg and gdbm source
116 mggdbmsourceclass *mggdbmsource = new mggdbmsourceclass ();
117 mggdbmsource->set_gdbmptr (gdbmhandler);
118 mggdbmsource->set_mgsearchptr (mgsearch);
119 cserver->add_source (mggdbmsource);
120 }
121#ifndef __WIN32__
122
123 else if (buildtype == "mgpp") {
124
125 mgppsearchclass *mgsearch = new mgppsearchclass();
126
127 // add a query filter
128 mgppqueryfilterclass *queryfilter = new mgppqueryfilterclass();
129 queryfilter->set_gdbmptr (gdbmhandler);
130 queryfilter->set_mgsearchptr (mgsearch);
131 cserver->add_filter (queryfilter);
132
133 // add a mg and gdbm source
134 mggdbmsourceclass *mggdbmsource = new mggdbmsourceclass ();
135 mggdbmsource->set_gdbmptr (gdbmhandler);
136 mggdbmsource->set_mgsearchptr (mgsearch);
137 cserver->add_source (mggdbmsource);
138 }
139#endif
140
141 // inform collection server and everything it contains about its
142 // collection name
143 cserver->configure ("collection", *thiscol);
144 // AZIZ: added on 10/10/00
145 // the cserver object does not have a reference to gsdlhome
146 cserver->configure ("gsdlhome", gsdlhome);
147
148 // GRB: removed proto.add_collectserver (cserver);
149 // GRB: added to build our own cservers list
150 cservers.addcollectserver (cserver);
151
152 thiscol ++;
153 }
154 }
155 }
156 }
157}
158
159collectset::~collectset () {
160 collectservermapclass::iterator here = cservers.begin();
161 collectservermapclass::iterator end = cservers.end();
162
163 while (here != end) {
164 if ((*here).second.c != NULL) {
165 delete (*here).second.c;
166 }
167 here ++;
168 }
169 cservers.clear();
170}
171
172bool collectset::init (ostream &logout) {
173 collectservermapclass::iterator here = cservers.begin();
174 collectservermapclass::iterator end = cservers.end();
175
176 while (here != end) {
177 assert ((*here).second.c != NULL);
178 if ((*here).second.c != NULL) {
179 const colservrconf &configinfo = (*here).second.c->get_configinfo ();
180
181 // configure this collection server
182
183 // note that we read build.cfg before collect.cfg so that the indexmaps
184 // are available to decode defaultindex, defaultsubcollection, and
185 // defaultlanguage
186 if (!build_cfg_read (*((*here).second.c), configinfo.gsdlhome,
187 configinfo.collection)) {
188 outconvertclass text_t2ascii;
189 logout << text_t2ascii
190 << "Warning: couldn't read build.cfg file for collection \"" //****
191 << configinfo.collection << "\", gsdlhome=\""
192 << configinfo.gsdlhome << "\"\n";
193 here ++;
194 continue;
195 }
196
197 if (!collect_cfg_read (*((*here).second.c), configinfo.gsdlhome,
198 configinfo.collection)) {
199 outconvertclass text_t2ascii;
200 logout << text_t2ascii
201 << "Warning: couldn't read collect.cfg file for collection \""
202 << configinfo.collection << "\", gsdlhome=\""
203 << configinfo.gsdlhome << "\"\n";
204 here ++;
205 continue;
206 }
207
208 if (!(*here).second.c->init (logout)) return false;
209
210 (*here).second.c->configure("httpdomain",httpdomain);
211 (*here).second.c->configure("httpprefix",httpprefix);
212 }
213 here++;
214 }
215
216 return true;
217}
218
219collectservermapclass collectset::servers()
220{ return cservers;
221}
222
223// add_collection sets up the collectionserver and calls
224// add_collectserver
225void collectset::add_collection (const text_t &collection, void *recpt,
226 const text_t &gsdlhome, const text_t &gdbmhome) {
227
228 // read config file to see if built with mg or mgpp
229 // -- we can rely on the collection (and therefore the build.cfg)
230 // being here since this is the null protocol - a nicer way to
231 // do this would be preferable though - Stefan.
232 text_t buildtype = "mg"; // mg is default
233 // (for now we'll just ignore mgpp if on windows)
234#ifndef __WIN32__
235 text_tarray cfgline;
236 text_t key;
237 text_t build_cfg = filename_cat(gsdlhome, "collect", collection, "index", "build.cfg");
238 char *build_cfgc = build_cfg.getcstr();
239 ifstream confin(build_cfgc);
240
241 if (confin) {
242 while (read_cfg_line(confin, cfgline) >= 0) {
243 if (cfgline.size() == 2) {
244 key = cfgline[0];
245 cfgline.erase(cfgline.begin());
246 if (key == "buildtype") {
247 buildtype = cfgline[0];
248 break;
249 }
250 }
251 }
252 confin.close();
253 }
254 delete build_cfgc;
255#endif
256
257 collectserver *cserver = new collectserver();
258 gdbmclass *gdbmhandler = new gdbmclass();
259
260 // add a null filter
261 filterclass *filter = new filterclass ();
262 cserver->add_filter (filter);
263
264 // add a browse filter
265 browsefilterclass *browsefilter = new browsefilterclass();
266 browsefilter->set_gdbmptr (gdbmhandler);
267
268 cserver->add_filter (browsefilter);
269
270 if (buildtype == "mg") {
271 mgsearchclass *mgsearch = new mgsearchclass();
272
273 // add a query filter
274 mgqueryfilterclass *queryfilter = new mgqueryfilterclass();
275 queryfilter->set_gdbmptr (gdbmhandler);
276 queryfilter->set_mgsearchptr (mgsearch);
277 cserver->add_filter (queryfilter);
278
279 // add a mg and gdbm source
280 mggdbmsourceclass *mggdbmsource = new mggdbmsourceclass ();
281 mggdbmsource->set_gdbmptr (gdbmhandler);
282 mggdbmsource->set_mgsearchptr (mgsearch);
283 cserver->add_source (mggdbmsource);
284 }
285
286#ifndef __WIN32__
287
288 else if (buildtype == "mgpp") {
289
290 mgppsearchclass *mgsearch = new mgppsearchclass();
291
292 // add a query filter
293 mgppqueryfilterclass *queryfilter = new mgppqueryfilterclass();
294 queryfilter->set_gdbmptr (gdbmhandler);
295 queryfilter->set_mgsearchptr (mgsearch);
296 cserver->add_filter (queryfilter);
297
298 // add a mg and gdbm source
299 mggdbmsourceclass *mggdbmsource = new mggdbmsourceclass ();
300 mggdbmsource->set_gdbmptr (gdbmhandler);
301 mggdbmsource->set_mgsearchptr (mgsearch);
302 cserver->add_source (mggdbmsource);
303
304 }
305#endif
306
307 // inform collection server and everything it contains about its
308 // collection name
309 cserver->configure ("collection", collection);
310
311 /* Removed from add_collection 24/11/2000; already done elsewhere in collectset.
312 // configure receptionist's collectinfo structure
313 text_tarray colinfo;
314 colinfo.push_back (collection);
315 colinfo.push_back (gsdlhome);
316 colinfo.push_back (gdbmhome);
317 */
318 cservers.addcollectserver (cserver);
319}
320
321// remove_collection deletes the collection server of collection.
322// This only needs to be called if a collectionserver is to be
323// removed while the library is running. The destructor function
324// cleans up all collectservers when the program exits.
325void collectset::remove_collection (const text_t &collection, ostream &logout) {
326 collectservermapclass::iterator here = cservers.begin();
327 collectservermapclass::iterator end = cservers.end();
328
329 while (here != end) {
330 if ((*here).second.c != NULL && (*here).first == collection) {
331 delete (*here).second.c;
332 cservers.erase (here);
333 return;
334 }
335 here ++;
336 }
337 outconvertclass text_t2ascii;
338 logout << text_t2ascii << "nullproto::remove_collection: failed to remove collectserver for "
339 << collection << "\n";
340}
341
342void collectset::configure(const text_t &key, const text_tarray &cfgline)
343{
344 if (key == "collection" || key == "collectdir") return;
345
346 collectservermapclass::iterator here = cservers.begin();
347 collectservermapclass::iterator end = cservers.end();
348
349 while (here != end) {
350 assert ((*here).second.c != NULL);
351 if ((*here).second.c != NULL) {
352 if (key == "collectinfo") {
353 if ((*here).first == cfgline[0]) {
354 (*here).second.c->configure ("gsdlhome", cfgline[1]);
355 (*here).second.c->configure ("gdbmhome", cfgline[2]);
356 }
357 } else {
358 (*here).second.c->configure (key, cfgline);
359 }
360 }
361
362 here++;
363 }
364}
365
366void collectset::getCollectionList (text_tarray &collist)
367{
368 collist.erase(collist.begin(),collist.end());
369
370 collectservermapclass::iterator here = cservers.begin();
371 collectservermapclass::iterator end = cservers.end();
372 while (here != end) {
373 assert ((*here).second.c != NULL);
374 if ((*here).second.c != NULL) {
375 collist.push_back ((*here).second.c->get_collection_name());
376 }
377 here++;
378 }
379}
380
381void collectset::setReceptionistServers(receptionist &recpt, text_t &gsdlhome)
382{
383 collectservermapclass::iterator here = cservers.begin();
384 collectservermapclass::iterator end = cservers.end();
385 while (here != end) {
386 assert ((*here).second.c != NULL);
387
388 text_tarray colinfo;
389 colinfo.push_back((*here).second.c->get_collection_name());
390 colinfo.push_back(gsdlhome);
391 colinfo.push_back(gsdlhome);
392 recpt.configure("collectinfo", colinfo);
393
394 here++;
395 }
396}
Note: See TracBrowser for help on using the repository browser.