Save This Page
Home » jdo2-util-2.3-ea-src » org.apache.jdo.util.web » [javadoc | source]
org.apache.jdo.util.web
public class: JDOFilter [javadoc | source]
java.lang.Object
   org.apache.jdo.util.web.JDOFilter

All Implemented Interfaces:
    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
public static final  String PMF_PROPS_RESOURCE_PARAM    The name of the JDOFilter initialization parameter allowing to specify the name of the pmf properties resource. 
public static final  String PMF_PROPS_RESOURCE_DEFAULT    The default PMF properties resource. 
public static final  String PM_REQUEST_ATTR_NAME_PARAM    The name of the JDOFilter initialization parameter allowing to specify the name of the pm request attribute. 
public static final  String PM_REQUEST_ATTR_NAME_DEFAULT    The name of the request attribute storing the PersistenceManager. 
Method from org.apache.jdo.util.web.JDOFilter Summary:
destroy,   doFilter,   getThreadLocalPM,   init
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.jdo.util.web.JDOFilter Detail:
 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.
 public  void doFilter(ServletRequest request,
    ServletResponse response,
    FilterChain chain) throws IOException, 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.
 public static PersistenceManager getThreadLocalPM() 
    Returns the PersistenceManager instance bound to the current thread using a ThreadLocal.
 public  void init(FilterConfig filterConfig) throws 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.