eclipse / eclipse-collections

Eclipse Collections is a collections framework for Java with optimized data structures and a rich, functional and fluent API.
http://www.eclipse.org/collections
2.39k stars 596 forks source link

[OSGi] Opting in to Service Loader Mediator #1568

Open fipro78 opened 3 months ago

fipro78 commented 3 months ago

The design of Eclipse Collections is to have the API and the implementation in separate bundles. Internally the Java ServiceLoader API is used. This means the API uses Services that are provided by the implementation. The ServiceLoader API by default only works if consumer and provider are using the same classloader. Therefore it does not work in an OSGi context.

To make Eclipse Collections work in an OSGi context like Eclipse, an additional artifact is created and published via p2 update site, which combines API and IMPL in a single bundle, which solves the ServiceLoader classloader issue.

For a while now it is possible to consume dependencies directly from Maven in an Eclipse Target Platform. Also there are OSGi projects that are not based on Eclipse, that might want to consume Eclipse Collections but can't use a p2 update site.

If you try to use Eclipse Collections API and Implementation from Maven in an OSGi project, you will see the following exception at runtime:

java.lang.IllegalStateException: Could not find any implementations of MutableListFactory. Check that eclipse-collections.jar is on the classpath and that its META-INF/services directory is intact.

It is possible to use Eclipse Collections API and IMPL from Maven in an OSGi project, if the Service Loader Mediator is used. You then need to add for example SPI Fly to the runtime, which is the reference implementation of the Service Loader Mediator Specification.

To make it work, the API and the IMPL bundles need to be registered to be processed by the Service Loader Mediator. With the current setup this can only be done by setting system properties, e.g. the following setting in a .bndrun file:

-runproperties: \
    org.apache.aries.spifly.auto.consumers=org.eclipse.collections.api,\
    org.apache.aries.spifly.auto.providers=org.eclipse.collections.impl

To avoid additional configurations for the runtime, the bundles could also opting in by requiring the Service Loader Mediator in the manifest.

The API needs:

Require-Capability:                                         
  osgi.extender;
    filter:="(&(osgi.extender=osgi.serviceloader.processor)
             (version>=1.0)(!(version>=2.0)))"

The IMPL needs:

Require-Capability:                                         
    osgi.extender; 
        filter:="(&(osgi.extender=osgi.serviceloader.registrar)
                 (version>=1.0)(!(version>=2.0)))"
kavyabala23 commented 3 months ago

Hi, may I work on this issue?

fipro78 commented 3 months ago

I have already provided a PR for this issue. Not sure what you want to do now.

kavyabala23 commented 3 months ago

Oh ok, please let me know if you find out.