Ignore:
Timestamp:
2014-04-16T17:33:23+12:00 (10 years ago)
Author:
ak19
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/common-src/src/lib/gsdlsitecfg.cpp

    r16310 r28979  
    2626#include "fileutil.h"
    2727#include "gsdlsitecfg.h"
     28#include <stdlib.h> // for get/putenv
     29#include <string.h> // for get/putenv
    2830
    2931#if defined(GSDL_USE_OBJECTSPACE)
     
    126128      *collection = cfgline[0];
    127129    }
     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    }   
    128164
    129165    if (actions != NULL && key == "actions") {
Note: See TracChangeset for help on using the changeset viewer.