Writing your own rule
Problem
You have some custom logic that you want to use to determine whether an interceptor should be applied.
Solution
Write a class that implements InterceptionRule.
The InterceptionRule
interface defines two methods. The shouldIntercept(Class<T>, T , MethodInterceptor)
method is used to
deterime whether to apply the interceptor to any of the methods on a given object. The shouldIntercept(WrappedMethodInvocation, MethodInterceptor)
method is used to deterime whether individual methods should intercepted.
public ObjectInterceptionStrategy shouldIntercept(Class<T> type, T endPoint, MethodInterceptor interceptor)
The DynamicInterceptorFactory
will call this method to determine whether "interceptor
" should be applied around the object "endPoint
".
The class "type
" is the interface behind which "endPoint
" is being proxied (i.e. it whatever value was given in the first argument to
InterceptionInjector.wrapObject(...)).
If the return value from this method indicates that interception is not required, then the interceptor will not be applied to then end point (i.e. the
DynamicInterceptorFactory
will return null) and shouldIntercept(WrappedMethodInvocation, MethodInterceptor)
will not be called.
If the return value from the method indicates that interception is required for all methods, then the interceptor will be applied to the end point
and shouldIntercept(WrappedMethodInvocation, MethodInterceptor)
will not be called (this is because it has already been established that all methods
require interception and so performing additional checks on each method is superfluous and inefficient).
If the return value from the method indicates that interception may be required for some methods, then the interceptor will be wrapped in a
ConditionalInterceptor
and this will be applied to the end point.
The shouldIntercept(WrappedMethodInvocation, MethodInterceptor)
method will be called as appropriate (see below) to determine which methods to
intercept.
The method must return a member of the ObjectInterceptionStrategy
enum. There are a number of possible values that can be used, as described in
the javadoc however the majority of them are only required if a
cache is being used. In other cases the three values: ALWAYS_INTERCEPT_OBJECT
,
NEVER_INTERCEPT_OBJECT
and INTERCEPT_SOME_METHODS_ON_OBJECT
should be sufficient.
public MethodInterceptionStrategy shouldIntercept(WrappedMethodInvocation invocation, MethodInterceptor interceptor)
This method will be called if the ObjectInterceptionStrategy
indicated that some (but not all) methods should be intercepted.
The invocation
parameter will indicate which method is being called, and with which arguments, and the interceptor
parameter
refers to the interceptor for which this rule is being applied.
As with the ObjectInterceptionStrategy
there are a number of possible return values for the MethodInterceptionStrategy
,
but the two of most relevance are: INTERCEPT_THIS_CALL
and DONT_INTERCEPT_THIS_CALL
.