FileDocCategorySizeDatePackage
ScopeInterceptor.javaAPI DocExample15744Mon Jul 23 13:26:52 BST 2007org.apache.struts2.interceptor

ScopeInterceptor

public class ScopeInterceptor extends com.opensymphony.xwork2.interceptor.AbstractInterceptor implements com.opensymphony.xwork2.interceptor.PreResultListener
This is designed to solve a few simple issues related to wizard-like functionality in Struts. One of those issues is that some applications have a application-wide parameters commonly used, such pageLen (used for records per page). Rather than requiring that each action check if such parameters are supplied, this interceptor can look for specified parameters and pull them out of the session.

This works by setting listed properties at action start with values from session/application attributes keyed after the action's class, the action's name, or any supplied key. After action is executed all the listed properties are taken back and put in session or application context.

To make sure that each execution of the action is consistent it makes use of session-level locking. This way it guarantees that each action execution is atomic at the session level. It doesn't guarantee application level consistency however there has yet to be enough reasons to do so. Application level consistency would also be a big performance overkill.

Note that this interceptor takes a snapshot of action properties just before result is presented (using a {@link PreResultListener}), rather than after action is invoked. There is a reason for that: At this moment we know that action's state is "complete" as it's values may depend on the rest of the stack and specifically - on the values of nested interceptors.

Interceptor parameters:

  • session - a list of action properties to be bound to session scope
  • application - a list of action properties to be bound to application scope
  • key - a session/application attribute key prefix, can contain following values:
    • CLASS - that creates a unique key prefix based on action namespace and action class, it's a default value
    • ACTION - creates a unique key prefix based on action namespace and action name
    • any other value is taken literally as key prefix
  • type - with one of the following
    • start - means it's a start action of the wizard-like action sequence and all session scoped properties are reset to their defaults
    • end - means that session scoped properties are removed from session after action is run
    • any other value or no value means that it's in-the-middle action that is set with session properties before it's executed, and it's properties are put back to session after execution
  • sessionReset - boolean value causing all session values to be reset to action's default values or application scope values, note that it is similliar to type="start" and in fact it does the same, but in our team it is sometimes semantically preferred. We use session scope in two patterns - sometimes there are wizzard-like action sequences that have start and end, and sometimes we just want simply reset current session values.

Extending the interceptor:

There are no know extension points for this interceptor.

Example code:


<!-- As the filter and orderBy parameters are common for all my browse-type actions,
you can move control to the scope interceptor. In the session parameter you can list
action properties that are going to be automatically managed over session. You can
do the same for application-scoped variables-->
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="hibernate"/>
<interceptor-ref name="scope">
<param name="session">filter,orderBy</param>
<param name="autoCreateSession">true</param>
</interceptor-ref>
<result name="success">good_result.ftl</result>
</action>

Fields Summary
private static final long
serialVersionUID
private static final Log
LOG
private String[]
application
private String[]
session
private String
key
private String
type
private boolean
autoCreateSession
private String
sessionReset
private boolean
reset
private static final Object
NULL
private static Map
locks
Constructors Summary
public ScopeInterceptor()
The constructor

        super();
    
Methods Summary
protected voidafter(com.opensymphony.xwork2.ActionInvocation invocation, java.lang.String result)

        Map ses = ActionContext.getContext().getSession();
        if ( ses != null) {
            unlock(ses);
        }
    
protected voidbefore(com.opensymphony.xwork2.ActionInvocation invocation)

        invocation.addPreResultListener(this);
        Map ses = ActionContext.getContext().getSession();
        if (ses == null && autoCreateSession) {
            ses = new SessionMap(ServletActionContext.getRequest());
            ActionContext.getContext().setSession(ses);
        }

        if ( ses != null) {
            lock(ses, invocation);
        }

        String key = getKey(invocation);
        Map app = ActionContext.getContext().getApplication();
        final ValueStack stack = ActionContext.getContext().getValueStack();

        if (LOG.isDebugEnabled()) {
            LOG.debug("scope interceptor before");
        }

        if (application != null)
            for (int i = 0; i < application.length; i++) {
                String string = application[i];
                Object attribute = app.get(key + string);
                if (attribute != null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("application scoped variable set " + string + " = " + String.valueOf(attribute));
                    }

                    stack.setValue(string, nullConvert(attribute));
                }
            }

        if (ActionContext.getContext().getParameters().get(sessionReset) != null) {
            return;
        }

        if (reset) {
            return;
        }

        if (ses == null) {
            LOG.debug("No HttpSession created... Cannot set session scoped variables");
            return;
        }

        if (session != null && (!"start".equals(type))) {
            for (int i = 0; i < session.length; i++) {
                String string = session[i];
                Object attribute = ses.get(key + string);
                if (attribute != null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("session scoped variable set " + string + " = " + String.valueOf(attribute));
                    }
                    stack.setValue(string, nullConvert(attribute));
                }
            }
        }
    
