Every Tomcat instance we have has an isProduction
flag defined in the GlobalNamingResources
section of the server.xml
file.
server.xml
:
<Server ...>
...
<GlobalNamingResources>
<Environment name="isProduction" value="false" type="java.lang.Boolean" override="false" />
</GlobalNamingResources>
<Service name="Catalina">
... etc ...
</Service>
</Server>
This allows the property to be available throughout the app by creating a property in the context.xml that references the resource:
context.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<Context ...>
<ResourceLink name="isProduction" global="isProduction" type="java.lang.Boolean" />
...
</Context>
To fetch the value:
public boolean isProduction() {
Object o;
try {
o = (new InitialContext()).lookup("java:comp/env/isProduction");
} catch (NamingException e) {
o = Boolean.FALSE; // assumes FALSE if the value isn't declared
}
return o == null ? Boolean.FALSE : (Boolean) o;
}