I use Java EE and WebLogic server. I declare interceptor with annotated style: @Interceptor. I need to add some functionality which would disable certain interceptor. I use annotation to mark methods when Interceptor needs to invoke.
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Traced {
}
@Traced
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
public class MethodInvocationInterceptor implements Serializable {
private static final Logger LOG = Logger.getLogger(MethodInvocationInterceptor.class);
@AroundInvoke
public Object intercept(final InvocationContext ctx) throws Exception {
LOG.trace(ctx.getMethod().getName() + " method started");
return ctx.proceed();
}
There are three reasonable approaches I can think of.
First is same as what @Nikos suggested, keep interceptor enabled at all times and just add conditional logic based on configuration.
Second approach requires CDI extension where you monitor AfterTypeDiscovery
event. This allows you to get hold of lists of alternatives, interceptors and decorators. Those lists are mutable and once you change them, container has to take the changes values into consideration. Removing the interceptor should result in interceptor being disabled.
This approach, again with CDI extension, requires you to monitor for the AnnotatedType
of interceptor using ProcessAnnotatedType
event and disable it using veto()
method. Alternatively, if vetoing doesn't cut it, you can alter the AnnotatedType
and remove/replace the interceptor binding (although that approach sounds a bit "hacky" as you end with an interceptor without bindings).