[cdi-dev] Interceptor bindings from interceptor when using 2.0 interception factory

Martin Kouba mkouba at redhat.com
Tue Sep 19 07:59:12 EDT 2017


Dne 19.9.2017 v 13:37 arjan tijms napsal(a):
> Hi,
> 
> On Mon, Aug 28, 2017 at 9:41 AM, Martin Kouba <mkouba at redhat.com 
> <mailto:mkouba at redhat.com>> wrote:
> 
>         Set<Annotation> bindings = (Set<Annotation>)
>         invocationContext.getContextData().get("org.jboss.weld.interceptor.bindings");
> 
> 
>     In fact, a more safe way is to use the Weld API, cast the invocation
>     context to org.jboss.weld.interceptor.WeldInvocationContext and use
>     the appropriate methods.
> 
> 
> Looking in the code that seems to do the same thing, at least in Weld 3.0.1.

No really. Weld stores the bindings also in context data but the API is 
using a direct reference to a stored set.

> 
> Going via getContextData() is much easier though for code that has to 
> support both Weld and OWB.

Yep, standardizing the key is the only viable solution without 
interceptors spec change. However, it's not typesafe, easy-to-use, etc.

> 
> 
> 
>     Yes. There is already a spec issue:
>     https://issues.jboss.org/browse/CDI-468
>     <https://issues.jboss.org/browse/CDI-468> but this would require a
>     change in interceptors spec.
> 
> 
> I read the issue, thanks for the pointer. I do wonder if the entry in 
> the context data isn't the easiest option regardless. That might only 
> require an update in the CDI spec. Essentially, just standardise the key 
> being used?
> 
> Currently the interceptor factory is an absolutely fantastic feature, 
> but its usefulness is greatly diminished by the fact that no interceptor 
> at this moment can (reliably) retrieve the interceptor bindings for all 
> CDI implementations.
> 
> The spec even uses dynamically adding @Transactional as an example;
> 
>   * <pre>
>   * &#64;Produces
>   * &#64;RequestScoped
>   * public MyClass produceMyClass(InterceptionFactory<MyClass> factory) {
>   *     factory.configure().add(new AnnotationLiteral<Transactional>() {
>   *     });
>   *     return factory.createInterceptedInstance(new MyClass());
>   * }
> 
> 
> But all the current interceptor implementations backing @Transactional 
> do something like:
> 
> public Object proceed(InvocationContext ctx) throws Exception {
>          Transactional transactionalAnnotation =
>                  ctx.getMethod().getAnnotation(Transactional.class);

And this is obviously wrong. Recently, we've reviewed a Narayana PR 
which is about to fix this problem:
https://github.com/jbosstm/narayana/pull/1211

I personally think that JTA TCK is missing some essential tests here.

> 
> So that clearly won't work.
> 
> The spec text for InterceptionFactory also doesn't mention this caveat 
> at all. So I wonder, what was the intended approach for   
> InterceptionFactory and intercepter bindings with attributes?
> 
> Or was InterceptionFactory solely intended for intercepter bindings 
> without (non-binding) attributes?

I don't think this is an InterceptionFactory-specific problem. It's a 
general issue. It should work the same if you use an extension to add an 
interceptor binding.

> 
> Kind regards,
> Arjan Tijms
> 
> 
> 
> 
> 
>         Is there a standard way to get the bindings? Perhaps getting
>         hold of the Bean<T> that represents the current Interceptor?
> 
> 
>     You can inject a bean with scope @Dependent, qualifier @Default and
>     type Interceptor into any interceptor instance. However, this will
>     not help for @Nonbinding value members of an interceptor binding.
> 
> 
>         Kind regards,
>         Arjan Tijms
> 
> 
>         _______________________________________________
>         cdi-dev mailing list
>         cdi-dev at lists.jboss.org <mailto:cdi-dev at lists.jboss.org>
>         https://lists.jboss.org/mailman/listinfo/cdi-dev
>         <https://lists.jboss.org/mailman/listinfo/cdi-dev>
> 
>         Note that for all code provided on this list, the provider
>         licenses the code under the Apache License, Version 2
>         (http://www.apache.org/licenses/LICENSE-2.0.html
>         <http://www.apache.org/licenses/LICENSE-2.0.html>). For all
>         other ideas provided on this list, the provider waives all
>         patent and other intellectual property rights inherent in such
>         information.
> 
> 
>     -- 
>     Martin Kouba
>     Senior Software Engineer
>     Red Hat, Czech Republic
> 
> 

-- 
Martin Kouba
Senior Software Engineer
Red Hat, Czech Republic


More information about the cdi-dev mailing list