Easy way to get 'original' type from alternative producers

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Easy way to get 'original' type from alternative producers

arjan tijms
Hi,

When writing an alternative producer, e.g.

@Alternative
@Priority(500)
@ApplicationScoped
public class ApplicationInit {
     
    @Produces
    public HttpAuthenticationMechanism produce(BeanManager beanManager) {
        return ...
    }
}

You not rarely need the bean the producer is going to be an alternative for. For instance to wrap it, or otherwise augment it, or perhaps to take a few values from.

In order to get that bean, a bunch of quite verbose code is needed. I.e I came up with:

HttpAuthenticationMechanism mechanism =
    createRef(
        beanManager.resolve(
            beanManager.getBeans(HttpAuthenticationMechanism.class)
                       .stream()
                       .filter(e -> !e.getBeanClass().equals(ApplicationInit.class))
                       .collect(toSet())), 
            beanManager);


And:

HttpAuthenticationMechanism createRef(Bean<?> bean, BeanManager beanManager) {
    return (HttpAuthenticationMechanism) 
        beanManager.getReference(
            bean, 
            HttpAuthenticationMechanism.class, 
            beanManager.createCreationalContext(bean));
}

I wonder if it would not be a good idea to introduce something to get that original bean more easily, i.e. just like a decorator and @Delegate.

E.g.

@Alternative
@Priority(500)
@ApplicationScoped
public class ApplicationInit {
     
    @Produces
    public HttpAuthenticationMechanism produce(BeanManager beanManager, HttpAuthenticationMechanism original) {
        return ...
    }
}

Or reuse the @Delegate (perhaps with the @Alternative annotation)

@Alternative
@Priority(500)
@ApplicationScoped
public class ApplicationInit {

    @Inject
    @Delegate
    @Alternative (?)
    private HttpAuthenticationMechanism original;
     
    @Produces
    public HttpAuthenticationMechanism produce(BeanManager beanManager) {
        return ...
    }
}

Thoughts?

Kind regards,
Arjan Tijms

_______________________________________________
cdi-dev mailing list
[hidden email]
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). For all other ideas provided on this list, the provider waives all patent and other intellectual property rights inherent in such information.
Reply | Threaded
Open this post in threaded view
|

Re: Easy way to get 'original' type from alternative producers

Martin Kouba

Dne 13.2.2018 v 13:13 arjan tijms napsal(a):

> Hi,
>
> When writing an alternative producer, e.g.
>
> @Alternative
> @Priority(500)
> @ApplicationScoped
> public class ApplicationInit {
>      @Produces
>      public HttpAuthenticationMechanism produce(BeanManager beanManager) {
>          return ...
>      }
> }
>
> You not rarely need the bean the producer is going to be an alternative
> for. For instance to wrap it, or otherwise augment it, or perhaps to
> take a few values from.

I think it could be a nice feature. Pls file a JIRA issue.
>
> In order to get that bean, a bunch of quite verbose code is needed. I.e
> I came up with:
>
> HttpAuthenticationMechanism mechanism =
>      createRef(
>          beanManager.resolve(

This wouldn't work because BeanManager.resolve() attempts to resolve the
ambiguities thus eliminate all non-alternative beans.

>              beanManager.getBeans(HttpAuthenticationMechanism.class)
>                         .stream()
>                         .filter(e ->
> !e.getBeanClass().equals(ApplicationInit.class))
>                         .collect(toSet())),
>              beanManager);
>
>
> And:
>
> HttpAuthenticationMechanism createRef(Bean<?> bean, BeanManager
> beanManager) {
>      return (HttpAuthenticationMechanism)
>          beanManager.getReference(
>              bean,
>              HttpAuthenticationMechanism.class,
>              beanManager.createCreationalContext(bean));
> }
>
> I wonder if it would not be a good idea to introduce something to get
> that original bean more easily, i.e. just like a decorator and @Delegate.
>
> E.g.
>
> @Alternative
> @Priority(500)
> @ApplicationScoped
> public class ApplicationInit {
>      @Produces
>      public HttpAuthenticationMechanism produce(BeanManager beanManager,
> HttpAuthenticationMechanism original) {

This would not work - you would need to distinguish the original
injection point.

>          return ...
>      }
> }
>
> Or reuse the @Delegate (perhaps with the @Alternative annotation)

-1 This might be confusing.

