jakartaee / mail-api

Jakarta Mail Specification project
https://jakartaee.github.io/mail-api
Other
244 stars 101 forks source link

jakarta.mail-api-2.1.0.jar does not work in OSGi environment #629

Closed mnlipp closed 1 year ago

mnlipp commented 1 year ago

Describe the bug An attempt has been made to solve service loader problems in an OSGi environment with org.glassfish.hk2.osgi-resource-locator.

The presence of the required class org.glassfish.hk2.osgiresourcelocator.ServiceLoader is detected with the code

    private static boolean isOsgi() {
        try {
            Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
            return true;
        } catch (ClassNotFoundException ignored) {
        }
        return false;
    }

In order for this to work, class org.glassfish.hk2.osgiresourcelocator.ServiceLoader must be in the bundle's classpath (jar becomes "bundle" in OSGi). But the required import is missng in MANIFEST.MF and therefore the provider services are not found.

How it should be For comparison: jakarta.activation-api-2.1.0.jar has in its MANIFEST.MF the line:

DynamicImport-Package: org.glassfish.hk2.osgiresourcelocator

which ensures that the class org.glassfish.hk2.osgiresourcelocator.ServiceLoader is in the classpath and the Class.forName is successful.

To Reproduce Steps to reproduce the behavior:

  1. Deploy jakarta.mail-api-2.1.0.jar in an OSGi enviroment
  2. Attempt to use the IMAP provider

Expected behavior Provider should be found.

mnlipp commented 1 year ago

Digging further, I found that the hk2 service locator is used in FactoryFinder only. Session.java uses the ServiceLoader without the hk2 "wrapper" and therefore fails to load the providers.

jbescos commented 1 year ago

Hello,

It was reported one OSGi issue here: https://github.com/eclipse-ee4j/angus-mail/issues/15

There is already a fix here, but not merged yet: https://github.com/eclipse-ee4j/angus-mail/pull/19

Do you think that fix is valid here?.

mnlipp commented 1 year ago

Do you think that fix is valid here?.

Issue #15 is completely independent, i.e. I had to work around that sun.security.util issue in addition to the problem described here.

The proper fix for this issue (#629) -- both the missing import and the inconsistent usage of hk2 (https://github.com/eclipse-ee4j/mail/issues/629#issuecomment-1287850248) -- is actually to "fix" #630. This will make #629 obsolete (This is why I provided a PR for #630 rather than #629.)

mnlipp commented 1 year ago

The (interesting) discussion may have caused the focus to be lost. So only as a reminder: this issue is about the missing DynamicImport-Package: org.glassfish.hk2.osgiresourcelocator (see initial description).

jbescos commented 1 year ago

@mnlipp this was merged: https://github.com/jakartaee/mail-api/pull/633

Can we close this?

mnlipp commented 1 year ago

Can we close this?

No. If you don't have the OSGi service mediator in your environment, the fallback is to use the hk2 service locator. This can only be found if it is in your class path, i.e. if it is imported. Therefore you need the import (as it is properly done in jakarta-api).