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

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

GTI on nzdl needs path to java executable. Since a custom javahome property can be set in gsdlsite.cfg, this is now read in if site from gsdlsite.cfg by common-src's gsdlsitecfg.cpp and set as an env var with putenv(), following Dr Bainbridge's idea. This env var is then read back in using getenv in gtiaction.cpp, so that it can run the appropriate java.

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