6 Replies Latest reply on Jun 1, 2010 4:39 AM by Pete Muir

    Add extension result in Logger ambiguous dependencies

    wu haixing Newbie

      exception as follow:
      enviroment:
      jboss-6.0.0.20100429-M3,
      weld 1.0.1 final
      weld extension snapshot
      I created a extension class and service file in meta-inf,after deploy,jboss threw the exception:


      caused by: org.jboss.weld.exceptions.AmbiguousResolutionException: WELD-001318 Cannot resolve an ambiguous dependency between [org.jboss.weld.bean-flat-ProducerMethod-org.jboss.w
      ld.log.LoggerProducer.method produceLog(javax.enterprise.inject.spi.InjectionPoint), org.jboss.weld.bean-flat-ProducerMethod-org.jboss.weld.extensions.log.LoggerProducer.method
      roduceLog(javax.enterprise.inject.spi.InjectionPoint)]



      Is it a bug?

        • 2. Re: Add extension result in Logger ambiguous dependencies
          wu haixing Newbie

          just as seam-faces's FormValidationTypeOverrideExtension,named DAOTypeOverrideExtension.

          • 3. Re: Add extension result in Logger ambiguous dependencies
            wu haixing Newbie

            Qualifier DAO:




            public @interface Dao {
                  @Nonbinding Class<?> value() default Object.class;
            }




            DAO producer:




            public class DaoFactory {
                 @PersistenceContext EntityManager em;
            
                 @Produces @Dao
                 public Object createDao(InjectionPoint injectionPoint) throws Exception {
                 
                      Class<?> entityType = injectionPoint.getAnnotated().getAnnotation(Dao.class).value();
                      Class<?> daoType = (Class)injectionPoint.getType();
                      logger.info("create dao {}",daoType.getName());
                      if (!daoType.isInterface()) {  
                                   throw new UnsupportedOperationException("Only results of methods " +  
                                      "which produce implementations of an interface can be replaced!");  
                      }  
                      GenericDao genericDao = new GenericDaoImpl(em,entityType);
                      
                      Object result = Proxy.newProxyInstance(  
                                daoType.getClassLoader(),  
                            new Class[] { daoType },  
                            new DaoInvocationHandler(genericDao));  
                    
                      return result;
            
                 }
            }



            DAO type override extension to replace WidgetDAO with Object:




            @ApplicationScoped
            public class DaoTypeOverrideExtension implements Extension {
                 private final Map<Class<?>, AnnotatedType<?>> typeOverrides = new HashMap<Class<?>, AnnotatedType<?>>();
            
                    public <T> void processAnnotatedType(@Observes final ProcessAnnotatedType<T> event)
                    {
                       AnnotatedTypeBuilder<T> builder = AnnotatedTypeBuilder.newInstance(event.getAnnotatedType());
                       builder.readAnnotationsFromUnderlyingType();
                       boolean modifiedType = false;
            
                       for (AnnotatedField<?> f : event.getAnnotatedType().getFields())
                       {
                          if (f.isAnnotationPresent(Dao.class))
                          {
                             builder.overrideFieldType(f.getJavaMember(), Object.class);
                             modifiedType = true;
                          }
                       }
            
                       if (modifiedType)
                       {
                          AnnotatedType<T> replacement = builder.create();
                          typeOverrides.put(replacement.getJavaClass(), replacement);
                          event.setAnnotatedType(replacement);
                       }
                    }
            
                    public boolean hasOverriddenType(final Class<?> clazz)
                    {
                       return typeOverrides.containsKey(clazz);
                    }
            
                    public AnnotatedType<?> getOverriddenType(final Class<?> clazz)
                    {
                       return typeOverrides.get(clazz);
                    }
            }



            DAO interface:




            public interface WidgetDao extends GenericDao<Widget,Long>{
                    @QuerySpecifiction("select w from Widget w order by w.name")
                 public List<Widget> getWidgets();
            }



            usage :




            private @Inject @Dao(Widget.class) WidgetDao widgetDao;



            • 4. Re: Add extension result in Logger ambiguous dependencies
              wu haixing Newbie

              I only found org.jboss.weld.extensions.log.LoggerProducer in the repository,where is org.jboss.weld.log.LoggerProducer?

              • 5. Re: Add extension result in Logger ambiguous dependencies
                wu haixing Newbie

                documentation about weld logger not suit for weld extension snapshot,remove the weld-logger dependency



                <dependency>
                         <groupId>org.jboss.weld</groupId>
                         <artifactId>weld-logger</artifactId>
                         <version>1.0.0-CR2</version>
                         <exclusions>
                            <exclusion>
                               <groupId>org.slf4j</groupId>
                               <artifactId>slf4j-api</artifactId>
                            </exclusion>
                         </exclusions>
                </dependency>