>
> @Alternative
> @Priority(500)
> @ApplicationScoped
> public class ApplicationInit {
>
>      @Inject
>      @Delegate
>      @Alternative (?)
>      private HttpAuthenticationMechanism original;
>      @Produces
>      public HttpAuthenticationMechanism produce(BeanManager beanManager) {
>          return ...
>      }
> }
>
> Thoughts?
>
> Kind regards,
> Arjan Tijms
>
>
> _______________________________________________
> cdi-dev mailing list
> [hidden email]
> 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). 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
_______________________________________________
cdi-dev mailing list
[hidden email]
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). For all other ideas provided on this list, the provider waives all patent and other intellectual property rights inherent in such information.
Reply | Threaded
Open this post in threaded view
|

Re: Easy way to get 'original' type from alternative producers

arjan tijms
Hi,

On Thu, Feb 15, 2018 at 10:57 AM, Martin Kouba <[hidden email]> wrote:

I think it could be a nice feature. Pls file a JIRA issue.

Allright, I'll do that. Thx!

 


In order to get that bean, a bunch of quite verbose code is needed. I.e I came up with:

HttpAuthenticationMechanism mechanism =
     createRef(
         beanManager.resolve(

This wouldn't work because BeanManager.resolve() attempts to resolve the ambiguities thus eliminate all non-alternative beans.

It actually does seem to work, and I've been using it for a while now.

 


This would not work - you would need to distinguish the original injection point.

         return ...
     }
}

Or reuse the @Delegate (perhaps with the @Alternative annotation)

-1 This might be confusing.

Agreed with both, it was just a bit of brain storming. Not sure what would be a better name though. @Original? @NonAlternative?

Kind regards,
Arjan Tijms



_______________________________________________
cdi-dev mailing list
[hidden email]
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). For all other ideas provided on this list, the provider waives all patent and other intellectual property rights inherent in such information.
Reply | Threaded
Open this post in threaded view
|

Re: Easy way to get 'original' type from alternative producers

Martin Kouba
Dne 15.2.2018 v 18:15 arjan tijms napsal(a):

> Hi,
>
> On Thu, Feb 15, 2018 at 10:57 AM, Martin Kouba <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>
>     I think it could be a nice feature. Pls file a JIRA issue.
>
>
> Allright, I'll do that. Thx!
>
>
>
>         In order to get that bean, a bunch of quite verbose code is
>         needed. I.e I came up with:
>
>         HttpAuthenticationMechanism mechanism =
>               createRef(
>                   beanManager.resolve(
>
>
>     This wouldn't work because BeanManager.resolve() attempts to resolve
>     the ambiguities thus eliminate all non-alternative beans.
>
>
> It actually does seem to work, and I've been using it for a while now.

You're right - you're actually filtering the alternative bean
(ApplicationInit producer) and so there is only one bean left.

>
>
>
>     This would not work - you would need to distinguish the original
>     injection point.
>
>                   return ...
>               }
>         }
>
>         Or reuse the @Delegate (perhaps with the @Alternative annotation)
>
>
>     -1 This might be confusing.
>
>
> Agreed with both, it was just a bit of brain storming. Not sure what
> would be a better name though. @Original? @NonAlternative?
>
> Kind regards,
> Arjan Tijms
>
>

--
Martin Kouba
Senior Software Engineer
Red Hat, Czech Republic
_______________________________________________
cdi-dev mailing list
[hidden email]
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). For all other ideas provided on this list, the provider waives all patent and other intellectual property rights inherent in such information.
Reply | Threaded
Open this post in threaded view
|

Re: Easy way to get 'original' type from alternative producers

Matej Novotny
FYI I have taken the liberty to create CDI issue so that this is not forgotten (for I believe that it could be valuable addition).
Feel free to comment - https://issues.jboss.org/browse/CDI-723

Matej

----- Original Message -----

