You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by Steinar Bang <sb...@dod.no> on 2023/11/01 19:18:14 UTC
Need help with providing multiple Service Loader Mediator services
I have helped the TwelveMonkeys image manipulation library[1] with OSGi
support. However, when I try using I see that only the image reader
services are exported from the bundles.
I need help figuring out why the image writer services aren't exported.
I need advice on the maven-bundle-plugin config.
TwelveMonkeys hooks into the JRE ImageIO API using the Java Service
Provider Interface (SPI)[2].
I handle the SPI services by using the OSGi ServiceLoader Mediator mechanism[3].
My changes to TwelveMonkeys[4] consists of maven-bundle-plugin config,
adding package export/imports and Require-Capability and
Provide-Capability headers for the OSGi service loader mediator.
I use apache Aries SPI Fly[5] at runtime to load the services.
In Apache Karaf, when I look at a bundle that should provide two
services (javax.imageio.spi.ImageReaderSpi and javax.imageio.spi.ImageWriterSpi)
it provides only javax.imageio.spi.ImageReaderSpi:
https://gist.github.com/steinarb/5c84fef1e76976e276681f6f0b697e35
This is what I'm trying to do to provide the services:
https://github.com/haraldk/TwelveMonkeys/blob/master/imageio/imageio-jpeg/pom.xml#L43
However this results in the following MANIFEST.MF:
https://gist.github.com/steinarb/5256d998721f0c9dd33d25c100bc420d#file-gistfile1-txt-L32
Note the "~=" between osgi.serviceloader and "javax.imageio.spi.ImageWriterSpi"
Does that means "exclude" rather than "include"?
And if so: what should the maven-bundle-config look like to provide both services?
Thanks!
- Steinar
References:
[1] <https://github.com/haraldk/TwelveMonkeys>
[2] <https://www.baeldung.com/java-spi>
[3] <https://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.loader.html>
[4] <https://github.com/haraldk/TwelveMonkeys/pull/814>
[5] <https://aries.apache.org/documentation/modules/spi-fly.html>
Re: Need help with providing multiple Service Loader Mediator services
Posted by Steinar Bang <sb...@dod.no>.
>>>>> Raymond Augé <ra...@liferay.com.INVALID>:
> 👍 Nice work! Sorry I couldn't offer feedback earlier.
Thanks! :-)
Here's my reported bug with attached PR for those interested in
following it.
https://github.com/haraldk/TwelveMonkeys/issues/855
Re: Need help with providing multiple Service Loader Mediator services
Posted by Raymond Augé <ra...@liferay.com.INVALID>.
👍 Nice work! Sorry I couldn't offer feedback earlier.
On Wed, Nov 1, 2023 at 4:20 PM Steinar Bang <sb...@dod.no> wrote:
> >>>>> Steinar Bang <sb...@dod.no>:
>
> > I have helped the TwelveMonkeys image manipulation library[1] with OSGi
> > support. However, when I try using I see that only the image reader
> > services are exported from the bundles.
>
> > I need help figuring out why the image writer services aren't exported.
>
> > I need advice on the maven-bundle-plugin config.
> [snip!]
>
> > In Apache Karaf, when I look at a bundle that should provide two
> > services (javax.imageio.spi.ImageReaderSpi and
> javax.imageio.spi.ImageWriterSpi)
> > it provides only javax.imageio.spi.ImageReaderSpi:
> > https://gist.github.com/steinarb/5c84fef1e76976e276681f6f0b697e35
>
> > This is what I'm trying to do to provide the services:
> >
> https://github.com/haraldk/TwelveMonkeys/blob/master/imageio/imageio-jpeg/pom.xml#L43
>
> > However this results in the following MANIFEST.MF:
> >
> https://gist.github.com/steinarb/5256d998721f0c9dd33d25c100bc420d#file-gistfile1-txt-L32
>
> I have figured it out, I think.
>
> I needed to change stuff like this:
> <instructions>
> <Provide-Capability>
> osgi.serviceloader;
> osgi.serviceloader=javax.imageio.spi.ImageReaderSpi;
> osgi.serviceloader=javax.imageio.spi.ImageWriterSpi
> </Provide-Capability>
> </instructions>
>
> into this:
> <instructions>
> <Provide-Capability>
> osgi.serviceloader;
> osgi.serviceloader=javax.imageio.spi.ImageReaderSpi,
> osgi.serviceloader;
> osgi.serviceloader=javax.imageio.spi.ImageWriterSpi
> </Provide-Capability>
> </instructions>
>
> I.e. replace the semicolon after ImageReaderSpi with a comma, and add an
> extra osgi.serviceloader; before the second sercice.
>
> And then the manifest looks like this ("=" instead of "~=" in front of the
> second service):
>
> https://gist.github.com/steinarb/23c272a171ea7873d1a410750670b902#file-gistfile1-txt-L31
>
> And the loaded bundle exposes two services:
> https://gist.github.com/steinarb/f0dae9211dd44781977bb0a32ffc317f
>
> And the service is loaded and registered.
>
> ImageIO writer lookup based on the reader, which worked fine for me in
> unit tests, doesn't work in OSGi. But that's because ImageIO uses
> ClassLoader.getSystemClassLoader() to find a class based on the fully
> qualified class name, and that doesn't find a class loaded with OSGi.
>
> The change that will become a PR to TwelveMonkeys, is:
>
> https://github.com/steinarb/TwelveMonkeys/commit/cdadd38e699be8e17593c5b0529dfe98c30d13af
>
>
--
*Raymond Augé* (@rotty3000)
Senior Software Architect *Liferay, Inc.* (@Liferay)
OSGi Fellow, Java Champion
Re: Need help with providing multiple Service Loader Mediator services
Posted by Steinar Bang <sb...@dod.no>.
>>>>> Steinar Bang <sb...@dod.no>:
> I have helped the TwelveMonkeys image manipulation library[1] with OSGi
> support. However, when I try using I see that only the image reader
> services are exported from the bundles.
> I need help figuring out why the image writer services aren't exported.
> I need advice on the maven-bundle-plugin config.
[snip!]
> In Apache Karaf, when I look at a bundle that should provide two
> services (javax.imageio.spi.ImageReaderSpi and javax.imageio.spi.ImageWriterSpi)
> it provides only javax.imageio.spi.ImageReaderSpi:
> https://gist.github.com/steinarb/5c84fef1e76976e276681f6f0b697e35
> This is what I'm trying to do to provide the services:
> https://github.com/haraldk/TwelveMonkeys/blob/master/imageio/imageio-jpeg/pom.xml#L43
> However this results in the following MANIFEST.MF:
> https://gist.github.com/steinarb/5256d998721f0c9dd33d25c100bc420d#file-gistfile1-txt-L32
I have figured it out, I think.
I needed to change stuff like this:
<instructions>
<Provide-Capability>
osgi.serviceloader;
osgi.serviceloader=javax.imageio.spi.ImageReaderSpi;
osgi.serviceloader=javax.imageio.spi.ImageWriterSpi
</Provide-Capability>
</instructions>
into this:
<instructions>
<Provide-Capability>
osgi.serviceloader;
osgi.serviceloader=javax.imageio.spi.ImageReaderSpi,
osgi.serviceloader;
osgi.serviceloader=javax.imageio.spi.ImageWriterSpi
</Provide-Capability>
</instructions>
I.e. replace the semicolon after ImageReaderSpi with a comma, and add an
extra osgi.serviceloader; before the second sercice.
And then the manifest looks like this ("=" instead of "~=" in front of the second service):
https://gist.github.com/steinarb/23c272a171ea7873d1a410750670b902#file-gistfile1-txt-L31
And the loaded bundle exposes two services:
https://gist.github.com/steinarb/f0dae9211dd44781977bb0a32ffc317f
And the service is loaded and registered.
ImageIO writer lookup based on the reader, which worked fine for me in
unit tests, doesn't work in OSGi. But that's because ImageIO uses
ClassLoader.getSystemClassLoader() to find a class based on the fully
qualified class name, and that doesn't find a class loaded with OSGi.
The change that will become a PR to TwelveMonkeys, is:
https://github.com/steinarb/TwelveMonkeys/commit/cdadd38e699be8e17593c5b0529dfe98c30d13af