resin $resin/java_tut/jndi-appconfig.xtp tutorial Application configuration files using a custom JNDI object

Applications often need to read, and possibly write, configuration files. An excellent way to accomplish this is to implement a custom JNDI object, which is easily configured and easily obtained from anywhere in the application.

This implementation of the concept allows you to configure a base directory for configuration files. An object of type AppConfig is obtained with a jndi lookup. It is used to open files relative to the base directory.

Configure the AppConfig object as a resource The AppConfig object provides input and output streams to configuration files A simple example usage of AppConfig that reads and writes a file The starting page for the tutorial

A custom JNDI object is implemented similar to a java-bean (see Bean-style initialization). Setter methods like setFoo(String foo) are used to set values that are specified in the configuration.

In this case, a single setter is provided that matches the configuration parameter "config-files-location". The init() method is called by Resin after all of the setters have been called.

public class AppConfig { ConfigFilesLocation _cfl = null; /** * Set the base for subsequent call's to openConfigFileRead() * and openConfigFileWrite() * * @param location a file path or url */ public void setConfigFilesLocation(String location) throws Exception { _cfl = new ConfigFilesLocation(); _cfl.setLocation(location); } public void init() throws Exception { if (_cfl == null) throw new Exception("'config-files-location' must be set"); } ...

Configuration of the JNDI object is done with the tag.

The example here configures the location of the configuration files as WEB-INF/config (which means you need to make sure the directory exists for the example to work). It is good to hide the files somewhere under WEB-INF, because a browser will not be able to read the files, just the application.

The EL configuration variable is used.

<resource name='config/Application'> <type>example.AppConfig</type> <init> <config-files-location>${'${'}app.docDir}/WEB-INF/config</config-files-location> </init> </resource>

An instance of the object is retrieved in the application using JNDI. Since the configuration will not change, it is best to store the results of the lookup.

In this example servlet, an instance of the object is retrieved in the init() method and stored in _appConfig.

final static String JNDI_NAME = "java:comp/env/config/Application"; public void init() throws ServletException { try { _appConfig = (AppConfig) new InitialContext().lookup(JNDI_NAME); if (_appConfig == null) throw new ServletException("`" + JNDI_NAME + "' is an unknown JNDI resource"); } catch (NamingException e) { throw new ServletException(e); }

_appConfig is used to open the configuration files for reading and writing.

... InputStream is = _appConfig.openConfigFileRead(inputFile); ... OutputStream os = _appConfig.openConfigFileWrite(outputFile); ...

The example in this tutorial is easily modified to allow the hiding of the configuration file behind get methods of the bean. Implementing getters on the confugration bean abstracts the configuration information, protecting code which uses the configuration information from implementation details of how the configuration information is read and stored.

package example; import java.util.*; import*; import javax.naming.*; public class AppConfig { public final static String APPCONFIG_JNDINAME = "java:comp/env/config/Application"; private final static String DEFAULT_PROPERTIES = "example/"; private String _configFile; private Properties _properties; /** * A convenience method that emulates the singleton pattern. * It is not a good idea to use static member variables to implement the * singleton pattern in an app server because of ClassLoader problems. * In this case, JNDI is used to store the object in a safe manner. * * The AppConfig object has already been instantiated by Resin, the JNDI * lookup is used to get a reference to an object that already exists. */ static public AppConfig getInstance() throws NamingException { AppConfig r = (AppConfig) new InitialContext().lookup(APPCONFIG_JNDINAME); if (r == null) throw new NamingException("no object found with jndi name `" + APPCONFIG_JNDINAME + "'"); return r; } /** * Optionally set the name of a file that provides properties that override * the defaults. The defaults are obtained from a file in the classpath * named 'example/' * * For example, the file containing default properties might be in * WEB-INF/classes/example/, * or if AppConfig.class is in a jar, the * could be in the jar file alongside the AppConfig.class file. * * contains values placed there by the developer. * The <config-file> is used to indicate a file that specifies properties * that override the defaults, perhaps properties that change depending * on the deployment environment. */ public void setConfigFile(String configFile) throws Exception { _configFile = configFile; } public void init() throws Exception { InputStream is = null; Properties defaults; // try to find a default configuration file in the classpath ClassLoader loader = Thread.currentThread().getContextClassLoader(); is = loader.getResourceAsStream(DEFAULT_PROPERTIES); if (is != null) defaults = new Properties(); defaults.load(is); } else { // throw an exception here to make the defaults required throw new FileNotFoundException(DEFAULT_PROPERTIES); } if (_configFile == null) { // just use the defaults _properties = defaults; } else { // the properties in _configFile override the defaults is = new FileInputStream(_configFile); _properties = new Properties(defaults); _properties.load(is); } } public String getFoo() { return _properties.getProperty("foo"); } public String getBar() { return _properties.getProperty("bar"); } } <web-app> <resource name='config/Application'> <type>example.AppConfig</type> </resource> </web-app> or <web-app> <resource name='config/Application'> <type>example.AppConfig</type> <init> <config-file>${'${'}app.docDir}/WEB-INF/</config-file> </init> </resource> </web-app> package example; import javax.servlet.*; import javax.servlet.http.*; import*; import java.util.*; import javax.naming.*; public class TestServlet extends HttpServlet { /** * _appConfig is stored locally, for efficiency. */ AppConfig _appConfig; public void init() throws ServletException { try { _appConfig = AppConfig.getInstance(); } catch (NamingException e) { throw new ServletException(e); } } ... String foo = _appConfig.getFoo(); String bar = _appConfig.getBar(); ... }

The availability of AppConfig to different web-apps depends upon the context that the <resource ...> configuration is placed within.

If the configuration is placed as a child of <web-app>, then that instance of AppConfig is available only to that web-app.

<web-app> <resource name='config/Application'> <type>example.AppConfig</type> </resource> </web-app>

If it is placed as a child of <host>, that instance of AppConfig is available to all web-app's within the host.

<host> ... <resource name='config/Application'> <type>example.AppConfig</type> </resource> ... </host>

If it is placed as a child of <server>, that instance of AppConfig is available to all web-app's within all host's within that server.

<server> ... <resource name='config/Application'> <type>example.AppConfig</type> </resource> ... </server>

In the case of <server> or <host>, the example.AppConfig class needs to be available in the classpath. The easiest way to accomplish that is to place a jar with that class in $RESIN_HOME/lib, or you can use an explicit .

Although the tag is Resin specific the pattern shown in this tutorial is good for portability. If your application needs to work in another app-server, you can use a startup facility such as a servlet to manually configure the bean, call the init() method, and stuff the instance into JNDI.