Java Reference
In-Depth Information
public Object[] getParameters();
public void setParameters(Object[]);
public java.util.Map<String,Object> getContextData();
public Object proceed() throws Exception;
}
The getTarget() method retrieves the bean instance that the intercepted method be-
longs to. This method is particularly valuable for checking the current state of the bean
through its instance variables or accessor methods.
The getMethod() method returns the method of the bean class for which the interceptor
was invoked. For @AroundInvoke methods, this is the business method on the bean
class; for lifecycle callback interceptor methods, getMethod() returns null.
The getParameters() method returns the parameters passed to the intercepted method
as an array of objects. The setParameters() method, on the other hand, allows you
to change these values at runtime before they're passed to the method. These two methods
are helpful for interceptors that manipulate bean parameters to change behavior at runtime.
An interceptor in ActionBazaar that transparently rounds off all monetary values to two
decimal places for all methods across the application could use the getParameters()
and setParameters() methods to accomplish its task.
The key to understanding the need for the InvocationCon-
text.getContextData() method is the fact that contexts are shared across the inter-
ceptor chain for a given method. As a result, data attached to an InvocationContext
can be used to communicate between interceptors. For example, assume that the security
validation interceptor stores the member status into invocation context data after the user is
validated:
invocationContext.getContextData().put("MemberStatus", "Gold");
As you can see, the invocation context data is simply a map used to store name/value pairs.
Another interceptor in the invocation chain can now retrieve this data and take specific ac-
tions based on the member status. For example, a discount calculator interceptor can reduce
the ActionBazaar item listing charges for a Gold member. The code to retrieve the member
status would look like this:
String memberStatus =
(String) invocationContext.getContextData().get("MemberStatus");
Search WWH ::




Custom Search