source: main/trunk/greenstone2/common-src/src/lib/gsdlsitecfg.cpp

Last change on this file was 29249, checked in by ak19, 10 years ago

Outstanding commit before this machine gets updated from research to scratch. Removing unncessary debug statement.

  • Property svn:executable set to *
File size: 8.2 KB
Line 
1/**********************************************************************
2 *
3 * gsdlsitecfg.cpp --
4 * Copyright (C) 2008 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 *********************************************************************/
25
26#include "fileutil.h"
27#include "gsdlsitecfg.h"
28#include <stdlib.h> // for get/putenv
29#include <string.h> // for get/putenv
30
31#if defined(GSDL_USE_OBJECTSPACE)
32# include <ospace/std/iostream>
33# include <ospace/std/fstream>
34#elif defined(GSDL_USE_IOS_H)
35# include <iostream.h>
36# include <fstream.h>
37#else
38# include <iostream>
39# include <fstream>
40#endif
41
42
43class __site_configuration : public configurable {
44private:
45 text_t *gsdlhome;
46 text_t *collecthome;
47 text_t *httpdomain;
48 text_t *httpprefix;
49 text_t *collection;
50 text_tset *actions;
51 text_tset *browsers;
52 int *maxrequests;
53public:
54 __site_configuration (text_t *_gsdlhome, text_t* _collecthome, int *_maxrequests) {
55 gsdlhome = _gsdlhome;
56 collecthome = _collecthome;
57 httpdomain = NULL;
58 httpprefix = NULL;
59 collection = NULL;
60 actions = NULL;
61 browsers = NULL;
62 maxrequests = _maxrequests;
63 }
64
65 __site_configuration (text_t *_gsdlhome, text_t* _collecthome,
66 text_t *_httpdomain, text_t *_httpprefix) {
67 gsdlhome = _gsdlhome;
68 collecthome = _collecthome;
69 httpdomain = _httpdomain;
70 httpprefix = _httpprefix;
71 collection = NULL;
72 actions = NULL;
73 browsers = NULL;
74 maxrequests = NULL;
75 }
76
77 __site_configuration (text_t *_gsdlhome, text_t* _collecthome,
78 text_t *_httpdomain,
79 text_t *_httpprefix, text_t *_collection) {
80 gsdlhome = _gsdlhome;
81 collecthome = _collecthome;
82 httpdomain = _httpdomain;
83 httpprefix = _httpprefix;
84 collection = _collection;
85 actions = NULL;
86 browsers = NULL;
87 maxrequests = NULL;
88 }
89
90 __site_configuration (text_t *_httpprefix, text_tset *_actions, text_tset *_browsers) {
91 gsdlhome = NULL;
92 collecthome = NULL;
93 httpdomain = NULL;
94 httpprefix = _httpprefix;
95 collection = NULL;
96 actions = _actions;
97 browsers = _browsers;
98 maxrequests = NULL;
99 }
100
101 inline void configure (const text_t &key, const text_tarray &cfgline) {
102 if (gsdlhome != NULL && key == "gsdlhome") {
103 *gsdlhome = cfgline[0];
104
105 if ((collecthome != NULL) && (collecthome->empty())) {
106 // defaults to <gsdlhome>/collect
107 *collecthome = filename_cat(*gsdlhome,"collect");
108 }
109 }
110
111 if (collecthome != NULL && key == "collecthome") {
112 if (!collecthome->empty()) {
113 // if it has been previously set to default, free and then reassign
114 collecthome->clear();
115 }
116 *collecthome = cfgline[0];
117 }
118
119 if (httpprefix != NULL && key == "httpprefix") {
120 *httpprefix = cfgline[0];
121 }
122
123 if (httpdomain != NULL && key == "httpdomain") {
124 *httpdomain = cfgline[0];
125 }
126
127 if (collection != NULL && key == "collection") {
128 *collection = cfgline[0];
129 }
130
131 if(key == "javahome") {
132
133 // javahome is a special variable that may or may not be set by the user in gsdlsite.cfg.
134 // It specifies a custom java install that the user wants to use. If set, assume this is
135 // meant to override the default JAVA_HOME. Any javahome read in from gsdlsite.cfg
136 // will not be stored into the __site_configuration struct however. Instead, directly
137 // do putenv(javahome), just like gtiaction.cpp uses putenv, after printing out a warning
138 // to StdErr if getenv(JAVA_HOME) shows that JAVA_HOME is already set to something.
139
140 text_t javahome(cfgline[0]);
141 text_t oldjavahome = "";
142
143 // getenv returns NULL if not set, http://www.cplusplus.com/reference/cstdlib/getenv/
144 // 'The string pointed by the pointer returned by this function shall not be modified by the program.'
145 char* existing_javahome = getenv("JAVA_HOME");
146
147 if(existing_javahome != NULL) {
148 oldjavahome += existing_javahome;
149 if(javahome != oldjavahome) {
150 // cerr goes to: apache-httpd/<OS>/logs/error_log
151 cerr << "Warning: JAVA_HOME is already set to: " << existing_javahome << endl;
152 cerr << "But overwriting with javahome set in gsdlsite.cfg: " << javahome << endl;
153 }
154 }
155
156 if(javahome != oldjavahome) {
157 // http://www.kev.pulo.com.au/pp/RESOURCES/cplusplus/ref/cstdlib/putenv.html
158 // putenv is not defined in ANSI-C, but is supported by many compilers, and is
159 // already used in gtiaction.cpp
160 javahome = "JAVA_HOME=" + javahome;
161 char* set_javahome_cstr = javahome.getcstr();
162 putenv(set_javahome_cstr);
163
164 // can't use locally declared char array that we string-copy the cstr into,
165 // since the array's value expires from putenv, possibly after this function's local scope
166 // Forced to assign the dynamically allocated cstr and resist the urge to delete this:
167 //delete[] set_javahome_cstr; // may not delete it, else the env var just set will become empty
168 }
169 }
170
171 if (actions != NULL && key == "actions") {
172 actions->clear();
173 text_tarray::const_iterator thisAction = cfgline.begin();
174 text_tarray::const_iterator endAction = cfgline.end();
175 while (thisAction != endAction) {
176 actions->insert(*thisAction);
177 ++thisAction;
178 }
179 }
180
181 if (browsers != NULL && key == "browsers") {
182 browsers->clear();
183 text_tarray::const_iterator thisBrowser = cfgline.begin();
184 text_tarray::const_iterator endBrowser = cfgline.end();
185 while (thisBrowser != endBrowser) {
186 browsers->insert(*thisBrowser);
187 ++thisBrowser;
188 }
189 }
190
191 if (maxrequests != NULL && key == "maxrequests") {
192 *maxrequests = cfgline[0].getint();
193 if ((*maxrequests) < 1) {
194 *maxrequests = 1;
195 }
196 }
197 }
198};
199
200
201// reads site configuration file returning true on success
202// also sets gsdlhome and maxrequests
203// gsdlsite.cfg should be in same directory as library executable
204bool site_cfg_read (configurator gsdlconfigurator, text_t &gsdlhome,
205 text_t& collecthome, int &maxrequests)
206{
207 __site_configuration sitecfg(&gsdlhome, &collecthome, &maxrequests);
208 gsdlconfigurator.add_configurable(&sitecfg);
209
210 // blank the gsdlhome and collecthome text
211 gsdlhome.clear();
212 collecthome.clear();
213
214 if (gsdlconfigurator.configure("gsdlsite.cfg"))
215 {
216 return true;
217 }
218
219 return false;
220}
221
222
223// this version grabs gsdlhome, collecthome, httpdomain and httpprefix,
224// returns false if it can't find all of them
225bool site_cfg_read (text_t &gsdlhome, text_t& collecthome, text_t &httpdomain,
226 text_t &httpprefix)
227{
228 // get gsdlhome etc
229 __site_configuration sitecfg(&gsdlhome, &collecthome, &httpdomain, &httpprefix);
230 configurator gsdlconfigurator(&sitecfg);
231
232 gsdlhome.clear();
233 collecthome.clear();
234 httpdomain.clear();
235 httpprefix.clear();
236
237 if (gsdlconfigurator.configure("gsdlsite.cfg"))
238 {
239 return true;
240 }
241
242 return false;
243}
244
245
246// this version grabs gsdlhome, collecthome, httpdomain, httpprefix, collection,
247// returns false if it can't find gsdlhome, httpdomain and httpprefix
248bool site_cfg_read (text_t &gsdlhome, text_t& collecthome, text_t &httpdomain,
249 text_t &httpprefix, text_t &collection)
250{
251 // get gsdlhome etc
252 __site_configuration sitecfg(&gsdlhome, &collecthome, &httpdomain, &httpprefix, &collection);
253 configurator gsdlconfigurator(&sitecfg);
254
255 gsdlhome.clear();
256 collecthome.clear();
257 httpdomain.clear();
258 httpprefix.clear();
259 collection.clear();
260
261 if (gsdlconfigurator.configure("gsdlsite.cfg") &&
262 !gsdlhome.empty() && !collecthome.empty()
263 && !httpdomain.empty() && !httpprefix.empty())
264 {
265 return true;
266 }
267
268 return false;
269}
Note: See TracBrowser for help on using the repository browser.