From a6015eff7d22a7bcad3740ef779bb4142a54faf8 Mon Sep 17 00:00:00 2001 From: Lukas Jungmann Date: Sat, 29 Apr 2023 17:16:46 +0200 Subject: [PATCH] #635: The hk2 service loader is not used everywhere (where necessary) Signed-off-by: Lukas Jungmann --- api/src/main/java/jakarta/mail/Session.java | 55 ++++++++++++++++++- .../java/jakarta/mail/util/FactoryFinder.java | 8 ++- doc/release/CHANGES.txt | 1 + 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/jakarta/mail/Session.java b/api/src/main/java/jakarta/mail/Session.java index bf23b5d3..78596e89 100644 --- a/api/src/main/java/jakarta/mail/Session.java +++ b/api/src/main/java/jakarta/mail/Session.java @@ -25,6 +25,7 @@ import java.io.PrintStream; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; +import java.lang.reflect.Method; import java.net.InetAddress; import java.net.URL; import java.security.AccessController; @@ -35,6 +36,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Hashtable; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; @@ -986,7 +988,17 @@ public void load(InputStream is) throws IOException { if (!containsDefaultProvider(p)) addProvider(p); } - + + // + handle Glassfish/OSGi (platform specific default) + if (isHk2Available()) { + Iterator iter = lookupUsingHk2ServiceLoader(Provider.class.getName()); + while (iter.hasNext()) { + Provider p = iter.next(); + if (!containsDefaultProvider(p)) + addProvider(p); + } + } + // load the META-INF/javamail.providers file supplied by an application loadAllResources("META-INF/javamail.providers", cl, loader); @@ -999,7 +1011,18 @@ public void load(InputStream is) throws IOException { if (containsDefaultProvider(p)) addProvider(p); } - + + // + handle Glassfish/OSGi (platform specific default) + if (isHk2Available()) { + Iterator iter = lookupUsingHk2ServiceLoader(Provider.class.getName()); + while (iter.hasNext()) { + Provider p = iter.next(); + if (containsDefaultProvider(p)) { + addProvider(p); + } + } + } + /* * If we haven't loaded any providers, fake it. */ @@ -1366,6 +1389,34 @@ public InputStream run() throws IOException { EventQueue getEventQueue() { return q; } + + private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "org.glassfish.hk2.osgiresourcelocator.ServiceLoader"; + + private static boolean isHk2Available() { + try { + Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME); + return true; + } catch (ClassNotFoundException ignored) { + } + return false; + } + + @SuppressWarnings({"unchecked"}) + private Iterator lookupUsingHk2ServiceLoader(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); + Iterable result = ((Iterable) m.invoke(null, (Object[]) args)); + return result != null ? result.iterator() : Collections.emptyIterator(); + } catch (Exception ignored) { + // log and continue + return Collections.emptyIterator(); + } + } + } /** diff --git a/api/src/main/java/jakarta/mail/util/FactoryFinder.java b/api/src/main/java/jakarta/mail/util/FactoryFinder.java index 59a3d175..da06c1c6 100644 --- a/api/src/main/java/jakarta/mail/util/FactoryFinder.java +++ b/api/src/main/java/jakarta/mail/util/FactoryFinder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -105,7 +105,11 @@ private static T lookupUsingHk2ServiceLoader(String 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(); + Iterable iterable = ((Iterable) m.invoke(null, (Object[]) args)); + if (iterable == null) { + return null; + } + Iterator iter = iterable.iterator(); return iter.hasNext() ? (T) iter.next() : null; } catch (Exception ignored) { // log and continue diff --git a/doc/release/CHANGES.txt b/doc/release/CHANGES.txt index fda437c2..961ddbd2 100644 --- a/doc/release/CHANGES.txt +++ b/doc/release/CHANGES.txt @@ -22,6 +22,7 @@ longer available. CHANGES IN THE 2.1.2 RELEASE ---------------------------- E 629 jakarta.mail-api-2.1.0.jar does not work in OSGi environment (hk2servicelocator) +E 635 The hk2 service loader is not used everywhere (where necessary) E 660 jakarta.mail-api is not a Multi Release JAR E 664 Typo in Session.setDebug Javadoc