users@hk2.java.net

Custom InjectionResolver not getting injected with custom injector annotation

From: Dave Trombley <dave.trombley_at_gmail.com>
Date: Fri, 6 Jun 2014 15:09:11 -0400

  Hi all -

    Could someone tell me if this sort of thing is supported? The idea is
that a custom annotation @ApplicationContext can inject a JPA
EntityManager, and I want another custom annotation @FieldQuery to use that
to construct and inject JPA queries into manager objects:

            protected void configure() {
               bind(ApplicationInjectionResolver.class)
                .to(new
TypeLiteral<InjectionResolver<ApplicationContext>>(){})
               bind(FieldQueryResolver.class)
                .to(new TypeLiteral<InjectionResolver<FieldQuery>>(){})
                .in(Singleton.class);
            }

-----

public class FieldQueryResolver implements InjectionResolver<FieldQuery>{

    @ApplicationContext EntityManager entityManager;

    @Override
    public Object resolve(Injectee injectee, ServiceHandle<?> root) {
       FieldQuery aFQ =
injectee.getParent().getAnnotation(FieldQuery.class);
       ...
    }
-----
   static class ApplicationInjectionResolver implements
InjectionResolver<ApplicationContext> {
        @Override
        public Object resolve(Injectee injectee, ServiceHandle<?> root) {
            Type targetType = injectee.getRequiredType();
            if (targetType instanceof Class) {
                Class targetClass = (Class)targetType;
                switch (targetClass.getCanonicalName()) {
                    case "com.chesapeakelogic.ath.Application": return
APPLICATION;
                    case "freemarker.template.Configuration": return
APPLICATION_FM_ERROR_CFG;
                    case "javax.persistence.EntityManager": return
APPLICATION_ENTITY_MANAGER;
                    case "javax.sql.DataSource": return
APPLICATION_DATA_SOURCE;
                    case "org.eclipse.jetty.server.Server": return
APPLICATION_SERVER;
                }
            }
            ...
        }



    That injection is working fine in other managed classes, but not in the
@FieldQuery injection resolver... The docs seem to indicate that it
should in fact work so long as the injection resolver isn't trying to
invoke itself -- but that second resolve() method I pasted isn't getting
called at all from the HK2-managed FieldQueryResolver instance...

   Any ideas? Is this sort of thing supposed to work, or is it too crazy?

   Also I noticed the ServiceHandle<?> passed as second parameters to those
are always null. Why should that be? It would be useful to get at
either the service handle data or the service locator from the resolver
implementations... any way to do that?

   -David