javawebparts.listener
Class AppConfigContextListener

java.lang.Object
  |
  +--javawebparts.listener.AppConfigContextListener
All Implemented Interfaces:
java.util.EventListener, javax.servlet.ServletContextListener

public class AppConfigContextListener
extends java.lang.Object
implements javax.servlet.ServletContextListener

Most applications of even modest complexity require configuration information. A common method of providing this information is via XML file. Often times a developer will want to expose this information via a simple Javabean throughout the rest of the application. This ServletContextListener provides a simple way to read in an XML configuration file and populate a bean, either one the developer creates or a simple "default" bean.

Init parameters are:

Three "modes" of operation are suppported and you can think of them as Basic, Intermediate and Advanced. The Basic configuration requires that you specify the configFile parameter only. In this mode, the AppConfig object will be used and the root element of the config file MUST be "config". The config file must be "flat", that is, elements can only be nested under the root, not under any other child nodes of root. In Intermediate mode, you will specify the configFile, as well as the root element and the configClass. Your class must expose setters for each element of the config file. In Advanced mode, you will also specify a class that sets Commons Digester rules to use to parse the XML. This is good if you have a more complex structure that you want to support (i.e., repeating elements for a collection or multi-level nesting).

Example configuration in web.xml:

<context-param>
  <param-name>configFile</param-name>
  <param-value>/WEB-INF/app_config.xml< /param-value></context-param>
<context-param>
  <param-name>rootElement</param-name>
  <param-value>config</param-value> </context-param>
<context-param>
  <param-name>configClass</param-name>
  <param-value>javawebparts.sampleapp. SampleAppConfigBean</param-value>
</context-param>
<context-param>
  <param-name>AjaxPartsTaglibConfig</param-name>
  <param-value>/WEB-INF/ajax_config.xml</param-value>
</context-param>

<listener>
  <listener-class>javawebparts.listener. AppConfigContextListener</listener-class>
</listener>

Author:
Frank W. Zammetti.

Field Summary
private  java.lang.String configClass
          The class that holds our config information.
private  java.lang.String configFile
          Path to the config file.
private static org.apache.commons.logging.Log log
          Log instance.
private  java.lang.String rootElement
          The root element of the config file.
private  java.lang.String rulesetClass
          The class that is called to configure the Digester ruleset.
 
Constructor Summary
AppConfigContextListener()
           
 
Method Summary
 void contextDestroyed(javax.servlet.ServletContextEvent sce)
          Doesn't do anything, but needed to be here.
 void contextInitialized(javax.servlet.ServletContextEvent sce)
          The container calls this when the context is initialzed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static org.apache.commons.logging.Log log
Log instance.


configFile

private java.lang.String configFile
Path to the config file.


rootElement

private java.lang.String rootElement
The root element of the config file.


configClass

private java.lang.String configClass
The class that holds our config information.


rulesetClass

private java.lang.String rulesetClass
The class that is called to configure the Digester ruleset.

Constructor Detail

AppConfigContextListener

public AppConfigContextListener()
Method Detail

contextInitialized

public void contextInitialized(javax.servlet.ServletContextEvent sce)
The container calls this when the context is initialzed.

Specified by:
contextInitialized in interface javax.servlet.ServletContextListener
Parameters:
sce - ServletContextEvent instance reference

contextDestroyed

public void contextDestroyed(javax.servlet.ServletContextEvent sce)
Doesn't do anything, but needed to be here.

Specified by:
contextDestroyed in interface javax.servlet.ServletContextListener
Parameters:
sce - ServletContextEvent instance reference


Copyright © 2005 Frank W. Zammetti