From 8fc297db4513c7adfaed0e9744a6853d07d3abec Mon Sep 17 00:00:00 2001 From: "Michael N. Lipp" Date: Sun, 23 Oct 2022 14:32:28 +0200 Subject: [PATCH] Fix for #630. --- api/pom.xml | 16 ++++++--- api/src/main/java/jakarta/mail/Session.java | 1 + .../java/jakarta/mail/util/FactoryFinder.java | 34 ------------------- .../jakarta/mail/util/StreamProvider.java | 1 + api/src/main/java/module-info.java | 2 ++ 5 files changed, 16 insertions(+), 38 deletions(-) diff --git a/api/pom.xml b/api/pom.xml index 65072574..eeb038d7 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -116,6 +116,18 @@ angus-activation test + + biz.aQute.bnd + biz.aQute.bnd.annotation + 6.3.1 + provided + + + org.osgi + osgi.annotation + 8.1.0 + provided + @@ -359,10 +371,6 @@ ${project.name} ${project.organization.name} ${buildNumber} - - !org.glassfish.hk2.osgiresourcelocator, - * - diff --git a/api/src/main/java/jakarta/mail/Session.java b/api/src/main/java/jakarta/mail/Session.java index e76b7acb..c993d460 100644 --- a/api/src/main/java/jakarta/mail/Session.java +++ b/api/src/main/java/jakarta/mail/Session.java @@ -198,6 +198,7 @@ * @author Max Spivak */ +@aQute.bnd.annotation.spi.ServiceConsumer(value = Provider.class) public final class Session { // Support legacy @DefaultProvider diff --git a/api/src/main/java/jakarta/mail/util/FactoryFinder.java b/api/src/main/java/jakarta/mail/util/FactoryFinder.java index b1f7dad0..5dde029c 100644 --- a/api/src/main/java/jakarta/mail/util/FactoryFinder.java +++ b/api/src/main/java/jakarta/mail/util/FactoryFinder.java @@ -54,13 +54,6 @@ static T find(Class factoryClass) throws RuntimeException { return factory; } - // handling Glassfish/OSGi (platform specific default) - if (isOsgi()) { - T result = lookupUsingOSGiServiceLoader(factoryId); - if (result != null) { - return result; - } - } throw new IllegalStateException("Not provider of " + factoryClass.getName() + " was found"); } @@ -86,33 +79,6 @@ private static String fromSystemProperty(String factoryId) { return systemProp; } - private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "org.glassfish.hk2.osgiresourcelocator.ServiceLoader"; - - private static boolean isOsgi() { - try { - Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME); - return true; - } catch (ClassNotFoundException ignored) { - } - return false; - } - - @SuppressWarnings({"unchecked"}) - private static T lookupUsingOSGiServiceLoader(String factoryId) { - try { - // Use reflection to avoid having any dependency on HK2 ServiceLoader class - Class serviceClass = Class.forName(factoryId); - Class[] args = new Class[]{serviceClass}; - Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME); - Method m = target.getMethod("lookupProviderInstances", Class.class); - Iterator iter = ((Iterable) m.invoke(null, (Object[]) args)).iterator(); - return iter.hasNext() ? (T) iter.next() : null; - } catch (Exception ignored) { - // log and continue - return null; - } - } - private static T factoryFromServiceLoader(Class factory) { try { ServiceLoader sl = ServiceLoader.load(factory); diff --git a/api/src/main/java/jakarta/mail/util/StreamProvider.java b/api/src/main/java/jakarta/mail/util/StreamProvider.java index 3a9e6ef3..21e59d15 100644 --- a/api/src/main/java/jakarta/mail/util/StreamProvider.java +++ b/api/src/main/java/jakarta/mail/util/StreamProvider.java @@ -31,6 +31,7 @@ * * @since JavaMail 2.1 */ +@aQute.bnd.annotation.spi.ServiceConsumer(value = StreamProvider.class) public interface StreamProvider { /** diff --git a/api/src/main/java/module-info.java b/api/src/main/java/module-info.java index 8fdf526f..897ff8d0 100644 --- a/api/src/main/java/module-info.java +++ b/api/src/main/java/module-info.java @@ -29,4 +29,6 @@ uses jakarta.mail.util.StreamProvider; //reflective call to java.beans.Beans.instantiate requires static java.desktop; + //annotations for OSGi service loader mediator + requires static biz.aQute.bnd.annotation; }