org.apache.jdo.util.web
Class JDOFilter

java.lang.Object
  extended by org.apache.jdo.util.web.JDOFilter
All Implemented Interfaces:
javax.servlet.Filter

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

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: This is an sample filter definition using initialization parameter:
 <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

PMF_PROPS_RESOURCE_PARAM

public static final java.lang.String PMF_PROPS_RESOURCE_PARAM
The name of the JDOFilter initialization parameter allowing to specify the name of the pmf properties resource.

See Also:
Constant Field Values

PMF_PROPS_RESOURCE_DEFAULT

public static final java.lang.String PMF_PROPS_RESOURCE_DEFAULT
The default PMF properties resource.

See Also:
Constant Field Values

PM_REQUEST_ATTR_NAME_PARAM

public static final 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.

See Also:
Constant Field Values

PM_REQUEST_ATTR_NAME_DEFAULT

public static final java.lang.String PM_REQUEST_ATTR_NAME_DEFAULT
The name of the request attribute storing the PersistenceManager.

See Also:
Constant Field Values
Constructor Detail

JDOFilter

public JDOFilter()
Method Detail

init

public void init(javax.servlet.FilterConfig filterConfig)
          throws javax.servlet.ServletException
Called by the web container to indicate to a filter that it is being placed into service.

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.

Specified by:
init in interface javax.servlet.Filter
Parameters:
filterConfig - the filter configuration object.
Throws:
javax.servlet.ServletException

doFilter

public void doFilter(javax.servlet.ServletRequest request,
                     javax.servlet.ServletResponse response,
                     javax.servlet.FilterChain chain)
              throws java.io.IOException,
                     javax.servlet.ServletException
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. This implementation creates a PersistenceManager, stores it as a request attribute and in a ThreadLocal and then calls the filter chain. It closes the PersistenceManager after the chain returns.

Specified by:
doFilter in interface javax.servlet.Filter
Parameters:
request - the resquest
response - the response
chain - the filter chain
Throws:
java.io.IOException
javax.servlet.ServletException

destroy

public void destroy()
Called by the web container to indicate to a filter that it is being taken out of service. This implementation closes the PersistenceManagerFactory.

Specified by:
destroy in interface javax.servlet.Filter

getThreadLocalPM

public static javax.jdo.PersistenceManager getThreadLocalPM()
Returns the PersistenceManager instance bound to the current thread using a ThreadLocal.

Returns:
the PersistenceManager bound to the current thread.


Copyright © 2005-2009 Apache Software Foundation. All Rights Reserved.