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

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

Improvement to commit of revision 28979: Dr Bainbridge suggested that we only set JAVA_HOME if it's not already set to the same value.

  • Property svn:executable set to *
File size: 8.3 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 } else {
154 cerr << "JAVA_HOME (" << oldjavahome << ") already set to " + javahome << endl;
155 }
156 }
157
158 if(javahome != oldjavahome) {
159 // http://www.kev.pulo.com.au/pp/RESOURCES/cplusplus/ref/cstdlib/putenv.html
160 // putenv is not defined in ANSI-C, but is supported by many compilers, and is
161 // already used in gtiaction.cpp
162 javahome = "JAVA_HOME=" + javahome;
163 char* set_javahome_cstr = javahome.getcstr();
164 putenv(set_javahome_cstr);
165
166 // can't use locally declared char array that we string-copy the cstr into,
167 // since the array's value expires from putenv, possibly after this function's local scope
168 // Forced to assign the dynamically allocated cstr and resist the urge to delete this:
169 //delete[] set_javahome_cstr; // may not delete it, else the env var just set will become empty
170 }
171 }
172
173 if (actions != NULL && key == "actions") {
174 actions->clear();
175 text_tarray::const_iterator thisAction = cfgline.begin();
176 text_tarray::const_iterator endAction = cfgline.end();
177 while (thisAction != endAction) {
178 actions->insert(*thisAction);
179 ++thisAction;
180 }
181 }
182
183 if (browsers != NULL && key == "browsers") {
184 browsers->clear();
185 text_tarray::const_iterator thisBrowser = cfgline.begin();
186 text_tarray::const_iterator endBrowser = cfgline.end();
187 while (thisBrowser != endBrowser) {
188 browsers->insert(*thisBrowser);
189 ++thisBrowser;
190 }
191 }
192
193 if (maxrequests != NULL && key == "maxrequests") {
194 *maxrequests = cfgline[0].getint();
195 if ((*maxrequests) < 1) {
196 *maxrequests = 1;
197 }
198 }
199 }
200};
201
202
203// reads site configuration file returning true on success
204// also sets gsdlhome and maxrequests
205// gsdlsite.cfg should be in same directory as library executable
206bool site_cfg_read (configurator gsdlconfigurator, text_t &gsdlhome,
207 text_t& collecthome, int &maxrequests)
208{
209 __site_configuration sitecfg(&gsdlhome, &collecthome, &maxrequests);
210 gsdlconfigurator.add_configurable(&sitecfg);
211
212 // blank the gsdlhome and collecthome text
213 gsdlhome.clear();
214 collecthome.clear();
215
216 if (gsdlconfigurator.configure("gsdlsite.cfg"))
217 {
218 return true;
219 }
220
221 return false;
222}
223
224
225// this version grabs gsdlhome, collecthome, httpdomain and httpprefix,
226// returns false if it can't find all of them
227bool site_cfg_read (text_t &gsdlhome, text_t& collecthome, text_t &httpdomain,
228 text_t &httpprefix)
229{
230 // get gsdlhome etc
231 __site_configuration sitecfg(&gsdlhome, &collecthome, &httpdomain, &httpprefix);
232 configurator gsdlconfigurator(&sitecfg);
233
234 gsdlhome.clear();
235 collecthome.clear();
236 httpdomain.clear();
237 httpprefix.clear();
238
239 if (gsdlconfigurator.configure("gsdlsite.cfg"))
240 {
241 return true;
242 }
243
244 return false;
245}
246
247
248// this version grabs gsdlhome, collecthome, httpdomain, httpprefix, collection,
249// returns false if it can't find gsdlhome, httpdomain and httpprefix
250bool site_cfg_read (text_t &gsdlhome, text_t& collecthome, text_t &httpdomain,
251 text_t &httpprefix, text_t &collection)
252{
253 // get gsdlhome etc
254 __site_configuration sitecfg(&gsdlhome, &collecthome, &httpdomain, &httpprefix, &collection);
255 configurator gsdlconfigurator(&sitecfg);
256
257 gsdlhome.clear();
258 collecthome.clear();
259 httpdomain.clear();
260 httpprefix.clear();
261 collection.clear();
262
263 if (gsdlconfigurator.configure("gsdlsite.cfg") &&
264 !gsdlhome.empty() && !collecthome.empty()
265 && !httpdomain.empty() && !httpprefix.empty())
266 {
267 return true;
268 }
269
270 return false;
271}
Note: See TracBrowser for help on using the repository browser.