org.mockejb.interceptor
Class InvocationContext

java.lang.Object
  extended byorg.mockejb.interceptor.InvocationContext

public class InvocationContext
extends java.lang.Object

Performs the invocation of interceptors in their order in the interceptor list. Each interceptor is called in turn until we get to the target object. At this point, the target object's method is called using reflection. Also keeps the invocation's custom context (properties). To be thread safe, clients should create a new object of this class for each method call.

Author:
Alexander Ananiev

Constructor Summary
InvocationContext(java.util.List interceptorList, java.lang.Object proxyObj, java.lang.reflect.Method proxyMethod, java.lang.Object targetObj, java.lang.reflect.Method targetMethod, java.lang.Object[] paramVals)
          Creates a new instance of the InvocationContext.
InvocationContext(java.util.List interceptorList, java.lang.Object proxyObj, java.lang.reflect.Method proxyMethod, java.lang.Object targetObj, java.lang.reflect.Method targetMethod, java.lang.Object[] paramVals, java.util.Map contextProperties)
          Creates a new instance of the InvocationContext.
 
Method Summary
 void clear()
          Clears the context properties and resets the interceptor iterator.
 java.lang.reflect.Method getInterceptedMethod()
          Deprecated. Use getProxyMethod instead
 java.lang.Object getInterceptedObject()
          Deprecated. Use getProxyObject instead
 java.util.ListIterator getInterceptorIterator()
          Returns the iterator currently in use to traverse the interceptor list.
 java.util.List getInterceptorList()
           
 java.lang.Object getOptionalPropertyValue(java.lang.String key)
          Returns the context property value associated with the provided key or null if the key is not found
 java.lang.Object[] getParamVals()
           
 java.lang.Object getPropertyValue(java.lang.String key)
          Returns the custome context's property value associated with the provided key or throws IllegalStateException if the key is not found
 java.lang.reflect.Method getProxyMethod()
          Returns the proxy method, the method that was called on the proxy.
 java.lang.Object getProxyObject()
          Returns the proxy object.
 java.lang.Object getReturnObject()
          Returns the return value of the invocation.
 java.lang.reflect.Method getTargetMethod()
          Returns the target method of the invocation.
 java.lang.Object getTargetObject()
          Returns the target object of the invocation.
 java.lang.Object getThrownThrowable()
          Returns the throwable thrown by the target method or by one of the interceptors.
protected  java.lang.Object invokeMethod(java.lang.Object targetObj, java.lang.reflect.Method method, java.lang.Object[] paramVals)
          Calls the object's method using reflection.
 void proceed()
          Calls the next interceptor in the list.
 void reset()
          Deprecated.  
 void setContext(java.lang.String key, java.lang.Object data)
          Adds the invocation context property.
 void setInterceptorList(java.util.List interceptorList)
          Sets the list of interceptors
 void setReturnObject(java.lang.Object returnObject)
          Sets the return value of the invocation.
 void setThrownThrowable(java.lang.Throwable throwable)
          Sets the throwable thrown by the invoked method
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

InvocationContext

public InvocationContext(java.util.List interceptorList,
                         java.lang.Object proxyObj,
                         java.lang.reflect.Method proxyMethod,
                         java.lang.Object targetObj,
                         java.lang.reflect.Method targetMethod,
                         java.lang.Object[] paramVals)
Creates a new instance of the InvocationContext.

Parameters:
interceptorList - interceptors that will be invoked before the target method
proxyObj - object that was intercepted, most likely it is the dynamic proxy object. Can be null.
proxyMethod - method invoked on the proxy. The declaring class of the method is the interface's class.
targetObj - target object being called.
targetMethod - method being called.
paramVals - parameter values

InvocationContext

public InvocationContext(java.util.List interceptorList,
                         java.lang.Object proxyObj,
                         java.lang.reflect.Method proxyMethod,
                         java.lang.Object targetObj,
                         java.lang.reflect.Method targetMethod,
                         java.lang.Object[] paramVals,
                         java.util.Map contextProperties)
Creates a new instance of the InvocationContext.

Parameters:
interceptorList - interceptors that will be invoked before the target method
proxyObj - object that was intercepted, most likely it is the dynamic proxy object. Can be null if the object is not known.
proxyMethod - method invoked on the proxy. The declaring class of the method is the interface's class.
targetObj - target object being called.
targetMethod - method being called.
paramVals - parameter values
contextProperties - any additional context info for the interceptors
Method Detail

