com.wideplay.warp.persist
Class PersistenceFilter

java.lang.Object
  extended by com.wideplay.warp.persist.PersistenceFilter
All Implemented Interfaces:
javax.servlet.Filter

@ThreadSafe
public class PersistenceFilter
extends Object
implements javax.servlet.Filter

Apply this filter to enable the HTTP Request unit of work and to have Warp Persist manage the lifecycle of all the active (module installed) PersistenceService instances. The filter automatically starts and stops all registered PersistenceService instances upon Filter.init(javax.servlet.FilterConfig) and Filter.destroy(). To disable the managing of PersistenceService instances, override Filter.init(javax.servlet.FilterConfig) and Filter.destroy().

To be able to use UnitOfWork.REQUEST, register this filter once in the web.xml or using Guice's 2.0 ServletModule. It is important that you register this filter before any other framework filter (except the Guice servlet filter). Example configuration:

<filter>
   <filter-name>persistenceFilter</filter-name>
   <filter-class>com.wideplay.warp.persist.PersistenceFilter</filter-class>
 </filter>

 <filter-mapping>
   <filter-name>persistenceFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>
 

Important note: Filter.init(javax.servlet.FilterConfig) will have no effect if Guice has not been started before it gets called. Usually this means Guice should be started in a ServletContextListener. If you can't for some reason, don't worry; all PersistenceService instances will automatically start when they first get used. Just make sure you don't get any incoming request before Guice starts.

Even though all mutable state is package local, this Filter is thread safe. This allows people to create injectors concurrently and deploy multiple Warp Persist applications within the same VM / web container.

Since:
2.0
See Also:
UnitOfWork

Constructor Summary
PersistenceFilter()
           
 
Method Summary
 void destroy()
          Stops all registered PersistenceService instances.
 void doFilter(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, javax.servlet.FilterChain filterChain)
          Activates the HTTP request unit of work.
 void init(javax.servlet.FilterConfig filterConfig)
          Starts all registered PersistenceService instances.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PersistenceFilter

public PersistenceFilter()
Method Detail

init

public void init(javax.servlet.FilterConfig filterConfig)
          throws javax.servlet.ServletException
Starts all registered PersistenceService instances.

Specified by:
init in interface javax.servlet.Filter
Parameters:
filterConfig - the filter config
Throws:
javax.servlet.ServletException - when one or more {code PersistenceService} instances could not be started

destroy

public void destroy()
Stops all registered PersistenceService instances.

Specified by:
destroy in interface javax.servlet.Filter

doFilter

public void doFilter(javax.servlet.ServletRequest servletRequest,
                     javax.servlet.ServletResponse servletResponse,
                     javax.servlet.FilterChain filterChain)
              throws IOException,
                     javax.servlet.ServletException
Activates the HTTP request unit of work.

Specified by:
doFilter in interface javax.servlet.Filter
Parameters:
servletRequest - HTTP request
servletResponse - HTTP response
filterChain - filter chain
Throws:
IOException
javax.servlet.ServletException