Skip to content

Commit

Permalink
Jakarta Mail erroneously assumes that classes can be loaded from Thre…
Browse files Browse the repository at this point in the history
…ad#getContextClassLoader

Signed-off-by: jmehrens <[email protected]>
  • Loading branch information
jmehrens committed Nov 12, 2023
1 parent 307dcf0 commit c7c7065
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
51 changes: 23 additions & 28 deletions api/src/main/java/jakarta/mail/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private Session(Properties props, Authenticator authenticator) {
this.authenticator = authenticator;
this.streamProvider = StreamProvider.provider();

if (Boolean.valueOf(props.getProperty("mail.debug")).booleanValue())
if (Boolean.parseBoolean(props.getProperty("mail.debug")))
debug = true;

initLogger();
Expand Down Expand Up @@ -983,21 +983,20 @@ public void load(InputStream is) throws IOException {
}

// next, add all the non-default services
ServiceLoader<Provider> sl = ServiceLoader.load(Provider.class);
ServiceLoader<Provider> sl = ServiceLoader.load(Provider.class, cl.getClassLoader());
for (Provider p : sl) {
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);
}
Iterator<Provider> iter = lookupUsingHk2ServiceLoader(Provider.class, cl.getClassLoader());
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 @@ -1006,27 +1005,26 @@ public void load(InputStream is) throws IOException {
loadResource("/META-INF/javamail.default.providers", cl, loader, false);

// finally, add all the default services
sl = ServiceLoader.load(Provider.class);
sl = ServiceLoader.load(Provider.class, cl.getClassLoader());
for (Provider p : sl) {
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);
}
iter = lookupUsingHk2ServiceLoader(Provider.class, cl.getClassLoader());
while (iter.hasNext()) {
Provider p = iter.next();
if (containsDefaultProvider(p)) {
addProvider(p);
}
}


/*
* If we haven't loaded any providers, fake it.
*/
if (providers.size() == 0) {
if (providers.isEmpty()) {
logger.config("failed to load any providers, using defaults");
// failed to load any providers, initialize with our defaults
addProvider(new Provider(Provider.Type.STORE,
Expand Down Expand Up @@ -1388,22 +1386,19 @@ EventQueue getEventQueue() {

private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "org.glassfish.hk2.osgiresourcelocator.ServiceLoader";

private static boolean isHk2Available() {
@SuppressWarnings({"unchecked"})
private <T> Iterator<T> lookupUsingHk2ServiceLoader(Class<T> factoryId, ClassLoader loader) {
Class<?> target;
try {
Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
return true;
target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME, false, loader);
} catch (ClassNotFoundException ignored) {
return Collections.emptyIterator();
}
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<?> serviceClass = Class.forName(factoryId.getName(), false, loader);
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();
Expand Down
5 changes: 0 additions & 5 deletions api/src/main/java/jakarta/mail/util/FactoryFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ static <T> T find(Class<T> factoryClass) throws RuntimeException {
return result;
}

result = find(factoryClass, FactoryFinder.class.getClassLoader());
if (result != null) {
return result;
}

result = find(factoryClass, ClassLoader.getSystemClassLoader());
if (result != null) {
return result;
Expand Down

0 comments on commit c7c7065

Please sign in to comment.