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:
- configFile - The context-relative path to the XML configuration
file for the context. This is the only required parameter, but
there are some limitations if it is all you supply. See
the comments after this parameter list for those details.
Required: yes. Default: none.
- rootElement - Defines what the root element of the config file is.
If none is specified, <config> is used by default. Required: no.
Default: config.
- configClass - This parameter names the class that will act as the
configuration information holder. Typically, this class will expose
getters and setters for each element in the configuration file and store all
the data in static members. In other words, something like this is typical:
For an XML config file like this:
<config>
<firstName>Frank</firstName>
</config>
The following bean could be used:
public class MyConfigBean {
private static String firstName;
public void setFirstName(String inFirstName) {
firstName = inFirstName;
}
public String getFirstName() {
return firstName;
}
}
If this parameter is not present, the AppConfig
class will be used. The AppConfig class defines a single
String get(String key) method for your use, whereas a custom class can
expose real setters and getters for each element, or even more complex
constructs if required (i.e., maybe one of your parameters is a
comma-separated list, and you want your setter to parse it into a List).
Required: no. Default: AppConfig class.
- rulesetClass - When a custom configClass is used, the developer
can also create a class that sets Commons Digester rules. This
allows for parsing of more complex XML files. This is only applicable when
configClass is used, and even then is optional. If a rulesetClass is NOT
defined, then the config file must follow the same rules as when a custom
appConfig class is not used, namely that the root element must be
<config> and it must be "flat" (i.e., all elements must be direct
children of <config>). Required: no. Default: none.
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. |
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 |
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.
AppConfigContextListener
public AppConfigContextListener()
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