camel-karaf icon indicating copy to clipboard operation
camel-karaf copied to clipboard

CXF bus extension not working as expected in OSGI

Open amergey opened this issue 11 months ago • 5 comments

Not sure it is an issue you are supposed to fix, but I think it is interesting to raise here just in case

In CXF 4.x, osgi and blueprint integration is removed. To workaround that I am using camel-cxf-all and camel-cxf-blueprint, waiting for CXF to restore it somehow.

I faced an issue caused by the fact that all CXF bundle were merged in one bundle. org.apache.camel.component.cxf.bus.osgi.CXFActivator#start is registering org.apache.camel.component.cxf.bus.osgi.CXFExtensionBundleListener, which is supposed to populate CXF ExtensionRegisty with OSGI wrapper arround CXF extension provided by various CXF bundles.

I do not really understand why it was like this in CXF, but the code registering extensions (org.apache.camel.component.cxf.bus.osgi.CXFExtensionBundleListener#registerExistingBundles) is excluding the current bundle (In original code base it was cxf-core), and as all bundles have been merged in one, no more osgi wrappers around Extension are registered anymore.

In my application it breaks CXF configuration with blueprint for JAXRS service <jaxrs:server id="customerService" address="/customers"> <jaxrs:serviceBeans> <ref component-id="serviceBean" /> </jaxrs:serviceBeans> </jaxrs:server>

The org.apache.cxf.transport.servlet.CXFNonSpringServlet registered in my osgi container is not able to route http://host/cxf/customers queries to the service, because it is not retrieving the correct org.apache.cxf.transport.http.HTTPTransportFactory OSGI service which contains all services configured trough blueprint.

My workaround, was to call org.apache.camel.component.cxf.bus.osgi.CXFExtensionBundleListener#registerExistingBundles from another bundle, but one way to fix that could be to update org.apache.camel.component.cxf.bus.osgi.CXFExtensionBundleListener#registerExistingBundles to remove this from the if && bundle.getBundleId() != context.getBundle().getBundleId()

My guess is there are probably multiple stuff broken in OSGI, because of this

amergey avatar Jan 15 '25 23:01 amergey

Thanks for the report. I sent a message to Apache CXF proposing to create cxf-karaf (as we have for camel) to fix OSGi/Karaf support there. We did a workaround in camel-karaf, it probably needs more changes. I will take a look on this issue.

jbonofre avatar Jan 16 '25 06:01 jbonofre

The way I fixed this issue locally : in the start method of HTTPTransportActivator : use the bus to create objects . Use <dependency> <groupId>org.ops4j.pax.web</groupId> <artifactId>pax-web-api</artifactId> <scope>provided</scope> <version>9.99.0</version> </dependency> dependency which reintroduced the HTTPService interface

//don't use constructor, use the extension manager to register the object

        HTTPTransportFactory transportFactory =  BusFactory.getDefaultBus().getExtension(HTTPTransportFactory.class);
        DestinationRegistry destinationRegistry = transportFactory.getRegistry();

        //FIXME : either we add this with dependency to pax web, or remove it
        HttpServiceTrackerCust customizer = new HttpServiceTrackerCust(destinationRegistry, context);
        httpServiceTracker = new ServiceTracker<>(context, HttpService.class, customizer);
        httpServiceTracker.open();

        context.registerService(DestinationRegistry.class.getName(), destinationRegistry, null);
        context.registerService(HTTPTransportFactory.class.getName(), transportFactory, null);
        //registers it also for the interface, as DestinationFactoryManagerImpl looks by DestinationFactory
        context.registerService(DestinationFactory.class.getName(), transportFactory, null);

f2par0 avatar Jan 31 '25 09:01 f2par0

Any progress on this issue ?

robinvish2794 avatar Jun 07 '25 07:06 robinvish2794

I made it work with jetty in Karaf 4.4.6, Had to make several changes in camel-cxf-all

robinvish2794 avatar Jun 13 '25 09:06 robinvish2794

@jbonofre I've added HTTP Jetty Transport Activators in camel-cxf-all and camel-cxf-blueprint and I also added camel-jetty feature in camel-cxf in order for CXF to work. Should I create a PR or Should we wait for official CXF 4 support for OSGi which is removed as far as I can see. Let me know your thoughts.

robinvish2794 avatar Jun 16 '25 11:06 robinvish2794