I was looking for a way to easily manage web application specific configuration in separate properties file. In our current version of application, a properties file was included in a war file of web application. However, this was problematic since the properties file had to be updated every time we deployed a new war file. I tried passing the name of the properties file in Tomcat command-line but that did not work well for multiple web applications. Then I came across Environment tags in Tomcat context definition that can be configured for each individual application. So I decided to us the following solution.
1. Add Environment entry for name of a properties file in context definition of web application.
<Context path="/app1" docBase="C:/app1/web-dir"
debug="0" reloadable="true">
<Environment name="appconfig" value="WebApp1.properties"
type="java.lang.String" override="false"/>
</Context>
2. Obtain the name of properties file specified as above using JNDI.
Context initCtx = new InitialContext();
String filename = (String) initCtx.lookup("java:comp/env/appconfig");
3. Put the properties file in a directory that is available on CLASSPATH and access the properties using Jakarta Commons utility.
Configuration config = new PropertiesConfiguration(filename);
public String get(String key) {
return config.getString(key);
}
Now it is easy to create and manage configuration for individual web application by specifying different filename in Context definition of the web application.
If you need to access the configuration from independent Java code running outside Tomcat, then use JNDI to query tomcat and obtain appropriate web application configuration.
1. Add Environment entry for name of a properties file in context definition of web application.
<Context path="/app1" docBase="C:/app1/web-dir"
debug="0" reloadable="true">
<Environment name="appconfig" value="WebApp1.properties"
type="java.lang.String" override="false"/>
</Context>
2. Obtain the name of properties file specified as above using JNDI.
Context initCtx = new InitialContext();
String filename = (String) initCtx.lookup("java:comp/env/appconfig");
3. Put the properties file in a directory that is available on CLASSPATH and access the properties using Jakarta Commons utility.
Configuration config = new PropertiesConfiguration(filename);
public String get(String key) {
return config.getString(key);
}
Now it is easy to create and manage configuration for individual web application by specifying different filename in Context definition of the web application.
If you need to access the configuration from independent Java code running outside Tomcat, then use JNDI to query tomcat and obtain appropriate web application configuration.
Comments