Gonçalo Marques created OPENEJB-2125:
----------------------------------------

             Summary: CDI extension that wraps an InjectionTarget (JAX-RS) 
causes a ClassCastException
                 Key: OPENEJB-2125
                 URL: https://issues.apache.org/jira/browse/OPENEJB-2125
             Project: OpenEJB
          Issue Type: Bug
          Components: integration, tomee
    Affects Versions: (not version related)
            Reporter: Gonçalo Marques


If an arbitrary CDI extension wraps a {{InjectionTarget}} during {{@Observes 
ProcessInjectionTarget<T>}} event, it will result in a {{ClassCastException}} 
in TomEE JAX-RS service startup.

*Example*

{code:borderStyle=solid}
// InjectionTarget wrapper
public class InjectionTargetWrapper<T> implements InjectionTarget<T>
{
    private final InjectionTarget<T> wrapped;
    
    public InjectionTargetWrapper(InjectionTarget<T> injectionTarget){
        this.wrapped = injectionTarget;
    }

    // All InjectionTarget interface methods
    // delegate to the wrapped instance and 
    // decorate the wrapped instance 
    // implementation where needed

}

// CDI extension
public class SomeExtension implements Extension {

    <T> void processInjectionTarget(@Observes ProcessInjectionTarget<T> pit, 
BeanManager beanManager) {
        InjectionTarget<T> it = pit.getInjectionTarget();
        pit.setInjectionTarget(new InjectionTargetWrapper<T>(it);
    }
}
{code}

With the aforementioned wrapping, any existing CDI managed JAX-RS endpoint that 
is processed by the CDI extension will have its {{InjectionTarget}} changed to 
the wrapper type: {{InjectionTargetWrapper}}.

This results in a {{ClassCastException}} during TomEE JAX-RS service startup, 
more precisely in 
{{org.apache.openejb.server.cxf.rs.CdiResourceProvider#findContexts(final 
Class<?> clazz)}}.

This method will be called by the CXF application deployment while processing 
the JAX-RS endpoint bean and will cast the bean {{InjectionTarget}} to 
{{InjectionTargetImpl}}. Since the JAX-RS bean {{InjectionTarget}} has been 
changed to the wrapper - of type {{InjectionTargetWrapper}} - this results in a 
{{ClassCastException}}.

This has been originally reported [at 
GitHub|https://github.com/gonmarques/cdi-properties/issues/2]. The specific CDI 
extension class may also be found at GitHub 
[com.byteslounge.cdi.extension.PropertyExtension|https://github.com/gonmarques/cdi-properties/blob/master/cdi-properties-main/src/main/java/com/byteslounge/cdi/extension/PropertyExtension.java].

While the just mentioned extension is more complex than the initial example 
described in this issue, this problem will occur for every CDI extension that 
happens to process and change a JAX-RS endpoint {{InjectionTarget}} to another 
type, ex: a wrapper (if the wrapper itself does not inherit from OpenWebBeans 
{{InjectionTargetImpl}} of course, but this is not likely to happen for the 
obvious reasons).

I will attach a very simple ready-to-build project that demonstrates the issue. 
The project has a JAX-RS endpoint and includes the aforementioned CDI 
extension. The problem will immediately occur if the application is deployed in 
TomEE JAX-RS v1.7.3



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to