> From: "Martin Kouba" <[hidden email]>
> To: "arjan tijms" <[hidden email]>
> Cc: [hidden email]
> Sent: Friday, February 16, 2018 11:28:10 AM
> Subject: Re: [cdi-dev] Easy way to get 'original' type from alternative producers
>
> Dne 15.2.2018 v 18:15 arjan tijms napsal(a):
> > Hi,
> >
> > On Thu, Feb 15, 2018 at 10:57 AM, Martin Kouba <[hidden email]
> > <mailto:[hidden email]>> wrote:
> >
> >
> >     I think it could be a nice feature. Pls file a JIRA issue.
> >
> >
> > Allright, I'll do that. Thx!
> >
> >
> >
> >         In order to get that bean, a bunch of quite verbose code is
> >         needed. I.e I came up with:
> >
> >         HttpAuthenticationMechanism mechanism =
> >               createRef(
> >                   beanManager.resolve(
> >
> >
> >     This wouldn't work because BeanManager.resolve() attempts to resolve
> >     the ambiguities thus eliminate all non-alternative beans.
> >
> >
> > It actually does seem to work, and I've been using it for a while now.
>
> You're right - you're actually filtering the alternative bean
> (ApplicationInit producer) and so there is only one bean left.
>
> >
> >
> >
> >     This would not work - you would need to distinguish the original
> >     injection point.
> >
> >                   return ...
> >               }
> >         }
> >
> >         Or reuse the @Delegate (perhaps with the @Alternative annotation)
> >
> >
> >     -1 This might be confusing.
> >
> >
> > Agreed with both, it was just a bit of brain storming. Not sure what
> > would be a better name though. @Original? @NonAlternative?
> >
> > Kind regards,
> > Arjan Tijms
> >
> >
>
> --
> Martin Kouba
> Senior Software Engineer
> Red Hat, Czech Republic
> _______________________________________________
> cdi-dev mailing list
> [hidden email]
> 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). For all other ideas
> provided on this list, the provider waives all patent and other intellectual
> property rights inherent in such information.

_______________________________________________
cdi-dev mailing list
[hidden email]
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). For all other ideas provided on this list, the provider waives all patent and other intellectual property rights inherent in such information.
Reply | Threaded
Open this post in threaded view
|

Re: Easy way to get 'original' type from alternative producers

arjan tijms
Thanks! Had it on my todo, but good that you created it ;)

On Tue, Feb 27, 2018 at 2:23 PM, Matej Novotny <[hidden email]> wrote:
FYI I have taken the liberty to create CDI issue so that this is not forgotten (for I believe that it could be valuable addition).
Feel free to comment - https://issues.jboss.org/browse/CDI-723

Matej

----- Original Message -----
> From: "Martin Kouba" <[hidden email]>
> To: "arjan tijms" <[hidden email]>
> Cc: [hidden email]
> Sent: Friday, February 16, 2018 11:28:10 AM
> Subject: Re: [cdi-dev] Easy way to get 'original' type from alternative producers
>
> Dne 15.2.2018 v 18:15 arjan tijms napsal(a):
> > Hi,
> >
> > On Thu, Feb 15, 2018 at 10:57 AM, Martin Kouba <[hidden email]
> > <mailto:[hidden email]>> wrote:
> >
> >
> >     I think it could be a nice feature. Pls file a JIRA issue.
> >
> >
> > Allright, I'll do that. Thx!
> >
> >
> >
> >         In order to get that bean, a bunch of quite verbose code is
> >         needed. I.e I came up with:
> >
> >         HttpAuthenticationMechanism mechanism =
> >               createRef(
> >                   beanManager.resolve(
> >
> >
> >     This wouldn't work because BeanManager.resolve() attempts to resolve
> >     the ambiguities thus eliminate all non-alternative beans.
> >
> >
> > It actually does seem to work, and I've been using it for a while now.
>
> You're right - you're actually filtering the alternative bean
> (ApplicationInit producer) and so there is only one bean left.
>
> >
> >
> >
> >     This would not work - you would need to distinguish the original
> >     injection point.
> >
> >                   return ...
> >               }
> >         }
> >
> >         Or reuse the @Delegate (perhaps with the @Alternative annotation)
> >
> >
> >     -1 This might be confusing.
> >
> >
> > Agreed with both, it was just a bit of brain storming. Not sure what
> > would be a better name though. @Original? @NonAlternative?
> >
> > Kind regards,
> > Arjan Tijms
> >
> >
>
> --
> Martin Kouba
> Senior Software Engineer
> Red Hat, Czech Republic
> _______________________________________________
> cdi-dev mailing list
> [hidden email]
> 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). For all other ideas
> provided on this list, the provider waives all patent and other intellectual
> property rights inherent in such information.


_______________________________________________
cdi-dev mailing list
[hidden email]
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). For all other ideas provided on this list, the provider waives all patent and other intellectual property rights inherent in such information.