javawebparts.filter
Class DependencyFilter

java.lang.Object
  |
  +--javawebparts.filter.DependencyFilter
All Implemented Interfaces:
javax.servlet.Filter

public class DependencyFilter
extends java.lang.Object
implements javax.servlet.Filter

This filter is a very simple hybrid IoC (Inversion Of Control) implementation. It is hybrid in that the dependencies are not strictly speaking "injected", as is usually the case when discussing IoC. They are created as needed, and made available to client code in a simple manner, so while the dependencies aren't injected, it is still very useful. They are in a sense injected into either the incoming request or session objects though... I'm sure you can see why I call it "hybrid" IoC! :) It allows you to define objects via XML configuration file that will be created per-session or per-request, and initialzed as you determine is neccessary.

Init parameters are:

Sample Configuration File:

<dependencies>
  <dependency scope="request" name="MyTestBean1" maxAge="10" createForPaths="/myPath.do">
        <className> javawebparts.sampleapp.DFTestBean</className>
        <callMethod name="setDOB" arguments="February,17,1973" />
        <initClass name="myInitClass" mathod="initBean" />
        <initProp name="firstName" value="Frank" />
        <initProp name="lastName" value="Zammetti" />
        <initList name="children">
            < listItem value="Andrew" />
            < listItem value="Michael" />
        </initList>
        <initList name="children" method="addChild">
            < listItem value="Ashley" />
        </initList>
        < initMap name="dimensions">
            < mapItem key="height" value="5ft, 9in" />
        </initMap>
        <initMap name="dimensions" method="addDimension">
            < mapItem key="weight" value="185lbs" />
        </initMap>
    </dependency>
</dependencies>

The simplest possible config file would be this:

<dependencies>
  <dependency scope="?AA?" name=" ?BB?">
        <className>?CC?< /className>
  </dependency>
</dependencies>

?AA? would be either "request" or "session", ?BB? would be the name of the object as it will be referenced from your code, and ?CC? is the class to create. The object will be created per-request or per-session as specified, and will not be initialized in any way. Note that if it is request-scoped, it will be created for EVERY request! You will be able to retrieve it by using:

?CC? obj = (?CC?)DependencyFilter.getDependency("?BB?", request);

?CC? is again the name of the class, ?BB? is the name and request is the current request object. You DO NOT need to specify whether the object is in request or session scope, it will be found either way.

More explanation on the config file:
Note that because Commons Beanutils is used to set the various properties, you can use whatever types it supports and conversions will be automatically handled.

The order things occur in when an object is created is:

(1) The object is created
(2) The simple properties (<initProp> elements) are done, if present
(3) The List properties (<initList> element) are done, if present
(4) The Map properties (<initMap> element) are done, if present
(5) The <initClass> element is done, if present
(6) The <callMethod> element(s) are done, if present

Author:
Frank W. Zammetti.

Field Summary
private  boolean createSession
          Flag: should sessions be created by this filter if one does not exist already, or not.
private static java.util.HashMap dependencyConfigs
          Collection of DependencyConfig objects.
private static org.apache.commons.logging.Log log
          Log instance.
 
Constructor Summary
DependencyFilter()
           
 
Method Summary
 void addDependency(DependencyConfig dc)
          This method is called during config file parsing by Digester to add the configured DepedencyConfig object to the collection.
 void destroy()
          Destroy.
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain filterChain)
          Do filter's work.
static java.lang.Object getDependency(java.lang.String name, javax.servlet.ServletRequest request)
          This method is called by client code to get a reference to a particular created object.
 void init(javax.servlet.FilterConfig filterConfig)
          Initialize this filter.
static void updateDependency(java.lang.String inName, java.lang.Object inObj, javax.servlet.ServletRequest inRequest)
          This method is called by client code to update a dependent object.
 
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.


dependencyConfigs

private static java.util.HashMap dependencyConfigs
Collection of DependencyConfig objects.


createSession

private boolean createSession
Flag: should sessions be created by this filter if one does not exist already, or not.

Constructor Detail

DependencyFilter

public DependencyFilter()
Method Detail

destroy

public void destroy()
Destroy.

Specified by:
destroy in interface javax.servlet.Filter

init

public void init(javax.servlet.FilterConfig filterConfig)
          throws javax.servlet.ServletException
Initialize this filter. This amounts to reading in the specifid config file and creating a bunch of DependencyConfig objects from it and adding them to the collection for later use.

Specified by:
init in interface javax.servlet.Filter
Parameters:
filterConfig - The configuration information for this filter.
Throws:
javax.servlet.ServletException - ServletException.

addDependency

public void addDependency(DependencyConfig dc)
This method is called during config file parsing by Digester to add the configured DepedencyConfig object to the collection.

Parameters:
dc - The fully-configured DependencyConfig object.

doFilter

public void doFilter(javax.servlet.ServletRequest request,
                     javax.servlet.ServletResponse response,
                     javax.servlet.FilterChain filterChain)
              throws javax.servlet.ServletException,
                     java.io.IOException
Do filter's work.

Specified by:
doFilter in interface javax.servlet.Filter
Parameters:
request - The current request object.
response - The current response object.
filterChain - The current filter chain.
Throws:
javax.servlet.ServletException - ServletException.
java.io.IOException - IOException.

getDependency

public static java.lang.Object getDependency(java.lang.String name,
                                             javax.servlet.ServletRequest request)
This method is called by client code to get a reference to a particular created object. The caller does not need to specify or even necessarily care where the object is (request or session).

Parameters:
name - The name of the object as specified in the config file.
request - The current ServletRequest instance.
Returns:
Reference to the specified object, or null if not present, or if object was in session but session was null.

updateDependency

public static void updateDependency(java.lang.String inName,
                                    java.lang.Object inObj,
                                    javax.servlet.ServletRequest inRequest)
This method is called by client code to update a dependent object. In other words, if a caller gets a reference to a bean in session, makes some changes to it and wants to save it again, they can call this method. This way, the caller doesn't have to know or care whether the object was in request or session.

Parameters:
inName - The name of the object as specified in the config file.
inObj - The object to store.
inRequest - Current request being services.


Copyright © 2005 Frank W. Zammetti