Passing-through environment variables to Tomcat 7.0 web app context

10,571

Tomcat Environment entries are different from system environment variables. Environment Entries specified by <Environment> markup are JNDI, accessible using InitialContext.lookup under java:/comp/env, while System.getEnv() is about system environment variables (of the tomcat process itself).

Share:
10,571
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin over 1 year

    Apache Tomcat 7.0, CentOS 5.8 i386

    A web application needs a specific environment variable XY to be present in its context. This variable is set in /etc/profile as a result of a computation (i.e. not a static value) and it is also used by other native applications running on the same system (hence it has to be the environment variable approach).

    Tomcat is started with a general script using a dedicated tomcat user and sudo. The first problem of passing XY through sudo is solved (thanks to stackoverflow) with the explicit definition within /etc/sudoers:

    Defaults    env_keep ="XY"
    

    It means that the environment variable XY is preserved by sudo which is not the default case.

    Now the environment variable XY is visible in the tomcat process. This can be verified with ps and /proc/tomcat-PID/environ or an explicit echo $XY in */your_tomcat/bin/startup.sh* (which is called by the init.d script using sudo). But seeing XY in the tomcat process does not mean the web app can see it. The web app dumps its environment to the log file with help of

    LOGGER.debug("Environment: " + System.getenv());  
    

    the astonishing result for me was: no XY at all, although tomcat had it!

    After reading the context documentation of tomcat 7.0 (be careful to distinguish between 7.0 and older versions of tomcat) I added the following entry to */your_tomcat/conf/context.xml*:

    <Context>
    ...
    <Environment name="XY" value="INIT_VALUE" type="java.lang.String"/>  
    ...
    </Context>
    

    Now the output of System.getenv() really contains my XY environment variable BUT it has the correct value from /etc/profile not the value INIT_VALUE I specified in context.xml. With other words my /etc/profile does overwrite the INIT_VALUE, which is what I needed but not what I expected as there is no word about this in the documentation.

    Did I find un-docmented behaviour that might be removed in later versions of tomcat or is this the way to go? So in the end I am happy having a working solution but I am not very confident that this is a recommended and proper way of passing-through environment variables. Any comments would be highly appreciated.

  • Stephane
    Stephane about 9 years
    Is there any way to have application specific environment variables (the same variable has a different value per application) without using JNDI ? I'm thinking of something like a -Dname=bob in an application specific context.
  • Stephane
    Stephane about 9 years