public voidbeforeResult(com.opensymphony.xwork2.ActionInvocation invocation, java.lang.String resultCode)

        String key = getKey(invocation);
        Map app = ActionContext.getContext().getApplication();
        final ValueStack stack = ActionContext.getContext().getValueStack();

        if (application != null)
            for (int i = 0; i < application.length; i++) {
                String string = application[i];
                Object value = stack.findValue(string);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("application scoped variable saved " + string + " = " + String.valueOf(value));
                }

                //if( value != null)
                app.put(key + string, nullConvert(value));
            }

        boolean ends = "end".equals(type);

        Map ses = ActionContext.getContext().getSession();
        if (ses != null) {

            if (session != null) {
                for (int i = 0; i < session.length; i++) {
                    String string = session[i];
                    if (ends) {
                        ses.remove(key + string);
                    } else {
                        Object value = stack.findValue(string);

                        if (LOG.isDebugEnabled()) {
                            LOG.debug("session scoped variable saved " + string + " = " + String.valueOf(value));
                        }

                        // Null value should be scoped too
                        //if( value != null)
                        ses.put(key + string, nullConvert(value));
                    }
                }
            }
            unlock(ses);
        } else {
            LOG.debug("No HttpSession created... Cannot save session scoped variables.");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("scope interceptor after (before result)");
        }
    
private java.lang.StringgetKey(com.opensymphony.xwork2.ActionInvocation invocation)

        ActionProxy proxy = invocation.getProxy();
        if (key == null || "CLASS".equals(key)) {
            return "struts.ScopeInterceptor:" + proxy.getAction().getClass();
        } else if ("ACTION".equals(key)) {
            return "struts.ScopeInterceptor:" + proxy.getNamespace() + ":" + proxy.getActionName();
        }
        return key;
    
public java.lang.StringgetSessionReset()

return
Gets the session reset parameter name

        return sessionReset;
    
public java.lang.StringgetType()

return
The type of scope operation, "start" or "end"

        return type;
    
public java.lang.Stringintercept(com.opensymphony.xwork2.ActionInvocation invocation)

        String result = null;
        Map ses = ActionContext.getContext().getSession();
        before(invocation);
        try {
            result = invocation.invoke();
            after(invocation, result);
        } finally {
            if (ses != null) {
                unlock(ses);
            }
        }

        return result;
    
public booleanisReset()

return
True if the scope is reset

        return reset;
    
static final voidlock(java.lang.Object o, com.opensymphony.xwork2.ActionInvocation invocation)


             
        synchronized (o) {
            int count = 3;
            Object previous = null;
            while ((previous = locks.get(o)) != null) {
                if (previous == invocation) {
                    return;
                }
                if (count-- <= 0) {
                    locks.remove(o);
                    o.notify();

                    throw new StrutsException("Deadlock in session lock");
                }
                o.wait(10000);
            }
            ;
            locks.put(o, invocation);
        }
    
private static final java.lang.ObjectnullConvert(java.lang.Object o)


          
        if (o == null) {
            return NULL;
        }

        if (o == NULL || NULL.equals(o)) {
            return null;
        }

        return o;
    
public voidsetApplication(java.lang.String s)
Sets a list of application scoped properties

param
s A comma-delimited list


                     
        
        if (s != null) {
            application = s.split(" *, *");
        }
    
public voidsetAutoCreateSession(java.lang.String value)
Sets if the session should be automatically created

param
value True if it should be created

        if (value != null && value.length() > 0) {
            this.autoCreateSession = new Boolean(value).booleanValue();
        }
    
public voidsetKey(java.lang.String key)

        this.key = key;
    
public voidsetReset(boolean reset)

param
reset True if the scope should be reset

        this.reset = reset;
    
public voidsetSession(java.lang.String s)
Sets a list of session scoped properties

param
s A comma-delimited list

        if (s != null) {
            session = s.split(" *, *");
        }
    
public voidsetSessionReset(java.lang.String sessionReset)

param
sessionReset The session reset parameter name

        this.sessionReset = sessionReset;
    
public voidsetType(java.lang.String type)
Sets the type of scope operation

param
type Either "start" or "end"

        type = type.toLowerCase();
        if ("start".equals(type) || "end".equals(type)) {
            this.type = type;
        } else {
            throw new IllegalArgumentException("Only start or end are allowed arguments for type");
        }
    
static final voidunlock(java.lang.Object o)

        synchronized (o) {
            locks.remove(o);
            o.notify();
        }