Skip to content

Commit

Permalink
#635: The hk2 service loader is not used everywhere (where necessary)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
lukasj committed Apr 29, 2023
1 parent 62f454e commit a6015ef
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
55 changes: 53 additions & 2 deletions api/src/main/java/jakarta/mail/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Provider> 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);

Expand All @@ -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<Provider> 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.
*/
Expand Down Expand Up @@ -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 <T> Iterator<T> 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<T> result = ((Iterable<T>) m.invoke(null, (Object[]) args));
return result != null ? result.iterator() : Collections.emptyIterator();
} catch (Exception ignored) {
// log and continue
return Collections.emptyIterator();
}
}

}

/**
Expand Down
8 changes: 6 additions & 2 deletions api/src/main/java/jakarta/mail/util/FactoryFinder.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -105,7 +105,11 @@ private static <T> 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
Expand Down
1 change: 1 addition & 0 deletions doc/release/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a6015ef

Please sign in to comment.