|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.jdo.util.web.JDOFilter
public class JDOFilter
This implementation of the servlet Filter interface creates a JDO PersistenceManager, stores it as a request attribute and as a ThreadLocal. It closes the PersistenceManager after the filter chain has returned. The idea for this class is taken from the JavaOne 2003 presentation "Using Struts with Java Data Objects" by Craig Russell, Craig McClanahan and Amy Roh.
To setup the filter add the following to your deployment descriptor:
<filter> <filter-name>JDOFilter</filter-name> <filter-class>org.apache.jdo.util.web.JDOFilter</filter-class> </filter> <filter-mapping> <filter-name>JDOFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>The JDOFilter supports two filter initialization paramters:
pmfPropsResource
: the name of the PersistenceManagerFactory
properties resource. Default is /WEB-INF/pmf.properties
.pmRequestAttrName
: the name of the request attribute used to
store the PersistenceManager instance. Default is jdoPM
.
<filter> <filter-name>JDOFilter</filter-name> <filter-class>org.apache.jdo.util.web.JDOFilter</filter-class> <init-param> <param-name>pmfPropsResource</param-name> <param-value>/WEB-INF/pmf.properties</param-value> </init-param> <init-param> <param-name>pmRequestAttrName</param-name> <param-value>jdoPM</param-value> </init-param> </filter>It is possible to define multiple filters in the deployment descriptor, all using the JDOFilter class. In this case it is important to specify the name of the PersistenceManager request attribute in the filter configuration by setting the pmRequestAttrName initialization paramter. Otherwise, the different filter instances would try to use the same request attribute. Please note, in case of multiple JDOFilter instances, only the first filter stores its PersistenceManager as a ThreadLocal.
The static method getThreadLocalPM()
allows retrieving the
PersistenceManager instance bound to the current thread.
Field Summary | |
---|---|
static java.lang.String |
PM_REQUEST_ATTR_NAME_DEFAULT
The name of the request attribute storing the PersistenceManager. |
static java.lang.String |
PM_REQUEST_ATTR_NAME_PARAM
The name of the JDOFilter initialization parameter allowing to specify the name of the pm request attribute. |
static java.lang.String |
PMF_PROPS_RESOURCE_DEFAULT
The default PMF properties resource. |
static java.lang.String |
PMF_PROPS_RESOURCE_PARAM
The name of the JDOFilter initialization parameter allowing to specify the name of the pmf properties resource. |
Constructor Summary | |
---|---|
JDOFilter()
|
Method Summary | |
---|---|
void |
destroy()
Called by the web container to indicate to a filter that it is being taken out of service. |
void |
doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain)
The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. |
static javax.jdo.PersistenceManager |
getThreadLocalPM()
Returns the PersistenceManager instance bound to the current thread using a ThreadLocal. |
void |
init(javax.servlet.FilterConfig filterConfig)
Called by the web container to indicate to a filter that it is being placed into service. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String PMF_PROPS_RESOURCE_PARAM
public static final java.lang.String PMF_PROPS_RESOURCE_DEFAULT
public static final java.lang.String PM_REQUEST_ATTR_NAME_PARAM
public static final java.lang.String PM_REQUEST_ATTR_NAME_DEFAULT
Constructor Detail |
---|
public JDOFilter()
Method Detail |
---|
public void init(javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException
This implementation creates a JDO PersistenceManagerFactory instance
using a properties resource specified by an initialization parameter
called PMF_PROPS_RESOURCE_PARAM
or defaulted to
PMF_PROPS_RESOURCE_DEFAULT
. The method checks for another
initialization parameter PM_REQUEST_ATTR_NAME_PARAM
that may be
used to specify the name of the request attribute holding the
PersistenceManager instance. The name defaults to
PM_REQUEST_ATTR_NAME_DEFAULT
if there is no such initialization
parameter.
init
in interface javax.servlet.Filter
filterConfig
- the filter configuration object.
javax.servlet.ServletException
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws java.io.IOException, javax.servlet.ServletException
doFilter
in interface javax.servlet.Filter
request
- the resquestresponse
- the responsechain
- the filter chain
java.io.IOException
javax.servlet.ServletException
public void destroy()
destroy
in interface javax.servlet.Filter
public static javax.jdo.PersistenceManager getThreadLocalPM()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |