Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use OSGi service loader mediator instead of osgi resource locator #630

Closed
mnlipp opened this issue Oct 21, 2022 · 2 comments · Fixed by #633
Closed

Use OSGi service loader mediator instead of osgi resource locator #630

mnlipp opened this issue Oct 21, 2022 · 2 comments · Fixed by #633

Comments

@mnlipp
Copy link
Contributor

mnlipp commented Oct 21, 2022

To separate the API and the implementation, the service loader pattern is used. This perfectly well suited mechanism is known to have problems in OSGi environments.

An attempt has been made in the jakarta mail api implementation to solve the problems with org.glassfish.hk2.osgi-resource-locator, which requires lots of special coding.

The "service loader and OSGi" problem has actually successfully been addressed before by http://docs.osgi.org/specification/osgi.enterprise/7.0.0/service.loader.html with the reference implementation (with an easier to read description) SPI Fly. What it boils down to is that instead of having to maintain the complicated code, you can simply use the service locator after adding some statements to MANIFEST.MF.

While this is not a runtime bug, because you can use Jakarta mail api in an OSGi environment (once you have found out that you need to include the hk2 jar and once #629 will be fixed), I think it is a bug with respect to the design.

@mnlipp
Copy link
Contributor Author

mnlipp commented Oct 22, 2022

Having found the usage of hk2 to be implemented only partially, I have patched the MANIFEST.MF files of the jars as required for the use of the service loader mediator. I did it manually, as I don't know enough about maven. The required headers are:

In jakarta.mail-api:

Require-Capability: osgi.extender;filter:="(osgi.extender=osgi.servicelo
 ader.processor)";resolution:=optional,osgi.ee;filter:="(&(osgi.ee=JavaS
 E)(version=1.8))"

In angus-core (I know this is a different project/repository, but headers have to be added for providers of the service as well):

Provide-Capability: osgi.serviceloader;osgi.serviceloader="jakarta.mail.
 util.StreamProvider"
Require-Capability: osgi.extender;filter:="(osgi.extender=osgi.servicelo
 ader.registrar)",osgi.extender;filter:="(osgi.extender=osgi.serviceload
 er.processor)";resolution:=optional,osgi.ee;filter:="(&(osgi.ee=JavaSE)
 (version=1.8))"

And in imap (using this as an example):

Provide-Capability: osgi.serviceloader;osgi.serviceloader="jakarta.mail.
 Provider"
Require-Capability: osgi.extender;filter:="(osgi.extender=osgi.servicelo
 ader.registrar)",osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"

Note that adding these headers completely obsoletes the need for hk2.

@jbescos
Copy link
Member

jbescos commented Oct 25, 2022

Ideally there should be a plugin to deal with the manifest.

You don't need an extra plugin. You can simply add this information using the Felix maven bundle plugin which is already in pom.xml with instructions <Provide-Capability> and <Require-Capability> (that's doing it "manually", see #630 (comment)). In general, the manual approach is considered a bit more difficult to maintain, but once it's there, it rarely changes (only if you add another loaded service).

If it helps, I can provide another PR with the pom.xml based solution.

In my opinion that new PR will be better. Also because there is a process to approve new dependencies (actually in APIs it is forbidden non-jakarta libs).

@mnlipp mnlipp mentioned this issue Oct 25, 2022
lukasj pushed a commit that referenced this issue Nov 4, 2022
* Fix for #630.

* Service loader mediator without annotations.
* HK2 is a special, not the general OSGi solution.
* Keep HK2 service loader optional.
@lukasj lukasj linked a pull request Nov 4, 2022 that will close this issue
@lukasj lukasj closed this as completed Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants