com.wideplay.warp.persist.jpa
Class SessionPerRequestInterceptor

java.lang.Object
  extended by com.wideplay.warp.persist.PersistenceFilter
      extended by com.wideplay.warp.persist.jpa.SessionPerRequestInterceptor
All Implemented Interfaces:
com.opensymphony.xwork2.interceptor.Interceptor, Serializable, javax.servlet.Filter

@Immutable
@ThreadSafe
public class SessionPerRequestInterceptor
extends PersistenceFilter
implements com.opensymphony.xwork2.interceptor.Interceptor

Struts 2 equivalent for PersistenceFilter. Needed because Guice's current (1.0.1) Struts 2 plugin creates the Guice Injector internally, which means that that injector instance is not available to other filters before the Struts filter executes. Also see here.

Do not use this interceptor in combination with the PersistenceFilter.

This Interceptor starts the PersistenceService at creation time.

It is vital that only one instance of this interceptor exists in an application. Currently Guice can't scope Struts 2 interceptors, so you have to rely on Struts' behaviour in this regard. Struts 2 interceptors are not true singletons; one instance exists per <interceptor-ref> in the XML. To make sure only one instance exists, create an <interceptor-stack> with this single interceptor, and then use the resulting stack in your application. Do not use the interceptor directly.

Example configuration:

<interceptors>
     <interceptor name="sessionPerRequestInterceptor"
                  class="com.wideplay.warp.jpa.SessionPerRequestInterceptor"/>
     <!-- Stack with single interceptor because we only want one instance -->
     <!-- Interceptors = one instance per interceptor-ref -->
     <interceptor-stack name="spriStack">
        <interceptor-ref name="sessionPerRequestInterceptor" />
     </interceptor-stack>
 </interceptors>
 
Example usage:
<interceptor-stack name="securedStack">
     <interceptor-ref name="spriStack" />
     <interceptor-ref name="authenticationInterceptor" />
     <interceptor-ref name="defaultStack" />
 </interceptor-stack>
 

It is likely that this class will be obsolete when Guice 2.0 is released. The hierarchical injectors feature would enable a redesign of Guice's current Struts 2 plugin; you would be able to provide one part of the injector ahead of time and then let the plugin merge your injector with its internal one.

This code originated from the book Google Guice (Apress, 2008, ISBN 978-1590599976).

See Also:
PersistenceFilter, Serialized Form

Constructor Summary
SessionPerRequestInterceptor()
           
 
Method Summary
 void destroy()
          Tries to close the EntityManagerFactory.
 void init()
          Does nothing.
 String intercept(com.opensymphony.xwork2.ActionInvocation ai)
          Makes sure an EntityManager instance is available while the current request is being processed.
 
Methods inherited from class com.wideplay.warp.persist.PersistenceFilter
doFilter, init
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SessionPerRequestInterceptor

public SessionPerRequestInterceptor()
Method Detail

intercept

public String intercept(com.opensymphony.xwork2.ActionInvocation ai)
                 throws Exception
Makes sure an EntityManager instance is available while the current request is being processed.

Specified by:
intercept in interface com.opensymphony.xwork2.interceptor.Interceptor
Throws:
Exception
See Also:
Interceptor.intercept(com.opensymphony.xwork2.ActionInvocation)

init

public void init()
Does nothing.

Specified by:
init in interface com.opensymphony.xwork2.interceptor.Interceptor
See Also:
Interceptor.init()

destroy

public void destroy()
Tries to close the EntityManagerFactory.

Specified by:
destroy in interface com.opensymphony.xwork2.interceptor.Interceptor
Specified by:
destroy in interface javax.servlet.Filter
Overrides:
destroy in class PersistenceFilter
See Also:
Interceptor.destroy()