[15402] | 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 |
|
---|
[16310] | 26 | #include "fileutil.h"
|
---|
[15402] | 27 | #include "gsdlsitecfg.h"
|
---|
[28979] | 28 | #include <stdlib.h> // for get/putenv
|
---|
| 29 | #include <string.h> // for get/putenv
|
---|
[15402] | 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 |
|
---|
| 43 | class __site_configuration : public configurable {
|
---|
| 44 | private:
|
---|
| 45 | text_t *gsdlhome;
|
---|
[16310] | 46 | text_t *collecthome;
|
---|
[15402] | 47 | text_t *httpdomain;
|
---|
| 48 | text_t *httpprefix;
|
---|
| 49 | text_t *collection;
|
---|
| 50 | text_tset *actions;
|
---|
| 51 | text_tset *browsers;
|
---|
| 52 | int *maxrequests;
|
---|
| 53 | public:
|
---|
[16310] | 54 | __site_configuration (text_t *_gsdlhome, text_t* _collecthome, int *_maxrequests) {
|
---|
[15402] | 55 | gsdlhome = _gsdlhome;
|
---|
[16310] | 56 | collecthome = _collecthome;
|
---|
[15402] | 57 | httpdomain = NULL;
|
---|
| 58 | httpprefix = NULL;
|
---|
| 59 | collection = NULL;
|
---|
| 60 | actions = NULL;
|
---|
| 61 | browsers = NULL;
|
---|
| 62 | maxrequests = _maxrequests;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[16310] | 65 | __site_configuration (text_t *_gsdlhome, text_t* _collecthome,
|
---|
| 66 | text_t *_httpdomain, text_t *_httpprefix) {
|
---|
[15402] | 67 | gsdlhome = _gsdlhome;
|
---|
[16310] | 68 | collecthome = _collecthome;
|
---|
[15402] | 69 | httpdomain = _httpdomain;
|
---|
| 70 | httpprefix = _httpprefix;
|
---|
| 71 | collection = NULL;
|
---|
| 72 | actions = NULL;
|
---|
| 73 | browsers = NULL;
|
---|
| 74 | maxrequests = NULL;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[16310] | 77 | __site_configuration (text_t *_gsdlhome, text_t* _collecthome,
|
---|
| 78 | text_t *_httpdomain,
|
---|
[15402] | 79 | text_t *_httpprefix, text_t *_collection) {
|
---|
| 80 | gsdlhome = _gsdlhome;
|
---|
[16310] | 81 | collecthome = _collecthome;
|
---|
[15402] | 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;
|
---|
[16310] | 92 | collecthome = NULL;
|
---|
[15402] | 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];
|
---|
[16310] | 104 |
|
---|
| 105 | if ((collecthome != NULL) && (collecthome->empty())) {
|
---|
| 106 | // defaults to <gsdlhome>/collect
|
---|
| 107 | *collecthome = filename_cat(*gsdlhome,"collect");
|
---|
| 108 | }
|
---|
[15402] | 109 | }
|
---|
| 110 |
|
---|
[16310] | 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 |
|
---|
[15402] | 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 |
|
---|
[28979] | 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]);
|
---|
[28990] | 141 | text_t oldjavahome = "";
|
---|
[28979] | 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) {
|
---|
[28990] | 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 | }
|
---|
[28979] | 154 | }
|
---|
| 155 |
|
---|
[28990] | 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 | }
|
---|
[28979] | 170 |
|
---|
[15402] | 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
|
---|
[16310] | 204 | bool site_cfg_read (configurator gsdlconfigurator, text_t &gsdlhome,
|
---|
| 205 | text_t& collecthome, int &maxrequests)
|
---|
[15402] | 206 | {
|
---|
[16310] | 207 | __site_configuration sitecfg(&gsdlhome, &collecthome, &maxrequests);
|
---|
[15402] | 208 | gsdlconfigurator.add_configurable(&sitecfg);
|
---|
| 209 |
|
---|
[16310] | 210 | // blank the gsdlhome and collecthome text
|
---|
[15402] | 211 | gsdlhome.clear();
|
---|
[16310] | 212 | collecthome.clear();
|
---|
[15402] | 213 |
|
---|
| 214 | if (gsdlconfigurator.configure("gsdlsite.cfg"))
|
---|
| 215 | {
|
---|
| 216 | return true;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | return false;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 |
|
---|
[16310] | 223 | // this version grabs gsdlhome, collecthome, httpdomain and httpprefix,
|
---|
[15402] | 224 | // returns false if it can't find all of them
|
---|
[16310] | 225 | bool site_cfg_read (text_t &gsdlhome, text_t& collecthome, text_t &httpdomain,
|
---|
[15402] | 226 | text_t &httpprefix)
|
---|
| 227 | {
|
---|
| 228 | // get gsdlhome etc
|
---|
[16310] | 229 | __site_configuration sitecfg(&gsdlhome, &collecthome, &httpdomain, &httpprefix);
|
---|
[15402] | 230 | configurator gsdlconfigurator(&sitecfg);
|
---|
| 231 |
|
---|
| 232 | gsdlhome.clear();
|
---|
[16310] | 233 | collecthome.clear();
|
---|
[15402] | 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 |
|
---|
[16310] | 246 | // this version grabs gsdlhome, collecthome, httpdomain, httpprefix, collection,
|
---|
[15402] | 247 | // returns false if it can't find gsdlhome, httpdomain and httpprefix
|
---|
[16310] | 248 | bool site_cfg_read (text_t &gsdlhome, text_t& collecthome, text_t &httpdomain,
|
---|
[15402] | 249 | text_t &httpprefix, text_t &collection)
|
---|
| 250 | {
|
---|
| 251 | // get gsdlhome etc
|
---|
[16310] | 252 | __site_configuration sitecfg(&gsdlhome, &collecthome, &httpdomain, &httpprefix, &collection);
|
---|
[15402] | 253 | configurator gsdlconfigurator(&sitecfg);
|
---|
| 254 |
|
---|
| 255 | gsdlhome.clear();
|
---|
[16310] | 256 | collecthome.clear();
|
---|
[15402] | 257 | httpdomain.clear();
|
---|
| 258 | httpprefix.clear();
|
---|
| 259 | collection.clear();
|
---|
| 260 |
|
---|
| 261 | if (gsdlconfigurator.configure("gsdlsite.cfg") &&
|
---|
[16310] | 262 | !gsdlhome.empty() && !collecthome.empty()
|
---|
| 263 | && !httpdomain.empty() && !httpprefix.empty())
|
---|
[15402] | 264 | {
|
---|
| 265 | return true;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | return false;
|
---|
| 269 | }
|
---|