Uploaded image for project: 'CXF'
  1. CXF
  2. CXF-7248

JAX-RS @Context injected fields cause a NPE in re-used providers from OSGI

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.7.18, 3.1.9, 3.0.12
    • 3.0.13, 3.1.11, 3.2.0
    • JAX-RS
    • None
    • Unknown

    Description

      Hi.

      The explanation is kind of long, but the code issue I found is simple, please look at the linked PR.
      Most of the text will be about what I have attempted, in hopes of getting any comment on what I'm trying to archieve:

      I have a bean implementing ContainerRequestFilter that I'm obtaining through OSGI, and this bean has a @Context injected field. All of the bundles in my application are setting this bean as a Provider via JAXRS.

      At first, the @Context annotation was used directly on the field of the implementing class. This caused the injection to fail on all bundles trying to use it, because it seems the imported object via osgi was a Proxy rather than the actual original bean that was exported. CXF attempted to look for any field to inject, but the Proxy never exposed anything of the underlying object.

      The next attempt was putting the @Context annotation on the setter method in a new interface the filter is now implementing.
      Now, the first bundle works properly, but then fails as described in the title of this issue: After the second bundle is deployed, trying to call any method of the injected field when handling the first endpoint (from the first bundle) will throw an NPE (the field itself isn't null tho)

      Digging through the code, I can see that a ThreadLocal proxy in combination with a map are being used for the injection.
      And that each endpoint can re use the same ThreadLocal proxy as long as CXF can retrieve it from the provider bean.

      Finally, I added a getter method for the injected field, but the problem persisted. The linked PR fixes InjectionUtils#getGetterFromSetter so this problem goes away.
      I wanted to also ask. Is this a good approach for reusing the filter through OSGI?

      Test: https://github.com/nhtzr/osgiee-web/blob/master/src/test/java/mx/nhtzr/osgiee/web/internal/MyFilterTest.java
      PR: https://github.com/apache/cxf/pull/235

      Attachments

        Issue Links

          Activity

            People

              sergey_beryozkin Sergey Beryozkin
              nhtzr Ezequiel Rosas Garcia
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: