public class DynamicInterceptor
extends java.lang.Object
implements java.lang.reflect.InvocationHandler
This class has been extracted and generalized from alma.acs.container.ContainerSealant in module jcont,
to allow reusing the code for intercepting other calls as well.
| Modifier and Type | Class and Description |
|---|---|
static interface |
DynamicInterceptor.InterceptionHandler
Users of the
DynamicInterceptor class must provide a DynamicInterceptor.InterceptionHandlerFactory
which will be used to create an instance of this DynamicInterceptor.InterceptionHandler for every intercepted call. |
static interface |
DynamicInterceptor.InterceptionHandlerFactory
We use a factory to allow for the convenient (though slightly less performant) design
of grouping the methods for notification before and after the intercepted call is performed
into a single dedicated
DynamicInterceptor.InterceptionHandler object. |
| Modifier | Constructor and Description |
|---|---|
protected |
DynamicInterceptor(java.lang.Object delegate,
java.util.logging.Logger logger,
java.lang.ClassLoader contextCL,
DynamicInterceptor.InterceptionHandlerFactory interceptionHandlerFactory)
To be called only from the proxy factory method
createDynamicInterceptor(Class, Object, Logger, ClassLoader, InterceptionHandlerFactory) |
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
createDynamicInterceptor(java.lang.Class<T> dynInterface,
java.lang.Object delegate,
java.util.logging.Logger logger,
java.lang.ClassLoader contextCL,
DynamicInterceptor.InterceptionHandlerFactory interceptionHandlerFactory)
Creates a
DynamicInterceptor instance and uses it as the invocation handler for the returned dynamic proxy
which implements dynInterface. |
java.lang.Object |
invoke(java.lang.Object proxy,
java.lang.reflect.Method method,
java.lang.Object[] args)
Receives the intercepted calls, and forwards them to the handler and delegate object.
|
protected DynamicInterceptor(java.lang.Object delegate,
java.util.logging.Logger logger,
java.lang.ClassLoader contextCL,
DynamicInterceptor.InterceptionHandlerFactory interceptionHandlerFactory)
createDynamicInterceptor(Class, Object, Logger, ClassLoader, InterceptionHandlerFactory)public static <T> T createDynamicInterceptor(java.lang.Class<T> dynInterface,
java.lang.Object delegate,
java.util.logging.Logger logger,
java.lang.ClassLoader contextCL,
DynamicInterceptor.InterceptionHandlerFactory interceptionHandlerFactory)
DynamicInterceptor instance and uses it as the invocation handler for the returned dynamic proxy
which implements dynInterface.
Note that if we want to allow a chain of handlers for each call, e.g. to more cleanly separate logging from permission checks,
then we should change this method to accept a List<InterceptionHandlerFactory>, to create many
InterceptionHandler objects for each call. These handlers must then be chained together in a way that the call aborts after the first
handler's callReceived has returned false, and the return value of the first handler's callFinished
is fed to the second handler as the retVal argument.
dynInterface - delegate - The delegation object.
logger - The Logger to be used by this class.contextCL - The class loader to be associated with the current thread during the forwarding of the call to the delegate object,
or null if the interceptor should not swap the classloader.interceptionHandlerFactory - Will be used to create the callback object (one per invocation).public java.lang.Object invoke(java.lang.Object proxy,
java.lang.reflect.Method method,
java.lang.Object[] args)
throws java.lang.Throwable
createDynamicInterceptor(Class, Object, Logger, ClassLoader, InterceptionHandlerFactory)
as the current thread's context class loader (see Thread.currentThread().getContextClassLoader()).invoke in interface java.lang.reflect.InvocationHandlerjava.lang.ThrowableInvocationHandler.invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])