You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by Andreas Beeker <ki...@apache.org> on 2018/09/23 18:07:57 UTC

ServiceLoader vs. OSGi

Hi,

I'm looking for an OSGi compliant way of providing plugable services.

In my usecase, I'd like to allow custom o.a.p.sl.draw.ImageRenderer.
I thought j.u.ServiceLoader is the way to go, but as usual that's problematic with OSGi [1]

And of course OSGi is not the only player for IOC / modules and I don't want to link to OSGi
services loaders.

- How would you implement this?

- Use Apache Aries? [2]

Based on [1], one could use ServiceLoader.load(class, classloader). Similar to our xmlbeans
loading, I would simply use the classloader of the interface class and hope that the
OSGi provides the multiple /META-INF/services/ImageRenderer resources.
I haven't used it like that yet and also need to create an OSGi testbed.

Andi.

[1]
newer version: https://blog.osgi.org/2013/02/javautilserviceloader-in-osgi.html
Original version: http://coderthoughts.blogspot.com/2011/08/javautilserviceloader-in-osgi.html

[2] http://aries.apache.org/modules/spi-fly.html


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


Re: ServiceLoader vs. OSGi

Posted by Yegor Kozlov <ye...@dinom.ru>.
Hi Andi,

The Aries SPI Fly component looks promising, but I've never tried it. If it
works then you don't need to fiddle with OSGi at all, SPI Fly will do all
the work for you.

Alternatively, the code can check if it is running in a OSGi container and
then get the services from either ServiceLoader or BundleContext .
Something like this:

import org.osgi.framework.*;

Bundle bundle = FrameworkUtil.getBundle(ImageRenderer.class);
if (bundle == null) { // running in a non-OSGi world
  ServiceLoader ldr = ServiceLoader.load(ImageRenderer.class);
  for (ImageRenderer renderer : ldr) {
       // select the renderer
  }
}
else { // running in a OSGi framework
  BundleContext bundleContext = bundle.getBundleContext();
  ServiceReference<ImageRenderer>[] refs =
bundleContext.getServiceReferences(ImageRenderer.class.getName());
  for(ServiceReference<ImageRenderer> ref : refs){
    ImageRenderer renderer = bundleContext.getService(ref);
    // use the renderer service
  }
}

Yegor




On Sun, Sep 23, 2018 at 9:08 PM Andreas Beeker <ki...@apache.org> wrote:

> Hi,
>
> I'm looking for an OSGi compliant way of providing plugable services.
>
> In my usecase, I'd like to allow custom o.a.p.sl.draw.ImageRenderer.
> I thought j.u.ServiceLoader is the way to go, but as usual that's
> problematic with OSGi [1]
>
> And of course OSGi is not the only player for IOC / modules and I don't
> want to link to OSGi
> services loaders.
>
> - How would you implement this?
>
> - Use Apache Aries? [2]
>
> Based on [1], one could use ServiceLoader.load(class, classloader).
> Similar to our xmlbeans
> loading, I would simply use the classloader of the interface class and
> hope that the
> OSGi provides the multiple /META-INF/services/ImageRenderer resources.
> I haven't used it like that yet and also need to create an OSGi testbed.
>
> Andi.
>
> [1]
> newer version:
> https://blog.osgi.org/2013/02/javautilserviceloader-in-osgi.html
> Original version:
> http://coderthoughts.blogspot.com/2011/08/javautilserviceloader-in-osgi.html
>
> [2] http://aries.apache.org/modules/spi-fly.html
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
> For additional commands, e-mail: dev-help@poi.apache.org
>
>