How to integrate 3rd party components that use ServiceLoader? SPI Fly?

Tags: #<Tag:0x00007fc8f6a74f60> #<Tag:0x00007fc8f6a74e98> #<Tag:0x00007fc8f6a74dd0>

Hi guys,

I’m working on a binding that depends on 3rd-party libraries which in turn make use of the java.util.ServiceLoader facility. AFAIK, that doesn’t work right out of the box in an OSGi container, leading to java.util.NoSuchElementExceptions when the service loader can’t find the service at runtime, despite being defined in the 3rd-party’s JAR at META-INF/services.

Now, how does one go about resolving this? To me it seems SPI Fly is the way to go and openHAB does indeed reference it.

If my analysis is correct so far, is there any example how to augment the 3rd-party code for use in an openHAB binding. IOW, are there any openHAB-specific constraints I need to take into account while implementing it? For instance, SPI Fly offers two distinct ways to do things; a OSGi spec-complaint way and a SPI Fly-specific way. Would both work for openHAB? Likewise, dynamic or static weaving? Ok, openHAB provides support for dynamic weaving (org.apache.aries.spifly.dynamic.bundle is active in my OH 3.0.1).

Also, can I expect SPI Fly to work within the bnd/demo.app debug environment and in production/karaf, in the same way?

Thanks!

So, I’m trying to test the above in org.openhab.demo.app and all the involved components now contain the required capability headers in their respective MANIFEST.MFs.

How can I adjust the start levels of the specified runtime bundles in app.bndrun? I need to make sure my addon and its dependencies are started in the right order, and all after SPI Fly. Right now all bundles have start level 1 so I’m not overly surprised that service loading still doesn’t work…

Update: start levels can be adjusted using the runbundles+ decorator in app.bndrun.