Skip to content

Interceptors

papousek edited this page Jul 30, 2012 · 1 revision

Intercept Objects Managed by Parasim

Interceptors are an easy way to influence the behaviour of some classes. The Parasim Core allows you to register your own interceptor which will be invoked whenever a method of an instance processed by Parasim is invoked.

To create your own interceptor implement Interceptor interface and inside the intercept method use Invocation parameter to continue in the invocation.

public class MyInterceptor implements Interceptor {
    @Override
    public Object intercept(final Object obj, final Method method, Object[] args, Invocation invocation) throws Throwable {
        // some stuff around
        Object result = invocation.invoke(obj, method, args);
        // some stuff around
        return result;
    }
}

In your loadable extension just put the following code:

builder.service(Interceptor.class, MyInterceptor.class);

Intercept External Object

To intercept an object which is not managed by Parasim use InterceptorRegistry. The class of the intercepted object can't be final and has to have an empty constructor.

InterceptorRegistry interceptorRegistry = manager.resolve(InterceptorRegistry.class, Standalone.class, manager.getRootContext());
if (!interceptorRegistry.canBeIntercepted(myObject)) {
    // the object can't be intercepted
} else {
    Object interceptedReference = interceptorRegistry.intercepted(instance).intercept(myInterceptor).getProxyObject();   
}