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:
- createSession - When this filter fires, it gets a reference
to the current session. If none exists, by default it creates it. However,
a developer may determine this should not happen. In that case, setting this
parameter to false will stop a session from being created. However, it is
important to realize that in such a case, a call to getDependency() will
result in null being returned, and it becomes the developers' responsibility
to deal with this. Required: No. Default: true.
- configFile - Context-relative path to the config file that
defines the dependencies. Reqired: Yes. Default: None.
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:
- scope attribute - The scope attribute of the <dependency>
element determines where the created object will be placed, either "request"
or "session". Obviously, if placed in request-scope, the object will not
persist beyond the current request, whereas it will in session-scope.
Required: Yes. Default: None.
- name attribute - The name attribute of the <dependency>
element defines what the name of the created object will be. This is the
name you will reference when getting the object. It is a request or
session attribute key, depending on the specified scope.
Required: Yes. Default: None.
-
- createForPaths attribute - The createForPaths attribute of the
<dependency> element is an optional attribute which specifies
what paths the object will be created for *IF* the specified scope is
"request" (if scope if "session", the object will be created with the first
path requested). This is a comma-separated list of paths, including
wildcard support (in the same way as all the other filters in JWP).
If you want the object to be created for all request paths, DO NOT include
this attribute at all. Required: No. Default: All request paths.
- maxAge attribute - The maxAge attribute of the <dependency>
element is an optional attribute which specifies the maximum age, in
seconds, for the dependency to be considered "fresh". In other words, if
you want to maintain a bean in a users' session that contains a list of
values read from a database, but you want the list to be refreshed every
five minutes, set the maxAge attribute of the dependency to 300
(5 minutes * 60 seconds) and each request that the filter handles will
check the age of the existing object instance in session and will
recreate it if its age exceeds this value. To mark a dependency as
never expiring, i.e., it will be created once per session only, set
maxAge to 0, or simply leave out the maxAge attribute.
- <className> - This is the class of the object to be
created. This element IS required.
- <initClass> - You can have a single one of these as a child
of the <dependency> element. This defines a class (via the name
attribute) and method (via the method attribute) that will be called after
the object has been instantiated. The object itself will be passed as the
only parameter to the method, and you can do whatever kind of initialization
you want. This method SHOULD NOT return a value as it will be ignored.
This element IS NOT required.
- <callMethod> - You can have one or more of these as a child
of the <dependency> element. This element specifies a method of the
object being created that will be called AFTER all initialization has
taken place. The name attribute specifies the name of the method that
will be called and the arguments attribute is a list of arguments
(comma-separated) that will be passed. NOTE THAT ONLY STRING ARGUMENTS
ARE SUPPORTED! This element IS NOT required.
- <initProp> - You can have one or more of these as a child
of the <dependency> element. This will initialize a property of
the object being created (as named by the name attribute) to the value
specified by the value attribute. This element IS NOT required.
- <initList> - You can have one or more of these as a child
of the <dependency> element. This will initialize a List property of
the object being created (as named by the name attribute). You can
optionally specify a method attribute. If NOT present, then a Javabean
spec-compliant method matching the name attribute must be present (i.e.,
if the property is named firstName, then setFirstName() must be an
accessible method). If the method attribute is present, that method will
be called instead. Within each <initList> element can be any number
of child <listItem> elements. For each of these simply specify the
value attribute as the value to be added to the list.
This element IS NOT required.
- <initMap> - You can have one or many of these as a child
of the <dependency> element. This will initialize a Map property of
the object being created (as named by the name attribute). You can
optionally specify a method attribute. If NOT present, then a Javabean
spec-compliant method matching the name attribute must be present (i.e.,
if the property is named firstName, the setFirstName() must be an
accessible method). If the method attribute is present, that method will
be called instead. Within each <initMap> element can be any number
of child <mapItem> elements. For each of these simply specify the
key attribute as the key to be added to the list and the value attribute as
the value to be added to the list.
This element IS NOT required.
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. |
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 |
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.
DependencyFilter
public DependencyFilter()
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