setInterceptorList

public void setInterceptorList(java.util.List interceptorList)
Sets the list of interceptors

Parameters:
interceptorList - list to set

getInterceptorList

public java.util.List getInterceptorList()

getInterceptorIterator

public java.util.ListIterator getInterceptorIterator()
Returns the iterator currently in use to traverse the interceptor list. Clients can use the returned iterator to find out their place in the call chain.

Returns:
list iterator

reset

public void reset()
Deprecated.  

Resets the interceptor iterator.


clear

public void clear()
Clears the context properties and resets the interceptor iterator.


proceed

public void proceed()
             throws java.lang.Exception
Calls the next interceptor in the list. If this is the end of the list, calls the given method of the target object using reflection if the target object is not null. "proceed" name is consistent with the "proceed" keyword used by AspectJ for "around" advices. Use "getReturnObject" to get the return value for this invocation.

Throws:
java.lang.Exception

getProxyObject

public java.lang.Object getProxyObject()
Returns the proxy object. This is a dynamic proxy object implementing an interface or a CGLIB-enhanced class

Returns:
intercepted object

getTargetObject

public java.lang.Object getTargetObject()
Returns the target object of the invocation. This is the target object being called in response to the call of the proxy (interface).

Returns:
target object

getTargetMethod

public java.lang.reflect.Method getTargetMethod()
Returns the target method of the invocation. This is the target method being called in response to the call to the proxy's method. For example, "find" method of the Entity business interface is the intercepted method, whereas "ejbFind" method of the entity implementation class is the target method.

Returns:
method

getInterceptedObject

public java.lang.Object getInterceptedObject()
Deprecated. Use getProxyObject instead

Returns:
proxy object

getInterceptedMethod

public java.lang.reflect.Method getInterceptedMethod()
Deprecated. Use getProxyMethod instead


getProxyMethod

public java.lang.reflect.Method getProxyMethod()
Returns the proxy method, the method that was called on the proxy. For example, "find" method of the Entity business interface is the proxy method, and "ejbFind" method of the entity implementation class is the target method.

Returns:
proxy method

getParamVals

public java.lang.Object[] getParamVals()

getReturnObject

public java.lang.Object getReturnObject()
Returns the return value of the invocation. Normally, this is a return value of the target method, however interceptors can change it.

Returns:
Object or null if the method has void type or if the method threw exception

setReturnObject

public void setReturnObject(java.lang.Object returnObject)
Sets the return value of the invocation. This allows interceptors to change the current return value.

Parameters:
returnObject - return object to set

getThrownThrowable

public java.lang.Object getThrownThrowable()
Returns the throwable thrown by the target method or by one of the interceptors.

Returns:
throwable or null if no exceptions were thrown during the invocation

setThrownThrowable

public void setThrownThrowable(java.lang.Throwable throwable)
Sets the throwable thrown by the invoked method

Parameters:
throwable -

setContext

public void setContext(java.lang.String key,
                       java.lang.Object data)
Adds the invocation context property. Context property is a piece of data made available to all interceptors. Interceptors can add/modify the context properties during the call.

Parameters:
key - key for this contextProperties's data
data - contextProperties data

getPropertyValue

public java.lang.Object getPropertyValue(java.lang.String key)
Returns the custome context's property value associated with the provided key or throws IllegalStateException if the key is not found

Parameters:
key - contextProperties key
Returns:
contextProperties data

getOptionalPropertyValue

public java.lang.Object getOptionalPropertyValue(java.lang.String key)
Returns the context property value associated with the provided key or null if the key is not found

Parameters:
key - contextProperties key
Returns:
contextProperties data

invokeMethod

protected java.lang.Object invokeMethod(java.lang.Object targetObj,
                                        java.lang.reflect.Method method,
                                        java.lang.Object[] paramVals)
                                 throws java.lang.Throwable
Calls the object's method using reflection. This method takes InvocationTargetException out of the stack in case of exception. This allows exception handlers not to deal with reflection-specific exceptions.

Parameters:
targetObj - target object being called
method - method being called
paramVals - parameter values
Returns:
value returned by the given method
Throws:
java.lang.Throwable

toString

public java.lang.String toString()