Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.struts2.interceptor
Class ScopeInterceptor

    java.lang.Object
      com.opensymphony.xwork2.interceptor.AbstractInterceptor
          org.apache.struts2.interceptor.ScopeInterceptor

All Implemented Interfaces:
  Interceptor, PreResultListener, Serializable

    public class ScopeInterceptor

extends AbstractInterceptor
implements 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 `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>

See Also:
  Serialized Form

Field Summary
`private String[]` | `application`
`private boolean` | `autoCreateSession`
`private String` | `key`
`private static Map` | `locks`
`private static Log` | `LOG`
`private static Object` | `NULL`
`private boolean` | `reset`
`private static long` | `serialVersionUID`
`private String[]` | `session`
`private String` | `sessionReset`
`private String` | `type`

Constructor Summary
`ScopeInterceptor()` The constructor

Method Summary
`protected void` | `after(ActionInvocation invocation, String result)`
`protected void` | `before(ActionInvocation invocation)`
`void` | `beforeResult(ActionInvocation invocation, String resultCode)` This callback method will be called after the Action execution and before the Result execution.
`private String` | `getKey(ActionInvocation invocation)`
`String` | `getSessionReset()`
`String` | `getType()`
`String` | `intercept(ActionInvocation invocation)` Override to handle interception
`boolean` | `isReset()`
`(package private) static void` | `lock(Object o, ActionInvocation invocation)`
`private static Object` | `nullConvert(Object o)`
`void` | `setApplication(String s)` Sets a list of application scoped properties
`void` | `setAutoCreateSession(String value)` Sets if the session should be automatically created
`void` | `setKey(String key)`
`void` | `setReset(boolean reset)`
`void` | `setSession(String s)` Sets a list of session scoped properties
`void` | `setSessionReset(String sessionReset)`
`void` | `setType(String type)` Sets the type of scope operation
`(package private) static void` | `unlock(Object o)`

Methods inherited from class com.opensymphony.xwork2.interceptor.AbstractInterceptor
`destroy, init`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### serialVersionUID

    private static final long serialVersionUID

See Also:
  Constant Field Values

### LOG

    private static final Log LOG

### application

    private String[] application

### session

    private String[] session

### key

    private String key

### type

    private String type

### autoCreateSession

    private boolean autoCreateSession

### sessionReset

    private String sessionReset

### reset

    private boolean reset

### NULL

    private static final Object NULL

### locks

    private static Map locks

Constructor Detail

### ScopeInterceptor

    public ScopeInterceptor()

  The constructor

Method Detail

### setApplication

    public void setApplication(String s)

  Sets a list of application scoped properties

  Parameters:
    `s` - A comma-delimited list

### setSession

    public void setSession(String s)

  Sets a list of session scoped properties

  Parameters:
    `s` - A comma-delimited list

### setAutoCreateSession

    public void setAutoCreateSession(String value)

  Sets if the session should be automatically created

  Parameters:
    `value` - True if it should be created

### getKey

    private String getKey(ActionInvocation invocation)

### nullConvert

    private static final Object nullConvert(Object o)

### lock

    static final void lock(Object o,
                           ActionInvocation invocation)
                    throws Exception

  Throws:
    `Exception`

### unlock

    static final void unlock(Object o)

### after

    protected void after(ActionInvocation invocation,
                         String result)
                  throws Exception

  Throws:
    `Exception`

### before

    protected void before(ActionInvocation invocation)
                   throws Exception

  Throws:
    `Exception`

### setKey

    public void setKey(String key)

### beforeResult

    public void beforeResult(ActionInvocation invocation,
                             String resultCode)

  Description copied from interface: `PreResultListener`
  This callback method will be called after the Action execution and before the Result execution.

  Specified by:
    `beforeResult` in interface `PreResultListener`

### getType

    public String getType()

  Returns:
    The type of scope operation, "start" or "end"

### setType

    public void setType(String type)

  Sets the type of scope operation

  Parameters:
    `type` - Either "start" or "end"

### getSessionReset

    public String getSessionReset()

  Returns:
    Gets the session reset parameter name

### setSessionReset

    public void setSessionReset(String sessionReset)

  Parameters:
    `sessionReset` - The session reset parameter name

### intercept

    public String intercept(ActionInvocation invocation)
                     throws Exception

  Description copied from class: `AbstractInterceptor`
  Override to handle interception

  Specified by:
    `intercept` in interface `Interceptor`
  Specified by:
    `intercept` in class `AbstractInterceptor`
  Returns:
    the return code, either returned from `ActionInvocation.invoke()`, or from the interceptor itself.
  Throws:
    `Exception` - any system-level error, as defined in `Action.execute()`.

### isReset

    public boolean isReset()

  Returns:
    True if the scope is reset

### setReset

    public void setReset(boolean reset)

  Parameters:
    `reset` - True if the scope should be reset

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2000-2007 Apache Software Foundation. All Rights Reserved.