9 Replies Latest reply on Nov 10, 2009 3:25 PM by alexanderbell

    CR1: @Qualifier linked with @Alternative

      Hi,


      I've got two Producer-Methods which provide an EntitiyManager:



      @Produces @Persistence
      public EntityManager getEntityManager() {...}





      @Produces @Test @Persistence
      public EntityManager getTestEntityManager() {...}




      My meta-annotations:



      @Qualifier
      @Retention(RetentionPolicy.RUNTIME)
      @Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER})
      public @interface Persistence {}





      @Alternative
      @Retention(RetentionPolicy.RUNTIME)
      @Target({ElementType.METHOD, ElementType.TYPE})
      public @interface Test {}



      And my beans.xml



      <beans>
           <alternatives>
                <!--stereotype>eu.unicreditgroup.cope.type.Test</stereotype-->
                <!--stereotype>eu.unicreditgroup.cope.type.Mock</stereotype-->
           </alternatives>
      </beans>





      So the two alternatives are disabled.


      Last but not least my injection point:



      public class TestServiceImpl implements TestService {
           private @Inject @Persistence EntityManager em;
      }




      So, actually the injection point is unambiguous but I get the following Exception:



      org.jboss.weld.DeploymentException: Injection point has ambiguous dependencies. Injection point: field eu.unicreditgroup.cope.service.impl.TransactionlistServiceImpl.em; Qualifiers: [@eu.unicreditgroup.cope.type.Persistence()]; Possible dependencies: [org.jboss.weld.bean-web-module-ProducerMethod-eu.unicreditgroup.cope.util.PersistenceUtil.getEntityManager(), org.jboss.weld.bean-web-module-ProducerMethod-eu.unicreditgroup.cope.util.PersistenceUtil.getTestEntityManager()]
           at org.jboss.weld.Validator.validateInjectionPoint(Validator.java:166)
           at org.jboss.weld.Validator.validateBean(Validator.java:75)
           at org.jboss.weld.Validator.validateRIBean(Validator.java:95)
           at org.jboss.weld.Validator.validateBeans(Validator.java:211)
           at org.jboss.weld.Validator.validateDeployment(Validator.java:198)
           at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:295)
           at org.jboss.weld.environment.servlet.Listener.contextInitialized(Listener.java:156)
           at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
           at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
           at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
           at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
           at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
           at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:627)
           at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
           at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
           at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)
           at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
           at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
           at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
           at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
           at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
           at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
           at org.apache.catalina.core.StandardService.start(StandardService.java:516)
           at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
           at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:585)
           at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
           at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)



      With the previous version (WebBeans) it works with the deprecated Annotations @BindingType and @DeploymentType


      Any ideas?


      Alex

        • 1. Re: CR1: @Qualifier linked with @Alternative

          Sorry this is the Exception



          org.jboss.weld.DeploymentException: Injection point has ambiguous dependencies. Injection point: field org.j4fry.impl.TestServiceImpl.em; Qualifiers: [@org.j4fry.type.Persistence()]; Possible dependencies: [org.jboss.weld.bean-web-module-ProducerMethod-org.j4fry.util.PersistenceUtil.getEntityManager(), org.jboss.weld.bean-web-module-ProducerMethod-org.j4fry.util.PersistenceUtil.getTestEntityManager()]
               at org.jboss.weld.Validator.validateInjectionPoint(Validator.java:166)
               at org.jboss.weld.Validator.validateBean(Validator.java:75)
               at org.jboss.weld.Validator.validateRIBean(Validator.java:95)
               at org.jboss.weld.Validator.validateBeans(Validator.java:211)
               at org.jboss.weld.Validator.validateDeployment(Validator.java:198)
               at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:295)
               at org.jboss.weld.environment.servlet.Listener.contextInitialized(Listener.java:156)
               at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
               at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
               at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
               at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
               at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
               at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:627)
               at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
               at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
               at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)
               at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
               at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
               at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
               at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
               at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
               at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
               at org.apache.catalina.core.StandardService.start(StandardService.java:516)
               at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
               at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
               at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
               at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
               at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
               at java.lang.reflect.Method.invoke(Method.java:585)
               at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
               at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)





          And the producer methods are in the class org.j4fry.util.PersistenceUtil


          Alex


          BTW: How could I edit my first postint?

          • 2. Re: CR1: @Qualifier linked with @Alternative
            pmuir

            Try making @Test a stereotype :-)

            • 3. Re: CR1: @Qualifier linked with @Alternative

              That works, thanks!


              I've got another problem now. I want to get the BeanManager from the JNDI-Context within my Servlet with the following code:



              Context ctx = new InitialContext();
              ((Context)ctx.lookup("java:comp/env")).lookup("comp/BeanManager");





              my web.xml



              <web-app...>
                      ...
                   <resource-env-ref>
                        <description>Weld BeanManager</description>
                        <resource-env-ref-name>comp/BeanManager</resource-env-ref-name>
                        <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
                   </resource-env-ref>
              </web-app>





              The lookup throws the following exception:



              javax.naming.NamingException: Cannot create resource instance
                   at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:114)
                   at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
                   at org.apache.naming.NamingContext.lookup(NamingContext.java:793)
                   at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
                   at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
                   at org.apache.naming.NamingContext.lookup(NamingContext.java:153)



              Alex

              • 4. Re: CR1: @Qualifier linked with @Alternative
                pmuir

                Which app server are you using? JBoss? If so, try java:app/Manager - this is a bug with JBoss AS integration that we need to fix.


                But better yet, just do @Inject BeanManager beanManager;


                :-)

                • 5. Re: CR1: @Qualifier linked with @Alternative

                  I'm using Tomcat as servlet container. Unfortunately I cannot use the injection mechanism because I am in a servlet which I cannot modify (Apache Axis Servlet).
                  So I have to get the BeanManager programmatically.


                  Alex

                  • 6. Re: CR1: @Qualifier linked with @Alternative
                    pmuir

                    You also have the META-INF/context.xml needed? If so, do some deubgging, see if ManagerObjectFactory is called.


                    Alternatively, we do push the BeanManager into the Servlet context, with the attribute name javax.enterprise.inject.spi.BeanManager.

                    • 7. Re: CR1: @Qualifier linked with @Alternative

                      I get the BeanManager from the ServletContext so that works, thanks.
                      Back to the situation mentioned in my first post.
                      Now I use the sterotype Test so my beans.xml looks like this:



                      <beans>
                           <alternatives>
                                <stereotype>org.j4fry.type.Test</stereotype>
                           </alternatives>
                      </beans>





                      Unfortunately the Method getEntityManager is called instead of getTestEntityManager even if I use the stereotype Test



                      @Stereotype
                      @Alternative
                      @Retention(RetentionPolicy.RUNTIME)
                      @Target({ElementType.METHOD, ElementType.TYPE})
                      public @interface Test {}



                      When I call the toString()-Method of BeanManager I get the following Output:



                      Manager
                      Enabled policies: [] [interface org.j4fry.type.Test]
                      Registered contexts: [interface javax.enterprise.context.ConversationScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.Dependent, interface javax.enterprise.context.RequestScoped, interface javax.inject.Singleton, interface javax.enterprise.context.SessionScoped]
                      Registered beans: 69



                      So the Test-Alternative is loaded successfully but it isn't recognized during the injection-phase.


                      Alex

                      • 8. Re: CR1: @Qualifier linked with @Alternative
                        pmuir

                        Ok, please file a JIRA issue, ideally with a test. Whatever, make sure you attach classes sufficient to reproduce.

                        • 9. Re: CR1: @Qualifier linked with @Alternative

                          ok, i'm gonna create a sample and file a jira issue