2 Replies Latest reply: Apr 2, 2012 7:18 AM by Mathieu Lachance RSS

weld + infinispan

Mathieu Lachance Newbie

Hello everyone,

 

I'm currently having issues with an invalid observer injection point (claimed by weld) in the Infinispan cdi module.

 

At this point, I'm trying to understand what is considerated as a valid or invalid injection point to eventually fill a JIRA report, either on Infinispan or Weld.

 

Right now I'm looking at org.jboss.weld.event.ObserverMethodImpl :

 

    /**
     * Performs validation of the observer method for compliance with the
     * specifications.
     */
    private void checkObserverMethod() {
        // Make sure exactly one and only one parameter is annotated with Observes
        List<?> eventObjects = this.observerMethod.getAnnotated().getWeldParameters(Observes.class);
        if (this.reception.equals(Reception.IF_EXISTS) && declaringBean.getScope().equals(Dependent.class)) {
            throw new DefinitionException(INVALID_SCOPED_CONDITIONAL_OBSERVER, this);
        }
        if (eventObjects.size() > 1) {
            throw new DefinitionException(MULTIPLE_EVENT_PARAMETERS, this);
        }
        // Check for parameters annotated with @Disposes
        List<?> disposeParams = this.observerMethod.getAnnotated().getWeldParameters(Disposes.class);
        if (disposeParams.size() > 0) {
            throw new DefinitionException(INVALID_DISPOSES_PARAMETER, this);
        }
        // Check annotations on the method to make sure this is not a producer
        // method, initializer method, or destructor method.
        if (this.observerMethod.getAnnotated().isAnnotationPresent(Produces.class)) {
            throw new DefinitionException(INVALID_PRODUCER, this);
        }
        if (this.observerMethod.getAnnotated().isAnnotationPresent(Inject.class)) {
            throw new DefinitionException(INVALID_INITIALIZER, this);
        }
        boolean containerLifecycleObserverMethod = Observers.isContainerLifecycleObserverMethod(this);
        for (WeldParameter<?, ?> parameter : getMethod().getAnnotated().getWeldParameters()) {
            if (parameter.isAnnotationPresent(Named.class) && parameter.getAnnotation(Named.class).value().equals("")) {
                throw new DefinitionException(NON_FIELD_INJECTION_POINT_CANNOT_USE_NAMED, getMethod());
            }
            // if this is an observer method for container lifecycle event, it must not inject anything besides BeanManager
            if (containerLifecycleObserverMethod && !parameter.isAnnotationPresent(Observes.class) && !BeanManager.class.equals(parameter.getBaseType())) {
                throw new DefinitionException(INVALID_INJECTION_POINT, this);
            }
        }
 
    }

 

the issue is related to this validation

 

            // if this is an observer method for container lifecycle event, it must not inject anything besides BeanManager
            if (containerLifecycleObserverMethod && !parameter.isAnnotationPresent(Observes.class) && !BeanManager.class.equals(parameter.getBaseType())) {
                throw new DefinitionException(INVALID_INJECTION_POINT, this);
            }

 

which make sense when looking at the infinispan org.infinispan.cdi.InfinispanExtension :

 

void registerCacheConfigurations(@Observes AfterDeploymentValidation event, CacheManagerEventBridge eventBridge, @Any Instance<EmbeddedCacheManager> cacheManagers, BeanManager beanManager) {

 

If commenting out the invalid injection point exception in weld implementation, method in infinispan api will behave normally.

 

So, is Infinispan cdi implementation wrong ? or it's the weld implementation that is too strict, or maybe not implemented correctly ?

 

Here is the original infinispan forum question which also contain a test case to reproduce the issue :

https://community.jboss.org/message/727049

  • 1. Re: weld + infinispan
    Jozef Hartinger Master

    Mathieu,

     

    the only thing a portable application may inject into an observer method defined on a CDI extension is BeanManager. However, this rule was not enforced in Weld in the past and it was technically possible to inject other beans in late bootstrap phases (e.g. AfterDeploymentValidation). Since CDI 1.0 does not provide an event that would allow application components to initialize after the CDI implementation is fully initialized, developers tend to misuse injection into extension observer methods (which is by spec non-portable as the container is not fully initialized yet).

     

    This has been identified as CDI-109 and is planned to be addressed in CDI 1.1. You are seeing this issue because Weld 2.0.0.Alpha1 (which I believe you are using) already implements this logic.

     

    Please go ahead and file an Infinispan issue.

  • 2. Re: weld + infinispan
    Mathieu Lachance Newbie

    issue created : https://issues.jboss.org/browse/ISPN-1962

     

    big thanks Jozef !